1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

Merge pull request #2597 from Slimefun/fixes/blocks fixes #2560

Refactoring + Fixes to Block Handlers
This commit is contained in:
TheBusyBiscuit 2021-02-22 18:34:19 +01:00 committed by GitHub
commit 338fe0f84b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 490 additions and 131 deletions

View File

@ -53,11 +53,14 @@
* Added Tier 3 Electric Ore Grinder * Added Tier 3 Electric Ore Grinder
#### Changes #### Changes
* (API) Improvements to the BlockBreakHandler
* Massive performance improvements to holograms/armorstands * Massive performance improvements to holograms/armorstands
* Slimefun no longer requires CS-CoreLib to be installed * Slimefun no longer requires CS-CoreLib to be installed
#### Fixes #### Fixes
* Fixed elevator floor order * Fixed elevator floor order
* Fixed "block-explosions" (e.g. beds in Nether) not properly respecting explosion-resistant blocks
* Fixed #2560
* Fixed #2449 * Fixed #2449
* Fixed #2511 * Fixed #2511
* Fixed #2636 * Fixed #2636
@ -87,6 +90,7 @@
#### Changes #### Changes
* Performance optimizations for Cargo networks * Performance optimizations for Cargo networks
* Removed an old version of bStats * Removed an old version of bStats
* General performance improvements
* CraftBukkit is officially no longer supported, Slimefun will now be disabled on old builds of CraftBukkit * CraftBukkit is officially no longer supported, Slimefun will now be disabled on old builds of CraftBukkit
* Removed the deprecated ItemManipulationAPI for BlockMenus * Removed the deprecated ItemManipulationAPI for BlockMenus
* Removed the "Written Book" variant of the Slimefun Guide * Removed the "Written Book" variant of the Slimefun Guide

View File

@ -333,6 +333,7 @@ public final class SlimefunRegistry {
return globalItemHandlers; return globalItemHandlers;
} }
@Deprecated
@Nonnull @Nonnull
public Map<String, SlimefunBlockHandler> getBlockHandlers() { public Map<String, SlimefunBlockHandler> getBlockHandlers() {
return blockHandlers; return blockHandlers;

View File

@ -2,18 +2,103 @@ package io.github.thebusybiscuit.slimefun4.core.handlers;
import java.util.List; import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.slimefun4.api.events.AndroidMineEvent;
import io.github.thebusybiscuit.slimefun4.implementation.items.androids.MinerAndroid;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.handlers.ItemHandler; import me.mrCookieSlime.Slimefun.Objects.handlers.ItemHandler;
@FunctionalInterface /**
public interface BlockBreakHandler extends ItemHandler { * The {@link BlockBreakHandler} is called when a {@link Block} is broken
* which holds a {@link SlimefunItem}.
* The {@link BlockBreakHandler} provides three methods for this, one for block breaking
* caused by a {@link Player}, one for a {@link MinerAndroid} and one method for a {@link Block}
* being destroyed by an explosion.
*
* @author TheBusyBiscuit
*
* @see BlockPlaceHandler
*
*/
public abstract class BlockBreakHandler implements ItemHandler {
boolean onBlockBreak(BlockBreakEvent e, ItemStack item, int fortune, List<ItemStack> drops); /**
* Whether a {@link MinerAndroid} is allowed to break this block.
*/
private final boolean allowAndroids;
/**
* Whether an explosion is allowed to destroy this block.
*/
private final boolean allowExplosions;
/**
* This constructs a new {@link BlockBreakHandler}.
*
* @param allowAndroids
* Whether a {@link MinerAndroid} is allowed to break blocks of this type
* @param allowExplosions
* Whether blocks of this type are allowed to be broken by explosions
*/
public BlockBreakHandler(boolean allowAndroids, boolean allowExplosions) {
this.allowAndroids = allowAndroids;
this.allowExplosions = allowExplosions;
}
@ParametersAreNonnullByDefault
public abstract void onPlayerBreak(BlockBreakEvent e, ItemStack item, List<ItemStack> drops);
@ParametersAreNonnullByDefault
public void onExplode(Block b, List<ItemStack> drops) {
// This can be overridden, if necessary
}
@ParametersAreNonnullByDefault
public void onAndroidBreak(AndroidMineEvent e) {
// This can be overridden, if necessary
}
/**
* This returns whether an explosion is able to break the given {@link Block}.
*
* @param b
* The {@link Block}
* @return Whether explosions can destroy this {@link Block}
*/
public boolean isExplosionAllowed(@Nonnull Block b) {
/*
* By default our flag is returned, but you can override it
* to be handled on a per-Block basis.
*/
return allowExplosions;
}
/**
* This returns whether a {@link MinerAndroid} is allowed to break
* the given {@link Block}.
*
* @param b
* The {@link Block}
*
* @return Whether androids can break the given {@link Block}
*/
public boolean isAndroidAllowed(@Nonnull Block b) {
/*
* By default our flag is returned, but you can override it
* to be handled on a per-Block basis.
*/
return allowAndroids;
}
@Override @Override
default Class<? extends ItemHandler> getIdentifier() { public final Class<? extends ItemHandler> getIdentifier() {
return BlockBreakHandler.class; return BlockBreakHandler.class;
} }
} }

View File

@ -1,5 +1,7 @@
package io.github.thebusybiscuit.slimefun4.core.handlers; package io.github.thebusybiscuit.slimefun4.core.handlers;
import javax.annotation.Nonnull;
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.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
@ -31,7 +33,7 @@ public abstract class BlockPlaceHandler implements ItemHandler {
* @param e * @param e
* The corresponding {@link BlockPlaceEvent} * The corresponding {@link BlockPlaceEvent}
*/ */
public abstract void onPlayerPlace(BlockPlaceEvent e); public abstract void onPlayerPlace(@Nonnull BlockPlaceEvent e);
/** /**
* This method is called whenever a {@link BlockPlacer} places this {@link Block}. * This method is called whenever a {@link BlockPlacer} places this {@link Block}.
@ -42,7 +44,7 @@ public abstract class BlockPlaceHandler implements ItemHandler {
* @param e * @param e
* The corresponding {@link BlockPlacerPlaceEvent} * The corresponding {@link BlockPlacerPlaceEvent}
*/ */
public void onBlockPlacerPlace(BlockPlacerPlaceEvent e) { public void onBlockPlacerPlace(@Nonnull BlockPlacerPlaceEvent e) {
// This can be overridden, if necessary // This can be overridden, if necessary
} }
@ -56,7 +58,7 @@ public abstract class BlockPlaceHandler implements ItemHandler {
} }
@Override @Override
public Class<? extends ItemHandler> getIdentifier() { public final Class<? extends ItemHandler> getIdentifier() {
return BlockPlaceHandler.class; return BlockPlaceHandler.class;
} }
} }

