From ef1735985e3fae288f0beb654e7c703307a975d8 Mon Sep 17 00:00:00 2001 From: LinoxGH Date: Sun, 28 Jun 2020 16:42:46 +0300 Subject: [PATCH] Did the requested changes, enabled and fixed JUnit test. --- .../api/events/ClimbingPickLaunchEvent.java | 30 +++++- .../items/tools/ClimbingPick.java | 91 ++++++++----------- .../Objects/SlimefunItem/SlimefunItem.java | 1 - .../tools/TestClimbingPick.java | 11 +-- 4 files changed, 71 insertions(+), 62 deletions(-) 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 929f21948..2e7f37c0a 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,6 +1,7 @@ package io.github.thebusybiscuit.slimefun4.api.events; import org.apache.commons.lang.Validate; +import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.Event; @@ -26,13 +27,18 @@ public class ClimbingPickLaunchEvent extends PlayerEvent implements Cancellable private Vector velocity; private final ClimbingPick pick; + private final ItemStack itemStack; + private final Block block; + private boolean cancelled; - public ClimbingPickLaunchEvent(Player player, Vector velocity, ClimbingPick pick) { + public ClimbingPickLaunchEvent(Player player, Vector velocity, ClimbingPick pick, ItemStack itemStack, Block block) { super(player); this.velocity = velocity; this.pick = pick; + this.itemStack = itemStack; + this.block = block; } /** @@ -59,10 +65,28 @@ public class ClimbingPickLaunchEvent extends PlayerEvent implements Cancellable /** * This returns the {@link ClimbingPick} that was used. * + * @return The {@link ClimbingPick} that was used + */ + public ClimbingPick getPick() { + return this.pick; + } + + /** + * This returns the {@link ItemStack} that was used. + * * @return The {@link ItemStack} that was used */ - public ClimbingPick getItem() { - return this.pick; + public ItemStack getItemStack() { + return this.itemStack; + } + + /** + * This returns the {@link Block} that was climbed. + * + * @return The {@link Block} that was climbed + */ + public Block getBlock() { + return this.block; } @Override 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 ecc36364f..b317c258e 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 @@ -1,5 +1,6 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.tools; +import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -8,27 +9,26 @@ import java.util.Set; import java.util.UUID; import org.bukkit.Bukkit; +import org.bukkit.Effect; import org.bukkit.GameMode; import org.bukkit.Material; -import org.bukkit.Sound; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; -import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.util.Vector; +import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.cscorelib2.materials.MaterialCollections; import io.github.thebusybiscuit.slimefun4.api.events.ClimbingPickLaunchEvent; -import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting; import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion; import io.github.thebusybiscuit.slimefun4.core.attributes.DamageableItem; +import io.github.thebusybiscuit.slimefun4.core.attributes.RecipeDisplayItem; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Objects.Category; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SimpleSlimefunItem; -import me.mrCookieSlime.Slimefun.Objects.handlers.BlockBreakHandler; import me.mrCookieSlime.Slimefun.Objects.handlers.ItemUseHandler; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; @@ -40,15 +40,15 @@ import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; * @author Linox * */ -public class ClimbingPick extends SimpleSlimefunItem implements DamageableItem { +public class ClimbingPick extends SimpleSlimefunItem implements DamageableItem, RecipeDisplayItem { - private final Map materialSpeedsDef = new HashMap<>(); - private final ItemSetting> materialSpeeds = new ItemSetting<>("launch-amounts", materialSpeedsDef); + private final Map materialSpeeds; private final Set users = new HashSet<>(); public ClimbingPick(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { super(category, item, recipeType, recipe); + Map materialSpeedsDef = new HashMap<>(); for (Material mat : MaterialCollections.getAllIceBlocks()) { materialSpeedsDef.put(mat.name(), 1D); } @@ -61,25 +61,16 @@ public class ClimbingPick extends SimpleSlimefunItem implements materialSpeedsDef.put(Material.GRAVEL.name(), 0.4); materialSpeedsDef.put(Material.SAND.name(), 0.4); - materialSpeedsDef.put(Material.STONE.name(), 0.6D); - materialSpeedsDef.put(Material.DIORITE.name(), 0.6D); - materialSpeedsDef.put(Material.GRANITE.name(), 0.6D); - materialSpeedsDef.put(Material.ANDESITE.name(), 0.6D); - materialSpeedsDef.put(Material.NETHERRACK.name(), 0.6D); + materialSpeedsDef.put(Material.STONE.name(), 0.6); + materialSpeedsDef.put(Material.DIORITE.name(), 0.6); + materialSpeedsDef.put(Material.GRANITE.name(), 0.6); + materialSpeedsDef.put(Material.ANDESITE.name(), 0.6); + materialSpeedsDef.put(Material.NETHERRACK.name(), 0.6); if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_16)) { - materialSpeedsDef.put(Material.BLACKSTONE.name(), 0.6D); + materialSpeedsDef.put(Material.BLACKSTONE.name(), 0.6); } - - addItemSetting(materialSpeeds); + materialSpeeds = SlimefunPlugin.getItemCfg().getOrSetDefault(getID() + ".launch-amounts", materialSpeedsDef); } - - @Override - public void preRegister() { - super.preRegister(); - - addItemHandler(getBlockBreakHandler()); - } - @Override public ItemUseHandler getItemHandler() { @@ -90,58 +81,54 @@ public class ClimbingPick extends SimpleSlimefunItem implements ItemStack item = e.getItem(); Player p = e.getPlayer(); - if (!getID().startsWith("TEST_CLIMBING_PICK") && p.getLocation().distance(block.getLocation()) > 1.5) return; + if (p.getLocation().distanceSquared(block.getLocation()) > 2.25) return; if (e.getClickedFace() == BlockFace.DOWN || e.getClickedFace() == BlockFace.UP) return; if (!users.contains(p.getUniqueId())) { Material mat = block.getType(); - Double launch = materialSpeeds.getValue().get(mat.name()); + double launch = materialSpeeds.getOrDefault(mat.name(), 0D); Vector velocity = new Vector(0, 0, 0); - if (launch != null) { - Integer efficiencyLevel = item.getEnchantments().get(Enchantment.DIG_SPEED); - if (efficiencyLevel != null){ + if (launch > 0.05) { + int efficiencyLevel = item.getEnchantments().getOrDefault(Enchantment.DIG_SPEED, 0); + if (efficiencyLevel != 0){ launch += (efficiencyLevel * 0.2); } velocity.setY(launch); users.add(p.getUniqueId()); - Bukkit.getScheduler().runTaskLaterAsynchronously(SlimefunPlugin.instance, () -> users.remove(p.getUniqueId()), 4L); + Bukkit.getScheduler().runTaskLater(SlimefunPlugin.instance, () -> users.remove(p.getUniqueId()), 4L); } - ClimbingPickLaunchEvent event = new ClimbingPickLaunchEvent(p, velocity, this); + ClimbingPickLaunchEvent event = new ClimbingPickLaunchEvent(p, velocity, this, item, block); Bukkit.getPluginManager().callEvent(event); - p.setVelocity(event.getVelocity()); - p.playSound(p.getLocation(), Sound.ENTITY_ENDERMAN_TELEPORT, 1F, 1F); + if (!event.isCancelled()) { + p.setVelocity(event.getVelocity()); + p.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, 1); - if (p.getGameMode() != GameMode.CREATIVE) { - damageItem(p, e.getItem()); + if (p.getGameMode() != GameMode.CREATIVE) { + damageItem(p, e.getItem()); + } } } }; } - private BlockBreakHandler getBlockBreakHandler() { - return new BlockBreakHandler() { - @Override - public boolean onBlockBreak(BlockBreakEvent e, ItemStack item, int fortune, List drops) { - if (isItem(item)) { - e.setCancelled(true); - return true; - } - return false; - } - - @Override - public boolean isPrivate() { - return false; - } - }; - } - @Override public boolean isDamageable() { return true; } + + @Override + public List getDisplayRecipes() { + List display = new ArrayList<>(); + for (String matName : materialSpeeds.keySet()) { + Material mat = Material.getMaterial(matName); + if (mat == null) continue; + display.add(new CustomItem(mat, "&bCan Climb This Block")); + } + + return display; + } } diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/SlimefunItem.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/SlimefunItem.java index e181cc57a..755b679de 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/SlimefunItem.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/SlimefunItem.java @@ -430,7 +430,6 @@ public class SlimefunItem implements Placeable { if (!handler.isPrivate()) { Set handlerset = getPublicItemHandlers(handler.getIdentifier()); handlerset.add(handler); - SlimefunPlugin.getRegistry().getPublicItemHandlers().replace(handler.getIdentifier(), handlerset); } } } 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 288a3423f..92fcc3ff8 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,12 +1,12 @@ package io.github.thebusybiscuit.slimefun4.testing.tests.items.implementations.tools; +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.BeforeAll; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; @@ -49,20 +49,19 @@ public class TestClimbingPick implements SlimefunItemTest { @ParameterizedTest @EnumSource(value = BlockFace.class) - @Disabled("Player velocity is currently not fully implemented in MockBukkit") public void testItemUse(BlockFace face) { 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 = false; - if (face == BlockFace.DOWN || face == BlockFace.UP) shouldCancel = true; + boolean shouldCancel = face == BlockFace.DOWN || face == BlockFace.UP; - BlockMock block1 = new BlockMock(Material.STONE); + BlockMock block1 = new BlockMock(Material.STONE, 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); + 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);