Skip to content

Commit

Permalink
Merge pull request #3953 from iTwins/fix/geo_miner_voiding_resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Sfiguz7 authored Dec 6, 2023
2 parents b646e15 + c7506d8 commit 243293a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand Down Expand Up @@ -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) {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -53,15 +53,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 +77,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 @@ -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()) {
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 243293a

Please sign in to comment.