Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add item_pre_anvil event #1384

Merged
merged 4 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/com/laytonsmith/abstraction/MCAnvilInventory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.laytonsmith.abstraction;

public interface MCAnvilInventory extends MCInventory {
MCItemStack getFirstItem();
MCItemStack getSecondItem();
MCItemStack getResult();
int getMaximumRepairCost();
int getRepairCost();
int getRepairCostAmount();
String getRenameText();

void setFirstItem(MCItemStack stack);
void setSecondItem(MCItemStack stack);
void setResult(MCItemStack stack);
void setMaximumRepairCost(int levels);
void setRepairCost(int levels);
void setRepairCostAmount(int levels);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.laytonsmith.abstraction.bukkit;

import com.laytonsmith.abstraction.MCAnvilInventory;
import com.laytonsmith.abstraction.MCItemStack;
import org.bukkit.inventory.AnvilInventory;

public class BukkitMCAnvilInventory extends BukkitMCInventory implements MCAnvilInventory {

AnvilInventory ai;

public BukkitMCAnvilInventory(AnvilInventory inventory) {
super(inventory);
ai = inventory;
}

@Override
public MCItemStack getFirstItem() {
return new BukkitMCItemStack(ai.getItem(0));
}

@Override
public MCItemStack getSecondItem() {
return new BukkitMCItemStack(ai.getItem(1));
}

@Override
public MCItemStack getResult() {
return new BukkitMCItemStack(ai.getItem(2));
}

@Override
public int getMaximumRepairCost() {
return ai.getMaximumRepairCost();
}

@Override
public int getRepairCost() {
return ai.getRepairCost();
}

@Override
public int getRepairCostAmount() {
return ai.getRepairCostAmount();
}

@Override
public String getRenameText() {
return ai.getRenameText();
}

@Override
public void setFirstItem(MCItemStack stack) {
ai.setItem(0, ((BukkitMCItemStack) stack).asItemStack());
}

@Override
public void setSecondItem(MCItemStack stack) {
ai.setItem(1, ((BukkitMCItemStack) stack).asItemStack());
}

@Override
public void setResult(MCItemStack stack) {
ai.setItem(2, ((BukkitMCItemStack) stack).asItemStack());
}

@Override
public void setMaximumRepairCost(int levels) {
ai.setMaximumRepairCost(levels);
}

@Override
public void setRepairCost(int levels) {
ai.setRepairCost(levels);
}

@Override
public void setRepairCostAmount(int levels) {
ai.setRepairCostAmount(levels);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.laytonsmith.abstraction.bukkit.events;

import com.laytonsmith.abstraction.MCAnvilInventory;
import com.laytonsmith.abstraction.MCCraftingInventory;
import com.laytonsmith.abstraction.MCEnchantmentOffer;
import com.laytonsmith.abstraction.MCHumanEntity;
Expand All @@ -10,6 +11,7 @@
import com.laytonsmith.abstraction.MCRecipe;
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.BukkitMCInventory;
Expand Down Expand Up @@ -37,6 +39,7 @@
import com.laytonsmith.abstraction.events.MCInventoryOpenEvent;
import com.laytonsmith.abstraction.events.MCItemHeldEvent;
import com.laytonsmith.abstraction.events.MCItemSwapEvent;
import com.laytonsmith.abstraction.events.MCPrepareAnvilEvent;
import com.laytonsmith.abstraction.events.MCPrepareItemCraftEvent;
import com.laytonsmith.abstraction.events.MCPrepareItemEnchantEvent;
import org.bukkit.enchantments.Enchantment;
Expand All @@ -52,6 +55,7 @@
import org.bukkit.event.inventory.InventoryEvent;
import org.bukkit.event.inventory.InventoryInteractEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
Expand Down Expand Up @@ -544,4 +548,28 @@ public MCCraftingInventory getInventory() {
return new BukkitMCCraftingInventory(e.getInventory());
}
}

public static class BukkitMCPrepareAnvilEvent extends BukkitMCInventoryEvent implements MCPrepareAnvilEvent {
PrepareAnvilEvent e;

public BukkitMCPrepareAnvilEvent(PrepareAnvilEvent event) {
super(event);
e = event;
}

@Override
public void setResult(MCItemStack i) {
e.setResult(((BukkitMCItemStack) i).asItemStack());
}

@Override
public MCPlayer getPlayer() {
return new BukkitMCPlayer(e.getViewers().get(0));
}

@Override
public MCAnvilInventory getInventory() {
return new BukkitMCAnvilInventory(e.getInventory());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.laytonsmith.abstraction.bukkit.events.BukkitInventoryEvents.BukkitMCInventoryOpenEvent;
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.BukkitMCPrepareItemCraftEvent;
import com.laytonsmith.abstraction.bukkit.events.BukkitInventoryEvents.BukkitMCPrepareItemEnchantEvent;
import com.laytonsmith.core.events.Driver;
Expand All @@ -21,6 +22,7 @@
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
Expand Down Expand Up @@ -80,4 +82,10 @@ public void onPreCraft(PrepareItemCraftEvent event) {
BukkitMCPrepareItemCraftEvent pc = new BukkitInventoryEvents.BukkitMCPrepareItemCraftEvent(event);
EventUtils.TriggerListener(Driver.ITEM_PRE_CRAFT, "item_pre_craft", pc);
}

@EventHandler(priority = EventPriority.LOWEST)
public void onPreAnvil(PrepareAnvilEvent event) {
BukkitMCPrepareAnvilEvent pa = new BukkitInventoryEvents.BukkitMCPrepareAnvilEvent(event);
EventUtils.TriggerListener(Driver.ITEM_PRE_ANVIL, "item_pre_anvil", pa);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.laytonsmith.abstraction.events;

import com.laytonsmith.abstraction.MCPlayer;
import com.laytonsmith.abstraction.MCItemStack;

public interface MCPrepareAnvilEvent extends MCInventoryEvent {
MCPlayer getPlayer();

void setResult(MCItemStack i);
}
1 change: 1 addition & 0 deletions src/main/java/com/laytonsmith/core/events/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public enum Driver {
ITEM_SWAP,
ITEM_PRE_CRAFT,
ITEM_PRE_ENCHANT,
ITEM_PRE_ANVIL,
/**
* Player events
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.laytonsmith.PureUtilities.Common.StringUtils;
import com.laytonsmith.PureUtilities.Version;
import com.laytonsmith.abstraction.MCAnvilInventory;
import com.laytonsmith.abstraction.MCEnchantmentOffer;
import com.laytonsmith.abstraction.MCHumanEntity;
import com.laytonsmith.abstraction.MCInventory;
Expand All @@ -19,6 +20,7 @@
import com.laytonsmith.abstraction.events.MCInventoryOpenEvent;
import com.laytonsmith.abstraction.events.MCItemHeldEvent;
import com.laytonsmith.abstraction.events.MCItemSwapEvent;
import com.laytonsmith.abstraction.events.MCPrepareAnvilEvent;
import com.laytonsmith.abstraction.events.MCPrepareItemCraftEvent;
import com.laytonsmith.abstraction.events.MCPrepareItemEnchantEvent;
import com.laytonsmith.annotations.api;
Expand Down Expand Up @@ -987,4 +989,83 @@ public Version since() {
return MSVersion.V3_3_1;
}
}

@api
public static class item_pre_anvil extends AbstractEvent {

@Override
public String getName() {
return "item_pre_anvil";
}

@Override
public String docs() {
return "{}"
+ " Fires when a recipe is formed in an anvil, but the result has not yet been clicked."
+ " { player: the player using the anvil."
+ " | first_item: the first item being used in the recipe."
+ " | second_item: the second item being used in the recipe."
+ " | result: the result of the recipe."
+ " | max_repair_cost: the maximum possible cost of this repair."
+ " | level_repair_cost: how many levels are needed to perform this repair."
+ " | item_repair_cost: how many items are needed to perform this repair. }"
+ " { result: the result of the anvil }"
+ " {}";
}

@Override
public boolean matches(Map<String, Mixed> prefilter, BindableEvent event) throws PrefilterNonMatchException {
if(event instanceof MCPrepareAnvilEvent) {
return true;
}
return false;
}

@Override
public BindableEvent convert(CArray manualObject, Target t) {
throw ConfigRuntimeException.CreateUncatchableException("Unsupported operation.", Target.UNKNOWN);
}

@Override
public Map<String, Mixed> evaluate(BindableEvent event) throws EventException {
if(event instanceof MCPrepareAnvilEvent e) {
MCAnvilInventory anvil = (MCAnvilInventory) e.getInventory();
Map<String, Mixed> ret = evaluate_helper(e);

ret.put("player", new CString(e.getPlayer().getName(), Target.UNKNOWN));
ret.put("first_item", ObjectGenerator.GetGenerator().item(anvil.getFirstItem(), Target.UNKNOWN));
ret.put("second_item", ObjectGenerator.GetGenerator().item(anvil.getSecondItem(), Target.UNKNOWN));
ret.put("result", ObjectGenerator.GetGenerator().item(anvil.getResult(), Target.UNKNOWN));
ret.put("max_repair_cost", new CInt(anvil.getMaximumRepairCost(), Target.UNKNOWN));
ret.put("level_repair_cost", new CInt(anvil.getRepairCost(), Target.UNKNOWN));
ret.put("item_repair_cost", new CInt(anvil.getRepairCostAmount(), Target.UNKNOWN));

return ret;
} else {
throw new EventException("Event received was not an MCPrepareAnvilEvent.");
}
}

@Override
public Driver driver() {
return Driver.ITEM_PRE_ANVIL;
}

@Override
public boolean modifyEvent(String key, Mixed value, BindableEvent event) {
if(event instanceof MCPrepareAnvilEvent 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;
}
}
}
Loading