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 3 commits
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 onCancel() {}

}
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.onCancel();
}

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 All @@ -34,7 +35,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;
Expand All @@ -53,15 +54,15 @@
*
* @see GEOResource
*/
public class GEOMiner extends SlimefunItem implements RecipeDisplayItem, EnergyNetComponent, InventoryBlock, HologramOwner, MachineProcessHolder<MiningOperation> {
public class GEOMiner extends SlimefunItem implements RecipeDisplayItem, EnergyNetComponent, InventoryBlock, HologramOwner, MachineProcessHolder<GEOMiningOperation> {

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 };
private static final int[] OUTPUT_SLOTS = { 29, 30, 31, 32, 33, 38, 39, 40, 41, 42 };

private static final int PROCESSING_TIME = 14;

private final MachineProcessor<MiningOperation> processor = new MachineProcessor<>(this);
private final MachineProcessor<GEOMiningOperation> processor = new MachineProcessor<>(this);

private int energyConsumedPerTick = -1;
private int energyCapacity = -1;
Expand All @@ -77,7 +78,7 @@ public GEOMiner(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeTy
}

@Override
public MachineProcessor<MiningOperation> getMachineProcessor() {
public @Nonnull MachineProcessor<GEOMiningOperation> 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 All @@ -298,7 +294,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()) {
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 GEOMiningOperation(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
@@ -0,0 +1,46 @@
package io.github.thebusybiscuit.slimefun4.implementation.operations;

import java.util.OptionalInt;

import javax.annotation.Nonnull;

import org.bukkit.block.Block;

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
*
iTwins marked this conversation as resolved.
Show resolved Hide resolved
*/
public class GEOMiningOperation extends MiningOperation {

private final GEOResource resource;
private final Block block;

public GEOMiningOperation(@Nonnull GEOResource resource, @Nonnull Block block, int totalTicks) {
super(resource.getItem().clone(), totalTicks);
this.resource = resource;
this.block = block;
}

/**
* This returns the {@link GEOResource} back to the chunk
* when the {@link GEOMiningOperation} gets cancelled
*/
@Override
public void onCancel() {
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));
}

}
Loading