From 0af8c41eeba24e68f98d7e1a725ae3e1088b5df0 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Sun, 24 May 2020 01:11:10 +0200 Subject: [PATCH] Added a toggle for #1924 and fixes #1933 --- CHANGELOG.md | 2 + .../items/tools/ExplosivePickaxe.java | 121 ++-------------- .../items/tools/ExplosiveShovel.java | 83 ++--------- .../items/tools/ExplosiveTool.java | 133 ++++++++++++++++++ 4 files changed, 157 insertions(+), 182 deletions(-) create mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/ExplosiveTool.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 6af6be8a6..212d91198 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ * Added /sf backpack to restore lost backpacks * Added automated Unit Tests * Added WaypointCreateEvent +* Added an option to call an explosion event when using explosive tools #### Changes * Little performance improvements @@ -55,6 +56,7 @@ * Fixed file errors with PerWorldSettingsService * Fixed ChestTerminals deleting items from Cargo networks (TheBusyBiscuit/ChestTerminal#25) * Fixed #1926 +* Fixed #1933 ## Release Candidate 11 (25 Apr 2020) 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 30ff110e6..27a01f047 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,125 +1,24 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.tools; -import java.util.ArrayList; -import java.util.List; - -import org.bukkit.Bukkit; -import org.bukkit.Effect; -import org.bukkit.Material; -import org.bukkit.Sound; -import org.bukkit.block.Block; -import org.bukkit.entity.Player; -import org.bukkit.event.block.BlockBreakEvent; -import org.bukkit.event.block.BlockExplodeEvent; import org.bukkit.inventory.ItemStack; -import io.github.thebusybiscuit.cscorelib2.item.CustomItem; -import io.github.thebusybiscuit.cscorelib2.materials.MaterialCollections; -import io.github.thebusybiscuit.cscorelib2.protection.ProtectableAction; -import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting; -import io.github.thebusybiscuit.slimefun4.core.attributes.DamageableItem; -import io.github.thebusybiscuit.slimefun4.core.attributes.NotPlaceable; -import me.mrCookieSlime.Slimefun.SlimefunPlugin; import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Objects.Category; -import me.mrCookieSlime.Slimefun.Objects.SlimefunBlockHandler; -import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.HandledBlock; -import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SimpleSlimefunItem; -import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; -import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.UnregisterReason; -import me.mrCookieSlime.Slimefun.Objects.handlers.BlockBreakHandler; -import me.mrCookieSlime.Slimefun.api.BlockStorage; -import me.mrCookieSlime.Slimefun.api.Slimefun; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; -public class ExplosivePickaxe extends SimpleSlimefunItem implements NotPlaceable, DamageableItem { - - private final ItemSetting damageOnUse = new ItemSetting<>("damage-on-use", true); +/** + * The {@link ExplosivePickaxe} is a pickaxe which can destroy blocks in a size of 3 by 3. + * It also creates a explosion animation. + * + * @author TheBusyBiscuit + * + * @see ExplosiveShovel + * + */ +public class ExplosivePickaxe extends ExplosiveTool { public ExplosivePickaxe(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { super(category, item, recipeType, recipe); - - addItemSetting(damageOnUse); - } - - @Override - public BlockBreakHandler getItemHandler() { - return new BlockBreakHandler() { - - @Override - public boolean isPrivate() { - return false; - } - - @Override - public boolean onBlockBreak(BlockBreakEvent e, ItemStack item, int fortune, List drops) { - if (isItem(item)) { - if (Slimefun.hasUnlocked(e.getPlayer(), ExplosivePickaxe.this, true)) { - e.getBlock().getWorld().createExplosion(e.getBlock().getLocation(), 0.0F); - e.getBlock().getWorld().playSound(e.getBlock().getLocation(), Sound.ENTITY_GENERIC_EXPLODE, 0.3F, 1F); - - List blocks = new ArrayList<>(); - for (int x = -1; x <= 1; x++) { - for (int y = -1; y <= 1; y++) { - for (int z = -1; z <= 1; z++) { - // We can skip the center block since that will break as usual - if (x == 0 && y == 0 && z == 0) { - continue; - } - - blocks.add(e.getBlock().getRelative(x, y, z)); - } - } - } - - BlockExplodeEvent blockExplodeEvent = new BlockExplodeEvent(e.getBlock(), blocks, 0); - Bukkit.getServer().getPluginManager().callEvent(blockExplodeEvent); - if (!blockExplodeEvent.isCancelled()) { - blockExplodeEvent.blockList().forEach(b -> breakBlock(e.getPlayer(), item, b, fortune, drops)); - } - } - - return true; - } - else { - return false; - } - } - }; - } - - private void breakBlock(Player p, ItemStack item, Block b, int fortune, List drops) { - if (b.getType() != Material.AIR && !b.isLiquid() && !MaterialCollections.getAllUnbreakableBlocks().contains(b.getType()) && SlimefunPlugin.getProtectionManager().hasPermission(p, b.getLocation(), ProtectableAction.BREAK_BLOCK)) { - SlimefunPlugin.getProtectionManager().logAction(p, b, ProtectableAction.BREAK_BLOCK); - - b.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, b.getType()); - SlimefunItem sfItem = BlockStorage.check(b); - - if (sfItem != null && !(sfItem instanceof HandledBlock)) { - SlimefunBlockHandler handler = SlimefunPlugin.getRegistry().getBlockHandlers().get(sfItem.getID()); - - if (handler != null && !handler.onBreak(p, b, sfItem, UnregisterReason.PLAYER_BREAK)) { - drops.add(BlockStorage.retrieve(b)); - } - } - else if (b.getType() == Material.PLAYER_HEAD || b.getType().name().endsWith("_SHULKER_BOX")) { - b.breakNaturally(); - } - else { - for (ItemStack drop : b.getDrops(getItem())) { - b.getWorld().dropItemNaturally(b.getLocation(), (b.getType().toString().endsWith("_ORE") && b.getType() != Material.IRON_ORE && b.getType() != Material.GOLD_ORE) ? new CustomItem(drop, fortune) : drop); - } - - b.setType(Material.AIR); - } - - damageItem(p, item); - } - } - - @Override - public boolean isDamageable() { - return damageOnUse.getValue(); } } 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 33fd3a927..ec2204cb7 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 @@ -1,28 +1,18 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.tools; -import java.util.ArrayList; import java.util.List; -import org.bukkit.Bukkit; import org.bukkit.Effect; import org.bukkit.Material; -import org.bukkit.Sound; import org.bukkit.block.Block; -import org.bukkit.event.block.BlockBreakEvent; -import org.bukkit.event.block.BlockExplodeEvent; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import io.github.thebusybiscuit.cscorelib2.materials.MaterialTools; import io.github.thebusybiscuit.cscorelib2.protection.ProtectableAction; -import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting; -import io.github.thebusybiscuit.slimefun4.core.attributes.DamageableItem; -import io.github.thebusybiscuit.slimefun4.core.attributes.NotPlaceable; import me.mrCookieSlime.Slimefun.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.api.Slimefun; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; /** @@ -34,77 +24,28 @@ import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; * @see ExplosivePickaxe * */ -public class ExplosiveShovel extends SimpleSlimefunItem implements NotPlaceable, DamageableItem { - - private final ItemSetting damageOnUse = new ItemSetting<>("damage-on-use", true); +public class ExplosiveShovel extends ExplosiveTool { public ExplosiveShovel(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { super(category, item, recipeType, recipe); - - addItemSetting(damageOnUse); } @Override - public BlockBreakHandler getItemHandler() { - return new BlockBreakHandler() { + protected void breakBlock(Player p, ItemStack item, Block b, int fortune, List drops) { + if (MaterialTools.getBreakableByShovel().contains(b.getType()) && SlimefunPlugin.getProtectionManager().hasPermission(p, b.getLocation(), ProtectableAction.BREAK_BLOCK)) { + SlimefunPlugin.getProtectionManager().logAction(p, b, ProtectableAction.BREAK_BLOCK); - @Override - public boolean isPrivate() { - return false; - } + b.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, b.getType()); - @Override - public boolean onBlockBreak(BlockBreakEvent e, ItemStack item, int fortune, List drops) { - if (isItem(item)) { - if (Slimefun.hasUnlocked(e.getPlayer(), ExplosiveShovel.this, true)) { - e.getBlock().getWorld().createExplosion(e.getBlock().getLocation(), 0.0F); - e.getBlock().getWorld().playSound(e.getBlock().getLocation(), Sound.ENTITY_GENERIC_EXPLODE, 0.3F, 1F); - - List blocks = new ArrayList<>(); - for (int x = -1; x <= 1; x++) { - for (int y = -1; y <= 1; y++) { - for (int z = -1; z <= 1; z++) { - if (x == 0 && y == 0 && z == 0) { - continue; - } - - blocks.add(e.getBlock().getRelative(x, y, z)); - } - } - } - - BlockExplodeEvent blockExplodeEvent = new BlockExplodeEvent(e.getBlock(), blocks, 0); - Bukkit.getServer().getPluginManager().callEvent(blockExplodeEvent); - if (!blockExplodeEvent.isCancelled()) { - blockExplodeEvent.blockList().forEach(b -> { - if (MaterialTools.getBreakableByShovel().contains(b.getType()) && SlimefunPlugin.getProtectionManager().hasPermission(e.getPlayer(), b.getLocation(), ProtectableAction.BREAK_BLOCK)) { - SlimefunPlugin.getProtectionManager().logAction(e.getPlayer(), b, ProtectableAction.BREAK_BLOCK); - - b.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, b.getType()); - - for (ItemStack drop : b.getDrops(getItem())) { - if (drop != null) { - b.getWorld().dropItemNaturally(b.getLocation(), drop); - } - } - - b.setType(Material.AIR); - damageItem(e.getPlayer(), item); - } - }); - } - } - - return true; + for (ItemStack drop : b.getDrops(getItem())) { + if (drop != null) { + b.getWorld().dropItemNaturally(b.getLocation(), drop); } - else return false; } - }; - } - @Override - public boolean isDamageable() { - return damageOnUse.getValue(); + b.setType(Material.AIR); + damageItem(p, item); + } } } 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 new file mode 100644 index 000000000..17076c716 --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/tools/ExplosiveTool.java @@ -0,0 +1,133 @@ +package io.github.thebusybiscuit.slimefun4.implementation.items.tools; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.Effect; +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockExplodeEvent; +import org.bukkit.inventory.ItemStack; + +import io.github.thebusybiscuit.cscorelib2.item.CustomItem; +import io.github.thebusybiscuit.cscorelib2.materials.MaterialCollections; +import io.github.thebusybiscuit.cscorelib2.protection.ProtectableAction; +import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting; +import io.github.thebusybiscuit.slimefun4.core.attributes.DamageableItem; +import io.github.thebusybiscuit.slimefun4.core.attributes.NotPlaceable; +import me.mrCookieSlime.Slimefun.SlimefunPlugin; +import me.mrCookieSlime.Slimefun.Lists.RecipeType; +import me.mrCookieSlime.Slimefun.Objects.Category; +import me.mrCookieSlime.Slimefun.Objects.SlimefunBlockHandler; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.HandledBlock; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SimpleSlimefunItem; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.UnregisterReason; +import me.mrCookieSlime.Slimefun.Objects.handlers.BlockBreakHandler; +import me.mrCookieSlime.Slimefun.api.BlockStorage; +import me.mrCookieSlime.Slimefun.api.Slimefun; +import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; + +class ExplosiveTool extends SimpleSlimefunItem implements NotPlaceable, DamageableItem { + + private final ItemSetting damageOnUse = new ItemSetting<>("damage-on-use", true); + private final ItemSetting callExplosionEvent = new ItemSetting<>("call-explosion-event", false); + + public ExplosiveTool(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { + super(category, item, recipeType, recipe); + addItemSetting(damageOnUse, callExplosionEvent); + } + + @Override + public BlockBreakHandler getItemHandler() { + return new BlockBreakHandler() { + + @Override + public boolean isPrivate() { + return false; + } + + @Override + public boolean onBlockBreak(BlockBreakEvent e, ItemStack item, int fortune, List drops) { + if (isItem(item)) { + if (Slimefun.hasUnlocked(e.getPlayer(), ExplosiveTool.this, true)) { + e.getBlock().getWorld().createExplosion(e.getBlock().getLocation(), 0.0F); + e.getBlock().getWorld().playSound(e.getBlock().getLocation(), Sound.ENTITY_GENERIC_EXPLODE, 0.2F, 1F); + + List blocks = new ArrayList<>(); + for (int x = -1; x <= 1; x++) { + for (int y = -1; y <= 1; y++) { + for (int z = -1; z <= 1; z++) { + // We can skip the center block since that will break as usual + if (x == 0 && y == 0 && z == 0) { + continue; + } + + blocks.add(e.getBlock().getRelative(x, y, z)); + } + } + } + + if (callExplosionEvent.getValue().booleanValue()) { + BlockExplodeEvent blockExplodeEvent = new BlockExplodeEvent(e.getBlock(), blocks, 0); + Bukkit.getServer().getPluginManager().callEvent(blockExplodeEvent); + + if (!blockExplodeEvent.isCancelled()) { + blockExplodeEvent.blockList().forEach(b -> breakBlock(e.getPlayer(), item, b, fortune, drops)); + } + } + else { + for (Block b : blocks) { + breakBlock(e.getPlayer(), item, b, fortune, drops); + } + } + } + + return true; + } + else { + return false; + } + } + }; + } + + @Override + public boolean isDamageable() { + return damageOnUse.getValue(); + } + + protected void breakBlock(Player p, ItemStack item, Block b, int fortune, List drops) { + if (b.getType() != Material.AIR && !b.isLiquid() && !MaterialCollections.getAllUnbreakableBlocks().contains(b.getType()) && SlimefunPlugin.getProtectionManager().hasPermission(p, b.getLocation(), ProtectableAction.BREAK_BLOCK)) { + SlimefunPlugin.getProtectionManager().logAction(p, b, ProtectableAction.BREAK_BLOCK); + + b.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, b.getType()); + SlimefunItem sfItem = BlockStorage.check(b); + + if (sfItem != null && !(sfItem instanceof HandledBlock)) { + SlimefunBlockHandler handler = SlimefunPlugin.getRegistry().getBlockHandlers().get(sfItem.getID()); + + if (handler != null && !handler.onBreak(p, b, sfItem, UnregisterReason.PLAYER_BREAK)) { + drops.add(BlockStorage.retrieve(b)); + } + } + else if (b.getType() == Material.PLAYER_HEAD || b.getType().name().endsWith("_SHULKER_BOX")) { + b.breakNaturally(); + } + else { + for (ItemStack drop : b.getDrops(getItem())) { + b.getWorld().dropItemNaturally(b.getLocation(), (b.getType().toString().endsWith("_ORE") && b.getType() != Material.IRON_ORE && b.getType() != Material.GOLD_ORE) ? new CustomItem(drop, fortune) : drop); + } + + b.setType(Material.AIR); + } + + damageItem(p, item); + } + } + +}