From 89a5db259325a2c3b5ccbd206e9aa2bab36b8c29 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Mon, 24 Aug 2020 20:45:45 +0200 Subject: [PATCH] Fixed Unit Tests for Climbing Pick --- .../items/tools/ClimbingPick.java | 31 +++++++++------- .../tools/TestClimbingPick.java | 37 +++++++++++++------ 2 files changed, 43 insertions(+), 25 deletions(-) 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 76915c11b..5551ef14a 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 @@ -149,8 +149,7 @@ public class ClimbingPick extends SimpleSlimefunItem implements if (!event.isCancelled()) { Slimefun.runSync(() -> users.remove(p.getUniqueId()), 3L); p.setVelocity(event.getVelocity()); - p.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, block.getType()); - swing(p, hand, item); + swing(p, block, hand, item); } } } @@ -159,32 +158,38 @@ public class ClimbingPick extends SimpleSlimefunItem implements } } - private void swing(Player p, EquipmentSlot hand, ItemStack item) { + private void swing(Player p, Block b, EquipmentSlot hand, ItemStack item) { if (p.getGameMode() != GameMode.CREATIVE) { if (isDualWieldingEnabled()) { if (ThreadLocalRandom.current().nextBoolean()) { damageItem(p, p.getInventory().getItemInMainHand()); - playSwingAnimation(p, EquipmentSlot.HAND); + playAnimation(p, b, EquipmentSlot.HAND); } else { damageItem(p, p.getInventory().getItemInOffHand()); - playSwingAnimation(p, EquipmentSlot.OFF_HAND); + playAnimation(p, b, EquipmentSlot.OFF_HAND); } } else { damageItem(p, item); - playSwingAnimation(p, hand); + playAnimation(p, b, hand); } } } - private void playSwingAnimation(Player p, EquipmentSlot hand) { - if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_15)) { - if (hand == EquipmentSlot.HAND) { - p.swingMainHand(); - } - else { - p.swingOffHand(); + private void playAnimation(Player p, Block b, EquipmentSlot hand) { + MinecraftVersion version = SlimefunPlugin.getMinecraftVersion(); + + if (version != MinecraftVersion.UNIT_TEST) { + p.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, b.getType()); + + if (version.isAtLeast(MinecraftVersion.MINECRAFT_1_15)) { + if (hand == EquipmentSlot.HAND) { + p.swingMainHand(); + } + else { + p.swingOffHand(); + } } } } 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 92fcc3ff8..4acbc0474 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 @@ -2,11 +2,12 @@ package io.github.thebusybiscuit.slimefun4.testing.tests.items.implementations.t import org.bukkit.Location; 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.Assertions; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; @@ -22,7 +23,7 @@ import io.github.thebusybiscuit.slimefun4.testing.interfaces.SlimefunItemTest; import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; -public class TestClimbingPick implements SlimefunItemTest { +class TestClimbingPick implements SlimefunItemTest { private static ServerMock server; private static SlimefunPlugin plugin; @@ -42,28 +43,40 @@ public class TestClimbingPick implements SlimefunItemTest { @Override public ClimbingPick registerSlimefunItem(SlimefunPlugin plugin, String id) { SlimefunItemStack item = new SlimefunItemStack(id, Material.IRON_PICKAXE, "&5Test Pick"); - ClimbingPick pick = new ClimbingPick(TestUtilities.getCategory(plugin, "climbing_pick"), item, RecipeType.NULL, new ItemStack[9]); + + ClimbingPick pick = new ClimbingPick(TestUtilities.getCategory(plugin, "climbing_pick"), item, RecipeType.NULL, new ItemStack[9]) { + + @Override + public boolean isDualWieldingEnabled() { + return false; + } + + }; + pick.register(plugin); return pick; } @ParameterizedTest + @DisplayName("Test Climbing Pick on various Block Faces") @EnumSource(value = BlockFace.class) - public void testItemUse(BlockFace face) { + void testItemUse(BlockFace face) { + server.getPluginManager().clearEvents(); PlayerMock player = server.addPlayer(); ClimbingPick pick = registerSlimefunItem(plugin, "TEST_CLIMBING_PICK_" + face.name()); Location blockLocation = new Location(player.getLocation().getWorld(), player.getLocation().getBlockX() + 1, player.getLocation().getBlockY(), player.getLocation().getBlockZ()); - boolean shouldCancel = face == BlockFace.DOWN || face == BlockFace.UP; + boolean shouldFireEvent = face != BlockFace.DOWN && face != BlockFace.UP; - BlockMock block1 = new BlockMock(Material.STONE, blockLocation); + BlockMock block1 = new BlockMock(Material.ICE, blockLocation); simulateRightClickBlock(player, pick, block1, face); - server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class); - if (!shouldCancel) player.assertSoundHeard(Sound.ENTITY_ENDERMAN_TELEPORT); - BlockMock block2 = new BlockMock(Material.DIRT, blockLocation); - simulateRightClickBlock(player, pick, block2, face); - server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class); - if (!shouldCancel) player.assertSoundHeard(Sound.ENTITY_ENDERMAN_TELEPORT); + if (shouldFireEvent) { + server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class); + Assertions.assertTrue(player.getVelocity().length() > 0); + } + else { + Assertions.assertEquals(0, player.getVelocity().length()); + } } }