View File

@ -240,11 +240,11 @@ final class CargoUtils {
int maxSlot = range[1]; int maxSlot = range[1];
for (int slot = minSlot; slot < maxSlot; slot++) { for (int slot = minSlot; slot < maxSlot; slot++) {
ItemStack is = contents[slot]; ItemStack item = contents[slot];
if (matchesFilter(network, node, is)) { if (matchesFilter(network, node, item)) {
inv.setItem(slot, null); inv.setItem(slot, null);
return new ItemStackAndInteger(is, slot); return new ItemStackAndInteger(item, slot);
} }
} }

View File

@ -79,6 +79,7 @@ import io.github.thebusybiscuit.slimefun4.implementation.listeners.GrapplingHook
import io.github.thebusybiscuit.slimefun4.implementation.listeners.HopperListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.HopperListener;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.ItemDropListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.ItemDropListener;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.ItemPickupListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.ItemPickupListener;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.MiningAndroidListener;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.MultiBlockListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.MultiBlockListener;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.NetworkListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.NetworkListener;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.PlayerProfileListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.PlayerProfileListener;
@ -637,6 +638,7 @@ public final class SlimefunPlugin extends JavaPlugin implements SlimefunAddon {
new GrindstoneListener(this); new GrindstoneListener(this);
new CartographyTableListener(this); new CartographyTableListener(this);
new ButcherAndroidListener(this); new ButcherAndroidListener(this);
new MiningAndroidListener(this);
new NetworkListener(this, networkManager); new NetworkListener(this, networkManager);
new HopperListener(this); new HopperListener(this);
new TalismanListener(this); new TalismanListener(this);

View File

@ -0,0 +1,59 @@
package io.github.thebusybiscuit.slimefun4.implementation.handlers;
import java.util.List;
import javax.annotation.Nonnull;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.slimefun4.api.events.AndroidMineEvent;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.implementation.items.androids.MinerAndroid;
/**
* This simple implementation of {@link BlockBreakHandler} will execute the same code
* for when the {@link Block} is broken by a {@link Player}, by a {@link MinerAndroid} or an explosion.
* By default, both androids and explosions are allowed.
*
* @author TheBusyBiscuit
*
* @see BlockBreakHandler
*
*/
public abstract class SimpleBlockBreakHandler extends BlockBreakHandler {
/**
* This constructs a new {@link SimpleBlockBreakHandler}.
*/
public SimpleBlockBreakHandler() {
super(true, true);
}
/**
* This method is called when a {@link Block} of this type is broken by a {@link Player},
* by a {@link MinerAndroid} or through an explosion.
*
* @param b
* The broken {@link Block}
*/
public abstract void onBlockBreak(@Nonnull Block b);
@Override
public void onPlayerBreak(BlockBreakEvent e, ItemStack item, List<ItemStack> drops) {
onBlockBreak(e.getBlock());
}
@Override
public void onAndroidBreak(AndroidMineEvent e) {
onBlockBreak(e.getBlock());
}
@Override
public void onExplode(Block b, List<ItemStack> drops) {
onBlockBreak(b);
}
}

View File

@ -0,0 +1,5 @@
/**
* This package holds simple implementations of {@link me.mrCookieSlime.Slimefun.Objects.handlers.ItemHandler}.
* These are just handlers that can be re-used frequently.
*/
package io.github.thebusybiscuit.slimefun4.implementation.handlers;

View File

@ -21,15 +21,16 @@ import org.bukkit.util.Vector;
import io.github.thebusybiscuit.cscorelib2.chat.ChatColors; import io.github.thebusybiscuit.cscorelib2.chat.ChatColors;
import io.github.thebusybiscuit.cscorelib2.inventory.ItemUtils; import io.github.thebusybiscuit.cscorelib2.inventory.ItemUtils;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockDispenseHandler; import io.github.thebusybiscuit.slimefun4.core.handlers.BlockDispenseHandler;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.handlers.SimpleBlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.implementation.items.SimpleSlimefunItem; import io.github.thebusybiscuit.slimefun4.implementation.items.SimpleSlimefunItem;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.AncientAltarListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.AncientAltarListener;
import io.github.thebusybiscuit.slimefun4.implementation.tasks.AncientAltarTask; import io.github.thebusybiscuit.slimefun4.implementation.tasks.AncientAltarTask;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils; import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
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.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
/** /**
@ -53,7 +54,15 @@ public class AncientPedestal extends SimpleSlimefunItem<BlockDispenseHandler> {
public AncientPedestal(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, ItemStack recipeOutput) { public AncientPedestal(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, ItemStack recipeOutput) {
super(category, item, recipeType, recipe, recipeOutput); super(category, item, recipeType, recipe, recipeOutput);
SlimefunItem.registerBlockHandler(getId(), (p, b, tool, reason) -> { addItemHandler(onBreak());
}
@Nonnull
private BlockBreakHandler onBreak() {
return new SimpleBlockBreakHandler() {
@Override
public void onBlockBreak(@Nonnull Block b) {
Optional<Item> entity = getPlacedItem(b); Optional<Item> entity = getPlacedItem(b);
if (entity.isPresent()) { if (entity.isPresent()) {
@ -65,9 +74,8 @@ public class AncientPedestal extends SimpleSlimefunItem<BlockDispenseHandler> {
stack.remove(); stack.remove();
} }
} }
}
return true; };
});
} }
@Override @Override

View File

@ -56,7 +56,6 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.UnregisterReason;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.MachineFuel; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.MachineFuel;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.interfaces.InventoryBlock; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.interfaces.InventoryBlock;
import me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker; import me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker;
import me.mrCookieSlime.Slimefun.Objects.handlers.ItemHandler;
import me.mrCookieSlime.Slimefun.api.BlockStorage; import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu; import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
@ -151,7 +150,8 @@ public class ProgrammableAndroid extends SlimefunItem implements InventoryBlock,
addItemHandler(onPlace()); addItemHandler(onPlace());
} }
private ItemHandler onPlace() { @Nonnull
private BlockPlaceHandler onPlace() {
return new BlockPlaceHandler(false) { return new BlockPlaceHandler(false) {
@Override @Override

View File

@ -1,5 +1,8 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.blocks; package io.github.thebusybiscuit.slimefun4.implementation.items.blocks;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -16,6 +19,7 @@ import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler; import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler; import io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.handlers.SimpleBlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.utils.ChatUtils; import io.github.thebusybiscuit.slimefun4.utils.ChatUtils;
import io.github.thebusybiscuit.slimefun4.utils.NumberUtils; import io.github.thebusybiscuit.slimefun4.utils.NumberUtils;
import io.github.thebusybiscuit.slimefun4.utils.holograms.SimpleHologram; import io.github.thebusybiscuit.slimefun4.utils.holograms.SimpleHologram;
@ -30,12 +34,14 @@ public class HologramProjector extends SlimefunItem implements HologramOwner {
private static final String OFFSET_PARAMETER = "offset"; private static final String OFFSET_PARAMETER = "offset";
@ParametersAreNonnullByDefault
public HologramProjector(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, ItemStack recipeOutput) { public HologramProjector(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, ItemStack recipeOutput) {
super(category, item, recipeType, recipe, recipeOutput); super(category, item, recipeType, recipe, recipeOutput);
addItemHandler(onPlace(), onRightClick(), onBreak()); addItemHandler(onPlace(), onRightClick(), onBreak());
} }
@Nonnull
private BlockPlaceHandler onPlace() { private BlockPlaceHandler onPlace() {
return new BlockPlaceHandler(false) { return new BlockPlaceHandler(false) {
@ -52,13 +58,18 @@ public class HologramProjector extends SlimefunItem implements HologramOwner {
}; };
} }
@Nonnull
private BlockBreakHandler onBreak() { private BlockBreakHandler onBreak() {
return (e, item, fortune, drops) -> { return new SimpleBlockBreakHandler() {
remove(e.getBlock());
return true; @Override
public void onBlockBreak(@Nonnull Block b) {
remove(b);
}
}; };
} }
@Nonnull
public BlockUseHandler onRightClick() { public BlockUseHandler onRightClick() {
return e -> { return e -> {
e.cancel(); e.cancel();
@ -72,7 +83,7 @@ public class HologramProjector extends SlimefunItem implements HologramOwner {
}; };
} }
private static void openEditor(Player p, Block projector) { private static void openEditor(@Nonnull Player p, @Nonnull Block projector) {
ChestMenu menu = new ChestMenu(SlimefunPlugin.getLocalization().getMessage(p, "machines.HOLOGRAM_PROJECTOR.inventory-title")); ChestMenu menu = new ChestMenu(SlimefunPlugin.getLocalization().getMessage(p, "machines.HOLOGRAM_PROJECTOR.inventory-title"));
menu.addItem(0, new CustomItem(Material.NAME_TAG, "&7Text &e(Click to edit)", "", "&r" + ChatColors.color(BlockStorage.getLocationInfo(projector.getLocation(), "text")))); menu.addItem(0, new CustomItem(Material.NAME_TAG, "&7Text &e(Click to edit)", "", "&r" + ChatColors.color(BlockStorage.getLocationInfo(projector.getLocation(), "text"))));
@ -105,7 +116,7 @@ public class HologramProjector extends SlimefunItem implements HologramOwner {
menu.open(p); menu.open(p);
} }
private static ArmorStand getArmorStand(Block projector, boolean createIfNoneExists) { private static ArmorStand getArmorStand(@Nonnull Block projector, boolean createIfNoneExists) {
String nametag = BlockStorage.getLocationInfo(projector.getLocation(), "text"); String nametag = BlockStorage.getLocationInfo(projector.getLocation(), "text");
double offset = Double.parseDouble(BlockStorage.getLocationInfo(projector.getLocation(), OFFSET_PARAMETER)); double offset = Double.parseDouble(BlockStorage.getLocationInfo(projector.getLocation(), OFFSET_PARAMETER));
Location l = new Location(projector.getWorld(), projector.getX() + 0.5, projector.getY() + offset, projector.getZ() + 0.5); Location l = new Location(projector.getWorld(), projector.getX() + 0.5, projector.getY() + offset, projector.getZ() + 0.5);
@ -129,7 +140,7 @@ public class HologramProjector extends SlimefunItem implements HologramOwner {
return hologram; return hologram;
} }
private static void remove(Block b) { private static void remove(@Nonnull Block b) {
ArmorStand hologram = getArmorStand(b, false); ArmorStand hologram = getArmorStand(b, false);
if (hologram != null) { if (hologram != null) {

View File

@ -11,7 +11,9 @@ import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.core.networks.cargo.CargoNet; import io.github.thebusybiscuit.slimefun4.core.networks.cargo.CargoNet;
import io.github.thebusybiscuit.slimefun4.implementation.handlers.SimpleBlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category; import me.mrCookieSlime.Slimefun.Objects.Category;
@ -39,15 +41,22 @@ abstract class AbstractFilterNode extends AbstractCargoNode {
public AbstractFilterNode(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, @Nullable ItemStack recipeOutput) { public AbstractFilterNode(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, @Nullable ItemStack recipeOutput) {
super(category, item, recipeType, recipe, recipeOutput); super(category, item, recipeType, recipe, recipeOutput);
registerBlockHandler(getId(), (p, b, stack, reason) -> { addItemHandler(onBreak());
}
@Nonnull
private BlockBreakHandler onBreak() {
return new SimpleBlockBreakHandler() {
@Override
public void onBlockBreak(@Nonnull Block b) {
BlockMenu inv = BlockStorage.getInventory(b); BlockMenu inv = BlockStorage.getInventory(b);
if (inv != null) { if (inv != null) {
inv.dropItems(b.getLocation(), SLOTS); inv.dropItems(b.getLocation(), SLOTS);
} }
}
return true; };
});
} }
@Nonnull @Nonnull

View File

@ -2,6 +2,7 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.cargo;
import java.util.Optional; import java.util.Optional;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault; import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -11,8 +12,10 @@ import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.slimefun4.api.events.PlayerRightClickEvent; import io.github.thebusybiscuit.slimefun4.api.events.PlayerRightClickEvent;
import io.github.thebusybiscuit.slimefun4.core.attributes.HologramOwner; import io.github.thebusybiscuit.slimefun4.core.attributes.HologramOwner;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler; import io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler;
import io.github.thebusybiscuit.slimefun4.core.networks.cargo.CargoNet; import io.github.thebusybiscuit.slimefun4.core.networks.cargo.CargoNet;
import io.github.thebusybiscuit.slimefun4.implementation.handlers.SimpleBlockBreakHandler;
import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config; import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category; import me.mrCookieSlime.Slimefun.Objects.Category;
@ -27,10 +30,18 @@ public class CargoManager extends SlimefunItem implements HologramOwner {
public CargoManager(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { public CargoManager(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
super(category, item, recipeType, recipe); super(category, item, recipeType, recipe);
registerBlockHandler(getId(), (p, b, tool, reason) -> { addItemHandler(onBreak());
}
@Nonnull
private BlockBreakHandler onBreak() {
return new SimpleBlockBreakHandler() {
@Override
public void onBlockBreak(@Nonnull Block b) {
removeHologram(b); removeHologram(b);
return true; }
}); };
} }
@Override @Override

View File

@ -12,9 +12,11 @@ import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.cscorelib2.protection.ProtectableAction; import io.github.thebusybiscuit.cscorelib2.protection.ProtectableAction;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.core.networks.cargo.CargoNet; import io.github.thebusybiscuit.slimefun4.core.networks.cargo.CargoNet;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.handlers.SimpleBlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.implementation.items.electric.reactors.Reactor; import io.github.thebusybiscuit.slimefun4.implementation.items.electric.reactors.Reactor;
import io.github.thebusybiscuit.slimefun4.implementation.items.misc.CoolantCell; import io.github.thebusybiscuit.slimefun4.implementation.items.misc.CoolantCell;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
@ -50,6 +52,8 @@ public class ReactorAccessPort extends SlimefunItem {
public ReactorAccessPort(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { public ReactorAccessPort(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
super(category, item, recipeType, recipe); super(category, item, recipeType, recipe);
addItemHandler(onBreak());
new BlockMenuPreset(getId(), "&2Reactor Access Port") { new BlockMenuPreset(getId(), "&2Reactor Access Port") {
@Override @Override
@ -108,8 +112,14 @@ public class ReactorAccessPort extends SlimefunItem {
} }
} }
}; };
}
registerBlockHandler(getId(), (p, b, tool, reason) -> { @Nonnull
private BlockBreakHandler onBreak() {
return new SimpleBlockBreakHandler() {
@Override
public void onBlockBreak(@Nonnull Block b) {
BlockMenu inv = BlockStorage.getInventory(b); BlockMenu inv = BlockStorage.getInventory(b);
if (inv != null) { if (inv != null) {
@ -117,9 +127,8 @@ public class ReactorAccessPort extends SlimefunItem {
inv.dropItems(b.getLocation(), getCoolantSlots()); inv.dropItems(b.getLocation(), getCoolantSlots());
inv.dropItems(b.getLocation(), getOutputSlots()); inv.dropItems(b.getLocation(), getOutputSlots());
} }
}
return true; };
});
} }
private void constructMenu(@Nonnull BlockMenuPreset preset) { private void constructMenu(@Nonnull BlockMenuPreset preset) {

View File

@ -9,8 +9,10 @@ import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent; import io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent;
import io.github.thebusybiscuit.slimefun4.core.attributes.HologramOwner; import io.github.thebusybiscuit.slimefun4.core.attributes.HologramOwner;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler; import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler;
import io.github.thebusybiscuit.slimefun4.core.networks.energy.EnergyNet; import io.github.thebusybiscuit.slimefun4.core.networks.energy.EnergyNet;
import io.github.thebusybiscuit.slimefun4.implementation.handlers.SimpleBlockBreakHandler;
import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config; import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category; import me.mrCookieSlime.Slimefun.Objects.Category;
@ -34,10 +36,18 @@ public class EnergyRegulator extends SlimefunItem implements HologramOwner {
public EnergyRegulator(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { public EnergyRegulator(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
super(category, item, recipeType, recipe); super(category, item, recipeType, recipe);
SlimefunItem.registerBlockHandler(getId(), (p, b, stack, reason) -> { addItemHandler(onBreak());
}
@Nonnull
private BlockBreakHandler onBreak() {
return new SimpleBlockBreakHandler() {
@Override
public void onBlockBreak(@Nonnull Block b) {
removeHologram(b); removeHologram(b);
return true; }
}); };
} }
@Nonnull @Nonnull

View File

@ -23,9 +23,11 @@ import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.cscorelib2.protection.ProtectableAction; import io.github.thebusybiscuit.cscorelib2.protection.ProtectableAction;
import io.github.thebusybiscuit.slimefun4.api.events.AsyncReactorProcessCompleteEvent; import io.github.thebusybiscuit.slimefun4.api.events.AsyncReactorProcessCompleteEvent;
import io.github.thebusybiscuit.slimefun4.api.events.ReactorExplodeEvent; import io.github.thebusybiscuit.slimefun4.api.events.ReactorExplodeEvent;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.core.attributes.HologramOwner; import io.github.thebusybiscuit.slimefun4.core.attributes.HologramOwner;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.handlers.SimpleBlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.implementation.items.cargo.ReactorAccessPort; import io.github.thebusybiscuit.slimefun4.implementation.items.cargo.ReactorAccessPort;
import io.github.thebusybiscuit.slimefun4.implementation.items.electric.AbstractEnergyProvider; import io.github.thebusybiscuit.slimefun4.implementation.items.electric.AbstractEnergyProvider;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
@ -106,7 +108,16 @@ public abstract class Reactor extends AbstractEnergyProvider implements Hologram
} }
}; };
registerBlockHandler(getId(), (p, b, tool, reason) -> { addItemHandler(onBreak());
registerDefaultFuelTypes();
}
@Nonnull
private BlockBreakHandler onBreak() {
return new SimpleBlockBreakHandler() {
@Override
public void onBlockBreak(@Nonnull Block b) {
BlockMenu inv = BlockStorage.getInventory(b); BlockMenu inv = BlockStorage.getInventory(b);
if (inv != null) { if (inv != null) {
@ -118,10 +129,8 @@ public abstract class Reactor extends AbstractEnergyProvider implements Hologram
progress.remove(b.getLocation()); progress.remove(b.getLocation());
processing.remove(b.getLocation()); processing.remove(b.getLocation());
removeHologram(b); removeHologram(b);
return true; }
}); };
registerDefaultFuelTypes();
} }
protected void updateInventory(@Nonnull BlockMenu menu, @Nonnull Block b) { protected void updateInventory(@Nonnull BlockMenu menu, @Nonnull Block b) {

View File

@ -4,6 +4,9 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.OptionalInt; import java.util.OptionalInt;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -16,8 +19,10 @@ import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.slimefun4.api.geo.GEOResource; import io.github.thebusybiscuit.slimefun4.api.geo.GEOResource;
import io.github.thebusybiscuit.slimefun4.core.attributes.HologramOwner; import io.github.thebusybiscuit.slimefun4.core.attributes.HologramOwner;
import io.github.thebusybiscuit.slimefun4.core.attributes.RecipeDisplayItem; import io.github.thebusybiscuit.slimefun4.core.attributes.RecipeDisplayItem;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler; import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.handlers.SimpleBlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu.AdvancedMenuClickHandler; import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu.AdvancedMenuClickHandler;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ClickAction; import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ClickAction;
@ -37,11 +42,31 @@ public class GEOMiner extends AContainer implements RecipeDisplayItem, HologramO
private static final int[] OUTPUT_SLOTS = { 29, 30, 31, 32, 33, 38, 39, 40, 41, 42 }; private static final int[] OUTPUT_SLOTS = { 29, 30, 31, 32, 33, 38, 39, 40, 41, 42 };
private static final int PROCESSING_TIME = 14; private static final int PROCESSING_TIME = 14;
@ParametersAreNonnullByDefault
public GEOMiner(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { public GEOMiner(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
super(category, item, recipeType, recipe); super(category, item, recipeType, recipe);
addItemHandler(onPlace()); addItemHandler(onPlace());
registerBlockHandler(getId(), (p, b, stack, reason) -> { addItemHandler(onBreak());
}
@Nonnull
private BlockPlaceHandler onPlace() {
return new BlockPlaceHandler(false) {
@Override
public void onPlayerPlace(BlockPlaceEvent e) {
updateHologram(e.getBlock(), "&7Idling...");
}
};
}
@Nonnull
private BlockBreakHandler onBreak() {
return new SimpleBlockBreakHandler() {
@Override
public void onBlockBreak(@Nonnull Block b) {
removeHologram(b); removeHologram(b);
BlockMenu inv = BlockStorage.getInventory(b); BlockMenu inv = BlockStorage.getInventory(b);
@ -52,16 +77,6 @@ public class GEOMiner extends AContainer implements RecipeDisplayItem, HologramO
progress.remove(b); progress.remove(b);
processing.remove(b); processing.remove(b);
return true;
});
}
private BlockPlaceHandler onPlace() {
return new BlockPlaceHandler(false) {
@Override
public void onPlayerPlace(BlockPlaceEvent e) {
updateHologram(e.getBlock(), "&7Idling...");
} }
}; };
} }

View File

@ -1,12 +1,19 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.gps; package io.github.thebusybiscuit.slimefun4.implementation.items.gps;
import java.util.List;
import java.util.UUID; import java.util.UUID;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.Location;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent; import io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler; import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler;
import io.github.thebusybiscuit.slimefun4.core.networks.energy.EnergyNetComponentType; import io.github.thebusybiscuit.slimefun4.core.networks.energy.EnergyNetComponentType;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
@ -23,16 +30,12 @@ public abstract class GPSTransmitter extends SimpleSlimefunItem<BlockTicker> imp
private final int capacity; private final int capacity;
@ParametersAreNonnullByDefault
public GPSTransmitter(Category category, int tier, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { public GPSTransmitter(Category category, int tier, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
super(category, item, recipeType, recipe); super(category, item, recipeType, recipe);
this.capacity = 4 << (2 * tier); this.capacity = 4 << (2 * tier);
addItemHandler(onPlace()); addItemHandler(onPlace(), onBreak());
registerBlockHandler(getId(), (p, b, stack, reason) -> {
UUID owner = UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner"));
SlimefunPlugin.getGPSNetwork().updateTransmitter(b.getLocation(), owner, false);
return true;
});
} }
@Override @Override
@ -40,6 +43,7 @@ public abstract class GPSTransmitter extends SimpleSlimefunItem<BlockTicker> imp
return capacity; return capacity;
} }
@Nonnull
private BlockPlaceHandler onPlace() { private BlockPlaceHandler onPlace() {
return new BlockPlaceHandler(false) { return new BlockPlaceHandler(false) {
@ -50,6 +54,19 @@ public abstract class GPSTransmitter extends SimpleSlimefunItem<BlockTicker> imp
}; };
} }
@Nonnull
private BlockBreakHandler onBreak() {
return new BlockBreakHandler(false, false) {
@Override
public void onPlayerBreak(BlockBreakEvent e, ItemStack item, List<ItemStack> drops) {
Location l = e.getBlock().getLocation();
UUID owner = UUID.fromString(BlockStorage.getLocationInfo(l, "owner"));
SlimefunPlugin.getGPSNetwork().updateTransmitter(l, owner, false);
}
};
}
public abstract int getMultiplier(int y); public abstract int getMultiplier(int y);
public abstract int getEnergyConsumption(); public abstract int getEnergyConsumption();

View File

@ -56,14 +56,9 @@ public class BlockListener implements Listener {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onBlockPlaceExisting(BlockPlaceEvent e) { public void onBlockPlaceExisting(BlockPlaceEvent e) {
/*
* This prevents Players from placing a block where another block already exists.
* While this can cause ghost blocks it also prevents them from replacing grass
* or saplings etc...
*/
Block block = e.getBlock(); Block block = e.getBlock();
// Fixes #2636 // Fixes #2636 - This will solve the "ghost blocks" issue
if (e.getBlockReplacedState().getType().isAir()) { if (e.getBlockReplacedState().getType().isAir()) {
SlimefunItem sfItem = BlockStorage.check(block); SlimefunItem sfItem = BlockStorage.check(block);
@ -77,6 +72,7 @@ public class BlockListener implements Listener {
BlockStorage.clearBlockInfo(block); BlockStorage.clearBlockInfo(block);
} }
} else if (BlockStorage.hasBlockInfo(e.getBlock())) { } else if (BlockStorage.hasBlockInfo(e.getBlock())) {
// If there is no air (e.g. grass) then don't let the block be placed
e.setCancelled(true); e.setCancelled(true);
} }
} }
@ -117,18 +113,18 @@ public class BlockListener implements Listener {
return; return;
} }
checkForSensitiveBlockAbove(e.getPlayer(), e.getBlock());
ItemStack item = e.getPlayer().getInventory().getItemInMainHand(); ItemStack item = e.getPlayer().getInventory().getItemInMainHand();
checkForSensitiveBlockAbove(e, item);
int fortune = getBonusDropsWithFortune(item, e.getBlock()); int fortune = getBonusDropsWithFortune(item, e.getBlock());
List<ItemStack> drops = new ArrayList<>(); List<ItemStack> drops = new ArrayList<>();
if (!item.getType().isAir()) { if (!e.isCancelled() && !item.getType().isAir()) {
callToolHandler(e, item, fortune, drops); callToolHandler(e, item, fortune, drops);
} }
if (!e.isCancelled()) { if (!e.isCancelled()) {
callBlockHandler(e, item, fortune, drops); callBlockHandler(e, item, drops);
} }
dropItems(e, drops); dropItems(e, drops);
@ -148,7 +144,7 @@ public class BlockListener implements Listener {
} }
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
private void callBlockHandler(BlockBreakEvent e, ItemStack item, int fortune, List<ItemStack> drops) { private void callBlockHandler(BlockBreakEvent e, ItemStack item, List<ItemStack> drops) {
SlimefunItem sfItem = BlockStorage.check(e.getBlock()); SlimefunItem sfItem = BlockStorage.check(e.getBlock());
if (sfItem == null && SlimefunPlugin.getBlockDataService().isTileEntity(e.getBlock().getType())) { if (sfItem == null && SlimefunPlugin.getBlockDataService().isTileEntity(e.getBlock().getType())) {
@ -172,7 +168,7 @@ public class BlockListener implements Listener {
sfItem.error("Something went wrong while triggering a BlockHandler", x); sfItem.error("Something went wrong while triggering a BlockHandler", x);
} }
} else { } else {
sfItem.callItemHandler(BlockBreakHandler.class, handler -> handler.onBlockBreak(e, item, fortune, drops)); sfItem.callItemHandler(BlockBreakHandler.class, handler -> handler.onPlayerBreak(e, item, drops));
} }
drops.addAll(sfItem.getDrops()); drops.addAll(sfItem.getDrops());
@ -182,13 +178,15 @@ public class BlockListener implements Listener {
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
private void dropItems(BlockBreakEvent e, List<ItemStack> drops) { private void dropItems(BlockBreakEvent e, List<ItemStack> drops) {
if (!drops.isEmpty()) { if (!drops.isEmpty() && !e.isCancelled()) {
e.getBlock().setType(Material.AIR);
// Notify plugins like CoreProtect // Notify plugins like CoreProtect
SlimefunPlugin.getProtectionManager().logAction(e.getPlayer(), e.getBlock(), ProtectableAction.BREAK_BLOCK); SlimefunPlugin.getProtectionManager().logAction(e.getPlayer(), e.getBlock(), ProtectableAction.BREAK_BLOCK);
// Fixes #2560
if (e.isDropItems()) { if (e.isDropItems()) {
// Disable normal block drops
e.setDropItems(false);
for (ItemStack drop : drops) { for (ItemStack drop : drops) {
if (drop != null && drop.getType() != Material.AIR) { if (drop != null && drop.getType() != Material.AIR) {
e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), drop); e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), drop);
@ -209,8 +207,8 @@ public class BlockListener implements Listener {
* The {@link Block} that was broken * The {@link Block} that was broken
*/ */
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
private void checkForSensitiveBlockAbove(Player p, Block b) { private void checkForSensitiveBlockAbove(BlockBreakEvent e, ItemStack item) {
Block blockAbove = b.getRelative(BlockFace.UP); Block blockAbove = e.getBlock().getRelative(BlockFace.UP);
if (SlimefunTag.SENSITIVE_MATERIALS.isTagged(blockAbove.getType())) { if (SlimefunTag.SENSITIVE_MATERIALS.isTagged(blockAbove.getType())) {
SlimefunItem sfItem = BlockStorage.check(blockAbove); SlimefunItem sfItem = BlockStorage.check(blockAbove);
@ -219,13 +217,29 @@ public class BlockListener implements Listener {
SlimefunBlockHandler blockHandler = SlimefunPlugin.getRegistry().getBlockHandlers().get(sfItem.getId()); SlimefunBlockHandler blockHandler = SlimefunPlugin.getRegistry().getBlockHandlers().get(sfItem.getId());
if (blockHandler != null) { if (blockHandler != null) {
if (blockHandler.onBreak(p, blockAbove, sfItem, UnregisterReason.PLAYER_BREAK)) { if (blockHandler.onBreak(e.getPlayer(), blockAbove, sfItem, UnregisterReason.PLAYER_BREAK)) {
blockAbove.getWorld().dropItemNaturally(blockAbove.getLocation(), BlockStorage.retrieve(blockAbove)); blockAbove.getWorld().dropItemNaturally(blockAbove.getLocation(), BlockStorage.retrieve(blockAbove));
blockAbove.setType(Material.AIR); blockAbove.setType(Material.AIR);
} }
} else { } else {
blockAbove.getWorld().dropItemNaturally(blockAbove.getLocation(), BlockStorage.retrieve(blockAbove)); /*
* 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));
blockAbove.setType(Material.AIR); blockAbove.setType(Material.AIR);
if (!dummyEvent.isCancelled() && dummyEvent.isDropItems()) {
for (ItemStack drop : drops) {
if (drop != null && drop.getType() != Material.AIR) {
blockAbove.getWorld().dropItemNaturally(blockAbove.getLocation(), drop);
}
}
}
} }
} }
} }

View File

@ -1,6 +1,8 @@
package io.github.thebusybiscuit.slimefun4.implementation.listeners; package io.github.thebusybiscuit.slimefun4.implementation.listeners;
import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -9,15 +11,29 @@ import org.bukkit.block.Block;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.EntityExplodeEvent;
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.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Objects.SlimefunBlockHandler; 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.Objects.SlimefunItem.UnregisterReason;
import me.mrCookieSlime.Slimefun.api.BlockStorage; import me.mrCookieSlime.Slimefun.api.BlockStorage;
/**
* The {@link ExplosionsListener} is a {@link Listener} which listens to any explosion events.
* Any {@link WitherProof} block is excluded from these explosions and this {@link Listener} also
* calls the explosive part of the {@link BlockBreakHandler}.
*
* @author TheBusyBiscuit
*
* @see BlockBreakHandler
* @see WitherProof
*
*/
public class ExplosionsListener implements Listener { public class ExplosionsListener implements Listener {
public ExplosionsListener(@Nonnull SlimefunPlugin plugin) { public ExplosionsListener(@Nonnull SlimefunPlugin plugin) {
@ -26,12 +42,19 @@ public class ExplosionsListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onEntityExplode(EntityExplodeEvent e) { public void onEntityExplode(EntityExplodeEvent e) {
Iterator<Block> blocks = e.blockList().iterator(); removeResistantBlocks(e.blockList().iterator());
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockExplode(BlockExplodeEvent e) {
removeResistantBlocks(e.blockList().iterator());
}
private void removeResistantBlocks(@Nonnull Iterator<Block> blocks) {
while (blocks.hasNext()) { while (blocks.hasNext()) {
Block block = blocks.next(); Block block = blocks.next();
SlimefunItem item = BlockStorage.check(block); SlimefunItem item = BlockStorage.check(block);
if (item != null) { if (item != null) {
blocks.remove(); blocks.remove();
@ -41,6 +64,24 @@ public class ExplosionsListener implements Listener {
if (blockHandler != null) { if (blockHandler != null) {
success = blockHandler.onBreak(null, block, item, UnregisterReason.EXPLODE); 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);
List<ItemStack> 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) { if (success) {

View File

@ -0,0 +1,32 @@
package io.github.thebusybiscuit.slimefun4.implementation.listeners;
import javax.annotation.Nonnull;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import io.github.thebusybiscuit.slimefun4.api.events.AndroidMineEvent;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
public class MiningAndroidListener implements Listener {
public MiningAndroidListener(@Nonnull SlimefunPlugin plugin) {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
@EventHandler(ignoreCancelled = true)
public void onAndroidMine(AndroidMineEvent e) {
SlimefunItem item = BlockStorage.check(e.getBlock());
item.callItemHandler(BlockBreakHandler.class, handler -> {
if (handler.isAndroidAllowed(e.getBlock())) {
handler.onAndroidBreak(e);
} else {
e.setCancelled(true);
}
});
}
}

View File

@ -4,6 +4,7 @@ import org.bukkit.block.Block;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Event; 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.SlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.UnregisterReason; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.UnregisterReason;
@ -16,8 +17,10 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.UnregisterReason;
* *
* @author TheBusyBiscuit * @author TheBusyBiscuit
* *
* @deprecated Please use the {@link BlockBreakHandler} instead.
* *
*/ */
@Deprecated
@FunctionalInterface @FunctionalInterface
public interface SlimefunBlockHandler { public interface SlimefunBlockHandler {

View File

@ -40,6 +40,7 @@ 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;
@ -1178,6 +1179,17 @@ 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(String id, SlimefunBlockHandler handler) { public static void registerBlockHandler(String id, SlimefunBlockHandler handler) {
SlimefunPlugin.getRegistry().getBlockHandlers().put(id, handler); SlimefunPlugin.getRegistry().getBlockHandlers().put(id, handler);
} }

View File

@ -10,9 +10,12 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunBlockHandler;
* *
* @author TheBusyBiscuit * @author TheBusyBiscuit
* *
* @deprecated This enum is no longer needed
*
* @see SlimefunBlockHandler * @see SlimefunBlockHandler
* *
*/ */
@Deprecated
public enum UnregisterReason { public enum UnregisterReason {
/** /**

View File

@ -19,6 +19,7 @@ import me.mrCookieSlime.Slimefun.api.item_transport.ItemTransportFlow;
* @deprecated This interface is not designed to be used by addons. * @deprecated This interface is not designed to be used by addons.
* *
*/ */
@Deprecated
public interface InventoryBlock { public interface InventoryBlock {
/** /**

View File

@ -2,6 +2,8 @@ package me.mrCookieSlime.Slimefun.Objects.handlers;
import java.util.Optional; import java.util.Optional;
import javax.annotation.Nonnull;
import io.github.thebusybiscuit.slimefun4.api.exceptions.IncompatibleItemHandlerException; import io.github.thebusybiscuit.slimefun4.api.exceptions.IncompatibleItemHandlerException;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler; import io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler;
import io.github.thebusybiscuit.slimefun4.core.handlers.BowShootHandler; import io.github.thebusybiscuit.slimefun4.core.handlers.BowShootHandler;
@ -51,7 +53,8 @@ public interface ItemHandler {
* @return An {@link Optional} describing the result, it will contain an {@link IncompatibleItemHandlerException} * @return An {@link Optional} describing the result, it will contain an {@link IncompatibleItemHandlerException}
* should there be an issue * should there be an issue
*/ */
default Optional<IncompatibleItemHandlerException> validate(SlimefunItem item) { @Nonnull
default Optional<IncompatibleItemHandlerException> validate(@Nonnull SlimefunItem item) {
return Optional.empty(); return Optional.empty();
} }
@ -61,5 +64,6 @@ public interface ItemHandler {
* *
* @return The {@link Class} identifier for this {@link ItemHandler} * @return The {@link Class} identifier for this {@link ItemHandler}
*/ */
@Nonnull
Class<? extends ItemHandler> getIdentifier(); Class<? extends ItemHandler> getIdentifier();
} }

View File

@ -417,21 +417,16 @@ public class BlockStorage {
* @return the SlimefunItem's ItemStack corresponding to the block if it has one, otherwise null * @return the SlimefunItem's ItemStack corresponding to the block if it has one, otherwise null
*/ */
@Nullable @Nullable
public static ItemStack retrieve(Block block) { public static ItemStack retrieve(@Nonnull Block block) {
if (!hasBlockInfo(block)) { SlimefunItem item = check(block);
return null;
} else {
String id = getLocationInfo(block.getLocation(), "id");
SlimefunItem item = SlimefunItem.getByID(id);
clearBlockInfo(block);
if (item == null) { if (item == null) {
return null; return null;
} else { } else {
clearBlockInfo(block);
return item.getItem(); return item.getItem();
} }
} }
}
@Nonnull @Nonnull
public static Config getLocationInfo(Location l) { public static Config getLocationInfo(Location l) {
@ -705,6 +700,11 @@ public class BlockStorage {
return id == null ? null : SlimefunItem.getByID(id); return id == null ? null : SlimefunItem.getByID(id);
} }
public static boolean check(Block block, String slimefunItem) {
String id = checkID(block);
return id != null && id.equals(slimefunItem);
}
@Nullable @Nullable
public static String checkID(@Nonnull Block b) { public static String checkID(@Nonnull Block b) {
// Only access the BlockState when on the main thread // Only access the BlockState when on the main thread
@ -719,16 +719,8 @@ public class BlockStorage {
return checkID(b.getLocation()); return checkID(b.getLocation());
} }
public static boolean check(Block block, String slimefunItem) { @Nullable
String id = checkID(block); public static String checkID(@Nonnull Location l) {
return id != null && id.equals(slimefunItem);
}
public static String checkID(Location l) {
if (!hasBlockInfo(l)) {
return null;
}
return getLocationInfo(l, "id"); return getLocationInfo(l, "id");
} }
@ -741,7 +733,7 @@ public class BlockStorage {
return id != null && id.equals(slimefunItem); return id != null && id.equals(slimefunItem);
} }
public static boolean isWorldLoaded(World world) { public static boolean isWorldLoaded(@Nonnull World world) {
return SlimefunPlugin.getRegistry().getWorlds().containsKey(world.getName()); return SlimefunPlugin.getRegistry().getWorlds().containsKey(world.getName());
} }