From 508b5aa198dd5068b8f9ddf43d8d12242abed31e Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Fri, 30 Oct 2020 19:37:39 +0100 Subject: [PATCH] Refactored AGenerator --- .../electric/generators/BioGenerator.java | 2 +- .../electric/generators/CoalGenerator.java | 2 +- .../generators/CombustionGenerator.java | 2 +- .../electric/generators/LavaGenerator.java | 2 +- .../generators/MagnesiumGenerator.java | 2 +- .../setup/SlimefunItemSetup.java | 119 +++++------------- .../abstractItems/AGenerator.java | 83 +++++++++++- 7 files changed, 115 insertions(+), 97 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/BioGenerator.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/BioGenerator.java index e1c624ede..ef165e5ba 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/BioGenerator.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/BioGenerator.java @@ -13,7 +13,7 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.AGenerator; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.MachineFuel; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; -public abstract class BioGenerator extends AGenerator { +public class BioGenerator extends AGenerator { public BioGenerator(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { super(category, item, recipeType, recipe); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/CoalGenerator.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/CoalGenerator.java index 886164817..a375dc2b0 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/CoalGenerator.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/CoalGenerator.java @@ -12,7 +12,7 @@ import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; import javax.annotation.Nonnull; -public abstract class CoalGenerator extends AGenerator { +public class CoalGenerator extends AGenerator { public CoalGenerator(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { super(category, item, recipeType, recipe); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/CombustionGenerator.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/CombustionGenerator.java index 2c7680b23..2f014413d 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/CombustionGenerator.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/CombustionGenerator.java @@ -10,7 +10,7 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.AGenerator; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.MachineFuel; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; -public abstract class CombustionGenerator extends AGenerator { +public class CombustionGenerator extends AGenerator { public CombustionGenerator(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { super(category, item, recipeType, recipe); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/LavaGenerator.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/LavaGenerator.java index 096e92c91..eee08c34a 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/LavaGenerator.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/LavaGenerator.java @@ -9,7 +9,7 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.AGenerator; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.MachineFuel; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; -public abstract class LavaGenerator extends AGenerator { +public class LavaGenerator extends AGenerator { public LavaGenerator(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { super(category, item, recipeType, recipe); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/MagnesiumGenerator.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/MagnesiumGenerator.java index ad4974988..e298926b0 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/MagnesiumGenerator.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/MagnesiumGenerator.java @@ -10,7 +10,7 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.AGenerator; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.MachineFuel; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; -public abstract class MagnesiumGenerator extends AGenerator { +public class MagnesiumGenerator extends AGenerator { public MagnesiumGenerator(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { super(category, item, recipeType, recipe); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/SlimefunItemSetup.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/SlimefunItemSetup.java index a81f359e3..d741cb76b 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/SlimefunItemSetup.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/SlimefunItemSetup.java @@ -1680,49 +1680,22 @@ public final class SlimefunItemSetup { .register(plugin); new CoalGenerator(categories.electricity, SlimefunItems.COAL_GENERATOR, RecipeType.ENHANCED_CRAFTING_TABLE, - new ItemStack[] {SlimefunItems.HEATING_COIL, new ItemStack(Material.FURNACE), SlimefunItems.HEATING_COIL, SlimefunItems.NICKEL_INGOT, SlimefunItems.ELECTRIC_MOTOR, SlimefunItems.NICKEL_INGOT, null, SlimefunItems.NICKEL_INGOT, null}) { - - @Override - public int getEnergyProduction() { - return 8; - } - - @Override - public int getCapacity() { - return 64; - } - - }.register(plugin); + new ItemStack[] {SlimefunItems.HEATING_COIL, new ItemStack(Material.FURNACE), SlimefunItems.HEATING_COIL, SlimefunItems.NICKEL_INGOT, SlimefunItems.ELECTRIC_MOTOR, SlimefunItems.NICKEL_INGOT, null, SlimefunItems.NICKEL_INGOT, null}) + .setCapacity(64) + .setEnergyConsumption(8) + .register(plugin); new CoalGenerator(categories.electricity, SlimefunItems.COAL_GENERATOR_2, RecipeType.ENHANCED_CRAFTING_TABLE, - new ItemStack[] {new ItemStack(Material.MAGMA_BLOCK), SlimefunItems.HEATING_COIL, new ItemStack(Material.MAGMA_BLOCK), SlimefunItems.HARDENED_METAL_INGOT, SlimefunItems.COAL_GENERATOR, SlimefunItems.HARDENED_METAL_INGOT, null, SlimefunItems.ELECTRIC_MOTOR, null}) { - - @Override - public int getEnergyProduction() { - return 15; - } - - @Override - public int getCapacity() { - return 256; - } - - }.register(plugin); + new ItemStack[] {new ItemStack(Material.MAGMA_BLOCK), SlimefunItems.HEATING_COIL, new ItemStack(Material.MAGMA_BLOCK), SlimefunItems.HARDENED_METAL_INGOT, SlimefunItems.COAL_GENERATOR, SlimefunItems.HARDENED_METAL_INGOT, null, SlimefunItems.ELECTRIC_MOTOR, null}) + .setCapacity(256) + .setEnergyConsumption(15) + .register(plugin); new BioGenerator(categories.electricity, SlimefunItems.BIO_REACTOR, RecipeType.ENHANCED_CRAFTING_TABLE, - new ItemStack[] {SlimefunItems.HEATING_COIL, SlimefunItems.COMPOSTER, SlimefunItems.HEATING_COIL, SlimefunItems.ALUMINUM_BRASS_INGOT, SlimefunItems.ELECTRIC_MOTOR, SlimefunItems.ALUMINUM_BRASS_INGOT, null, SlimefunItems.ALUMINUM_BRASS_INGOT, null}) { - - @Override - public int getEnergyProduction() { - return 4; - } - - @Override - public int getCapacity() { - return 128; - } - - }.register(plugin); + new ItemStack[] {SlimefunItems.HEATING_COIL, SlimefunItems.COMPOSTER, SlimefunItems.HEATING_COIL, SlimefunItems.ALUMINUM_BRASS_INGOT, SlimefunItems.ELECTRIC_MOTOR, SlimefunItems.ALUMINUM_BRASS_INGOT, null, SlimefunItems.ALUMINUM_BRASS_INGOT, null}) + .setCapacity(128) + .setEnergyConsumption(4) + .register(plugin); new AutoDrier(categories.electricity, SlimefunItems.AUTO_DRIER, RecipeType.ENHANCED_CRAFTING_TABLE, new ItemStack[]{null, null, null, SlimefunItems.HEATING_COIL, new ItemStack(Material.SMOKER), SlimefunItems.HEATING_COIL, null, new ItemStack(Material.CAMPFIRE), null}) @@ -1757,19 +1730,10 @@ public final class SlimefunItemSetup { .register(plugin); new MagnesiumGenerator(categories.electricity, SlimefunItems.MAGNESIUM_GENERATOR, RecipeType.ENHANCED_CRAFTING_TABLE, - new ItemStack[] {null, SlimefunItems.ELECTRIC_MOTOR, null, SlimefunItems.COMPRESSED_CARBON, new ItemStack(Material.WATER_BUCKET), SlimefunItems.COMPRESSED_CARBON, SlimefunItems.DURALUMIN_INGOT, SlimefunItems.DURALUMIN_INGOT, SlimefunItems.DURALUMIN_INGOT}) { - - @Override - public int getEnergyProduction() { - return 18; - } - - @Override - public int getCapacity() { - return 128; - } - - }.register(plugin); + new ItemStack[] {null, SlimefunItems.ELECTRIC_MOTOR, null, SlimefunItems.COMPRESSED_CARBON, new ItemStack(Material.WATER_BUCKET), SlimefunItems.COMPRESSED_CARBON, SlimefunItems.DURALUMIN_INGOT, SlimefunItems.DURALUMIN_INGOT, SlimefunItems.DURALUMIN_INGOT}) + .setCapacity(128) + .setEnergyConsumption(18) + .register(plugin); new AutoEnchanter(categories.electricity, SlimefunItems.AUTO_ENCHANTER, RecipeType.ENHANCED_CRAFTING_TABLE, new ItemStack[] {null, new ItemStack(Material.ENCHANTING_TABLE), null, SlimefunItems.CARBONADO, SlimefunItems.ELECTRIC_MOTOR, SlimefunItems.CARBONADO, SlimefunItems.WITHER_PROOF_OBSIDIAN, SlimefunItems.WITHER_PROOF_OBSIDIAN, SlimefunItems.WITHER_PROOF_OBSIDIAN}) @@ -2188,49 +2152,22 @@ public final class SlimefunItemSetup { .register(plugin); new LavaGenerator(categories.electricity, SlimefunItems.LAVA_GENERATOR, RecipeType.ENHANCED_CRAFTING_TABLE, - new ItemStack[] {null, SlimefunItems.GOLD_16K, null, SlimefunItems.DAMASCUS_STEEL_INGOT, SlimefunItems.ELECTRIC_MOTOR, SlimefunItems.DAMASCUS_STEEL_INGOT, SlimefunItems.HEATING_COIL, SlimefunItems.DAMASCUS_STEEL_INGOT, SlimefunItems.HEATING_COIL}) { - - @Override - public int getEnergyProduction() { - return 10; - } - - @Override - public int getCapacity() { - return 512; - } - - }.register(plugin); + new ItemStack[] {null, SlimefunItems.GOLD_16K, null, SlimefunItems.DAMASCUS_STEEL_INGOT, SlimefunItems.ELECTRIC_MOTOR, SlimefunItems.DAMASCUS_STEEL_INGOT, SlimefunItems.HEATING_COIL, SlimefunItems.DAMASCUS_STEEL_INGOT, SlimefunItems.HEATING_COIL}) + .setCapacity(512) + .setEnergyConsumption(10) + .register(plugin); new LavaGenerator(categories.electricity, SlimefunItems.LAVA_GENERATOR_2, RecipeType.ENHANCED_CRAFTING_TABLE, - new ItemStack[] {SlimefunItems.CORINTHIAN_BRONZE_INGOT, SlimefunItems.CORINTHIAN_BRONZE_INGOT, SlimefunItems.CORINTHIAN_BRONZE_INGOT, SlimefunItems.COMPRESSED_CARBON, SlimefunItems.LAVA_GENERATOR, SlimefunItems.COMPRESSED_CARBON, SlimefunItems.HEATING_COIL, SlimefunItems.COMPRESSED_CARBON, SlimefunItems.HEATING_COIL}) { - - @Override - public int getEnergyProduction() { - return 20; - } - - @Override - public int getCapacity() { - return 1024; - } - - }.register(plugin); + new ItemStack[] {SlimefunItems.CORINTHIAN_BRONZE_INGOT, SlimefunItems.CORINTHIAN_BRONZE_INGOT, SlimefunItems.CORINTHIAN_BRONZE_INGOT, SlimefunItems.COMPRESSED_CARBON, SlimefunItems.LAVA_GENERATOR, SlimefunItems.COMPRESSED_CARBON, SlimefunItems.HEATING_COIL, SlimefunItems.COMPRESSED_CARBON, SlimefunItems.HEATING_COIL}) + .setCapacity(1024) + .setEnergyConsumption(20) + .register(plugin); new CombustionGenerator(categories.electricity, SlimefunItems.COMBUSTION_REACTOR, RecipeType.ENHANCED_CRAFTING_TABLE, - new ItemStack[] {null, SlimefunItems.STEEL_INGOT, null, SlimefunItems.STEEL_INGOT, SlimefunItems.ELECTRIC_MOTOR, SlimefunItems.STEEL_INGOT, SlimefunItems.HEATING_COIL, SlimefunItems.STEEL_INGOT, SlimefunItems.HEATING_COIL}) { - - @Override - public int getEnergyProduction() { - return 12; - } - - @Override - public int getCapacity() { - return 256; - } - - }.register(plugin); + new ItemStack[] {null, SlimefunItems.STEEL_INGOT, null, SlimefunItems.STEEL_INGOT, SlimefunItems.ELECTRIC_MOTOR, SlimefunItems.STEEL_INGOT, SlimefunItems.HEATING_COIL, SlimefunItems.STEEL_INGOT, SlimefunItems.HEATING_COIL}) + .setCapacity(256) + .setEnergyConsumption(12) + .register(plugin); new TeleporterPylon(categories.gps, SlimefunItems.GPS_TELEPORTER_PYLON, RecipeType.ENHANCED_CRAFTING_TABLE, new ItemStack[] {SlimefunItems.ZINC_INGOT, new ItemStack(Material.GLASS), SlimefunItems.ZINC_INGOT, new ItemStack(Material.GLASS), SlimefunItems.HEATING_COIL, new ItemStack(Material.GLASS), SlimefunItems.ZINC_INGOT, new ItemStack(Material.GLASS), SlimefunItems.ZINC_INGOT}, diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AGenerator.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AGenerator.java index 43287a3d3..f650c60dd 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AGenerator.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AGenerator.java @@ -3,8 +3,11 @@ package me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems; import java.util.HashMap; import java.util.Map; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; +import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; @@ -15,7 +18,9 @@ import org.bukkit.inventory.ItemStack; import io.github.thebusybiscuit.cscorelib2.item.CustomItem; 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.implementation.SlimefunItems; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.items.electric.AbstractEnergyProvider; @@ -42,6 +47,9 @@ public abstract class AGenerator extends AbstractEnergyProvider { private static final int[] border_in = { 9, 10, 11, 12, 18, 21, 27, 28, 29, 30 }; private static final int[] border_out = { 14, 15, 16, 17, 23, 26, 32, 33, 34, 35 }; + private int energyProducedPerTick = -1; + private int energyCapacity = -1; + @ParametersAreNonnullByDefault public AGenerator(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { super(category, item, recipeType, recipe); @@ -188,7 +196,7 @@ public abstract class AGenerator extends AbstractEnergyProvider { } } - private boolean isBucket(ItemStack item) { + private boolean isBucket(@Nullable ItemStack item) { if (item == null) { return false; } @@ -210,4 +218,77 @@ public abstract class AGenerator extends AbstractEnergyProvider { return null; } + /** + * This method returns the max amount of electricity this machine can hold. + * + * @return The max amount of electricity this Block can store. + */ + public final int getCapacity() { + return energyCapacity; + } + + /** + * This method returns the amount of energy that is consumed per operation. + * + * @return The rate of energy consumption + */ + @Override + public final int getEnergyProduction() { + return energyProducedPerTick; + } + + /** + * This sets the energy capacity for this machine. + * This method must be called before registering the item + * and only before registering. + * + * @param capacity + * The amount of energy this machine can store + * + * @return This method will return the current instance of {@link AGenerator}, so that can be chained. + */ + public final AGenerator setCapacity(int capacity) { + Validate.isTrue(capacity > 0, "The capacity must be greater than zero!"); + + if (getState() == ItemState.UNREGISTERED) { + this.energyCapacity = capacity; + return this; + } else { + throw new IllegalStateException("You cannot modify the capacity after the Item was registered."); + } + } + + /** + * This method sets the energy produced by this machine per tick. + * + * @param energyProduced + * The energy produced per tick + * + * @return This method will return the current instance of {@link AGenerator}, so that can be chained. + */ + public final AGenerator setEnergyConsumption(int energyProduced) { + Validate.isTrue(energyProduced > 0, "The energy consumption must be greater than zero!"); + Validate.isTrue(energyProduced <= energyCapacity, "The energy consumption cannot be higher than the capacity (" + energyCapacity + ')'); + + this.energyProducedPerTick = energyProduced; + return this; + } + + @Override + public void register(@Nonnull SlimefunAddon addon) { + if (energyCapacity <= 0) { + warn("The capacity has not been configured correctly. The Item was disabled."); + warn("Make sure to call '" + getClass().getSimpleName() + "#setEnergyCapacity(...)' before registering!"); + } + + if (energyProducedPerTick <= 0) { + warn("The energy consumption has not been configured correctly. The Item was disabled."); + warn("Make sure to call '" + getClass().getSimpleName() + "#setEnergyProduction(...)' before registering!"); + } + + if (energyCapacity > 0 && energyProducedPerTick > 0) { + super.register(addon); + } + } + }