From f60896f620e3d367df845bdebf8e1ef68d383746 Mon Sep 17 00:00:00 2001 From: iTwins Date: Sun, 20 Aug 2023 02:37:24 +0200 Subject: [PATCH] fix geo miner voiding resources --- .../core/machines/MachineOperation.java | 6 +++ .../core/machines/MachineProcessor.java | 37 +++++++-------- .../implementation/items/geo/GEOMiner.java | 46 +++++++++---------- .../operations/MiningOperation.java | 29 +++++++++--- 4 files changed, 69 insertions(+), 49 deletions(-) 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..67fda66d29 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 @@ -57,4 +57,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 cancel() {} + } 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..4ccf41192c 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 @@ -6,7 +6,8 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; -import org.apache.commons.lang.Validate; +import com.google.common.base.Preconditions; + import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.block.Block; @@ -46,7 +47,7 @@ public class MachineProcessor { * The owner of this {@link MachineProcessor}. */ public MachineProcessor(@Nonnull MachineProcessHolder owner) { - Validate.notNull(owner, "The MachineProcessHolder cannot be null."); + Preconditions.checkArgument(owner != null, "The MachineProcessHolder cannot be null."); this.owner = owner; } @@ -93,8 +94,8 @@ public void setProgressBar(@Nullable ItemStack progressBar) { * {@link MachineOperation} has already been started at that {@link Location}. */ public boolean startOperation(@Nonnull Location loc, @Nonnull T operation) { - Validate.notNull(loc, "The location must not be null"); - Validate.notNull(operation, "The operation cannot be null"); + Preconditions.checkArgument(loc != null, "The location must not be null"); + Preconditions.checkArgument(operation != null, "The operation cannot be null"); return startOperation(new BlockPosition(loc), operation); } @@ -111,8 +112,8 @@ public boolean startOperation(@Nonnull Location loc, @Nonnull T operation) { * {@link MachineOperation} has already been started at that {@link Block}. */ public boolean startOperation(@Nonnull Block b, @Nonnull T operation) { - Validate.notNull(b, "The Block must not be null"); - Validate.notNull(operation, "The machine operation cannot be null"); + Preconditions.checkArgument(b != null, "The Block must not be null"); + Preconditions.checkArgument(operation != null, "The machine operation cannot be null"); return startOperation(new BlockPosition(b), operation); } @@ -129,8 +130,8 @@ public boolean startOperation(@Nonnull Block b, @Nonnull T operation) { * {@link MachineOperation} has already been started at that {@link BlockPosition}. */ public boolean startOperation(@Nonnull BlockPosition pos, @Nonnull T operation) { - Validate.notNull(pos, "The BlockPosition must not be null"); - Validate.notNull(operation, "The machine operation cannot be null"); + Preconditions.checkArgument(pos != null, "The BlockPosition must not be null"); + Preconditions.checkArgument(operation != null, "The machine operation cannot be null"); return machines.putIfAbsent(pos, operation) == null; } @@ -144,7 +145,7 @@ public boolean startOperation(@Nonnull BlockPosition pos, @Nonnull T operation) * @return The current {@link MachineOperation} or null. */ public @Nullable T getOperation(@Nonnull Location loc) { - Validate.notNull(loc, "The location cannot be null"); + Preconditions.checkArgument(loc != null, "The location cannot be null"); return getOperation(new BlockPosition(loc)); } @@ -158,15 +159,13 @@ public boolean startOperation(@Nonnull BlockPosition pos, @Nonnull T operation) * @return The current {@link MachineOperation} or null. */ public @Nullable T getOperation(@Nonnull Block b) { - Validate.notNull(b, "The Block cannot be null"); + Preconditions.checkArgument(b != null, "The Block cannot be null"); return getOperation(new BlockPosition(b)); } /** * 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. @@ -174,7 +173,7 @@ public boolean startOperation(@Nonnull BlockPosition pos, @Nonnull T operation) * @return The current {@link MachineOperation} or null. */ public @Nullable T getOperation(@Nonnull BlockPosition pos) { - Validate.notNull(pos, "The BlockPosition must not be null"); + Preconditions.checkArgument(pos != null, "The BlockPosition must not be null"); return machines.get(pos); } @@ -189,7 +188,7 @@ public boolean startOperation(@Nonnull BlockPosition pos, @Nonnull T operation) * {@link MachineOperation} to begin with. */ public boolean endOperation(@Nonnull Location loc) { - Validate.notNull(loc, "The location should not be null"); + Preconditions.checkArgument(loc != null, "The location should not be null"); return endOperation(new BlockPosition(loc)); } @@ -204,7 +203,7 @@ public boolean endOperation(@Nonnull Location loc) { * {@link MachineOperation} to begin with. */ public boolean endOperation(@Nonnull Block b) { - Validate.notNull(b, "The Block should not be null"); + Preconditions.checkArgument(b != null, "The Block should not be null"); return endOperation(new BlockPosition(b)); } @@ -219,7 +218,7 @@ public boolean endOperation(@Nonnull Block b) { * {@link MachineOperation} to begin with. */ public boolean endOperation(@Nonnull BlockPosition pos) { - Validate.notNull(pos, "The BlockPosition cannot be null"); + Preconditions.checkArgument(pos != null, "The BlockPosition cannot be null"); T operation = machines.remove(pos); @@ -231,6 +230,8 @@ public boolean endOperation(@Nonnull BlockPosition pos) { if (operation.isFinished()) { Event event = new AsyncMachineOperationFinishEvent(pos, this, operation); Bukkit.getPluginManager().callEvent(event); + } else { + operation.cancel(); } return true; @@ -240,8 +241,8 @@ public boolean endOperation(@Nonnull BlockPosition pos) { } public void updateProgressBar(@Nonnull BlockMenu inv, int slot, @Nonnull T operation) { - Validate.notNull(inv, "The inventory must not be null."); - Validate.notNull(operation, "The MachineOperation must not be null."); + Preconditions.checkArgument(inv != null, "The inventory must not be null."); + Preconditions.checkArgument(operation != null, "The MachineOperation must not be null."); if (getProgressBar() == null) { // No progress bar, no need to update anything. 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..184215a2a8 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 @@ -7,7 +7,8 @@ import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; -import org.apache.commons.lang.Validate; +import com.google.common.base.Preconditions; + import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.block.Block; @@ -77,7 +78,7 @@ public GEOMiner(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeTy } @Override - public MachineProcessor getMachineProcessor() { + public @Nonnull MachineProcessor getMachineProcessor() { return processor; } @@ -121,8 +122,8 @@ public int getSpeed() { * * @return This method will return the current instance of {@link GEOMiner}, so that can be chained. */ - public final GEOMiner setCapacity(int capacity) { - Validate.isTrue(capacity > 0, "The capacity must be greater than zero!"); + public final @Nonnull GEOMiner setCapacity(int capacity) { + Preconditions.checkArgument(capacity > 0, "The capacity must be greater than zero!"); if (getState() == ItemState.UNREGISTERED) { this.energyCapacity = capacity; @@ -140,8 +141,8 @@ public final GEOMiner setCapacity(int capacity) { * * @return This method will return the current instance of {@link GEOMiner}, so that can be chained. */ - public final GEOMiner setProcessingSpeed(int speed) { - Validate.isTrue(speed > 0, "The speed must be greater than zero!"); + public final @Nonnull GEOMiner setProcessingSpeed(int speed) { + Preconditions.checkArgument(speed > 0, "The speed must be greater than zero!"); this.processingSpeed = speed; return this; @@ -155,10 +156,10 @@ public final GEOMiner setProcessingSpeed(int speed) { * * @return This method will return the current instance of {@link GEOMiner}, so that can be chained. */ - public final GEOMiner setEnergyConsumption(int energyConsumption) { - Validate.isTrue(energyConsumption > 0, "The energy consumption must be greater than zero!"); - Validate.isTrue(energyCapacity > 0, "You must specify the capacity before you can set the consumption amount."); - Validate.isTrue(energyConsumption <= energyCapacity, "The energy consumption cannot be higher than the capacity (" + energyCapacity + ')'); + public final @Nonnull GEOMiner setEnergyConsumption(int energyConsumption) { + Preconditions.checkArgument(energyConsumption > 0, "The energy consumption must be greater than zero!"); + Preconditions.checkArgument(energyCapacity > 0, "You must specify the capacity before you can set the consumption amount."); + Preconditions.checkArgument(energyConsumption <= energyCapacity, "The energy consumption cannot be higher than the capacity (" + energyCapacity + ')'); this.energyConsumedPerTick = energyConsumption; return this; @@ -188,19 +189,17 @@ public void register(@Nonnull SlimefunAddon addon) { } } - @Nonnull - private BlockPlaceHandler onBlockPlace() { + private @Nonnull BlockPlaceHandler onBlockPlace() { return new BlockPlaceHandler(false) { @Override - public void onPlayerPlace(BlockPlaceEvent e) { + public void onPlayerPlace(@Nonnull BlockPlaceEvent e) { updateHologram(e.getBlock(), "&7Idling..."); } }; } - @Nonnull - private BlockBreakHandler onBlockBreak() { + private @Nonnull BlockBreakHandler onBlockBreak() { return new SimpleBlockBreakHandler() { @Override @@ -217,21 +216,18 @@ public void onBlockBreak(@Nonnull Block b) { }; } - @Nonnull @Override - public int[] getInputSlots() { + public @Nonnull int[] getInputSlots() { return new int[0]; } - @Nonnull @Override - public int[] getOutputSlots() { + public @Nonnull int[] getOutputSlots() { return OUTPUT_SLOTS; } - @Nonnull @Override - public List getDisplayRecipes() { + public @Nonnull List getDisplayRecipes() { List displayRecipes = new LinkedList<>(); for (GEOResource resource : Slimefun.getRegistry().getGEOResources().values()) { @@ -249,7 +245,7 @@ public List getDisplayRecipes() { } @Override - public EnergyNetComponentType getEnergyComponentType() { + public @Nonnull EnergyNetComponentType getEnergyComponentType() { return EnergyNetComponentType.CONSUMER; } @@ -274,7 +270,7 @@ public boolean onClick(Player p, int slot, ItemStack cursor, ClickAction action) @Override public boolean onClick(InventoryClickEvent e, Player p, int slot, ItemStack cursor, ClickAction action) { - return cursor == null || cursor.getType() == null || cursor.getType() == Material.AIR; + return cursor == null || cursor.getType() == Material.AIR; } }); } @@ -328,7 +324,7 @@ private void start(@Nonnull Block b, @Nonnull BlockMenu inv) { if (resource.isObtainableFromGEOMiner()) { OptionalInt optional = Slimefun.getGPSNetwork().getResourceManager().getSupplies(resource, b.getWorld(), b.getX() >> 4, b.getZ() >> 4); - if (!optional.isPresent()) { + if (optional.isEmpty()) { updateHologram(b, "&4GEO-Scan required!"); return; } @@ -339,7 +335,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 MiningOperation(resource, b, 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/MiningOperation.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/operations/MiningOperation.java index 2a1201855f..a7dd33395c 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 @@ -1,12 +1,18 @@ package io.github.thebusybiscuit.slimefun4.implementation.operations; +import java.util.OptionalInt; + import javax.annotation.Nonnull; -import org.apache.commons.lang.Validate; +import com.google.common.base.Preconditions; + +import org.bukkit.block.Block; import org.bukkit.inventory.ItemStack; 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; /** @@ -22,20 +28,24 @@ public class MiningOperation implements MachineOperation { private final ItemStack result; + private final GEOResource resource; + private final Block block; private final int totalTicks; private int currentTicks = 0; - public MiningOperation(@Nonnull ItemStack result, int totalTicks) { - Validate.notNull(result, "The result cannot be null"); - Validate.isTrue(totalTicks >= 0, "The amount of total ticks must be a positive integer or zero, received: " + totalTicks); + public MiningOperation(@Nonnull GEOResource resource, Block block, int totalTicks) { + Preconditions.checkArgument(resource != null, "The resource cannot be null"); + Preconditions.checkArgument(totalTicks >= 0, "The amount of total ticks must be a positive integer or zero, received: " + totalTicks); - this.result = result; + this.resource = resource; + this.result = resource.getItem().clone(); + this.block = block; this.totalTicks = totalTicks; } @Override public void addProgress(int num) { - Validate.isTrue(num > 0, "Progress must be positive."); + Preconditions.checkArgument(num > 0, "Progress must be positive."); currentTicks += num; } @@ -59,4 +69,11 @@ public int getTotalTicks() { return totalTicks; } + @Override + public void cancel() { + ResourceManager resourceManager = Slimefun.getGPSNetwork().getResourceManager(); + OptionalInt supplies = resourceManager.getSupplies(resource, block.getWorld(), block.getX() >> 4, block.getZ() >> 4); + supplies.ifPresent(s -> resourceManager.setSupplies(resource, block.getWorld(), block.getX() >> 4, block.getZ() >> 4, s + 1)); + } + }