1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +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
#### Changes
* (API) Improvements to the BlockBreakHandler
* Massive performance improvements to holograms/armorstands
* Slimefun no longer requires CS-CoreLib to be installed
#### Fixes
* Fixed elevator floor order
* Fixed "block-explosions" (e.g. beds in Nether) not properly respecting explosion-resistant blocks
* Fixed #2560
* Fixed #2449
* Fixed #2511
* Fixed #2636
@ -87,6 +90,7 @@
#### Changes
* Performance optimizations for Cargo networks
* 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
* Removed the deprecated ItemManipulationAPI for BlockMenus
* Removed the "Written Book" variant of the Slimefun Guide

View File

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

View File

@ -2,18 +2,103 @@ package io.github.thebusybiscuit.slimefun4.core.handlers;
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.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;
@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
default Class<? extends ItemHandler> getIdentifier() {
public final Class<? extends ItemHandler> getIdentifier() {
return BlockBreakHandler.class;
}
}

View File

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

View File

@ -240,11 +240,11 @@ final class CargoUtils {
int maxSlot = range[1];
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);
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.ItemDropListener;
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.NetworkListener;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.PlayerProfileListener;
@ -637,6 +638,7 @@ public final class SlimefunPlugin extends JavaPlugin implements SlimefunAddon {
new GrindstoneListener(this);
new CartographyTableListener(this);
new ButcherAndroidListener(this);
new MiningAndroidListener(this);
new NetworkListener(this, networkManager);
new HopperListener(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.inventory.ItemUtils;
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.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.handlers.SimpleBlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.implementation.items.SimpleSlimefunItem;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.AncientAltarListener;
import io.github.thebusybiscuit.slimefun4.implementation.tasks.AncientAltarTask;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
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) {
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);
if (entity.isPresent()) {
@ -65,9 +74,8 @@ public class AncientPedestal extends SimpleSlimefunItem<BlockDispenseHandler> {
stack.remove();
}
}
return true;
});
}
};
}
@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.interfaces.InventoryBlock;
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.SlimefunItemStack;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
@ -151,7 +150,8 @@ public class ProgrammableAndroid extends SlimefunItem implements InventoryBlock,
addItemHandler(onPlace());
}
private ItemHandler onPlace() {
@Nonnull
private BlockPlaceHandler onPlace() {
return new BlockPlaceHandler(false) {
@Override

View File

@ -1,5 +1,8 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.blocks;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.Location;
import org.bukkit.Material;
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.BlockUseHandler;
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.NumberUtils;
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";
@ParametersAreNonnullByDefault
public HologramProjector(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, ItemStack recipeOutput) {
super(category, item, recipeType, recipe, recipeOutput);
addItemHandler(onPlace(), onRightClick(), onBreak());
}
@Nonnull
private BlockPlaceHandler onPlace() {
return new BlockPlaceHandler(false) {
@ -52,13 +58,18 @@ public class HologramProjector extends SlimefunItem implements HologramOwner {
};
}
@Nonnull
private BlockBreakHandler onBreak() {
return (e, item, fortune, drops) -> {
remove(e.getBlock());
return true;
return new SimpleBlockBreakHandler() {
@Override
public void onBlockBreak(@Nonnull Block b) {
remove(b);
}
};
}
@Nonnull
public BlockUseHandler onRightClick() {
return e -> {
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"));
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);
}
private static ArmorStand getArmorStand(Block projector, boolean createIfNoneExists) {
private static ArmorStand getArmorStand(@Nonnull Block projector, boolean createIfNoneExists) {
String nametag = BlockStorage.getLocationInfo(projector.getLocation(), "text");
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);
@ -129,7 +140,7 @@ public class HologramProjector extends SlimefunItem implements HologramOwner {
return hologram;
}
private static void remove(Block b) {
private static void remove(@Nonnull Block b) {
ArmorStand hologram = getArmorStand(b, false);
if (hologram != null) {

View File

@ -11,7 +11,9 @@ import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;
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.implementation.handlers.SimpleBlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
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) {
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);
if (inv != null) {
inv.dropItems(b.getLocation(), SLOTS);
}
return true;
});
}
};
}
@Nonnull

View File

@ -2,6 +2,7 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.cargo;
import java.util.Optional;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
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.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.networks.cargo.CargoNet;
import io.github.thebusybiscuit.slimefun4.implementation.handlers.SimpleBlockBreakHandler;
import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
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) {
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);
return true;
});
}
};
}
@Override

