1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Removed SlimefunBlockHandler

This commit is contained in:
TheBusyBiscuit 2021-04-16 15:05:39 +02:00
parent c3028058c0
commit d2e17af2a1
8 changed files with 43 additions and 185 deletions

View File

@ -29,6 +29,7 @@
#### Changes #### Changes
* Renamed "Solar Panel" to "Photovoltaic Cell" to avoid confusions with solar generators * Renamed "Solar Panel" to "Photovoltaic Cell" to avoid confusions with solar generators
* (API) Removed deprecated "SlimefunBlockHandler"
#### Fixes #### Fixes

View File

@ -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.CheatSheetSlimefunGuide;
import io.github.thebusybiscuit.slimefun4.implementation.guide.SurvivalSlimefunGuide; import io.github.thebusybiscuit.slimefun4.implementation.guide.SurvivalSlimefunGuide;
import me.mrCookieSlime.Slimefun.Objects.Category; 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.SlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.handlers.ItemHandler; import me.mrCookieSlime.Slimefun.Objects.handlers.ItemHandler;
import me.mrCookieSlime.Slimefun.api.BlockInfoConfig; import me.mrCookieSlime.Slimefun.api.BlockInfoConfig;
@ -92,7 +91,6 @@ public final class SlimefunRegistry {
private final Map<String, BlockMenuPreset> blockMenuPresets = new HashMap<>(); private final Map<String, BlockMenuPreset> blockMenuPresets = new HashMap<>();
private final Map<String, UniversalBlockMenu> universalInventories = new HashMap<>(); private final Map<String, UniversalBlockMenu> universalInventories = new HashMap<>();
private final Map<Class<? extends ItemHandler>, Set<ItemHandler>> globalItemHandlers = new HashMap<>(); private final Map<Class<? extends ItemHandler>, Set<ItemHandler>> globalItemHandlers = new HashMap<>();
private final Map<String, SlimefunBlockHandler> blockHandlers = new HashMap<>();
public void load(@Nonnull SlimefunPlugin plugin, @Nonnull Config cfg) { public void load(@Nonnull SlimefunPlugin plugin, @Nonnull Config cfg) {
Validate.notNull(plugin, "The Plugin cannot be null!"); Validate.notNull(plugin, "The Plugin cannot be null!");
@ -333,12 +331,6 @@ public final class SlimefunRegistry {
return globalItemHandlers; return globalItemHandlers;
} }
@Deprecated
@Nonnull
public Map<String, SlimefunBlockHandler> getBlockHandlers() {
return blockHandlers;
}
@Nonnull @Nonnull
public Map<String, BlockStorage> getWorlds() { public Map<String, BlockStorage> getWorlds() {
return worlds; return worlds;

View File

@ -12,6 +12,7 @@ import org.bukkit.Material;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockExplodeEvent; import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.inventory.ItemStack; 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.api.items.ItemSetting;
import io.github.thebusybiscuit.slimefun4.core.attributes.DamageableItem; import io.github.thebusybiscuit.slimefun4.core.attributes.DamageableItem;
import io.github.thebusybiscuit.slimefun4.core.attributes.NotPlaceable; 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.core.handlers.ToolUseHandler;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.items.SimpleSlimefunItem; import io.github.thebusybiscuit.slimefun4.implementation.items.SimpleSlimefunItem;
import io.github.thebusybiscuit.slimefun4.utils.tags.SlimefunTag; import io.github.thebusybiscuit.slimefun4.utils.tags.SlimefunTag;
import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category; 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.SlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.UnregisterReason;
import me.mrCookieSlime.Slimefun.api.BlockStorage; import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
@ -64,12 +64,12 @@ public class ExplosiveTool extends SimpleSlimefunItem<ToolUseHandler> implements
b.getWorld().playSound(b.getLocation(), Sound.ENTITY_GENERIC_EXPLODE, 0.2F, 1F); b.getWorld().playSound(b.getLocation(), Sound.ENTITY_GENERIC_EXPLODE, 0.2F, 1F);
List<Block> blocks = findBlocks(b); List<Block> blocks = findBlocks(b);
breakBlocks(p, tool, b, blocks, drops); breakBlocks(e, p, tool, b, blocks, drops);
}; };
} }
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
private void breakBlocks(Player p, ItemStack item, Block b, List<Block> blocks, List<ItemStack> drops) { private void breakBlocks(BlockBreakEvent e, Player p, ItemStack item, Block b, List<Block> blocks, List<ItemStack> drops) {
List<Block> blocksToDestroy = new ArrayList<>(); List<Block> blocksToDestroy = new ArrayList<>();
if (callExplosionEvent.getValue().booleanValue()) { if (callExplosionEvent.getValue().booleanValue()) {
@ -96,7 +96,7 @@ public class ExplosiveTool extends SimpleSlimefunItem<ToolUseHandler> implements
if (!event.isCancelled()) { if (!event.isCancelled()) {
for (Block block : blocksToDestroy) { for (Block block : blocksToDestroy) {
breakBlock(p, item, block, drops); breakBlock(e, p, item, block, drops);
} }
} }
} }
@ -141,7 +141,7 @@ public class ExplosiveTool extends SimpleSlimefunItem<ToolUseHandler> implements
} }
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
private void breakBlock(Player p, ItemStack item, Block b, List<ItemStack> drops) { private void breakBlock(BlockBreakEvent e, Player p, ItemStack item, Block b, List<ItemStack> drops) {
SlimefunPlugin.getProtectionManager().logAction(p, b, ProtectableAction.BREAK_BLOCK); SlimefunPlugin.getProtectionManager().logAction(p, b, ProtectableAction.BREAK_BLOCK);
Material material = b.getType(); Material material = b.getType();
@ -149,10 +149,8 @@ public class ExplosiveTool extends SimpleSlimefunItem<ToolUseHandler> implements
SlimefunItem sfItem = BlockStorage.check(b); SlimefunItem sfItem = BlockStorage.check(b);
if (sfItem != null && !sfItem.useVanillaBlockBreaking()) { if (sfItem != null && !sfItem.useVanillaBlockBreaking()) {
SlimefunBlockHandler handler = SlimefunPlugin.getRegistry().getBlockHandlers().get(sfItem.getId()); if (!sfItem.callItemHandler(BlockBreakHandler.class, handler -> handler.onPlayerBreak(e, item, drops))) {
drops.addAll(sfItem.getDrops(p));
if (handler != null && !handler.onBreak(p, b, sfItem, UnregisterReason.PLAYER_BREAK)) {
drops.add(BlockStorage.retrieve(b));
} }
} else { } else {
b.breakNaturally(item); b.breakNaturally(item);

View File

@ -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.core.handlers.ToolUseHandler;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.utils.tags.SlimefunTag; 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.SlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.UnregisterReason;
import me.mrCookieSlime.Slimefun.api.BlockStorage; import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.Slimefun; import me.mrCookieSlime.Slimefun.api.Slimefun;
@ -157,22 +155,10 @@ public class BlockListener implements Listener {
} }
if (sfItem != null && !sfItem.useVanillaBlockBreaking()) { 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) { if (e.isCancelled()) {
try { return;
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;
}
} }
drops.addAll(sfItem.getDrops()); drops.addAll(sfItem.getDrops());
@ -219,30 +205,21 @@ public class BlockListener implements Listener {
SlimefunItem sfItem = BlockStorage.check(blockAbove); SlimefunItem sfItem = BlockStorage.check(blockAbove);
if (sfItem != null && !sfItem.useVanillaBlockBreaking()) { 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<ItemStack> drops = new ArrayList<>();
drops.addAll(sfItem.getDrops(e.getPlayer()));
if (blockHandler != null) { sfItem.callItemHandler(BlockBreakHandler.class, handler -> handler.onPlayerBreak(dummyEvent, item, drops));
if (blockHandler.onBreak(e.getPlayer(), blockAbove, sfItem, UnregisterReason.PLAYER_BREAK)) { blockAbove.setType(Material.AIR);
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<ItemStack> drops = new ArrayList<>();
drops.addAll(sfItem.getDrops(e.getPlayer()));
sfItem.callItemHandler(BlockBreakHandler.class, handler -> handler.onPlayerBreak(dummyEvent, item, drops)); if (!dummyEvent.isCancelled() && dummyEvent.isDropItems()) {
blockAbove.setType(Material.AIR); for (ItemStack drop : drops) {
if (drop != null && !drop.getType().isAir()) {
if (!dummyEvent.isCancelled() && dummyEvent.isDropItems()) { blockAbove.getWorld().dropItemNaturally(blockAbove.getLocation(), drop);
for (ItemStack drop : drops) {
if (drop != null && !drop.getType().isAir()) {
blockAbove.getWorld().dropItemNaturally(blockAbove.getLocation(), drop);
}
} }
} }
} }

View File

@ -5,6 +5,7 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; 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.attributes.WitherProof;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler; import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; 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.SlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.UnregisterReason;
import me.mrCookieSlime.Slimefun.api.BlockStorage; import me.mrCookieSlime.Slimefun.api.BlockStorage;
/** /**
@ -58,36 +57,26 @@ public class ExplosionsListener implements Listener {
if (item != null) { if (item != null) {
blocks.remove(); blocks.remove();
if (!(item instanceof WitherProof)) { if (!(item instanceof WitherProof) && !item.callItemHandler(BlockBreakHandler.class, handler -> handleExplosion(handler, block))) {
SlimefunBlockHandler blockHandler = SlimefunPlugin.getRegistry().getBlockHandlers().get(item.getId()); BlockStorage.clearBlockInfo(block);
boolean success = true; block.setType(Material.AIR);
}
}
}
}
if (blockHandler != null) { @ParametersAreNonnullByDefault
success = blockHandler.onBreak(null, block, item, UnregisterReason.EXPLODE); private void handleExplosion(BlockBreakHandler handler, Block block) {
} else { if (handler.isExplosionAllowed(block)) {
item.callItemHandler(BlockBreakHandler.class, handler -> { BlockStorage.clearBlockInfo(block);
if (handler.isExplosionAllowed(block)) { block.setType(Material.AIR);
BlockStorage.clearBlockInfo(block);
block.setType(Material.AIR);
List<ItemStack> drops = new ArrayList<>(); List<ItemStack> drops = new ArrayList<>();
handler.onExplode(block, drops); handler.onExplode(block, drops);
for (ItemStack drop : drops) { for (ItemStack drop : drops) {
if (drop != null && !drop.getType().isAir()) { if (drop != null && !drop.getType().isAir()) {
block.getWorld().dropItemNaturally(block.getLocation(), drop); block.getWorld().dropItemNaturally(block.getLocation(), drop);
}
}
}
});
return;
}
if (success) {
BlockStorage.clearBlockInfo(block);
block.setType(Material.AIR);
}
} }
} }
} }

View File

@ -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);
}

View File

@ -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.Radioactive;
import io.github.thebusybiscuit.slimefun4.core.attributes.Rechargeable; import io.github.thebusybiscuit.slimefun4.core.attributes.Rechargeable;
import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuide; 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.core.researching.Research;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem; 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 // Send out deprecation warnings for any classes or interfaces
checkForDeprecations(getClass()); 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 // Check for an illegal stack size
if (itemStackTemplate.getAmount() != 1) { if (itemStackTemplate.getAmount() != 1) {
// @formatter:off // @formatter:off
@ -1200,19 +1194,4 @@ public class SlimefunItem implements Placeable {
return SlimefunPlugin.getRegistry().getPublicItemHandlers().computeIfAbsent(identifier, c -> new HashSet<>()); 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);
}
} }

View File

@ -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.
* <p>
* 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
}