Skip to content

Commit

Permalink
Add MultiBlockCraftEvent (Updated version of #3439) (#3928)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustAHuman-xD authored Nov 21, 2023
1 parent 563ebec commit 6341403
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package io.github.thebusybiscuit.slimefun4.api.events;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.inventory.ItemStack;

import io.github.thebusybiscuit.slimefun4.core.multiblocks.MultiBlockMachine;

/**
* This {@link Event} is called when a {@link Player} crafts an item using a {@link MultiBlockMachine}.
* Unlike the {@link MultiBlockInteractEvent}, this event only fires if an output to a craft is expected.
* If this event is cancelled, ingredients will not be consumed and no output item results.
*
* @author char321
* @author JustAHuman
*/
public class MultiBlockCraftEvent extends PlayerEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();

private final MultiBlockMachine machine;
private final ItemStack[] input;
private ItemStack output;
private boolean cancelled;

/**
* Creates a new {@link MultiBlockCraftEvent}.
*
* @param p The player that crafts using a multiblock
* @param machine The multiblock machine used to craft
* @param input The input items of the craft
* @param output The resulting item of the craft
*/
@ParametersAreNonnullByDefault
public MultiBlockCraftEvent(Player p, MultiBlockMachine machine, ItemStack[] input, ItemStack output) {
super(p);
this.machine = machine;
this.input = input;
this.output = output;
}

/**
* Creates a new {@link MultiBlockCraftEvent}.
*
* @param p The player that crafts using a multiblock
* @param machine The multiblock machine used to craft
* @param input The input item of the craft
* @param output The resulting item of the craft
*/
@ParametersAreNonnullByDefault
public MultiBlockCraftEvent(Player p, MultiBlockMachine machine, ItemStack input, ItemStack output) {
this(p, machine, new ItemStack[]{input}, output);
}

/**
* Gets the machine that was used to craft.
*
* @return The {@link MultiBlockMachine} used to craft.
*/
public @Nonnull MultiBlockMachine getMachine() {
return machine;
}

/**
* Gets the input of the craft.
*
* @return The {@link ItemStack ItemStack[]} input that is used in the craft.
*/
public @Nonnull ItemStack[] getInput() {
return input;
}

/**
* Gets the output of the craft.
*
* @return The {@link ItemStack} output that results from the craft.
*/
public @Nonnull ItemStack getOutput() {
return output;
}

/**
* Sets the output of the craft. Keep in mind that this overwrites any existing output.
*
* @param output
* The new item for the event to produce.
*
* @return The previous {@link ItemStack} output that was replaced.
*/
public @Nullable ItemStack setOutput(@Nullable ItemStack output) {
ItemStack oldOutput = this.output;
this.output = output;
return oldOutput;
}

@Override
public boolean isCancelled() {
return cancelled;
}

@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}

public static @Nonnull HandlerList getHandlerList() {
return handlers;
}

