From f1aa8a08d0b9ea7dd9df7f542985c3af71faab0d Mon Sep 17 00:00:00 2001 From: LinoxGH Date: Fri, 26 Jun 2020 23:59:40 +0300 Subject: [PATCH] Did the requested changes --- .../items/tools/ClimbingPick.java | 57 ++++++++++++------- .../listeners/ClimbingPickListener.java | 41 ------------- .../Slimefun/SlimefunPlugin.java | 2 - .../tools/TestClimbingPick.java | 4 +- 4 files changed, 37 insertions(+), 67 deletions(-) delete mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/ClimbingPickListener.java 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 21dba944a..940df9903 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 @@ -7,8 +7,11 @@ import java.util.Map; import java.util.Set; import java.util.UUID; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; import org.bukkit.Bukkit; +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; @@ -63,8 +66,10 @@ public class ClimbingPick extends SimpleSlimefunItem implements materialSpeedsDef.put(Material.DIORITE.name(), 0.6D); materialSpeedsDef.put(Material.GRANITE.name(), 0.6D); materialSpeedsDef.put(Material.ANDESITE.name(), 0.6D); - if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_16)) materialSpeedsDef.put(Material.BLACKSTONE.name(), 0.6D); materialSpeedsDef.put(Material.NETHERRACK.name(), 0.6D); + if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_16)) { + materialSpeedsDef.put(Material.BLACKSTONE.name(), 0.6D); + } addItemSetting(materialSpeeds); } @@ -80,34 +85,42 @@ public class ClimbingPick extends SimpleSlimefunItem implements @Override public ItemUseHandler getItemHandler() { return e -> { - if (e.getClickedBlock().isPresent()) { - Block block = e.getClickedBlock().get(); - ItemStack item = e.getItem(); - Player p = e.getPlayer(); + if (!e.getClickedBlock().isPresent()) return; - if (!getID().equals("TEST_CLIMBING_PICK") && p.getLocation().distance(block.getLocation()) > 1.5) return; - if (e.getClickedFace() == BlockFace.DOWN || e.getClickedFace() == BlockFace.UP) return; + Block block = e.getClickedBlock().get(); + ItemStack item = e.getItem(); + Player p = e.getPlayer(); - if (!users.contains(p.getUniqueId())) { - Vector velocity = new Vector(1, 1, 1); - if (!getID().equals("TEST_CLIMBING_PICK")) { - velocity = p.getVelocity(); - } - - Material mat = block.getType(); - if (materialSpeeds.getValue().containsKey(mat.name())) { - double launch = materialSpeeds.getValue().get(mat.name()); - if (item.getEnchantments().containsKey(Enchantment.DIG_SPEED)) { - launch += (item.getEnchantmentLevel(Enchantment.DIG_SPEED) * 0.2); - } - velocity = velocity.setY(velocity.getY() + launch); + if (!getID().equals("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())) { + Vector velocity = new Vector(1, 1, 1); + if (!getID().equals("TEST_CLIMBING_PICK")) { + velocity = p.getVelocity(); + } + + Material mat = block.getType(); + Double launch = materialSpeeds.getValue().get(mat.name()); + if (launch != null) { + Integer efficiencyLevel = item.getEnchantments().get(Enchantment.DIG_SPEED); + if (efficiencyLevel != null){ + launch += (efficiencyLevel * 0.2); } + velocity = velocity.setY(velocity.getY() + launch); users.add(p.getUniqueId()); Bukkit.getScheduler().runTaskLaterAsynchronously(SlimefunPlugin.instance, () -> users.remove(p.getUniqueId()), 4L); + } - ClimbingPickLaunchEvent event = new ClimbingPickLaunchEvent(p, velocity, item); - Bukkit.getPluginManager().callEvent(event); + ClimbingPickLaunchEvent event = new ClimbingPickLaunchEvent(p, velocity, item); + Bukkit.getPluginManager().callEvent(event); + + p.setVelocity(event.getVelocity()); + p.playSound(p.getLocation(), Sound.ENTITY_ENDERMAN_TELEPORT, 1F, 1F); + + if (p.getGameMode() != GameMode.CREATIVE) { + ((ClimbingPick) SlimefunItem.getByItem(e.getItem())).damageItem(p, e.getItem()); } } }; diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/ClimbingPickListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/ClimbingPickListener.java deleted file mode 100644 index 077e5ba59..000000000 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/ClimbingPickListener.java +++ /dev/null @@ -1,41 +0,0 @@ -package io.github.thebusybiscuit.slimefun4.implementation.listeners; - -import org.bukkit.GameMode; -import org.bukkit.Sound; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.util.Vector; - -import io.github.thebusybiscuit.slimefun4.api.events.ClimbingPickLaunchEvent; -import io.github.thebusybiscuit.slimefun4.implementation.items.tools.ClimbingPick; -import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; -import me.mrCookieSlime.Slimefun.SlimefunPlugin; - -/** - * The special event listener for {@link ClimbingPick}. - * This is made like this to allow addons to add more materials to climbing pick. - * - * @author Linox - * - * @see ClimbingPick - * - */ -public class ClimbingPickListener implements Listener { - - public ClimbingPickListener(SlimefunPlugin plugin) { - plugin.getServer().getPluginManager().registerEvents(this, plugin); - } - - @EventHandler - public void onLaunch(ClimbingPickLaunchEvent e) { - Player p = e.getPlayer(); - - p.setVelocity(e.getVelocity()); - p.playSound(p.getLocation(), Sound.ENTITY_ENDERMAN_TELEPORT, 1F, 1F); - - if (p.getGameMode() != GameMode.CREATIVE) { - ((ClimbingPick) SlimefunItem.getByItem(e.getItem())).damageItem(p, e.getItem()); - } - } -} diff --git a/src/main/java/me/mrCookieSlime/Slimefun/SlimefunPlugin.java b/src/main/java/me/mrCookieSlime/Slimefun/SlimefunPlugin.java index eb3df0cfb..635821e44 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/SlimefunPlugin.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/SlimefunPlugin.java @@ -10,7 +10,6 @@ import java.util.Set; import java.util.logging.Level; import java.util.stream.Collectors; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.ClimbingPickListener; import org.bukkit.Bukkit; import org.bukkit.Server; import org.bukkit.command.Command; @@ -238,7 +237,6 @@ public final class SlimefunPlugin extends JavaPlugin implements SlimefunAddon { new WitherListener(this); new IronGolemListener(this); new PlayerInteractEntityListener(this); - new ClimbingPickListener(this); new MobDropListener(this, (BasicCircuitBoard) SlimefunItems.BASIC_CIRCUIT_BOARD.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 5f1013975..1955df367 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 @@ -53,11 +53,11 @@ public class TestClimbingPick implements SlimefunItemTest { for (BlockFace face : BlockFace.values()) { BlockMock block1 = new BlockMock(Material.STONE); simulateRightClickBlock(player, pick, block1, face); - server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class); + server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class, e -> !e.isCancelled()); BlockMock block2 = new BlockMock(Material.DIRT); simulateRightClickBlock(player, pick, block2, face); - server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class); + server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class, ClimbingPickLaunchEvent::isCancelled); } } }