diff --git a/plugin/java/me/insanj/mayor/MayorVillager.java b/plugin/java/me/insanj/mayor/MayorVillager.java deleted file mode 100644 index 4d7f8b4..0000000 --- a/plugin/java/me/insanj/mayor/MayorVillager.java +++ /dev/null @@ -1,88 +0,0 @@ -package me.insanj.mayor; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -import org.bukkit.Bukkit; -import org.bukkit.entity.Villager; -import org.bukkit.inventory.ItemStack; - -import net.minecraft.server.v1_13_R2.MerchantRecipeList; -import org.bukkit.craftbukkit.v1_13_R2.entity.CraftVillager; - -// https://bukkit.org/threads/class-custom-vilagers-trade-items.338739/ -public class MayorVillager { - private final MayorPlugin plugin; - - private Object ev; - private MerchantRecipeList list; - - private static final String bukkitversion = Bukkit.getServer().getClass().getPackage().getName().substring(23); - - public MayorVillager(MayorPlugin plugin, Villager villager) { - this.plugin = plugin; - - list = new MerchantRecipeList(); - ev = ((CraftVillager) villager).getHandle(); - } - - public MayorVillager addRecipe(ItemStack slotOne, ItemStack slotTwo, ItemStack output) { - try { - Class mechantRecipe = Class.forName("net.minecraft.server."+bukkitversion+".MerchantRecipe"); - Method add = list.getClass().getDeclaredMethod("a", mechantRecipe); - Class nmsItemStack = Class.forName("net.minecraft.server." - + bukkitversion + ".ItemStack"); - Constructor merchantRecipeConstructor = mechantRecipe.getDeclaredConstructor(nmsItemStack,nmsItemStack,nmsItemStack); - Object merchantRecipeObj = merchantRecipeConstructor.newInstance(toNMSItemStack(slotOne), toNMSItemStack(slotTwo),toNMSItemStack(output)); - add.invoke(list,merchantRecipeObj); - } catch (Exception e) { - plugin.logError(e); - } - - return this; - } - - public MayorVillager addRecipe(ItemStack slotOne, ItemStack output) { - try { - Class mechantRecipe = Class.forName("net.minecraft.server."+bukkitversion+".MerchantRecipe"); - Method add = list.getClass().getDeclaredMethod("a", mechantRecipe); - - Class nmsItemStack = Class.forName("net.minecraft.server." + bukkitversion + ".ItemStack"); - - Constructor merchantRecipeConstructor = mechantRecipe.getDeclaredConstructor(nmsItemStack,nmsItemStack); - Object merchantRecipeObj = merchantRecipeConstructor.newInstance(toNMSItemStack(slotOne),toNMSItemStack(output)); - - add.invoke(list,merchantRecipeObj); - } catch (Exception e) { - plugin.logError(e); - } - - return this; - } - - private Object toNMSItemStack(ItemStack i) { - try { - Class craftItemstack = Class.forName("org.bukkit.craftbukkit."+ bukkitversion + ".inventory.CraftItemStack"); - Method nmsCopy = craftItemstack.getDeclaredMethod("asNMSCopy", ItemStack.class); - return nmsCopy.invoke(craftItemstack, i); - } catch (Exception e) { - plugin.logError(e); - } - - return null; - } - - public boolean finish() { - try { - Field f = ev.getClass().getDeclaredField("bu"); - f.setAccessible(true); - f.set(ev, list); - return true; - } catch (Exception e) { - plugin.logError(e); - return false; - } - } -} diff --git a/plugin/java/me/insanj/mayor/MayorVillagerHandler.java b/plugin/java/me/insanj/mayor/MayorVillagerHandler.java index 01e562f..1687654 100644 --- a/plugin/java/me/insanj/mayor/MayorVillagerHandler.java +++ b/plugin/java/me/insanj/mayor/MayorVillagerHandler.java @@ -64,6 +64,4 @@ public boolean spawnVillager(Location location) { boolean spawnSuccess = builtVillager.finish(); return spawnSuccess; }*/ - - } diff --git a/plugin/java/me/insanj/mayor/MayorVillagerTradeListener.java b/plugin/java/me/insanj/mayor/MayorVillagerTradeListener.java index 47bde39..82d4919 100644 --- a/plugin/java/me/insanj/mayor/MayorVillagerTradeListener.java +++ b/plugin/java/me/insanj/mayor/MayorVillagerTradeListener.java @@ -1,7 +1,8 @@ package me.insanj.mayor; +import org.bukkit.ChatColor; import org.bukkit.Material; -import org.bukkit.inventory.Inventory; + import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.EventHandler; @@ -9,10 +10,13 @@ import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryInteractEvent; import org.bukkit.event.inventory.InventoryType; +import org.bukkit.event.inventory.InventoryOpenEvent; +import org.bukkit.inventory.Inventory; import org.bukkit.inventory.MerchantInventory; import org.bukkit.inventory.MerchantRecipe; -import org.bukkit.event.inventory.InventoryOpenEvent; -import org.bukkit.ChatColor; + +import org.bukkit.craftbukkit.v1_13_R2.inventory.CraftInventoryMerchant; +import net.minecraft.server.v1_13_R2.InventoryMerchant; class MayorVillagerTradeListener implements Listener { private final MayorPlugin plugin; @@ -21,10 +25,11 @@ public MayorVillagerTradeListener(MayorPlugin plugin) { this.plugin = plugin; } - @EventHandler public void onVillagerReplenishTrade(VillagerReplenishTradeEvent event) { - plugin.getLogger().info("VillagerReplenishTradeEvent"); + if (event.getEntity().getName().equals("Mayor")) { + plugin.getServer().broadcastMessage(ChatColor.GREEN + "The Mayor has agreed to build a new structure!"); + } } /* @@ -51,15 +56,18 @@ public void onInventoryClick(InventoryClickEvent event) { } }*/ +/* @EventHandler public void onVillagerTrade(InventoryOpenEvent event) { if ((event.getInventory() != null) && (event.getInventory().getType() == InventoryType.MERCHANT)) { - MerchantRecipe villagerTradeMeta = (MerchantRecipe) event.getInventory(); - if (villagerTradeMeta.getResult().getType() == Material.POPPY) { + CraftInventoryMerchant merchantInventory = (CraftInventoryMerchant) event.getInventory(); + InventoryMerchant inventory = merchantInventory.getInventory(); + MerchantRecipe recipe = inventory.getRecipe().asBukkit(); + if (recipe.getResult().getType() == Material.POPPY) { plugin.getServer().broadcastMessage(ChatColor.GREEN + event.getPlayer().getName() + " & the Mayor have agreed to build a new structure!"); } } - } + }*/ /* @EventHandler