From 9cc955d75eab4f584418998d8567f92736590ce4 Mon Sep 17 00:00:00 2001 From: Silent Date: Sun, 15 Oct 2023 01:39:20 -0700 Subject: [PATCH 1/4] Optional player for SlimefunItemSpawnEvent --- .../api/events/SlimefunItemSpawnEvent.java | 24 +++++++++++++++++++ .../items/altar/AncientPedestal.java | 2 +- .../items/seasonal/ChristmasPresent.java | 2 +- .../implementation/items/tools/GoldPan.java | 2 +- .../items/tools/PickaxeOfContainment.java | 2 +- .../slimefun4/utils/SlimefunUtils.java | 17 +++++++++++++ 6 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/SlimefunItemSpawnEvent.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/SlimefunItemSpawnEvent.java index 27f544e7e8..14dc2b4abf 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/SlimefunItemSpawnEvent.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/SlimefunItemSpawnEvent.java @@ -1,10 +1,14 @@ package io.github.thebusybiscuit.slimefun4.api.events; +import java.util.Optional; + import javax.annotation.Nonnull; +import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; import org.apache.commons.lang.Validate; import org.bukkit.Location; +import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; @@ -29,6 +33,7 @@ public class SlimefunItemSpawnEvent extends Event implements Cancellable { private ItemStack itemStack; private boolean cancelled; private final ItemSpawnReason itemSpawnReason; + private final Player player; @ParametersAreNonnullByDefault public SlimefunItemSpawnEvent(Location location, ItemStack itemStack, ItemSpawnReason itemSpawnReason) { @@ -36,6 +41,25 @@ public SlimefunItemSpawnEvent(Location location, ItemStack itemStack, ItemSpawnR this.itemStack = itemStack; this.itemSpawnReason = itemSpawnReason; this.cancelled = false; + this.player = null; + } + + @ParametersAreNonnullByDefault + public SlimefunItemSpawnEvent(@Nullable Player player, Location location, ItemStack itemStack, ItemSpawnReason itemSpawnReason) { + this.location = location; + this.itemStack = itemStack; + this.itemSpawnReason = itemSpawnReason; + this.cancelled = false; + this.player = player; + } + + /** + * Optionally returns the {@link Player} responsible for this spawn reason. + * + * @return The player responsible if applicable. + */ + public Optional getPlayer() { + return Optional.ofNullable(player); } /** diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/altar/AncientPedestal.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/altar/AncientPedestal.java index 99a7b15585..f0a4fa95c0 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/altar/AncientPedestal.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/altar/AncientPedestal.java @@ -167,7 +167,7 @@ public void placeItem(@Nonnull Player p, @Nonnull Block b) { ItemUtils.consumeItem(hand, false); } - Item entity = SlimefunUtils.spawnItem(b.getLocation().add(0.5, 1.2, 0.5), displayItem, ItemSpawnReason.ANCIENT_PEDESTAL_PLACE_ITEM); + Item entity = SlimefunUtils.spawnItem(p, b.getLocation().add(0.5, 1.2, 0.5), displayItem, ItemSpawnReason.ANCIENT_PEDESTAL_PLACE_ITEM, false); if (entity != null) { ArmorStand armorStand = getArmorStand(b, true); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/seasonal/ChristmasPresent.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/seasonal/ChristmasPresent.java index f4cc010628..3d16205a8f 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/seasonal/ChristmasPresent.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/seasonal/ChristmasPresent.java @@ -55,7 +55,7 @@ public ChristmasPresent(ItemGroup itemGroup, SlimefunItemStack item, RecipeType Block b = block.getRelative(e.getClickedFace()); ItemStack gift = gifts[ThreadLocalRandom.current().nextInt(gifts.length)].clone(); - SlimefunUtils.spawnItem(b.getLocation(), gift, ItemSpawnReason.CHRISTMAS_PRESENT_OPENED, true); + SlimefunUtils.spawnItem(e.getPlayer(), b.getLocation(), gift, ItemSpawnReason.CHRISTMAS_PRESENT_OPENED, true); }); }; } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/GoldPan.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/GoldPan.java index 01478526cb..d7c0fb52b2 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/GoldPan.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/GoldPan.java @@ -154,7 +154,7 @@ public void updateRandomizer() { // Make sure that the randomly selected item is not air if (output.getType() != Material.AIR) { - SlimefunUtils.spawnItem(b.getLocation(), output.clone(), ItemSpawnReason.GOLD_PAN_USE, true); + SlimefunUtils.spawnItem(e.getPlayer(), b.getLocation(), output.clone(), ItemSpawnReason.GOLD_PAN_USE, true); } } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/PickaxeOfContainment.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/PickaxeOfContainment.java index c8064299a0..6be4cc5d71 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/PickaxeOfContainment.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/PickaxeOfContainment.java @@ -50,7 +50,7 @@ public PickaxeOfContainment(ItemGroup itemGroup, SlimefunItemStack item, RecipeT if (b.getType() == Material.SPAWNER) { ItemStack spawner = breakSpawner(b); - SlimefunUtils.spawnItem(b.getLocation(), spawner, ItemSpawnReason.BROKEN_SPAWNER_DROP, true); + SlimefunUtils.spawnItem(e.getPlayer(), b.getLocation(), spawner, ItemSpawnReason.BROKEN_SPAWNER_DROP, true); e.setExpToDrop(0); e.setDropItems(false); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java index f57aaf4ac7..ada4a7db6f 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java @@ -610,6 +610,23 @@ public static boolean canPlayerUseItem(@Nonnull Player p, @Nullable ItemStack it } } + public static @Nullable Item spawnItem(Player player, Location loc, ItemStack item, ItemSpawnReason reason, boolean addRandomOffset) { + SlimefunItemSpawnEvent event = new SlimefunItemSpawnEvent(player, loc, item, reason); + Slimefun.instance().getServer().getPluginManager().callEvent(event); + + if (!event.isCancelled()) { + World world = event.getLocation().getWorld(); + + if (addRandomOffset) { + return world.dropItemNaturally(event.getLocation(), event.getItemStack()); + } else { + return world.dropItem(event.getLocation(), event.getItemStack()); + } + } else { + return null; + } + } + /** * Helper method to spawn an {@link ItemStack}. * This method automatically calls a {@link SlimefunItemSpawnEvent} to allow From bd4ca3e72fe156b20dfb9913943bc39f89d42cbe Mon Sep 17 00:00:00 2001 From: Silent <46107752+TheSilentPro@users.noreply.github.com> Date: Sun, 15 Oct 2023 03:47:31 -0700 Subject: [PATCH 2/4] Update src/main/java/io/github/thebusybiscuit/slimefun4/api/events/SlimefunItemSpawnEvent.java Mark optional as nonnull Co-authored-by: TheBusyBiscuit --- .../slimefun4/api/events/SlimefunItemSpawnEvent.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/SlimefunItemSpawnEvent.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/SlimefunItemSpawnEvent.java index 14dc2b4abf..f7aa63f3e0 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/SlimefunItemSpawnEvent.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/SlimefunItemSpawnEvent.java @@ -58,7 +58,7 @@ public SlimefunItemSpawnEvent(@Nullable Player player, Location location, ItemSt * * @return The player responsible if applicable. */ - public Optional getPlayer() { + public @Nonnull Optional getPlayer() { return Optional.ofNullable(player); } From 5527729109108e7c262a38ee9c883d4bf9240854 Mon Sep 17 00:00:00 2001 From: Silent Date: Mon, 16 Oct 2023 14:24:10 -0700 Subject: [PATCH 3/4] Reduce repeated code --- .../api/events/SlimefunItemSpawnEvent.java | 12 ++---- .../items/altar/AncientPedestal.java | 2 +- .../items/seasonal/ChristmasPresent.java | 2 +- .../implementation/items/tools/GoldPan.java | 2 +- .../items/tools/PickaxeOfContainment.java | 2 +- .../slimefun4/utils/SlimefunUtils.java | 40 +++++++++++-------- 6 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/SlimefunItemSpawnEvent.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/SlimefunItemSpawnEvent.java index 14dc2b4abf..07d284050d 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/SlimefunItemSpawnEvent.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/SlimefunItemSpawnEvent.java @@ -36,21 +36,17 @@ public class SlimefunItemSpawnEvent extends Event implements Cancellable { private final Player player; @ParametersAreNonnullByDefault - public SlimefunItemSpawnEvent(Location location, ItemStack itemStack, ItemSpawnReason itemSpawnReason) { + public SlimefunItemSpawnEvent(@Nullable Player player, Location location, ItemStack itemStack, ItemSpawnReason itemSpawnReason) { this.location = location; this.itemStack = itemStack; this.itemSpawnReason = itemSpawnReason; this.cancelled = false; - this.player = null; + this.player = player; } @ParametersAreNonnullByDefault - public SlimefunItemSpawnEvent(@Nullable Player player, Location location, ItemStack itemStack, ItemSpawnReason itemSpawnReason) { - this.location = location; - this.itemStack = itemStack; - this.itemSpawnReason = itemSpawnReason; - this.cancelled = false; - this.player = player; + public SlimefunItemSpawnEvent(Location location, ItemStack itemStack, ItemSpawnReason itemSpawnReason) { + this(null, location, itemStack, itemSpawnReason); } /** diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/altar/AncientPedestal.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/altar/AncientPedestal.java index f0a4fa95c0..bbf19d36d8 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/altar/AncientPedestal.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/altar/AncientPedestal.java @@ -167,7 +167,7 @@ public void placeItem(@Nonnull Player p, @Nonnull Block b) { ItemUtils.consumeItem(hand, false); } - Item entity = SlimefunUtils.spawnItem(p, b.getLocation().add(0.5, 1.2, 0.5), displayItem, ItemSpawnReason.ANCIENT_PEDESTAL_PLACE_ITEM, false); + Item entity = SlimefunUtils.spawnItem(b.getLocation().add(0.5, 1.2, 0.5), displayItem, ItemSpawnReason.ANCIENT_PEDESTAL_PLACE_ITEM, false, p); if (entity != null) { ArmorStand armorStand = getArmorStand(b, true); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/seasonal/ChristmasPresent.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/seasonal/ChristmasPresent.java index 3d16205a8f..37ec6cc244 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/seasonal/ChristmasPresent.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/seasonal/ChristmasPresent.java @@ -55,7 +55,7 @@ public ChristmasPresent(ItemGroup itemGroup, SlimefunItemStack item, RecipeType Block b = block.getRelative(e.getClickedFace()); ItemStack gift = gifts[ThreadLocalRandom.current().nextInt(gifts.length)].clone(); - SlimefunUtils.spawnItem(e.getPlayer(), b.getLocation(), gift, ItemSpawnReason.CHRISTMAS_PRESENT_OPENED, true); + SlimefunUtils.spawnItem(b.getLocation(), gift, ItemSpawnReason.CHRISTMAS_PRESENT_OPENED, true, e.getPlayer()); }); }; } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/GoldPan.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/GoldPan.java index d7c0fb52b2..08f69ec27a 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/GoldPan.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/GoldPan.java @@ -154,7 +154,7 @@ public void updateRandomizer() { // Make sure that the randomly selected item is not air if (output.getType() != Material.AIR) { - SlimefunUtils.spawnItem(e.getPlayer(), b.getLocation(), output.clone(), ItemSpawnReason.GOLD_PAN_USE, true); + SlimefunUtils.spawnItem(b.getLocation(), output.clone(), ItemSpawnReason.GOLD_PAN_USE, true, e.getPlayer()); } } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/PickaxeOfContainment.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/PickaxeOfContainment.java index 6be4cc5d71..86499f2bee 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/PickaxeOfContainment.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/PickaxeOfContainment.java @@ -50,7 +50,7 @@ public PickaxeOfContainment(ItemGroup itemGroup, SlimefunItemStack item, RecipeT if (b.getType() == Material.SPAWNER) { ItemStack spawner = breakSpawner(b); - SlimefunUtils.spawnItem(e.getPlayer(), b.getLocation(), spawner, ItemSpawnReason.BROKEN_SPAWNER_DROP, true); + SlimefunUtils.spawnItem(b.getLocation(), spawner, ItemSpawnReason.BROKEN_SPAWNER_DROP, true, e.getPlayer()); e.setExpToDrop(0); e.setDropItems(false); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java index ada4a7db6f..be19a2b5ab 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java @@ -589,12 +589,14 @@ public static boolean canPlayerUseItem(@Nonnull Player p, @Nullable ItemStack it * The {@link ItemSpawnReason} why the item is being dropped * @param addRandomOffset * Whether a random offset should be added (see {@link World#dropItemNaturally(Location, ItemStack)}) + * @param player + * The player that caused this {@link org.bukkit.event.entity.ItemSpawnEvent} * * @return The dropped {@link Item} (or null if the {@link SlimefunItemSpawnEvent} was cancelled) */ @ParametersAreNonnullByDefault - public static @Nullable Item spawnItem(Location loc, ItemStack item, ItemSpawnReason reason, boolean addRandomOffset) { - SlimefunItemSpawnEvent event = new SlimefunItemSpawnEvent(loc, item, reason); + public static @Nullable Item spawnItem(Location loc, ItemStack item, ItemSpawnReason reason, boolean addRandomOffset, @Nullable Player player) { + SlimefunItemSpawnEvent event = new SlimefunItemSpawnEvent(player, loc, item, reason); Slimefun.instance().getServer().getPluginManager().callEvent(event); if (!event.isCancelled()) { @@ -610,21 +612,25 @@ public static boolean canPlayerUseItem(@Nonnull Player p, @Nullable ItemStack it } } - public static @Nullable Item spawnItem(Player player, Location loc, ItemStack item, ItemSpawnReason reason, boolean addRandomOffset) { - SlimefunItemSpawnEvent event = new SlimefunItemSpawnEvent(player, loc, item, reason); - Slimefun.instance().getServer().getPluginManager().callEvent(event); - - if (!event.isCancelled()) { - World world = event.getLocation().getWorld(); - - if (addRandomOffset) { - return world.dropItemNaturally(event.getLocation(), event.getItemStack()); - } else { - return world.dropItem(event.getLocation(), event.getItemStack()); - } - } else { - return null; - } + /** + * Helper method to spawn an {@link ItemStack}. + * This method automatically calls a {@link SlimefunItemSpawnEvent} to allow + * other plugins to catch the item being dropped. + * + * @param loc + * The {@link Location} where to drop the item + * @param item + * The {@link ItemStack} to drop + * @param reason + * The {@link ItemSpawnReason} why the item is being dropped + * @param addRandomOffset + * Whether a random offset should be added (see {@link World#dropItemNaturally(Location, ItemStack)}) + * + * @return The dropped {@link Item} (or null if the {@link SlimefunItemSpawnEvent} was cancelled) + */ + @ParametersAreNonnullByDefault + public static @Nullable Item spawnItem(Location loc, ItemStack item, ItemSpawnReason reason, boolean addRandomOffset) { + return spawnItem(loc, item, reason, addRandomOffset, null); } /** From cb736d314e28b59f89f3f9ddf92ece2ca0e9e246 Mon Sep 17 00:00:00 2001 From: Silent Date: Mon, 16 Oct 2023 23:27:25 -0700 Subject: [PATCH 4/4] fix qualifier --- .../io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java index be19a2b5ab..5fc3bda47f 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java @@ -590,7 +590,7 @@ public static boolean canPlayerUseItem(@Nonnull Player p, @Nullable ItemStack it * @param addRandomOffset * Whether a random offset should be added (see {@link World#dropItemNaturally(Location, ItemStack)}) * @param player - * The player that caused this {@link org.bukkit.event.entity.ItemSpawnEvent} + * The player that caused this {@link SlimefunItemSpawnEvent} * * @return The dropped {@link Item} (or null if the {@link SlimefunItemSpawnEvent} was cancelled) */