1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

Fixed Unit Tests for Climbing Pick

This commit is contained in:
TheBusyBiscuit 2020-08-24 20:45:45 +02:00
parent 9e92f64222
commit 89a5db2593
2 changed files with 43 additions and 25 deletions

View File

@ -149,8 +149,7 @@ public class ClimbingPick extends SimpleSlimefunItem<ItemUseHandler> implements
if (!event.isCancelled()) { if (!event.isCancelled()) {
Slimefun.runSync(() -> users.remove(p.getUniqueId()), 3L); Slimefun.runSync(() -> users.remove(p.getUniqueId()), 3L);
p.setVelocity(event.getVelocity()); p.setVelocity(event.getVelocity());
p.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, block.getType()); swing(p, block, hand, item);
swing(p, hand, item);
} }
} }
} }
@ -159,27 +158,32 @@ public class ClimbingPick extends SimpleSlimefunItem<ItemUseHandler> 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 (p.getGameMode() != GameMode.CREATIVE) {
if (isDualWieldingEnabled()) { if (isDualWieldingEnabled()) {
if (ThreadLocalRandom.current().nextBoolean()) { if (ThreadLocalRandom.current().nextBoolean()) {
damageItem(p, p.getInventory().getItemInMainHand()); damageItem(p, p.getInventory().getItemInMainHand());
playSwingAnimation(p, EquipmentSlot.HAND); playAnimation(p, b, EquipmentSlot.HAND);
} }
else { else {
damageItem(p, p.getInventory().getItemInOffHand()); damageItem(p, p.getInventory().getItemInOffHand());
playSwingAnimation(p, EquipmentSlot.OFF_HAND); playAnimation(p, b, EquipmentSlot.OFF_HAND);
} }
} }
else { else {
damageItem(p, item); damageItem(p, item);
playSwingAnimation(p, hand); playAnimation(p, b, hand);
} }
} }
} }
private void playSwingAnimation(Player p, EquipmentSlot hand) { private void playAnimation(Player p, Block b, EquipmentSlot hand) {
if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_15)) { 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) { if (hand == EquipmentSlot.HAND) {
p.swingMainHand(); p.swingMainHand();
} }
@ -188,6 +192,7 @@ public class ClimbingPick extends SimpleSlimefunItem<ItemUseHandler> implements
} }
} }
} }
}
@Override @Override
public boolean isDamageable() { public boolean isDamageable() {

View File

@ -2,11 +2,12 @@ package io.github.thebusybiscuit.slimefun4.testing.tests.items.implementations.t
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource; 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.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
public class TestClimbingPick implements SlimefunItemTest<ClimbingPick> { class TestClimbingPick implements SlimefunItemTest<ClimbingPick> {
private static ServerMock server; private static ServerMock server;
private static SlimefunPlugin plugin; private static SlimefunPlugin plugin;
@ -42,28 +43,40 @@ public class TestClimbingPick implements SlimefunItemTest<ClimbingPick> {
@Override @Override
public ClimbingPick registerSlimefunItem(SlimefunPlugin plugin, String id) { public ClimbingPick registerSlimefunItem(SlimefunPlugin plugin, String id) {
SlimefunItemStack item = new SlimefunItemStack(id, Material.IRON_PICKAXE, "&5Test Pick"); 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); pick.register(plugin);
return pick; return pick;
} }
@ParameterizedTest @ParameterizedTest
@DisplayName("Test Climbing Pick on various Block Faces")
@EnumSource(value = BlockFace.class) @EnumSource(value = BlockFace.class)
public void testItemUse(BlockFace face) { void testItemUse(BlockFace face) {
server.getPluginManager().clearEvents();
PlayerMock player = server.addPlayer(); PlayerMock player = server.addPlayer();
ClimbingPick pick = registerSlimefunItem(plugin, "TEST_CLIMBING_PICK_" + face.name()); 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()); 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); 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); if (shouldFireEvent) {
simulateRightClickBlock(player, pick, block2, face);
server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class); server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class);
if (!shouldCancel) player.assertSoundHeard(Sound.ENTITY_ENDERMAN_TELEPORT); Assertions.assertTrue(player.getVelocity().length() > 0);
}
else {
Assertions.assertEquals(0, player.getVelocity().length());
}
} }
} }