diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/machines/MachineOperation.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/machines/MachineOperation.java index 6ee741882a..3da6569b6c 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/machines/MachineOperation.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/machines/MachineOperation.java @@ -1,5 +1,6 @@ package io.github.thebusybiscuit.slimefun4.core.machines; +import io.github.bakedlibs.dough.blocks.BlockPosition; import io.github.thebusybiscuit.slimefun4.core.attributes.MachineProcessHolder; /** @@ -57,4 +58,10 @@ default boolean isFinished() { return getRemainingTicks() <= 0; } + /** + * This method is called when a {@link MachineOperation} is interrupted before finishing. + * Implement to specify behaviour that should happen in this case. + */ + default void onCancel(BlockPosition position) {} + } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/machines/MachineProcessor.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/machines/MachineProcessor.java index 33ccb4111d..9ec92b578a 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/machines/MachineProcessor.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/machines/MachineProcessor.java @@ -165,8 +165,6 @@ public boolean startOperation(@Nonnull BlockPosition pos, @Nonnull T operation) /** * This returns the current {@link MachineOperation} at that given {@link BlockPosition}. - * We don't need to validate our input here as that is already - * covered in our public methods. * * @param pos * The {@link BlockPosition} at which our machine is located. @@ -231,6 +229,8 @@ public boolean endOperation(@Nonnull BlockPosition pos) { if (operation.isFinished()) { Event event = new AsyncMachineOperationFinishEvent(pos, this, operation); Bukkit.getPluginManager().callEvent(event); + } else { + operation.onCancel(pos); } return true; diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/geo/GEOMiner.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/geo/GEOMiner.java index e9b5d104ca..cc09eb0ac6 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/geo/GEOMiner.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/geo/GEOMiner.java @@ -34,7 +34,7 @@ import io.github.thebusybiscuit.slimefun4.core.networks.energy.EnergyNetComponentType; import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; import io.github.thebusybiscuit.slimefun4.implementation.handlers.SimpleBlockBreakHandler; -import io.github.thebusybiscuit.slimefun4.implementation.operations.MiningOperation; +import io.github.thebusybiscuit.slimefun4.implementation.operations.GEOMiningOperation; import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config; @@ -53,7 +53,7 @@ * * @see GEOResource */ -public class GEOMiner extends SlimefunItem implements RecipeDisplayItem, EnergyNetComponent, InventoryBlock, HologramOwner, MachineProcessHolder { +public class GEOMiner extends SlimefunItem implements RecipeDisplayItem, EnergyNetComponent, InventoryBlock, HologramOwner, MachineProcessHolder { private static final int[] BORDER = { 0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 26, 27, 35, 36, 44, 45, 53 }; private static final int[] OUTPUT_BORDER = { 19, 20, 21, 22, 23, 24, 25, 28, 34, 37, 43, 46, 47, 48, 49, 50, 51, 52 }; @@ -61,7 +61,7 @@ public class GEOMiner extends SlimefunItem implements RecipeDisplayItem, EnergyN private static final int PROCESSING_TIME = 14; - private final MachineProcessor processor = new MachineProcessor<>(this); + private final MachineProcessor processor = new MachineProcessor<>(this); private int energyConsumedPerTick = -1; private int energyCapacity = -1; @@ -77,7 +77,7 @@ public GEOMiner(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeTy } @Override - public MachineProcessor getMachineProcessor() { + public @Nonnull MachineProcessor getMachineProcessor() { return processor; } @@ -298,7 +298,7 @@ public boolean isSynchronized() { protected void tick(@Nonnull Block b) { BlockMenu inv = BlockStorage.getInventory(b); - MiningOperation operation = processor.getOperation(b); + GEOMiningOperation operation = processor.getOperation(b); if (operation != null) { if (!operation.isFinished()) { @@ -339,7 +339,7 @@ private void start(@Nonnull Block b, @Nonnull BlockMenu inv) { return; } - processor.startOperation(b, new MiningOperation(resource.getItem().clone(), PROCESSING_TIME)); + processor.startOperation(b, new GEOMiningOperation(resource, PROCESSING_TIME)); Slimefun.getGPSNetwork().getResourceManager().setSupplies(resource, b.getWorld(), b.getX() >> 4, b.getZ() >> 4, supplies - 1); updateHologram(b, "&7Mining: &r" + resource.getName()); return; diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/operations/GEOMiningOperation.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/operations/GEOMiningOperation.java new file mode 100644 index 0000000000..372f9f502c --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/operations/GEOMiningOperation.java @@ -0,0 +1,42 @@ +package io.github.thebusybiscuit.slimefun4.implementation.operations; + +import java.util.OptionalInt; + +import javax.annotation.Nonnull; + +import io.github.bakedlibs.dough.blocks.BlockPosition; +import io.github.thebusybiscuit.slimefun4.api.geo.GEOResource; +import io.github.thebusybiscuit.slimefun4.api.geo.ResourceManager; +import io.github.thebusybiscuit.slimefun4.core.machines.MachineOperation; +import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; +import io.github.thebusybiscuit.slimefun4.implementation.items.geo.GEOMiner; + +/** + * This {@link MachineOperation} represents a {@link GEOMiner} + * mining a {@link GEOResource}. + * + * @author iTwins + * + * @see GEOMiner + */ +public class GEOMiningOperation extends MiningOperation { + + private final GEOResource resource; + + public GEOMiningOperation(@Nonnull GEOResource resource, int totalTicks) { + super(resource.getItem().clone(), totalTicks); + this.resource = resource; + } + + /** + * This returns the {@link GEOResource} back to the chunk + * when the {@link GEOMiningOperation} gets cancelled + */ + @Override + public void onCancel(@Nonnull BlockPosition position) { + ResourceManager resourceManager = Slimefun.getGPSNetwork().getResourceManager(); + OptionalInt supplies = resourceManager.getSupplies(resource, position.getWorld(), position.getChunkX(), position.getChunkZ()); + supplies.ifPresent(s -> resourceManager.setSupplies(resource, position.getWorld(), position.getChunkX(), position.getChunkZ(), s + 1)); + } + +} diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/operations/MiningOperation.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/operations/MiningOperation.java index 2a1201855f..e7f94c98bf 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/operations/MiningOperation.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/operations/MiningOperation.java @@ -5,17 +5,13 @@ import org.apache.commons.lang.Validate; import org.bukkit.inventory.ItemStack; -import io.github.thebusybiscuit.slimefun4.api.geo.GEOResource; import io.github.thebusybiscuit.slimefun4.core.machines.MachineOperation; -import io.github.thebusybiscuit.slimefun4.implementation.items.geo.GEOMiner; /** - * This {@link MachineOperation} represents a {@link GEOMiner} - * mining a {@link GEOResource}. + * This {@link MachineOperation} represents an operation + * with no inputs, only a result. * * @author TheBusyBiscuit - * - * @see GEOMiner * */ public class MiningOperation implements MachineOperation {