From 05e983917ab15de67ce0efa96518df56ddfe84e7 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Tue, 10 Nov 2020 00:36:38 +0100 Subject: [PATCH] Added CoolerFeedPlayerEvent --- CHANGELOG.md | 1 + .../api/events/CoolerFeedPlayerEvent.java | 113 ++++++++++++++++++ .../listeners/CoolerListener.java | 34 ++++-- .../tests/listeners/TestCoolerListener.java | 3 + 4 files changed, 140 insertions(+), 11 deletions(-) create mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/api/events/CoolerFeedPlayerEvent.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 0cd5949f8..2e0473ab3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ * The Smelters Pick now also works on Ancient Debris * (API) Added PlayerPreResearchEvent * Added a config option to disable network visualizations +* (API) Added CoolerFeedPlayerEvent #### Changes * Removed 1.13 support diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/CoolerFeedPlayerEvent.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/CoolerFeedPlayerEvent.java new file mode 100644 index 000000000..b6a3d579c --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/CoolerFeedPlayerEvent.java @@ -0,0 +1,113 @@ +package io.github.thebusybiscuit.slimefun4.api.events; + +import javax.annotation.Nonnull; +import javax.annotation.ParametersAreNonnullByDefault; + +import org.apache.commons.lang.Validate; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.PotionMeta; +import org.bukkit.potion.PotionEffect; + +import io.github.thebusybiscuit.slimefun4.implementation.items.backpacks.Cooler; + +/** + * This {@link Event} is called whenever a {@link Player} is + * fed through a {@link Cooler}. + * + * @author TheBusyBiscuit + * + * @see Cooler + * + */ +public class CoolerFeedPlayerEvent extends PlayerEvent implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + + private final Cooler cooler; + private final ItemStack coolerItem; + + private ItemStack consumedItem; + private boolean cancelled; + + @ParametersAreNonnullByDefault + public CoolerFeedPlayerEvent(Player player, Cooler cooler, ItemStack coolerItem, ItemStack consumedItem) { + super(player); + + this.cooler = cooler; + this.coolerItem = coolerItem; + this.consumedItem = consumedItem; + } + + /** + * This returns the {@link Cooler} that was used. + * + * @return The {@link Cooler} that was used + */ + @Nonnull + public Cooler getCooler() { + return cooler; + } + + /** + * This returns the {@link Cooler} that was used (as an {@link ItemStack}) + * + * @return The {@link Cooler} that was used + */ + @Nonnull + public ItemStack getCoolerItem() { + return coolerItem; + } + + /** + * This returns the {@link ItemStack} that was consumed. + * The returned {@link ItemStack} is immutable. + * + * @return The {@link ItemStack} that was consumed + */ + @Nonnull + public ItemStack getConsumedItem() { + return consumedItem.clone(); + } + + /** + * This sets the {@link ItemStack} that should be "consumed". + * The {@link ItemStack} must be a potion. + * The {@link Player} will receive the {@link PotionEffect PotionEffects} of the + * provided potion upon consumption. + * + * @param item + * The new {@link ItemStack} + */ + public void setConsumedItem(@Nonnull ItemStack item) { + Validate.notNull(item, "The consumed Item cannot be null!"); + Validate.isTrue(item.getItemMeta() instanceof PotionMeta, "The item must be a potion!"); + + this.consumedItem = item; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + + @Nonnull + public static HandlerList getHandlerList() { + return handlers; + } + + @Nonnull + @Override + public HandlerList getHandlers() { + return getHandlerList(); + } +} diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/CoolerListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/CoolerListener.java index c54f501d2..5b28f4d81 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/CoolerListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/CoolerListener.java @@ -15,6 +15,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.potion.PotionEffect; +import io.github.thebusybiscuit.slimefun4.api.events.CoolerFeedPlayerEvent; import io.github.thebusybiscuit.slimefun4.api.player.PlayerBackpack; import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; @@ -37,11 +38,13 @@ import me.mrCookieSlime.Slimefun.api.Slimefun; */ public class CoolerListener implements Listener { + private final SlimefunPlugin plugin; private final Cooler cooler; public CoolerListener(@Nonnull SlimefunPlugin plugin, @Nonnull Cooler cooler) { plugin.getServer().getPluginManager().registerEvents(this, plugin); + this.plugin = plugin; this.cooler = cooler; } @@ -93,12 +96,12 @@ public class CoolerListener implements Listener { private void takeJuiceFromCooler(@Nonnull Player p, @Nonnull ItemStack cooler) { PlayerProfile.getBackpack(cooler, backpack -> { if (backpack != null) { - SlimefunPlugin.runSync(() -> consumeJuice(p, backpack)); + SlimefunPlugin.runSync(() -> consumeJuice(p, cooler, backpack)); } }); } - private boolean consumeJuice(@Nonnull Player p, @Nonnull PlayerBackpack backpack) { + private boolean consumeJuice(@Nonnull Player p, @Nonnull ItemStack coolerItem, @Nonnull PlayerBackpack backpack) { Inventory inv = backpack.getInventory(); int slot = -1; @@ -112,17 +115,26 @@ public class CoolerListener implements Listener { } if (slot >= 0) { - PotionMeta im = (PotionMeta) inv.getItem(slot).getItemMeta(); + ItemStack item = inv.getItem(slot); + CoolerFeedPlayerEvent event = new CoolerFeedPlayerEvent(p, cooler, coolerItem, item); + plugin.getServer().getPluginManager().callEvent(event); - for (PotionEffect effect : im.getCustomEffects()) { - p.addPotionEffect(effect); + if (!event.isCancelled()) { + PotionMeta im = (PotionMeta) event.getConsumedItem().getItemMeta(); + + for (PotionEffect effect : im.getCustomEffects()) { + p.addPotionEffect(effect); + } + + p.setSaturation(6F); + p.playSound(p.getLocation(), Sound.ENTITY_GENERIC_DRINK, 1F, 1F); + inv.setItem(slot, null); + backpack.markDirty(); + + return true; + } else { + return false; } - - p.setSaturation(6F); - p.playSound(p.getLocation(), Sound.ENTITY_GENERIC_DRINK, 1F, 1F); - inv.setItem(slot, null); - backpack.markDirty(); - return true; } return false; diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/listeners/TestCoolerListener.java b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/listeners/TestCoolerListener.java index e3dd6b8e7..fe95c167b 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/listeners/TestCoolerListener.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/listeners/TestCoolerListener.java @@ -17,6 +17,7 @@ import org.junit.jupiter.api.Test; import be.seeseemelk.mockbukkit.MockBukkit; import be.seeseemelk.mockbukkit.ServerMock; import io.github.thebusybiscuit.cscorelib2.item.CustomItem; +import io.github.thebusybiscuit.slimefun4.api.events.CoolerFeedPlayerEvent; import io.github.thebusybiscuit.slimefun4.api.player.PlayerBackpack; import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; @@ -81,6 +82,8 @@ class TestCoolerListener { FoodLevelChangeEvent event = new FoodLevelChangeEvent(player, 16); listener.onHungerLoss(event); + Assertions.assertTrue(player.hasPotionEffect(PotionEffectType.HEALTH_BOOST)); + server.getPluginManager().assertEventFired(CoolerFeedPlayerEvent.class, e -> e.getPlayer() == player && e.getCooler() == cooler); } }