From 55088dc31f864d455bbcb21e8ed4b44f8458e127 Mon Sep 17 00:00:00 2001 From: Lildirt Date: Sat, 6 Jul 2024 01:10:33 -0400 Subject: [PATCH] add item_pre_grindstone (PrepareGrindstoneEvent), item_pre_smithing (PrepareSmithingEvent) --- .../abstraction/MCGrindstoneInventory.java | 11 ++ .../abstraction/MCSmithingInventory.java | 14 ++ .../bukkit/BukkitMCGrindstoneInventory.java | 45 +++++ .../bukkit/BukkitMCSmithingInventory.java | 62 +++++++ .../bukkit/events/BukkitInventoryEvents.java | 56 ++++++ .../drivers/BukkitInventoryListener.java | 17 ++ .../events/MCPrepareGrindstoneEvent.java | 10 ++ .../events/MCPrepareSmithingEvent.java | 10 ++ .../com/laytonsmith/core/events/Driver.java | 2 + .../core/events/drivers/InventoryEvents.java | 159 ++++++++++++++++++ 10 files changed, 386 insertions(+) create mode 100644 src/main/java/com/laytonsmith/abstraction/MCGrindstoneInventory.java create mode 100644 src/main/java/com/laytonsmith/abstraction/MCSmithingInventory.java create mode 100644 src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCGrindstoneInventory.java create mode 100644 src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCSmithingInventory.java create mode 100644 src/main/java/com/laytonsmith/abstraction/events/MCPrepareGrindstoneEvent.java create mode 100644 src/main/java/com/laytonsmith/abstraction/events/MCPrepareSmithingEvent.java diff --git a/src/main/java/com/laytonsmith/abstraction/MCGrindstoneInventory.java b/src/main/java/com/laytonsmith/abstraction/MCGrindstoneInventory.java new file mode 100644 index 000000000..068cf4427 --- /dev/null +++ b/src/main/java/com/laytonsmith/abstraction/MCGrindstoneInventory.java @@ -0,0 +1,11 @@ +package com.laytonsmith.abstraction; + +public interface MCGrindstoneInventory extends MCInventory { + MCItemStack getUpperItem(); + MCItemStack getLowerItem(); + MCItemStack getResult(); + + void setUpperItem(MCItemStack i); + void setLowerItem(MCItemStack i); + void setResult(MCItemStack i); +} diff --git a/src/main/java/com/laytonsmith/abstraction/MCSmithingInventory.java b/src/main/java/com/laytonsmith/abstraction/MCSmithingInventory.java new file mode 100644 index 000000000..4c09d7586 --- /dev/null +++ b/src/main/java/com/laytonsmith/abstraction/MCSmithingInventory.java @@ -0,0 +1,14 @@ +package com.laytonsmith.abstraction; + +public interface MCSmithingInventory extends MCInventory { + MCItemStack getInputEquipment(); + MCItemStack getInputMaterial(); + MCItemStack getInputTemplate(); + MCRecipe getRecipe(); + MCItemStack getResult(); + + void setInputEquipment(MCItemStack stack); + void setInputMaterial(MCItemStack stack); + void setInputTemplate(MCItemStack stack); + void setResult(MCItemStack stack); +} diff --git a/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCGrindstoneInventory.java b/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCGrindstoneInventory.java new file mode 100644 index 000000000..e81be9585 --- /dev/null +++ b/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCGrindstoneInventory.java @@ -0,0 +1,45 @@ +package com.laytonsmith.abstraction.bukkit; + +import com.laytonsmith.abstraction.MCGrindstoneInventory; +import com.laytonsmith.abstraction.MCItemStack; +import org.bukkit.inventory.GrindstoneInventory; + +public class BukkitMCGrindstoneInventory extends BukkitMCInventory implements MCGrindstoneInventory { + + GrindstoneInventory gi; + + public BukkitMCGrindstoneInventory(GrindstoneInventory inventory) { + super(inventory); + gi = inventory; + } + + @Override + public MCItemStack getUpperItem() { + return new BukkitMCItemStack(gi.getItem(0)); + } + + @Override + public MCItemStack getLowerItem() { + return new BukkitMCItemStack(gi.getItem(1)); + } + + @Override + public MCItemStack getResult() { + return new BukkitMCItemStack(gi.getItem(2)); + } + + @Override + public void setUpperItem(MCItemStack stack) { + gi.setItem(0, ((BukkitMCItemStack) stack).asItemStack()); + } + + @Override + public void setLowerItem(MCItemStack stack) { + gi.setItem(1, ((BukkitMCItemStack) stack).asItemStack()); + } + + @Override + public void setResult(MCItemStack stack) { + gi.setItem(2, ((BukkitMCItemStack) stack).asItemStack()); + } +} diff --git a/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCSmithingInventory.java b/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCSmithingInventory.java new file mode 100644 index 000000000..cba9a04aa --- /dev/null +++ b/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCSmithingInventory.java @@ -0,0 +1,62 @@ +package com.laytonsmith.abstraction.bukkit; + +import com.laytonsmith.abstraction.MCItemStack; +import com.laytonsmith.abstraction.MCRecipe; +import com.laytonsmith.abstraction.MCSmithingInventory; +import org.bukkit.inventory.SmithingInventory; +import org.bukkit.inventory.SmithingRecipe; + +public class BukkitMCSmithingInventory extends BukkitMCInventory implements MCSmithingInventory { + + SmithingInventory si; + + public BukkitMCSmithingInventory(SmithingInventory inventory) { + super(inventory); + si = inventory; + } + + @Override + public MCItemStack getInputEquipment() { + return new BukkitMCItemStack(si.getItem(1)); + } + + @Override + public MCItemStack getInputMaterial() { + return new BukkitMCItemStack(si.getItem(2)); + } + + @Override + public MCItemStack getInputTemplate() { + return new BukkitMCItemStack(si.getItem(0)); + } + + @Override + public MCRecipe getRecipe() { + return new BukkitMCSmithingRecipe((SmithingRecipe) si.getRecipe()); + } + + @Override + public MCItemStack getResult() { + return new BukkitMCItemStack(si.getResult()); + } + + @Override + public void setInputEquipment(MCItemStack stack) { + si.setItem(1, ((BukkitMCItemStack) stack).asItemStack()); + } + + @Override + public void setInputMaterial(MCItemStack stack) { + si.setItem(2, ((BukkitMCItemStack) stack).asItemStack()); + } + + @Override + public void setInputTemplate(MCItemStack stack) { + si.setItem(0, ((BukkitMCItemStack) stack).asItemStack()); + } + + @Override + public void setResult(MCItemStack stack) { + si.setResult(((BukkitMCItemStack) stack).asItemStack()); + } +} diff --git a/src/main/java/com/laytonsmith/abstraction/bukkit/events/BukkitInventoryEvents.java b/src/main/java/com/laytonsmith/abstraction/bukkit/events/BukkitInventoryEvents.java index b1fb907c3..f968ae456 100644 --- a/src/main/java/com/laytonsmith/abstraction/bukkit/events/BukkitInventoryEvents.java +++ b/src/main/java/com/laytonsmith/abstraction/bukkit/events/BukkitInventoryEvents.java @@ -3,20 +3,24 @@ import com.laytonsmith.abstraction.MCAnvilInventory; import com.laytonsmith.abstraction.MCCraftingInventory; import com.laytonsmith.abstraction.MCEnchantmentOffer; +import com.laytonsmith.abstraction.MCGrindstoneInventory; import com.laytonsmith.abstraction.MCHumanEntity; import com.laytonsmith.abstraction.MCInventory; import com.laytonsmith.abstraction.MCInventoryView; import com.laytonsmith.abstraction.MCItemStack; import com.laytonsmith.abstraction.MCPlayer; import com.laytonsmith.abstraction.MCRecipe; +import com.laytonsmith.abstraction.MCSmithingInventory; import com.laytonsmith.abstraction.blocks.MCBlock; import com.laytonsmith.abstraction.bukkit.BukkitConvertor; import com.laytonsmith.abstraction.bukkit.BukkitMCAnvilInventory; import com.laytonsmith.abstraction.bukkit.BukkitMCCraftingInventory; import com.laytonsmith.abstraction.bukkit.BukkitMCEnchantmentOffer; +import com.laytonsmith.abstraction.bukkit.BukkitMCGrindstoneInventory; import com.laytonsmith.abstraction.bukkit.BukkitMCInventory; import com.laytonsmith.abstraction.bukkit.BukkitMCInventoryView; import com.laytonsmith.abstraction.bukkit.BukkitMCItemStack; +import com.laytonsmith.abstraction.bukkit.BukkitMCSmithingInventory; import com.laytonsmith.abstraction.bukkit.blocks.BukkitMCBlock; import com.laytonsmith.abstraction.bukkit.entities.BukkitMCHumanEntity; import com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer; @@ -40,8 +44,10 @@ import com.laytonsmith.abstraction.events.MCItemHeldEvent; import com.laytonsmith.abstraction.events.MCItemSwapEvent; import com.laytonsmith.abstraction.events.MCPrepareAnvilEvent; +import com.laytonsmith.abstraction.events.MCPrepareGrindstoneEvent; import com.laytonsmith.abstraction.events.MCPrepareItemCraftEvent; import com.laytonsmith.abstraction.events.MCPrepareItemEnchantEvent; +import com.laytonsmith.abstraction.events.MCPrepareSmithingEvent; import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.EnchantmentOffer; import org.bukkit.entity.HumanEntity; @@ -56,7 +62,9 @@ import org.bukkit.event.inventory.InventoryInteractEvent; import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.inventory.PrepareAnvilEvent; +import org.bukkit.event.inventory.PrepareGrindstoneEvent; import org.bukkit.event.inventory.PrepareItemCraftEvent; +import org.bukkit.event.inventory.PrepareSmithingEvent; import org.bukkit.event.player.PlayerItemHeldEvent; import org.bukkit.event.player.PlayerSwapHandItemsEvent; import org.bukkit.inventory.ItemStack; @@ -572,4 +580,52 @@ public MCAnvilInventory getInventory() { return new BukkitMCAnvilInventory(e.getInventory()); } } + + public static class BukkitMCPrepareSmithingEvent extends BukkitMCInventoryEvent implements MCPrepareSmithingEvent { + PrepareSmithingEvent e; + + public BukkitMCPrepareSmithingEvent(PrepareSmithingEvent event) { + super(event); + e = event; + } + + @Override + public MCPlayer getPlayer() { + return new BukkitMCPlayer(e.getViewers().get(0)); + } + + @Override + public void setResult(MCItemStack stack) { + e.setResult(((BukkitMCItemStack) stack).asItemStack()); + } + + @Override + public MCSmithingInventory getInventory() { + return new BukkitMCSmithingInventory(e.getInventory()); + } + } + + public static class BukkitMCPrepareGrindstoneEvent extends BukkitMCInventoryEvent implements MCPrepareGrindstoneEvent { + PrepareGrindstoneEvent e; + + public BukkitMCPrepareGrindstoneEvent(PrepareGrindstoneEvent event) { + super(event); + e = event; + } + + @Override + public MCPlayer getPlayer() { + return new BukkitMCPlayer(e.getViewers().get(0)); + } + + @Override + public void setResult(MCItemStack stack) { + e.setResult(((BukkitMCItemStack) stack).asItemStack()); + } + + @Override + public MCGrindstoneInventory getInventory() { + return new BukkitMCGrindstoneInventory(e.getInventory()); + } + } } diff --git a/src/main/java/com/laytonsmith/abstraction/bukkit/events/drivers/BukkitInventoryListener.java b/src/main/java/com/laytonsmith/abstraction/bukkit/events/drivers/BukkitInventoryListener.java index c63f74249..4952a2f02 100644 --- a/src/main/java/com/laytonsmith/abstraction/bukkit/events/drivers/BukkitInventoryListener.java +++ b/src/main/java/com/laytonsmith/abstraction/bukkit/events/drivers/BukkitInventoryListener.java @@ -9,8 +9,11 @@ import com.laytonsmith.abstraction.bukkit.events.BukkitInventoryEvents.BukkitMCItemHeldEvent; import com.laytonsmith.abstraction.bukkit.events.BukkitInventoryEvents.BukkitMCItemSwapEvent; import com.laytonsmith.abstraction.bukkit.events.BukkitInventoryEvents.BukkitMCPrepareAnvilEvent; +import com.laytonsmith.abstraction.bukkit.events.BukkitInventoryEvents.BukkitMCPrepareGrindstoneEvent; import com.laytonsmith.abstraction.bukkit.events.BukkitInventoryEvents.BukkitMCPrepareItemCraftEvent; import com.laytonsmith.abstraction.bukkit.events.BukkitInventoryEvents.BukkitMCPrepareItemEnchantEvent; +import com.laytonsmith.abstraction.bukkit.events.BukkitInventoryEvents.BukkitMCPrepareSmithingEvent; +import com.laytonsmith.core.Static; import com.laytonsmith.core.events.Driver; import com.laytonsmith.core.events.EventUtils; import org.bukkit.event.EventHandler; @@ -23,7 +26,9 @@ import org.bukkit.event.inventory.InventoryDragEvent; import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.inventory.PrepareAnvilEvent; +import org.bukkit.event.inventory.PrepareGrindstoneEvent; import org.bukkit.event.inventory.PrepareItemCraftEvent; +import org.bukkit.event.inventory.PrepareSmithingEvent; import org.bukkit.event.player.PlayerItemHeldEvent; import org.bukkit.event.player.PlayerSwapHandItemsEvent; @@ -88,4 +93,16 @@ public void onPreAnvil(PrepareAnvilEvent event) { BukkitMCPrepareAnvilEvent pa = new BukkitInventoryEvents.BukkitMCPrepareAnvilEvent(event); EventUtils.TriggerListener(Driver.ITEM_PRE_ANVIL, "item_pre_anvil", pa); } + + @EventHandler(priority = EventPriority.LOWEST) + public void onPreSmithing(PrepareSmithingEvent event) { + BukkitMCPrepareSmithingEvent ps = new BukkitInventoryEvents.BukkitMCPrepareSmithingEvent(event); + EventUtils.TriggerListener(Driver.ITEM_PRE_SMITHING, "item_pre_smithing", ps); + } + + @EventHandler(priority = EventPriority.LOWEST) + public void onPreGrindstone(PrepareGrindstoneEvent event) { + BukkitMCPrepareGrindstoneEvent pg = new BukkitInventoryEvents.BukkitMCPrepareGrindstoneEvent(event); + EventUtils.TriggerListener(Driver.ITEM_PRE_GRINDSTONE, "item_pre_grindstone", pg); + } } diff --git a/src/main/java/com/laytonsmith/abstraction/events/MCPrepareGrindstoneEvent.java b/src/main/java/com/laytonsmith/abstraction/events/MCPrepareGrindstoneEvent.java new file mode 100644 index 000000000..9d9a90b8f --- /dev/null +++ b/src/main/java/com/laytonsmith/abstraction/events/MCPrepareGrindstoneEvent.java @@ -0,0 +1,10 @@ +package com.laytonsmith.abstraction.events; + +import com.laytonsmith.abstraction.MCItemStack; +import com.laytonsmith.abstraction.MCPlayer; + +public interface MCPrepareGrindstoneEvent extends MCInventoryEvent { + MCPlayer getPlayer(); + + void setResult(MCItemStack stack); +} diff --git a/src/main/java/com/laytonsmith/abstraction/events/MCPrepareSmithingEvent.java b/src/main/java/com/laytonsmith/abstraction/events/MCPrepareSmithingEvent.java new file mode 100644 index 000000000..ca773cd47 --- /dev/null +++ b/src/main/java/com/laytonsmith/abstraction/events/MCPrepareSmithingEvent.java @@ -0,0 +1,10 @@ +package com.laytonsmith.abstraction.events; + +import com.laytonsmith.abstraction.MCItemStack; +import com.laytonsmith.abstraction.MCPlayer; + +public interface MCPrepareSmithingEvent extends MCInventoryEvent { + MCPlayer getPlayer(); + + void setResult(MCItemStack stack); +} diff --git a/src/main/java/com/laytonsmith/core/events/Driver.java b/src/main/java/com/laytonsmith/core/events/Driver.java index 95973a062..4fadc8061 100644 --- a/src/main/java/com/laytonsmith/core/events/Driver.java +++ b/src/main/java/com/laytonsmith/core/events/Driver.java @@ -68,6 +68,8 @@ public enum Driver { ITEM_PRE_CRAFT, ITEM_PRE_ENCHANT, ITEM_PRE_ANVIL, + ITEM_PRE_SMITHING, + ITEM_PRE_GRINDSTONE, /** * Player events */ diff --git a/src/main/java/com/laytonsmith/core/events/drivers/InventoryEvents.java b/src/main/java/com/laytonsmith/core/events/drivers/InventoryEvents.java index f49863ce7..41b10482a 100644 --- a/src/main/java/com/laytonsmith/core/events/drivers/InventoryEvents.java +++ b/src/main/java/com/laytonsmith/core/events/drivers/InventoryEvents.java @@ -4,9 +4,11 @@ import com.laytonsmith.PureUtilities.Version; import com.laytonsmith.abstraction.MCAnvilInventory; import com.laytonsmith.abstraction.MCEnchantmentOffer; +import com.laytonsmith.abstraction.MCGrindstoneInventory; import com.laytonsmith.abstraction.MCHumanEntity; import com.laytonsmith.abstraction.MCInventory; import com.laytonsmith.abstraction.MCItemStack; +import com.laytonsmith.abstraction.MCSmithingInventory; import com.laytonsmith.abstraction.MCVirtualInventoryHolder; import com.laytonsmith.abstraction.enums.MCClickType; import com.laytonsmith.abstraction.enums.MCDragType; @@ -21,8 +23,10 @@ import com.laytonsmith.abstraction.events.MCItemHeldEvent; import com.laytonsmith.abstraction.events.MCItemSwapEvent; import com.laytonsmith.abstraction.events.MCPrepareAnvilEvent; +import com.laytonsmith.abstraction.events.MCPrepareGrindstoneEvent; import com.laytonsmith.abstraction.events.MCPrepareItemCraftEvent; import com.laytonsmith.abstraction.events.MCPrepareItemEnchantEvent; +import com.laytonsmith.abstraction.events.MCPrepareSmithingEvent; import com.laytonsmith.annotations.api; import com.laytonsmith.core.ArgumentValidation; import com.laytonsmith.core.MSLog; @@ -1050,4 +1054,159 @@ public Version since() { return MSVersion.V3_3_5; } } + + @api + public static class item_pre_smithing extends AbstractEvent { + + @Override + public String getName() { + return "item_pre_smithing"; + } + + @Override + public String docs() { + return "{}" + + " Fires when a recipe is formed in a smithing table, but the result has not yet been clicked." + + " { player: the player using the smithing table." + + " | first_item: the first item being used in the recipe." + + " | second_item: the second item being used in the recipe." + + " | third_item: the third item being used in the recipe." + + " | recipe: information about the formed recipe, or null if there's not one." + + " | result: the result of the recipe. }" + + " { result: the result of the smithing table recipe. While the result will appear on the" + + " client-side, all items must be populated before a recipe can be executed. }" + + " {}"; + } + + @Override + public boolean matches(Map prefilter, BindableEvent event) throws PrefilterNonMatchException { + if(event instanceof MCPrepareSmithingEvent) { + return true; + } + return false; + } + + @Override + public BindableEvent convert(CArray manualObject, Target t) { + throw ConfigRuntimeException.CreateUncatchableException("Unsupported operation.", Target.UNKNOWN); + } + + @Override + public Map evaluate(BindableEvent event) throws EventException { + if(event instanceof MCPrepareSmithingEvent e) { + MCSmithingInventory smithing = (MCSmithingInventory) e.getInventory(); + Map ret = evaluate_helper(e); + + ret.put("player", new CString(e.getPlayer().getName(), Target.UNKNOWN)); + ret.put("first_item", ObjectGenerator.GetGenerator().item(smithing.getInputTemplate(), Target.UNKNOWN)); + ret.put("second_item", ObjectGenerator.GetGenerator().item(smithing.getInputEquipment(), Target.UNKNOWN)); + ret.put("third_item", ObjectGenerator.GetGenerator().item(smithing.getInputMaterial(), Target.UNKNOWN)); + try { + ret.put("recipe", ObjectGenerator.GetGenerator().recipe(smithing.getRecipe(), Target.UNKNOWN)); + } catch(NullPointerException ex) { + ret.put("recipe", CNull.NULL); + } + ret.put("result", ObjectGenerator.GetGenerator().item(smithing.getResult(), Target.UNKNOWN)); + + return ret; + } else { + throw new EventException("Event received was not an MCPrepareSmithingEvent."); + } + } + + @Override + public Driver driver() { + return Driver.ITEM_PRE_SMITHING; + } + + @Override + public boolean modifyEvent(String key, Mixed value, BindableEvent event) { + if(event instanceof MCPrepareSmithingEvent e) { + Target t = value.getTarget(); + if(key.equalsIgnoreCase("result")) { + e.setResult(ObjectGenerator.GetGenerator().item(value, t)); + return true; + } + } + return false; + } + + @Override + public Version since() { + return MSVersion.V3_3_5; + } + } + + @api + public static class item_pre_grindstone extends AbstractEvent { + + @Override + public String getName() { + return "item_pre_grindstone"; + } + + @Override + public String docs() { + return "{}" + + " Fires when a recipe is formed in a grindstone, but the result has not yet been clicked." + + " { player: the player using the grindstone." + + " | upper_item: the first item being used in the recipe." + + " | lower_item: the second item being used in the recipe." + + " | result: the result of the recipe. }" + + " { result: the result of the grindstone recipe. }" + + " {}"; + } + + @Override + public boolean matches(Map prefilter, BindableEvent event) throws PrefilterNonMatchException { + if(event instanceof MCPrepareGrindstoneEvent) { + return true; + } + return false; + } + + @Override + public BindableEvent convert(CArray manualObject, Target t) { + throw ConfigRuntimeException.CreateUncatchableException("Unsupported operation.", Target.UNKNOWN); + } + + @Override + public Map evaluate(BindableEvent event) throws EventException { + if(event instanceof MCPrepareGrindstoneEvent e) { + MCGrindstoneInventory grindstone = (MCGrindstoneInventory) e.getInventory(); + Map ret = evaluate_helper(e); + + ret.put("player", new CString(e.getPlayer().getName(), Target.UNKNOWN)); + ret.put("upper_item", ObjectGenerator.GetGenerator().item(grindstone.getUpperItem(), Target.UNKNOWN)); + ret.put("lower_item", ObjectGenerator.GetGenerator().item(grindstone.getLowerItem(), Target.UNKNOWN)); + ret.put("result", ObjectGenerator.GetGenerator().item(grindstone.getResult(), Target.UNKNOWN)); + + return ret; + } else { + throw new EventException("Event received was not an MCPrepareGrindstoneEvent."); + } + } + + @Override + public Driver driver() { + return Driver.ITEM_PRE_GRINDSTONE; + } + + @Override + public boolean modifyEvent(String key, Mixed value, BindableEvent event) { + if(event instanceof MCPrepareGrindstoneEvent e) { + Target t = value.getTarget(); + if(key.equalsIgnoreCase("result")) { + e.setResult(ObjectGenerator.GetGenerator().item(value, t)); + return true; + } + } + return false; + } + + @Override + public Version since() { + return MSVersion.V3_3_5; + } + } }