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 javax.annotation.Nullable;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; 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.MachineOperation;
import io.github.thebusybiscuit.slimefun4.core.machines.MachineProcessor; 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 static final HandlerList handlers = new HandlerList();
private final Location location; private final BlockPosition position;
private final MachineProcessor<?> machineProcessor; private final MachineProcessor<?> machineProcessor;
private final MachineOperation machineOperation; 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()); super(!Bukkit.isPrimaryThread());
this.location = l; this.position = pos;
this.machineProcessor = processor; this.machineProcessor = processor;
this.machineOperation = operation; 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 @Nonnull
public Location getLocation() { public BlockPosition getPosition() {
return location; return position;
} }
/** /**

View File

@ -9,11 +9,14 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.cscorelib2.blocks.BlockPosition; 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.core.attributes.MachineProcessHolder;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu; import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
@ -46,6 +49,7 @@ public class MachineProcessor<T extends MachineOperation> {
*/ */
public MachineProcessor(@Nonnull MachineProcessHolder<T> owner) { public MachineProcessor(@Nonnull MachineProcessHolder<T> owner) {
Validate.notNull(owner, "The MachineProcessHolder cannot be null."); Validate.notNull(owner, "The MachineProcessHolder cannot be null.");
this.owner = owner; this.owner = owner;
} }
@ -236,7 +240,22 @@ public class MachineProcessor<T extends MachineOperation> {
lock.writeLock().lock(); lock.writeLock().lock();
try { 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 { } finally {
lock.writeLock().unlock(); lock.writeLock().unlock();
} }

View File

@ -277,14 +277,6 @@ public abstract class Reactor extends AbstractEnergyProvider implements Hologram
return new int[] { 40 }; return new int[] { 40 };
} }
// public MachineFuel getProcessing(Location l) {
// return processing.get(l);
// }
//
// public boolean isProcessing(Location l) {
// return progress.containsKey(l);
// }
@Override @Override
public int getGeneratedOutput(Location l, Config data) { public int getGeneratedOutput(Location l, Config data) {
BlockMenu inv = BlockStorage.getInventory(l); 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); processor.endOperation(l);
} }

View File

@ -316,14 +316,6 @@ public abstract class AContainer extends SlimefunItem implements InventoryBlock,
return EnergyNetComponentType.CONSUMER; 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) { public void registerRecipe(MachineRecipe recipe) {
recipe.setTicks(recipe.getTicks() / getSpeed()); recipe.setTicks(recipe.getTicks() / getSpeed());
recipes.add(recipe); recipes.add(recipe);
@ -370,8 +362,6 @@ public abstract class AContainer extends SlimefunItem implements InventoryBlock,
inv.pushItem(output.clone(), getOutputSlots()); inv.pushItem(output.clone(), getOutputSlots());
} }
// Bukkit.getPluginManager().callEvent(new AsyncMachineProcessCompleteEvent(b.getLocation(),
// AContainer.this, getProcessing(b)));
processor.endOperation(b); processor.endOperation(b);
} }
} }

View File

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