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

Added a deprecation warning to the SlimefunBlockHandler

Also converted the remaining machines to the new system
This commit is contained in:
TheBusyBiscuit 2021-04-09 18:30:27 +02:00
parent 2d8ddfc675
commit dbda4d0b23
6 changed files with 56 additions and 33 deletions

View File

@ -36,6 +36,7 @@
* Added a new message when constructing a Multiblock successfully
* Added Crafting Motor
* Block Placers can now place down cake
* Added support for the "FunnyGuilds" plugin
* Added configurable enchantment level limit for both auto enchanter and auto disenchanter
#### Changes

View File

@ -53,10 +53,7 @@ public class GEOMiner extends AContainer implements RecipeDisplayItem, HologramO
public GEOMiner(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
super(category, item, recipeType, recipe);
addItemHandler(onPlace(), onBreak());
// Unregister the Block handler from AContainer (Fixes #2914)
registerBlockHandler(getId(), null);
addItemHandler(onPlace());
}
@Nonnull
@ -71,7 +68,8 @@ public class GEOMiner extends AContainer implements RecipeDisplayItem, HologramO
}
@Nonnull
private BlockBreakHandler onBreak() {
@Override
protected BlockBreakHandler onBlockBreak() {
return new SimpleBlockBreakHandler() {
@Override

View File

@ -192,6 +192,7 @@ public class BlockListener implements Listener {
e.setDropItems(false);
for (ItemStack drop : drops) {
// Prevent null or air from being dropped
if (drop != null && drop.getType() != Material.AIR) {
e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), drop);
}

View File

@ -532,6 +532,11 @@ public class SlimefunItem implements Placeable {
// Send out deprecation warnings for any classes or interfaces
checkForDeprecations(getClass());
// Inform addon developers about the BlockBreakHandler
if (SlimefunPlugin.getUpdater().getBranch() != SlimefunBranch.DEVELOPMENT && SlimefunPlugin.getRegistry().getBlockHandlers().containsKey(getId())) {
warn("This item uses a deprecated SlimefunBlockHandler which will be removed in the very near future! Please switch to the BlockBreakHandler as soon as possible.");
}
// Check for an illegal stack size
if (itemStackTemplate.getAmount() != 1) {
// @formatter:off
@ -805,14 +810,13 @@ public class SlimefunItem implements Placeable {
Validate.notEmpty(handlers, "You cannot add zero handlers...");
Validate.noNullElements(handlers, "You cannot add any 'null' ItemHandler!");
// Make sure they are added before the item was registered.
if (state != ItemState.UNREGISTERED) {
throw new UnsupportedOperationException("You cannot add an ItemHandler after the SlimefunItem was registered.");
}
for (ItemHandler handler : handlers) {
if (itemhandlers.put(handler.getIdentifier(), handler).isPresent()) {
warn("ItemHandler \"" + handler.getIdentifier().getSimpleName() + "\" has already been assigned to this item. It was overridden.");
}
itemhandlers.put(handler.getIdentifier(), handler);
// Tickers are a special case (at the moment at least)
if (handler instanceof BlockTicker) {

View File

@ -25,7 +25,9 @@ import io.github.thebusybiscuit.slimefun4.api.SlimefunAddon;
import io.github.thebusybiscuit.slimefun4.api.events.AsyncMachineProcessCompleteEvent;
import io.github.thebusybiscuit.slimefun4.api.items.ItemState;
import io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.core.networks.energy.EnergyNetComponentType;
import io.github.thebusybiscuit.slimefun4.implementation.handlers.SimpleBlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
import io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper;
@ -59,27 +61,35 @@ public abstract class AContainer extends SlimefunItem implements InventoryBlock,
private int processingSpeed = -1;
@ParametersAreNonnullByDefault
public AContainer(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
protected AContainer(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
super(category, item, recipeType, recipe);
createPreset(this, getInventoryTitle(), this::constructMenu);
registerBlockHandler(item.getItemId(), (p, b, tool, reason) -> {
BlockMenu inv = BlockStorage.getInventory(b);
addItemHandler(onBlockBreak());
}
if (inv != null) {
inv.dropItems(b.getLocation(), getInputSlots());
inv.dropItems(b.getLocation(), getOutputSlots());
@Nonnull
protected BlockBreakHandler onBlockBreak() {
return new SimpleBlockBreakHandler() {
@Override
public void onBlockBreak(Block b) {
BlockMenu inv = BlockStorage.getInventory(b);
if (inv != null) {
inv.dropItems(b.getLocation(), getInputSlots());
inv.dropItems(b.getLocation(), getOutputSlots());
}
progress.remove(b);
processing.remove(b);
}
progress.remove(b);
processing.remove(b);
return true;
});
};
}
@ParametersAreNonnullByDefault
public AContainer(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, ItemStack recipeOutput) {
protected AContainer(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, ItemStack recipeOutput) {
this(category, item, recipeType, recipe);
this.recipeOutput = recipeOutput;
}

View File

@ -21,8 +21,10 @@ import io.github.thebusybiscuit.cscorelib2.protection.ProtectableAction;
import io.github.thebusybiscuit.slimefun4.api.SlimefunAddon;
import io.github.thebusybiscuit.slimefun4.api.events.AsyncGeneratorProcessCompleteEvent;
import io.github.thebusybiscuit.slimefun4.api.items.ItemState;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
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.AbstractEnergyProvider;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
@ -76,22 +78,29 @@ public abstract class AGenerator extends AbstractEnergyProvider {
}
};
registerBlockHandler(item.getItemId(), (p, b, tool, reason) -> {
BlockMenu inv = BlockStorage.getInventory(b);
if (inv != null) {
inv.dropItems(b.getLocation(), getInputSlots());
inv.dropItems(b.getLocation(), getOutputSlots());
}
progress.remove(b.getLocation());
processing.remove(b.getLocation());
return true;
});
addItemHandler(onBlockBreak());
registerDefaultFuelTypes();
}
@Nonnull
protected BlockBreakHandler onBlockBreak() {
return new SimpleBlockBreakHandler() {
@Override
public void onBlockBreak(Block b) {
BlockMenu inv = BlockStorage.getInventory(b);
if (inv != null) {
inv.dropItems(b.getLocation(), getInputSlots());
inv.dropItems(b.getLocation(), getOutputSlots());
}
progress.remove(b.getLocation());
processing.remove(b.getLocation());
}
};
}
private void constructMenu(BlockMenuPreset preset) {
for (int i : border) {
preset.addItem(i, ChestMenuUtils.getBackground(), ChestMenuUtils.getEmptyClickHandler());