diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/ClimbingPickLaunchEvent.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/ClimbingPickLaunchEvent.java index 8e453e06b..929f21948 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/ClimbingPickLaunchEvent.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/ClimbingPickLaunchEvent.java @@ -1,9 +1,11 @@ package io.github.thebusybiscuit.slimefun4.api.events; +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.util.Vector; @@ -18,42 +20,23 @@ import io.github.thebusybiscuit.slimefun4.implementation.items.tools.ClimbingPic * @see ClimbingPick * */ -public class ClimbingPickLaunchEvent extends Event implements Cancellable { +public class ClimbingPickLaunchEvent extends PlayerEvent implements Cancellable { private static final HandlerList handlers = new HandlerList(); - private final Player player; - private final Vector velocity; - private final ItemStack pick; + private Vector velocity; + private final ClimbingPick pick; private boolean cancelled; - public ClimbingPickLaunchEvent(Player player, Vector velocity, ItemStack pick) { - super(false); + public ClimbingPickLaunchEvent(Player player, Vector velocity, ClimbingPick pick) { + super(player); - this.player = player; this.velocity = velocity; this.pick = pick; } - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } - /** - * This returns the {@link Player} that used the {@link ClimbingPick}. - * - * @return The {@link Player} that used - */ - public Player getPlayer() { - return player; - } - - /** - * This returns the {@link Vector} velocity that was applied to the {@link Player} + * This returns the velocity {@link Vector} that was applied to the {@link Player} * who used the {@link ClimbingPick}. * * @return The {@link Vector} of the applied velocity @@ -63,11 +46,22 @@ public class ClimbingPickLaunchEvent extends Event implements Cancellable { } /** - * This returns the {@link ClimbingPick} {@link ItemStack} that was used. + * Use this to change the velocity {@link Vector} applied to the {@link Player}. + * + * @param velocity + * The {@link Vector} velocity to apply + */ + public void setVelocity(Vector velocity) { + Validate.notNull(velocity); + this.velocity = velocity; + } + + /** + * This returns the {@link ClimbingPick} that was used. * * @return The {@link ItemStack} that was used */ - public ItemStack getItem() { + public ClimbingPick getItem() { return this.pick; } @@ -81,4 +75,8 @@ public class ClimbingPickLaunchEvent extends Event implements Cancellable { this.cancelled = cancel; } + @Override + public HandlerList getHandlers() { + return handlers; + } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/ClimbingPick.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/ClimbingPick.java index 09014b32a..d10e822a1 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/ClimbingPick.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/ClimbingPick.java @@ -91,7 +91,7 @@ public class ClimbingPick extends SimpleSlimefunItem implements ItemStack item = e.getItem(); Player p = e.getPlayer(); - if (!getID().equals("TEST_CLIMBING_PICK") && p.getLocation().distance(block.getLocation()) > 1.5) return; + if (!getID().startsWith("TEST_CLIMBING_PICK") && p.getLocation().distance(block.getLocation()) > 1.5) return; if (e.getClickedFace() == BlockFace.DOWN || e.getClickedFace() == BlockFace.UP) return; if (!users.contains(p.getUniqueId())) { @@ -110,7 +110,7 @@ public class ClimbingPick extends SimpleSlimefunItem implements Bukkit.getScheduler().runTaskLaterAsynchronously(SlimefunPlugin.instance, () -> users.remove(p.getUniqueId()), 4L); } - ClimbingPickLaunchEvent event = new ClimbingPickLaunchEvent(p, velocity, item); + ClimbingPickLaunchEvent event = new ClimbingPickLaunchEvent(p, velocity, this); Bukkit.getPluginManager().callEvent(event); p.setVelocity(event.getVelocity()); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/ExplosivePickaxe.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/ExplosivePickaxe.java index 27a01f047..b019499e7 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/ExplosivePickaxe.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/ExplosivePickaxe.java @@ -1,5 +1,6 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.tools; +import org.bukkit.block.Block; import org.bukkit.inventory.ItemStack; import me.mrCookieSlime.Slimefun.Lists.RecipeType; @@ -7,12 +8,13 @@ import me.mrCookieSlime.Slimefun.Objects.Category; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; /** - * The {@link ExplosivePickaxe} is a pickaxe which can destroy blocks in a size of 3 by 3. - * It also creates a explosion animation. + * The {@link ExplosivePickaxe} is a pickaxe which can destroy {@link Block}s + * in a size of 3 by 3. It also creates a explosion animation. * * @author TheBusyBiscuit * * @see ExplosiveShovel + * @see ExplosiveTool * */ public class ExplosivePickaxe extends ExplosiveTool { diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/ExplosiveShovel.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/ExplosiveShovel.java index e5c185c1b..348d2eb76 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/ExplosiveShovel.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/ExplosiveShovel.java @@ -20,7 +20,8 @@ import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; * However it can only break blocks that a shovel can break. * * @author Linox - * + * + * @see ExplosivePickaxe * @see ExplosiveTool * */ diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/ExplosiveTool.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/ExplosiveTool.java index 800e8a60c..549445851 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/ExplosiveTool.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/ExplosiveTool.java @@ -59,7 +59,7 @@ class ExplosiveTool extends SimpleSlimefunItem implements Not List blocks = findBlocks(e.getBlock()); - if (callExplosionEvent.getValue()) { + if (callExplosionEvent.getValue().booleanValue()) { BlockExplodeEvent blockExplodeEvent = new BlockExplodeEvent(e.getBlock(), blocks, 0); Bukkit.getServer().getPluginManager().callEvent(blockExplodeEvent); diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/testing/interfaces/SlimefunItemTest.java b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/interfaces/SlimefunItemTest.java index 57450e95f..8aad6160f 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/testing/interfaces/SlimefunItemTest.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/interfaces/SlimefunItemTest.java @@ -36,14 +36,15 @@ public interface SlimefunItemTest { item.callItemHandler(ItemUseHandler.class, handler -> handler.onRightClick(event)); } - default void simulateConsumption(Player player, T item) { - PlayerItemConsumeEvent event = new PlayerItemConsumeEvent(player, item.getItem().clone()); - item.callItemHandler(ItemConsumptionHandler.class, handler -> handler.onConsume(event, player, event.getItem())); - } - default void simulateRightClickBlock(Player player, T item, BlockMock block, BlockFace face) { PlayerInteractEvent e = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, item.getItem().clone(), block, face, EquipmentSlot.HAND); PlayerRightClickEvent event = new PlayerRightClickEvent(e); item.callItemHandler(ItemUseHandler.class, handler -> handler.onRightClick(event)); } + + default void simulateConsumption(Player player, T item) { + PlayerItemConsumeEvent event = new PlayerItemConsumeEvent(player, item.getItem().clone()); + item.callItemHandler(ItemConsumptionHandler.class, handler -> handler.onConsume(event, player, event.getItem())); + } + } diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/items/implementations/tools/TestClimbingPick.java b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/items/implementations/tools/TestClimbingPick.java index 1955df367..5a805bdd4 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/items/implementations/tools/TestClimbingPick.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/items/implementations/tools/TestClimbingPick.java @@ -1,11 +1,13 @@ package io.github.thebusybiscuit.slimefun4.testing.tests.items.implementations.tools; import org.bukkit.Material; +import org.bukkit.Sound; import org.bukkit.block.BlockFace; import org.bukkit.inventory.ItemStack; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; import be.seeseemelk.mockbukkit.MockBukkit; import be.seeseemelk.mockbukkit.ServerMock; @@ -23,7 +25,6 @@ public class TestClimbingPick implements SlimefunItemTest { private static ServerMock server; private static SlimefunPlugin plugin; - private static ClimbingPick pick; @BeforeAll public static void load() { @@ -45,19 +46,23 @@ public class TestClimbingPick implements SlimefunItemTest { return pick; } - @Test - public void testItemUse() { + @ParameterizedTest + @EnumSource(value = BlockFace.class) + public void testItemUse(BlockFace face) { PlayerMock player = server.addPlayer(); - if (pick == null) pick = registerSlimefunItem(plugin, "TEST_CLIMBING_PICK"); + ClimbingPick pick = registerSlimefunItem(plugin, "TEST_CLIMBING_PICK_" + face.name()); - for (BlockFace face : BlockFace.values()) { - BlockMock block1 = new BlockMock(Material.STONE); - simulateRightClickBlock(player, pick, block1, face); - server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class, e -> !e.isCancelled()); + boolean shouldCancel = false; + if (face == BlockFace.DOWN || face == BlockFace.UP) shouldCancel = true; - BlockMock block2 = new BlockMock(Material.DIRT); - simulateRightClickBlock(player, pick, block2, face); - server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class, ClimbingPickLaunchEvent::isCancelled); - } + BlockMock block1 = new BlockMock(Material.STONE); + simulateRightClickBlock(player, pick, block1, face); + server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class); + if (!shouldCancel) player.assertSoundHeard(Sound.ENTITY_ENDERMAN_TELEPORT); + + BlockMock block2 = new BlockMock(Material.DIRT); + simulateRightClickBlock(player, pick, block2, face); + server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class); + if (!shouldCancel) player.assertSoundHeard(Sound.ENTITY_ENDERMAN_TELEPORT); } }