From 605642bdbd8de5723348f2eff3394816fa3836c3 Mon Sep 17 00:00:00 2001 From: Ddggdd135 <1306334428@qq.com> Date: Sun, 25 Aug 2024 20:44:05 +0800 Subject: [PATCH 1/3] complete #949 --- .../api/events/AutoDisenchantEvent.java | 19 +++++++++++++++++++ .../api/events/AutoEnchantEvent.java | 19 +++++++++++++++++++ .../machines/enchanting/AutoDisenchanter.java | 6 +++++- .../machines/enchanting/AutoEnchanter.java | 6 +++++- 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/AutoDisenchantEvent.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/AutoDisenchantEvent.java index d77d787cc1..2a1f54660b 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/AutoDisenchantEvent.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/AutoDisenchantEvent.java @@ -2,6 +2,8 @@ import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.enchanting.AutoDisenchanter; import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.bukkit.block.Block; import org.bukkit.event.Cancellable; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; @@ -18,6 +20,7 @@ public class AutoDisenchantEvent extends Event implements Cancellable { private static final HandlerList handlers = new HandlerList(); private final ItemStack item; + private Block block; private boolean cancelled; public AutoDisenchantEvent(@Nonnull ItemStack item) { @@ -26,6 +29,13 @@ public AutoDisenchantEvent(@Nonnull ItemStack item) { this.item = item; } + public AutoDisenchantEvent(@Nonnull ItemStack item, @Nullable Block block) { + super(true); + + this.item = item; + this.block = block; + } + /** * This returns the {@link ItemStack} that is being disenchanted. * @@ -36,6 +46,15 @@ public ItemStack getItem() { return item; } + /** + * This returns the {@link Block} that is disenchanting items + * + * @return The {@link Block} that is disenchanting items + */ + @Nullable public Block getBlock() { + return block; + } + @Override public boolean isCancelled() { return cancelled; diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/AutoEnchantEvent.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/AutoEnchantEvent.java index 0ce7b14299..3016cd212a 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/AutoEnchantEvent.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/AutoEnchantEvent.java @@ -2,6 +2,8 @@ import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.enchanting.AutoEnchanter; import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.bukkit.block.Block; import org.bukkit.event.Cancellable; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; @@ -20,6 +22,7 @@ public class AutoEnchantEvent extends Event implements Cancellable { private static final HandlerList handlers = new HandlerList(); private final ItemStack item; + private Block block; private boolean cancelled; public AutoEnchantEvent(@Nonnull ItemStack item) { @@ -28,6 +31,13 @@ public AutoEnchantEvent(@Nonnull ItemStack item) { this.item = item; } + public AutoEnchantEvent(@Nonnull ItemStack item, @Nullable Block block) { + super(true); + + this.item = item; + this.block = block; + } + /** * This returns the {@link ItemStack} that is being enchanted. * @@ -38,6 +48,15 @@ public ItemStack getItem() { return item; } + /** + * This returns the {@link Block} that is enchanting items + * + * @return The {@link Block} that is enchanting items + */ + @Nullable public Block getBlock() { + return block; + } + @Override public boolean isCancelled() { return cancelled; diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/enchanting/AutoDisenchanter.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/enchanting/AutoDisenchanter.java index 98e67991ac..8137e10b72 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/enchanting/AutoDisenchanter.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/enchanting/AutoDisenchanter.java @@ -61,10 +61,14 @@ protected MachineRecipe findNextRecipe(BlockMenu menu) { } // Call an event so other Plugins can modify it. - AutoDisenchantEvent event = new AutoDisenchantEvent(item); + AutoDisenchantEvent event = new AutoDisenchantEvent(item, menu.getBlock()); Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) { + if (InvUtils.fitAll(menu.toInventory(), new ItemStack[] {item}, getOutputSlots())) { + menu.replaceExistingItem(slot, null); + menu.pushItem(item, getOutputSlots()); + } return null; } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/enchanting/AutoEnchanter.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/enchanting/AutoEnchanter.java index a72166cc12..774da27006 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/enchanting/AutoEnchanter.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/enchanting/AutoEnchanter.java @@ -62,10 +62,14 @@ protected MachineRecipe findNextRecipe(BlockMenu menu) { } // Call an event so other Plugins can modify it. - AutoEnchantEvent event = new AutoEnchantEvent(item); + AutoEnchantEvent event = new AutoEnchantEvent(item, menu.getBlock()); Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) { + if (InvUtils.fitAll(menu.toInventory(), new ItemStack[] {item}, getOutputSlots())) { + menu.replaceExistingItem(slot, null); + menu.pushItem(item, getOutputSlots()); + } return null; } From 02299b6d9bdbc45bfeabf47625420350a01c6092 Mon Sep 17 00:00:00 2001 From: Ddggdd135 <1306334428@qq.com> Date: Sun, 25 Aug 2024 21:04:32 +0800 Subject: [PATCH 2/3] fix(autoEnchanter): error logic --- .../items/electric/machines/enchanting/AutoEnchanter.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/enchanting/AutoEnchanter.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/enchanting/AutoEnchanter.java index 774da27006..d38d2ae718 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/enchanting/AutoEnchanter.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/enchanting/AutoEnchanter.java @@ -54,7 +54,9 @@ public ItemStack getProgressBar() { @Override protected MachineRecipe findNextRecipe(BlockMenu menu) { for (int slot : getInputSlots()) { - ItemStack item = menu.getItemInSlot(slot == getInputSlots()[0] ? getInputSlots()[1] : getInputSlots()[0]); + // Other item + int otherSlot = slot == getInputSlots()[0] ? getInputSlots()[1] : getInputSlots()[0]; + ItemStack item = menu.getItemInSlot(otherSlot); // Check if the item is enchantable if (!isEnchantable(item)) { @@ -65,9 +67,11 @@ protected MachineRecipe findNextRecipe(BlockMenu menu) { AutoEnchantEvent event = new AutoEnchantEvent(item, menu.getBlock()); Bukkit.getPluginManager().callEvent(event); + event.setCancelled(true); + if (event.isCancelled()) { if (InvUtils.fitAll(menu.toInventory(), new ItemStack[] {item}, getOutputSlots())) { - menu.replaceExistingItem(slot, null); + menu.replaceExistingItem(otherSlot, null); menu.pushItem(item, getOutputSlots()); } return null; From bc33bac00e45b86dc54192653cca6d2a1454db9b Mon Sep 17 00:00:00 2001 From: Ddggdd135 <1306334428@qq.com> Date: Sun, 25 Aug 2024 21:07:42 +0800 Subject: [PATCH 3/3] fix(autoEnchanter): remove debug code --- .../items/electric/machines/enchanting/AutoEnchanter.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/enchanting/AutoEnchanter.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/enchanting/AutoEnchanter.java index d38d2ae718..57919359c7 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/enchanting/AutoEnchanter.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/enchanting/AutoEnchanter.java @@ -67,8 +67,6 @@ protected MachineRecipe findNextRecipe(BlockMenu menu) { AutoEnchantEvent event = new AutoEnchantEvent(item, menu.getBlock()); Bukkit.getPluginManager().callEvent(event); - event.setCancelled(true); - if (event.isCancelled()) { if (InvUtils.fitAll(menu.toInventory(), new ItemStack[] {item}, getOutputSlots())) { menu.replaceExistingItem(otherSlot, null);