1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

Implemented Event.

This commit is contained in:
TheBusyBiscuit 2021-04-27 18:02:53 +02:00
parent 7bd588175b
commit a02d809a72
5 changed files with 29 additions and 41 deletions

View File

@ -4,10 +4,10 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import io.github.thebusybiscuit.cscorelib2.blocks.BlockPosition;
import io.github.thebusybiscuit.slimefun4.core.machines.MachineOperation;
import io.github.thebusybiscuit.slimefun4.core.machines.MachineProcessor;
@ -22,26 +22,26 @@ public class AsyncMachineOperationFinishEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private final Location location;
private final BlockPosition position;
private final MachineProcessor<?> machineProcessor;
private final MachineOperation machineOperation;
public <T extends MachineOperation> AsyncMachineOperationFinishEvent(Location l, MachineProcessor<T> processor, T operation) {
public <T extends MachineOperation> AsyncMachineOperationFinishEvent(BlockPosition pos, MachineProcessor<T> processor, T operation) {
super(!Bukkit.isPrimaryThread());
this.location = l;
this.position = pos;
this.machineProcessor = processor;
this.machineOperation = operation;
}
/**
* This returns the {@link Location} of the machine.
* This returns the {@link BlockPosition} of the machine.
*
* @return The {@link Location} of the machine
* @return The {@link BlockPosition} of the machine
*/
@Nonnull
public Location getLocation() {
return location;
public BlockPosition getPosition() {
return position;
}
/**

View File

@ -9,11 +9,14 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.cscorelib2.blocks.BlockPosition;
import io.github.thebusybiscuit.slimefun4.api.events.AsyncMachineOperationFinishEvent;
import io.github.thebusybiscuit.slimefun4.core.attributes.MachineProcessHolder;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
@ -46,6 +49,7 @@ public class MachineProcessor<T extends MachineOperation> {
*/
public MachineProcessor(@Nonnull MachineProcessHolder<T> owner) {
Validate.notNull(owner, "The MachineProcessHolder cannot be null.");
this.owner = owner;
}
@ -236,7 +240,22 @@ public class MachineProcessor<T extends MachineOperation> {
lock.writeLock().lock();
try {
return machines.remove(pos) != null;
T operation = machines.remove(pos);
if (operation != null) {
/*
* Only call an event if the operation actually finished.
* If it was ended prematurely (aka aborted), then we don't call any event.
*/
if (operation.isFinished()) {
Event event = new AsyncMachineOperationFinishEvent(pos, this, operation);
Bukkit.getPluginManager().callEvent(event);
}
return true;
} else {
return false;
}
} finally {
lock.writeLock().unlock();
}

View File

@ -277,14 +277,6 @@ public abstract class Reactor extends AbstractEnergyProvider implements Hologram
return new int[] { 40 };
}
// public MachineFuel getProcessing(Location l) {
// return processing.get(l);
// }
//
// public boolean isProcessing(Location l) {
// return progress.containsKey(l);
// }
@Override
public int getGeneratedOutput(Location l, Config data) {
BlockMenu inv = BlockStorage.getInventory(l);
@ -386,8 +378,6 @@ public abstract class Reactor extends AbstractEnergyProvider implements Hologram
}
}
// Bukkit.getPluginManager().callEvent(new AsyncReactorProcessCompleteEvent(l, Reactor.this, getProcessing(l)));
processor.endOperation(l);
}

View File

@ -316,14 +316,6 @@ public abstract class AContainer extends SlimefunItem implements InventoryBlock,
return EnergyNetComponentType.CONSUMER;
}
// public MachineRecipe getProcessing(Block b) {
// return processing.get(b);
// }
//
// public boolean isProcessing(Block b) {
// return getProcessing(b) != null;
// }
public void registerRecipe(MachineRecipe recipe) {
recipe.setTicks(recipe.getTicks() / getSpeed());
recipes.add(recipe);
@ -370,8 +362,6 @@ public abstract class AContainer extends SlimefunItem implements InventoryBlock,
inv.pushItem(output.clone(), getOutputSlots());
}
// Bukkit.getPluginManager().callEvent(new AsyncMachineProcessCompleteEvent(b.getLocation(),
// AContainer.this, getProcessing(b)));
processor.endOperation(b);
}
}

View File

@ -83,7 +83,7 @@ public abstract class AGenerator extends AbstractEnergyProvider implements Machi
addItemHandler(onBlockBreak());
registerDefaultFuelTypes();
}
@Override
public MachineProcessor<FuelOperation> getMachineProcessor() {
return processor;
@ -148,14 +148,6 @@ public abstract class AGenerator extends AbstractEnergyProvider implements Machi
return new int[] { 24, 25 };
}
// public MachineFuel getProcessing(Location l) {
// return processing.get(l);
// }
//
// public boolean isProcessing(Location l) {
// return progress.containsKey(l);
// }
@Override
public int getGeneratedOutput(Location l, Config data) {
BlockMenu inv = BlockStorage.getInventory(l);
@ -187,9 +179,6 @@ public abstract class AGenerator extends AbstractEnergyProvider implements Machi
inv.replaceExistingItem(22, new CustomItem(Material.BLACK_STAINED_GLASS_PANE, " "));
// Bukkit.getPluginManager().callEvent(new AsyncGeneratorProcessCompleteEvent(l, AGenerator.this,
// getProcessing(l)));
processor.endOperation(l);
return 0;
}