@Override
public @Nonnull HandlerList getHandlers() {
return getHandlerList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.annotation.ParametersAreNonnullByDefault;

import org.bukkit.Bukkit;
import org.bukkit.Effect;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
Expand All @@ -14,6 +15,7 @@
import org.bukkit.inventory.ItemStack;

import io.github.bakedlibs.dough.inventory.InvUtils;
import io.github.thebusybiscuit.slimefun4.api.events.MultiBlockCraftEvent;
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType;
Expand Down Expand Up @@ -48,12 +50,14 @@ public void onInteract(Player p, Block b) {
for (int i = 0; i < inputs.size(); i++) {
if (canCraft(inv, inputs, i)) {
ItemStack output = RecipeType.getRecipeOutputList(this, inputs.get(i)).clone();
MultiBlockCraftEvent event = new MultiBlockCraftEvent(p, this, inputs.get(i), output);

if (SlimefunUtils.canPlayerUseItem(p, output, true)) {
Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled() && SlimefunUtils.canPlayerUseItem(p, output, true)) {
Inventory outputInv = findOutputInventory(output, possibleDispenser, inv);

if (outputInv != null) {
craft(p, b, inv, inputs.get(i), output, outputInv);
craft(p, b, inv, inputs.get(i), event.getOutput(), outputInv);
} else {
Slimefun.getLocalization().sendMessage(p, "machines.full-inventory", true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.annotation.ParametersAreNonnullByDefault;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
Expand All @@ -13,9 +14,9 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import io.github.thebusybiscuit.slimefun4.core.services.sounds.SoundEffect;
import io.github.bakedlibs.dough.items.CustomItemStack;
import io.github.bakedlibs.dough.items.ItemUtils;
import io.github.thebusybiscuit.slimefun4.api.events.MultiBlockCraftEvent;
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType;
Expand Down Expand Up @@ -43,9 +44,11 @@ public void onInteract(Player p, Block b) {
for (ItemStack[] input : inputs) {
if (isCraftable(inv, input)) {
ItemStack output = RecipeType.getRecipeOutputList(this, input).clone();
MultiBlockCraftEvent event = new MultiBlockCraftEvent(p, this, input, output);

if (SlimefunUtils.canPlayerUseItem(p, output, true)) {
craft(p, output, inv, possibleDispenser);
Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled() && SlimefunUtils.canPlayerUseItem(p, output, true)) {
craft(p, event.getOutput(), inv, possibleDispenser);
}

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import org.bukkit.Bukkit;
import org.bukkit.Effect;
import org.bukkit.GameMode;
import org.bukkit.Material;
Expand All @@ -18,6 +19,7 @@

import io.github.bakedlibs.dough.items.ItemUtils;
import io.github.bakedlibs.dough.scheduling.TaskQueue;
import io.github.thebusybiscuit.slimefun4.api.events.MultiBlockCraftEvent;
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
import io.github.thebusybiscuit.slimefun4.core.multiblocks.MultiBlockMachine;
Expand Down Expand Up @@ -74,6 +76,14 @@ public void onInteract(Player p, Block b) {
return;
}

MultiBlockCraftEvent event = new MultiBlockCraftEvent(p, this, input, output);

Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
}

ItemStack finalOutput = event.getOutput();
if (p.getGameMode() != GameMode.CREATIVE) {
ItemUtils.consumeItem(input, false);
}
Expand All @@ -82,13 +92,13 @@ public void onInteract(Player p, Block b) {

queue.thenRepeatEvery(20, 5, () -> b.getWorld().playEffect(b.getRelative(BlockFace.DOWN).getLocation(), Effect.STEP_SOUND, material));
queue.thenRun(20, () -> {
if (output.getType() != Material.AIR) {
if (finalOutput.getType() != Material.AIR) {
Optional<Inventory> outputChest = OutputChest.findOutputChestFor(b.getRelative(BlockFace.DOWN), output);

if (outputChest.isPresent()) {
outputChest.get().addItem(output.clone());
outputChest.get().addItem(finalOutput.clone());
} else {
b.getWorld().dropItemNaturally(b.getLocation(), output.clone());
b.getWorld().dropItemNaturally(b.getLocation(), finalOutput.clone());
}

SoundEffect.AUTOMATED_PANNING_MACHINE_SUCCESS_SOUND.playAt(b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
Expand All @@ -16,6 +17,7 @@
import org.bukkit.inventory.ItemStack;

import io.github.bakedlibs.dough.items.CustomItemStack;
import io.github.thebusybiscuit.slimefun4.api.events.MultiBlockCraftEvent;
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType;
Expand Down Expand Up @@ -66,13 +68,19 @@ public void onInteract(Player p, Block b) {
if (recipeInput != null && SlimefunUtils.isItemSimilar(item, recipeInput, true)) {
ItemStack output = RecipeType.getRecipeOutput(this, recipeInput);
Inventory outputInv = findOutputInventory(output, dispBlock, inv);
MultiBlockCraftEvent event = new MultiBlockCraftEvent(p, this, item, output);

Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
}

if (outputInv != null) {
ItemStack removing = item.clone();
removing.setAmount(recipeInput.getAmount());
inv.removeItem(removing);

craft(p, output, dispBlock, inv);
craft(p, event.getOutput(), dispBlock, inv);
} else {
Slimefun.getLocalization().sendMessage(p, "machines.full-inventory", true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.annotation.ParametersAreNonnullByDefault;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
Expand All @@ -13,8 +14,8 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import io.github.thebusybiscuit.slimefun4.core.services.sounds.SoundEffect;
import io.github.bakedlibs.dough.items.ItemUtils;
import io.github.thebusybiscuit.slimefun4.api.events.MultiBlockCraftEvent;
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
Expand Down Expand Up @@ -44,9 +45,11 @@ public void onInteract(Player p, Block b) {
for (ItemStack[] input : inputs) {
if (isCraftable(inv, input)) {
ItemStack output = RecipeType.getRecipeOutputList(this, input).clone();
MultiBlockCraftEvent event = new MultiBlockCraftEvent(p, this, input, output);

if (SlimefunUtils.canPlayerUseItem(p, output, true)) {
craft(inv, possibleDispenser, p, b, output);
Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled() && SlimefunUtils.canPlayerUseItem(p, output, true)) {
craft(inv, possibleDispenser, p, b, event.getOutput());
}

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
Expand All @@ -16,6 +17,7 @@
import org.bukkit.inventory.ItemStack;

import io.github.bakedlibs.dough.items.CustomItemStack;
import io.github.thebusybiscuit.slimefun4.api.events.MultiBlockCraftEvent;
import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion;
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
Expand Down Expand Up @@ -126,12 +128,18 @@ public void onInteract(Player p, Block b) {
if (convert != null && SlimefunUtils.isItemSimilar(current, convert, true)) {
ItemStack output = RecipeType.getRecipeOutput(this, convert);
Inventory outputInv = findOutputInventory(output, possibleDispenser, inv);
MultiBlockCraftEvent event = new MultiBlockCraftEvent(p, this, current, output);

Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
}

if (outputInv != null) {
ItemStack removing = current.clone();
removing.setAmount(1);
inv.removeItem(removing);
outputInv.addItem(output);
outputInv.addItem(event.getOutput());
SoundEffect.GRIND_STONE_INTERACT_SOUND.playAt(b);
} else {
Slimefun.getLocalization().sendMessage(p, "machines.full-inventory", true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import org.bukkit.Bukkit;
import org.bukkit.Effect;
import org.bukkit.Material;
import org.bukkit.block.Block;
Expand All @@ -17,6 +18,7 @@
import org.bukkit.inventory.ItemStack;

import io.github.bakedlibs.dough.items.CustomItemStack;
import io.github.thebusybiscuit.slimefun4.api.events.MultiBlockCraftEvent;
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType;
Expand Down Expand Up @@ -62,12 +64,18 @@ public void onInteract(Player p, Block b) {
if (convert != null && SlimefunUtils.isItemSimilar(current, convert, true)) {
ItemStack adding = RecipeType.getRecipeOutput(this, convert);
Inventory outputInv = findOutputInventory(adding, possibleDispenser, inv);
MultiBlockCraftEvent event = new MultiBlockCraftEvent(p, this, current, adding);

Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
}

if (outputInv != null) {
ItemStack removing = current.clone();
removing.setAmount(1);
inv.removeItem(removing);
outputInv.addItem(adding);
outputInv.addItem(event.getOutput());

SoundEffect.JUICER_USE_SOUND.playAt(b);
// Not changed since this is supposed to be a natural sound.
Expand Down
Loading

0 comments on commit 6341403

Please sign in to comment.