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..7f464b76c --- /dev/null +++ b/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCSmithingInventory.java @@ -0,0 +1,66 @@ +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() { + if(si.getRecipe() == null) { + return null; + } else { + 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..b6a594821 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,10 @@ 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.events.Driver; import com.laytonsmith.core.events.EventUtils; import org.bukkit.event.EventHandler; @@ -23,7 +25,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 +92,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 e107352c9..fb469aaab 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; @@ -903,7 +907,7 @@ public String docs() { + " {viewers: all humanentities viewing the screen this event takes place in | matrix | result" + " | isRepair: true if this event was triggered by a repair operation (different than normal crafting)" + " | recipe: information about the formed recipe, or null if there is not one}" - + " {}"/*" {matrix: the slots that make up the crafting grid | result: the result slot of crafting}"*/ + + " { result: the product of the recipe. }" + " {}"; } @@ -953,34 +957,13 @@ public Driver driver() { @Override public boolean modifyEvent(String key, Mixed value, BindableEvent event) { - /*if(event instanceof MCPrepareItemCraftEvent) { - MCPrepareItemCraftEvent e = (MCPrepareItemCraftEvent) event; + if(event instanceof MCPrepareItemCraftEvent e) { + Target t = Target.UNKNOWN; if("result".equals(key)) { - e.getInventory().setResult(ObjectGenerator.GetGenerator().item(value, Target.UNKNOWN)); + e.getInventory().setResult(ObjectGenerator.GetGenerator().item(value, t)); return true; } - if("matrix".equals(key)) { - if(value.isInstanceOf(CArray.TYPE)) { - CArray va = (CArray) value; - MCItemStack[] old = e.getInventory().getMatrix(); - MCItemStack[] repl = new MCItemStack[old.length]; - for(int i=0; i 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)); + ret.put("recipe", ObjectGenerator.GetGenerator().recipe(smithing.getRecipe(), Target.UNKNOWN)); + 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; + } + } }