Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix geo miner voiding resources #3953

Merged
merged 8 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
iTwins marked this conversation as resolved.
Show resolved Hide resolved

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,7 +47,7 @@ public class MachineProcessor<T extends MachineOperation> {
* The owner of this {@link MachineProcessor}.
*/
public MachineProcessor(@Nonnull MachineProcessHolder<T> owner) {
Validate.notNull(owner, "The MachineProcessHolder cannot be null.");
Preconditions.checkArgument(owner != null, "The MachineProcessHolder cannot be null.");

this.owner = owner;
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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;
}
Expand All @@ -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));
}
Expand All @@ -158,23 +159,21 @@ 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.
*
* @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);
}
Expand All @@ -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));
}
Expand All @@ -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));
}
Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,7 +78,7 @@ public GEOMiner(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeTy
}

@Override
public MachineProcessor<MiningOperation> getMachineProcessor() {
public @Nonnull MachineProcessor<MiningOperation> getMachineProcessor() {
return processor;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -217,21 +216,18 @@ public void onBlockBreak(@Nonnull Block b) {
};
}

@Nonnull
@Override
public int[] getInputSlots() {
public @Nonnull int[] getInputSlots() {
Sefiraat marked this conversation as resolved.
Show resolved Hide resolved
return new int[0];
}

@Nonnull
@Override
public int[] getOutputSlots() {
public @Nonnull int[] getOutputSlots() {
Sefiraat marked this conversation as resolved.
Show resolved Hide resolved
return OUTPUT_SLOTS;
}

@Nonnull
@Override
public List<ItemStack> getDisplayRecipes() {
public @Nonnull List<ItemStack> getDisplayRecipes() {
List<ItemStack> displayRecipes = new LinkedList<>();

for (GEOResource resource : Slimefun.getRegistry().getGEOResources().values()) {
Expand All @@ -249,7 +245,7 @@ public List<ItemStack> getDisplayRecipes() {
}

@Override
public EnergyNetComponentType getEnergyComponentType() {
public @Nonnull EnergyNetComponentType getEnergyComponentType() {
return EnergyNetComponentType.CONSUMER;
}

Expand All @@ -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;
iTwins marked this conversation as resolved.
Show resolved Hide resolved
return cursor == null || cursor.getType() == Material.AIR;
}
});
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -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) {
iTwins marked this conversation as resolved.
Show resolved Hide resolved
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;
}

Expand All @@ -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));
}

}