View File

@ -12,9 +12,11 @@ import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
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.implementation.SlimefunItems;
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.misc.CoolantCell;
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) {
super(category, item, recipeType, recipe);
addItemHandler(onBreak());
new BlockMenuPreset(getId(), "&2Reactor Access Port") {
@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);
if (inv != null) {
@ -117,9 +127,8 @@ public class ReactorAccessPort extends SlimefunItem {
inv.dropItems(b.getLocation(), getCoolantSlots());
inv.dropItems(b.getLocation(), getOutputSlots());
}
return true;
});
}
};
}
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.HologramOwner;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler;
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.Slimefun.Lists.RecipeType;
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) {
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);
return true;
});
}
};
}
@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.slimefun4.api.events.AsyncReactorProcessCompleteEvent;
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.implementation.SlimefunItems;
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.electric.AbstractEnergyProvider;
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);
if (inv != null) {
@ -118,10 +129,8 @@ public abstract class Reactor extends AbstractEnergyProvider implements Hologram
progress.remove(b.getLocation());
processing.remove(b.getLocation());
removeHologram(b);
return true;
});
registerDefaultFuelTypes();
}
};
}
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.OptionalInt;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.ChatColor;
import org.bukkit.Material;
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.core.attributes.HologramOwner;
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.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.handlers.SimpleBlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu.AdvancedMenuClickHandler;
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 PROCESSING_TIME = 14;
@ParametersAreNonnullByDefault
public GEOMiner(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
super(category, item, recipeType, recipe);
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);
BlockMenu inv = BlockStorage.getInventory(b);
@ -52,16 +77,6 @@ public class GEOMiner extends AContainer implements RecipeDisplayItem, HologramO
progress.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;
import java.util.List;
import java.util.UUID;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;
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.networks.energy.EnergyNetComponentType;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
@ -23,16 +30,12 @@ public abstract class GPSTransmitter extends SimpleSlimefunItem<BlockTicker> imp
private final int capacity;
@ParametersAreNonnullByDefault
public GPSTransmitter(Category category, int tier, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
super(category, item, recipeType, recipe);
this.capacity = 4 << (2 * tier);
addItemHandler(onPlace());
registerBlockHandler(getId(), (p, b, stack, reason) -> {
UUID owner = UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner"));
SlimefunPlugin.getGPSNetwork().updateTransmitter(b.getLocation(), owner, false);
return true;
});
addItemHandler(onPlace(), onBreak());
}
@Override
@ -40,6 +43,7 @@ public abstract class GPSTransmitter extends SimpleSlimefunItem<BlockTicker> imp
return capacity;
}
@Nonnull
private BlockPlaceHandler onPlace() {
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 getEnergyConsumption();

View File

@ -56,14 +56,9 @@ public class BlockListener implements Listener {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
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();
// Fixes #2636
// Fixes #2636 - This will solve the "ghost blocks" issue
if (e.getBlockReplacedState().getType().isAir()) {
SlimefunItem sfItem = BlockStorage.check(block);
@ -77,6 +72,7 @@ public class BlockListener implements Listener {
BlockStorage.clearBlockInfo(block);
}
} 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);
}
}
@ -117,18 +113,18 @@ public class BlockListener implements Listener {
return;
}
checkForSensitiveBlockAbove(e.getPlayer(), e.getBlock());
ItemStack item = e.getPlayer().getInventory().getItemInMainHand();
checkForSensitiveBlockAbove(e, item);
int fortune = getBonusDropsWithFortune(item, e.getBlock());
List<ItemStack> drops = new ArrayList<>();
if (!item.getType().isAir()) {
if (!e.isCancelled() && !item.getType().isAir()) {
callToolHandler(e, item, fortune, drops);
}
if (!e.isCancelled()) {
callBlockHandler(e, item, fortune, drops);
callBlockHandler(e, item, drops);
}
dropItems(e, drops);
@ -148,7 +144,7 @@ public class BlockListener implements Listener {
}
@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());
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);
}
} 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());
@ -182,13 +178,15 @@ public class BlockListener implements Listener {
@ParametersAreNonnullByDefault
private void dropItems(BlockBreakEvent e, List<ItemStack> drops) {
if (!drops.isEmpty()) {
e.getBlock().setType(Material.AIR);
if (!drops.isEmpty() && !e.isCancelled()) {
// Notify plugins like CoreProtect
SlimefunPlugin.getProtectionManager().logAction(e.getPlayer(), e.getBlock(), ProtectableAction.BREAK_BLOCK);
// Fixes #2560
if (e.isDropItems()) {
// Disable normal block drops
e.setDropItems(false);
for (ItemStack drop : drops) {
if (drop != null && drop.getType() != Material.AIR) {
e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), drop);
@ -209,8 +207,8 @@ public class BlockListener implements Listener {
* The {@link Block} that was broken
*/
@ParametersAreNonnullByDefault
private void checkForSensitiveBlockAbove(Player p, Block b) {
Block blockAbove = b.getRelative(BlockFace.UP);
private void checkForSensitiveBlockAbove(BlockBreakEvent e, ItemStack item) {
Block blockAbove = e.getBlock().getRelative(BlockFace.UP);
if (SlimefunTag.SENSITIVE_MATERIALS.isTagged(blockAbove.getType())) {
SlimefunItem sfItem = BlockStorage.check(blockAbove);
@ -219,13 +217,29 @@ public class BlockListener implements Listener {
SlimefunBlockHandler blockHandler = SlimefunPlugin.getRegistry().getBlockHandlers().get(sfItem.getId());
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.setType(Material.AIR);
}
} 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);
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;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nonnull;
@ -9,15 +11,29 @@ import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockExplodeEvent;
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.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;
/**
* 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 ExplosionsListener(@Nonnull SlimefunPlugin plugin) {
@ -26,12 +42,19 @@ public class ExplosionsListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
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()) {
Block block = blocks.next();
SlimefunItem item = BlockStorage.check(block);
if (item != null) {
blocks.remove();
@ -41,6 +64,24 @@ public class ExplosionsListener implements Listener {
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);
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) {

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.event.Event;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.UnregisterReason;
@ -16,8 +17,10 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.UnregisterReason;
*
* @author TheBusyBiscuit
*
* @deprecated Please use the {@link BlockBreakHandler} instead.
*
*/
@Deprecated
@FunctionalInterface
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.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;
@ -1178,6 +1179,17 @@ 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(String id, SlimefunBlockHandler handler) {
SlimefunPlugin.getRegistry().getBlockHandlers().put(id, handler);
}

View File

@ -10,9 +10,12 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunBlockHandler;
*
* @author TheBusyBiscuit
*
* @deprecated This enum is no longer needed
*
* @see SlimefunBlockHandler
*
*/
@Deprecated
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
public interface InventoryBlock {
/**

View File

@ -2,6 +2,8 @@ package me.mrCookieSlime.Slimefun.Objects.handlers;
import java.util.Optional;
import javax.annotation.Nonnull;
import io.github.thebusybiscuit.slimefun4.api.exceptions.IncompatibleItemHandlerException;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler;
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}
* should there be an issue
*/
default Optional<IncompatibleItemHandlerException> validate(SlimefunItem item) {
@Nonnull
default Optional<IncompatibleItemHandlerException> validate(@Nonnull SlimefunItem item) {
return Optional.empty();
}
@ -61,5 +64,6 @@ public interface ItemHandler {
*
* @return The {@link Class} identifier for this {@link ItemHandler}
*/
@Nonnull
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
*/
@Nullable
public static ItemStack retrieve(Block block) {
if (!hasBlockInfo(block)) {
return null;
} else {
String id = getLocationInfo(block.getLocation(), "id");
SlimefunItem item = SlimefunItem.getByID(id);
clearBlockInfo(block);
public static ItemStack retrieve(@Nonnull Block block) {
SlimefunItem item = check(block);
if (item == null) {
return null;
} else {
clearBlockInfo(block);
return item.getItem();
}
}
}
@Nonnull
public static Config getLocationInfo(Location l) {
@ -705,6 +700,11 @@ public class BlockStorage {
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
public static String checkID(@Nonnull Block b) {
// Only access the BlockState when on the main thread
@ -719,16 +719,8 @@ public class BlockStorage {
return checkID(b.getLocation());
}
public static boolean check(Block block, String slimefunItem) {
String id = checkID(block);
return id != null && id.equals(slimefunItem);
}
public static String checkID(Location l) {
if (!hasBlockInfo(l)) {
return null;
}
@Nullable
public static String checkID(@Nonnull Location l) {
return getLocationInfo(l, "id");
}
@ -741,7 +733,7 @@ public class BlockStorage {
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());
}