Skip to content

Commit

Permalink
don't break MiningOperation, instead create new class
Browse files Browse the repository at this point in the history
  • Loading branch information
iTwins committed Aug 20, 2023
1 parent f60896f commit 65819d1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,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 @@ -54,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 @@ -78,7 +78,7 @@ public GEOMiner(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeTy
}

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

Expand Down Expand Up @@ -294,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 @@ -335,7 +335,7 @@ private void start(@Nonnull Block b, @Nonnull BlockMenu inv) {
return;
}

processor.startOperation(b, new MiningOperation(resource, b, 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
*
*/
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 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));
}

}
Original file line number Diff line number Diff line change
@@ -1,45 +1,32 @@
package io.github.thebusybiscuit.slimefun4.implementation.operations;

import java.util.OptionalInt;

import javax.annotation.Nonnull;

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;

/**
* 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 {

private final ItemStack result;

private final GEOResource resource;
private final Block block;
private final int totalTicks;
private int currentTicks = 0;

public MiningOperation(@Nonnull GEOResource resource, Block block, int totalTicks) {
Preconditions.checkArgument(resource != null, "The resource cannot be null");
public MiningOperation(@Nonnull ItemStack result, int totalTicks) {
Preconditions.checkArgument(result != null, "The result cannot be null");
Preconditions.checkArgument(totalTicks >= 0, "The amount of total ticks must be a positive integer or zero, received: " + totalTicks);

this.resource = resource;
this.result = resource.getItem().clone();
this.block = block;
this.result = result;
this.totalTicks = totalTicks;
}

Expand Down Expand Up @@ -69,11 +56,4 @@ 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));
}

}

0 comments on commit 65819d1

Please sign in to comment.