diff --git a/CHANGELOG.md b/CHANGELOG.md index 14b23af81..1c8ded8f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ #### Changes * Renamed "Solar Panel" to "Photovoltaic Cell" to avoid confusions with solar generators +* (API) Removed deprecated "SlimefunBlockHandler" #### Fixes diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java index 9763f6a9a..03ed71260 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java @@ -36,7 +36,6 @@ import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.guide.CheatSheetSlimefunGuide; import io.github.thebusybiscuit.slimefun4.implementation.guide.SurvivalSlimefunGuide; import me.mrCookieSlime.Slimefun.Objects.Category; -import me.mrCookieSlime.Slimefun.Objects.SlimefunBlockHandler; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; import me.mrCookieSlime.Slimefun.Objects.handlers.ItemHandler; import me.mrCookieSlime.Slimefun.api.BlockInfoConfig; @@ -92,7 +91,6 @@ public final class SlimefunRegistry { private final Map blockMenuPresets = new HashMap<>(); private final Map universalInventories = new HashMap<>(); private final Map, Set> globalItemHandlers = new HashMap<>(); - private final Map blockHandlers = new HashMap<>(); public void load(@Nonnull SlimefunPlugin plugin, @Nonnull Config cfg) { Validate.notNull(plugin, "The Plugin cannot be null!"); @@ -333,12 +331,6 @@ public final class SlimefunRegistry { return globalItemHandlers; } - @Deprecated - @Nonnull - public Map getBlockHandlers() { - return blockHandlers; - } - @Nonnull public Map getWorlds() { return worlds; 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 index 4bb51e450..ff06089f4 100644 --- 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 @@ -12,6 +12,7 @@ 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; @@ -20,15 +21,14 @@ import io.github.thebusybiscuit.slimefun4.api.events.ExplosiveToolBreakBlocksEve 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 io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler; import io.github.thebusybiscuit.slimefun4.core.handlers.ToolUseHandler; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.items.SimpleSlimefunItem; import io.github.thebusybiscuit.slimefun4.utils.tags.SlimefunTag; import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Objects.Category; -import me.mrCookieSlime.Slimefun.Objects.SlimefunBlockHandler; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; -import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.UnregisterReason; import me.mrCookieSlime.Slimefun.api.BlockStorage; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; @@ -64,12 +64,12 @@ public class ExplosiveTool extends SimpleSlimefunItem implements b.getWorld().playSound(b.getLocation(), Sound.ENTITY_GENERIC_EXPLODE, 0.2F, 1F); List blocks = findBlocks(b); - breakBlocks(p, tool, b, blocks, drops); + breakBlocks(e, p, tool, b, blocks, drops); }; } @ParametersAreNonnullByDefault - private void breakBlocks(Player p, ItemStack item, Block b, List blocks, List drops) { + private void breakBlocks(BlockBreakEvent e, Player p, ItemStack item, Block b, List blocks, List drops) { List blocksToDestroy = new ArrayList<>(); if (callExplosionEvent.getValue().booleanValue()) { @@ -96,7 +96,7 @@ public class ExplosiveTool extends SimpleSlimefunItem implements if (!event.isCancelled()) { for (Block block : blocksToDestroy) { - breakBlock(p, item, block, drops); + breakBlock(e, p, item, block, drops); } } } @@ -141,7 +141,7 @@ public class ExplosiveTool extends SimpleSlimefunItem implements } @ParametersAreNonnullByDefault - private void breakBlock(Player p, ItemStack item, Block b, List drops) { + private void breakBlock(BlockBreakEvent e, Player p, ItemStack item, Block b, List drops) { SlimefunPlugin.getProtectionManager().logAction(p, b, ProtectableAction.BREAK_BLOCK); Material material = b.getType(); @@ -149,10 +149,8 @@ public class ExplosiveTool extends SimpleSlimefunItem implements SlimefunItem sfItem = BlockStorage.check(b); if (sfItem != null && !sfItem.useVanillaBlockBreaking()) { - SlimefunBlockHandler handler = SlimefunPlugin.getRegistry().getBlockHandlers().get(sfItem.getId()); - - if (handler != null && !handler.onBreak(p, b, sfItem, UnregisterReason.PLAYER_BREAK)) { - drops.add(BlockStorage.retrieve(b)); + if (!sfItem.callItemHandler(BlockBreakHandler.class, handler -> handler.onPlayerBreak(e, item, drops))) { + drops.addAll(sfItem.getDrops(p)); } } else { b.breakNaturally(item); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockListener.java index 333409296..f8e754857 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockListener.java @@ -30,9 +30,7 @@ import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler; import io.github.thebusybiscuit.slimefun4.core.handlers.ToolUseHandler; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.utils.tags.SlimefunTag; -import me.mrCookieSlime.Slimefun.Objects.SlimefunBlockHandler; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; -import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.UnregisterReason; import me.mrCookieSlime.Slimefun.api.BlockStorage; import me.mrCookieSlime.Slimefun.api.Slimefun; @@ -157,22 +155,10 @@ public class BlockListener implements Listener { } if (sfItem != null && !sfItem.useVanillaBlockBreaking()) { - SlimefunBlockHandler blockHandler = SlimefunPlugin.getRegistry().getBlockHandlers().get(sfItem.getId()); + sfItem.callItemHandler(BlockBreakHandler.class, handler -> handler.onPlayerBreak(e, item, drops)); - if (blockHandler != null) { - try { - if (!blockHandler.onBreak(e.getPlayer(), e.getBlock(), sfItem, UnregisterReason.PLAYER_BREAK)) { - e.setCancelled(true); - return; - } - } catch (Exception | LinkageError x) { - sfItem.error("Something went wrong while triggering a BlockHandler", x); - } - } else { - sfItem.callItemHandler(BlockBreakHandler.class, handler -> handler.onPlayerBreak(e, item, drops)); - if (e.isCancelled()) { - return; - } + if (e.isCancelled()) { + return; } drops.addAll(sfItem.getDrops()); @@ -219,30 +205,21 @@ public class BlockListener implements Listener { SlimefunItem sfItem = BlockStorage.check(blockAbove); if (sfItem != null && !sfItem.useVanillaBlockBreaking()) { - SlimefunBlockHandler blockHandler = SlimefunPlugin.getRegistry().getBlockHandlers().get(sfItem.getId()); + /* + * We create a dummy here to pass onto the BlockBreakHandler. + * This will set the correct block context. + */ + BlockBreakEvent dummyEvent = new BlockBreakEvent(blockAbove, e.getPlayer()); + List drops = new ArrayList<>(); + drops.addAll(sfItem.getDrops(e.getPlayer())); - if (blockHandler != null) { - if (blockHandler.onBreak(e.getPlayer(), blockAbove, sfItem, UnregisterReason.PLAYER_BREAK)) { - blockAbove.getWorld().dropItemNaturally(blockAbove.getLocation(), BlockStorage.retrieve(blockAbove)); - blockAbove.setType(Material.AIR); - } - } else { - /* - * We create a dummy here to pass onto the BlockBreakHandler. - * This will set the correct block context. - */ - BlockBreakEvent dummyEvent = new BlockBreakEvent(blockAbove, e.getPlayer()); - List drops = new ArrayList<>(); - drops.addAll(sfItem.getDrops(e.getPlayer())); + sfItem.callItemHandler(BlockBreakHandler.class, handler -> handler.onPlayerBreak(dummyEvent, item, drops)); + blockAbove.setType(Material.AIR); - sfItem.callItemHandler(BlockBreakHandler.class, handler -> handler.onPlayerBreak(dummyEvent, item, drops)); - blockAbove.setType(Material.AIR); - - if (!dummyEvent.isCancelled() && dummyEvent.isDropItems()) { - for (ItemStack drop : drops) { - if (drop != null && !drop.getType().isAir()) { - blockAbove.getWorld().dropItemNaturally(blockAbove.getLocation(), drop); - } + if (!dummyEvent.isCancelled() && dummyEvent.isDropItems()) { + for (ItemStack drop : drops) { + if (drop != null && !drop.getType().isAir()) { + blockAbove.getWorld().dropItemNaturally(blockAbove.getLocation(), drop); } } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/ExplosionsListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/ExplosionsListener.java index 7026f4f4c..7bfc54a75 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/ExplosionsListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/ExplosionsListener.java @@ -5,6 +5,7 @@ import java.util.Iterator; import java.util.List; import javax.annotation.Nonnull; +import javax.annotation.ParametersAreNonnullByDefault; import org.bukkit.Material; import org.bukkit.block.Block; @@ -18,9 +19,7 @@ import org.bukkit.inventory.ItemStack; import io.github.thebusybiscuit.slimefun4.core.attributes.WitherProof; import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; -import me.mrCookieSlime.Slimefun.Objects.SlimefunBlockHandler; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; -import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.UnregisterReason; import me.mrCookieSlime.Slimefun.api.BlockStorage; /** @@ -58,36 +57,26 @@ public class ExplosionsListener implements Listener { if (item != null) { blocks.remove(); - if (!(item instanceof WitherProof)) { - SlimefunBlockHandler blockHandler = SlimefunPlugin.getRegistry().getBlockHandlers().get(item.getId()); - boolean success = true; + if (!(item instanceof WitherProof) && !item.callItemHandler(BlockBreakHandler.class, handler -> handleExplosion(handler, block))) { + BlockStorage.clearBlockInfo(block); + block.setType(Material.AIR); + } + } + } + } - if (blockHandler != null) { - success = blockHandler.onBreak(null, block, item, UnregisterReason.EXPLODE); - } else { - item.callItemHandler(BlockBreakHandler.class, handler -> { - if (handler.isExplosionAllowed(block)) { - BlockStorage.clearBlockInfo(block); - block.setType(Material.AIR); + @ParametersAreNonnullByDefault + private void handleExplosion(BlockBreakHandler handler, Block block) { + if (handler.isExplosionAllowed(block)) { + BlockStorage.clearBlockInfo(block); + block.setType(Material.AIR); - List drops = new ArrayList<>(); - handler.onExplode(block, drops); + List drops = new ArrayList<>(); + handler.onExplode(block, drops); - for (ItemStack drop : drops) { - if (drop != null && !drop.getType().isAir()) { - block.getWorld().dropItemNaturally(block.getLocation(), drop); - } - } - } - }); - - return; - } - - if (success) { - BlockStorage.clearBlockInfo(block); - block.setType(Material.AIR); - } + for (ItemStack drop : drops) { + if (drop != null && !drop.getType().isAir()) { + block.getWorld().dropItemNaturally(block.getLocation(), drop); } } } diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunBlockHandler.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunBlockHandler.java deleted file mode 100644 index 6009883e6..000000000 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunBlockHandler.java +++ /dev/null @@ -1,42 +0,0 @@ -package me.mrCookieSlime.Slimefun.Objects; - -import org.bukkit.block.Block; -import org.bukkit.entity.Player; -import org.bukkit.event.Event; - -import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler; -import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; -import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.UnregisterReason; - -/** - * A {@link SlimefunBlockHandler} handles breaking and placing of blocks. - * You can use this class to initialize block data but also to correctly - * destroy blocks. - * - * {@code SlimefunItem.registerBlockHandler(String, SlimefunBlockHandler); } - * - * @author TheBusyBiscuit - * - * @deprecated Please use the {@link BlockBreakHandler} instead. - * - */ -@Deprecated -@FunctionalInterface -public interface SlimefunBlockHandler { - - /** - * This method gets called when the {@link Block} is broken. - * The {@link Player} will be null if the {@link Block} exploded - * - * @param p - * The {@link Player} who broke the {@link Block} - * @param b - * The {@link Block} that was broken - * @param item - * The {@link SlimefunItem} that was stored in that {@link Block} - * @param reason - * The reason for the {@link Block} breaking - * @return Whether the {@link Event} should be cancelled - */ - boolean onBreak(Player p, Block b, SlimefunItem item, UnregisterReason reason); -} 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 015a4280c..054437b7e 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/SlimefunItem.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/SlimefunItem.java @@ -40,7 +40,6 @@ import io.github.thebusybiscuit.slimefun4.core.attributes.Placeable; import io.github.thebusybiscuit.slimefun4.core.attributes.Radioactive; import io.github.thebusybiscuit.slimefun4.core.attributes.Rechargeable; import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuide; -import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler; import io.github.thebusybiscuit.slimefun4.core.researching.Research; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem; @@ -532,11 +531,6 @@ public class SlimefunItem implements Placeable { // Send out deprecation warnings for any classes or interfaces checkForDeprecations(getClass()); - // Inform addon developers about the BlockBreakHandler - if (SlimefunPlugin.getUpdater().getBranch() != SlimefunBranch.DEVELOPMENT && SlimefunPlugin.getRegistry().getBlockHandlers().containsKey(getId())) { - warn("This item uses a deprecated SlimefunBlockHandler which will be removed in the very near future! Please switch to the BlockBreakHandler as soon as possible."); - } - // Check for an illegal stack size if (itemStackTemplate.getAmount() != 1) { // @formatter:off @@ -1200,19 +1194,4 @@ public class SlimefunItem implements Placeable { return SlimefunPlugin.getRegistry().getPublicItemHandlers().computeIfAbsent(identifier, c -> new HashSet<>()); } - /** - * This has been deprecated. - * - * @deprecated Please use {@link #addItemHandler(ItemHandler...)} and {@link BlockBreakHandler} instead - * - * @param id - * The id - * @param handler - * The handler - */ - @Deprecated - public static void registerBlockHandler(@Nonnull String id, @Nullable me.mrCookieSlime.Slimefun.Objects.SlimefunBlockHandler handler) { - SlimefunPlugin.getRegistry().getBlockHandlers().put(id, handler); - } - } \ No newline at end of file diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/UnregisterReason.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/UnregisterReason.java deleted file mode 100644 index 3361d2787..000000000 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/UnregisterReason.java +++ /dev/null @@ -1,36 +0,0 @@ -package me.mrCookieSlime.Slimefun.Objects.SlimefunItem; - -import me.mrCookieSlime.Slimefun.Objects.SlimefunBlockHandler; - -/** - * Defines how a block handled by Slimefun is being unregistered. - *

- * It is notably used by - * {@link me.mrCookieSlime.Slimefun.Objects.SlimefunBlockHandler#onBreak(org.bukkit.entity.Player, org.bukkit.block.Block, SlimefunItem, UnregisterReason)}. - * - * @author TheBusyBiscuit - * - * @deprecated This enum is no longer needed - * - * @see SlimefunBlockHandler - * - */ -@Deprecated -public enum UnregisterReason { - - /** - * An explosion destroys the block. - */ - EXPLODE, - - /** - * A player breaks the block. - */ - PLAYER_BREAK, - - /** - * An android miner breaks the block. - */ - ANDROID_DIG - -}