From 56086d759eced4672d861ec449c8f2462b0d0855 Mon Sep 17 00:00:00 2001 From: BenWoodworth Date: Tue, 5 Apr 2016 03:04:08 -0400 Subject: [PATCH] Added compatibility for Bukkit 1.7.2-R0.3 Updated for Bukkit 1.9.2-R0.1-SNAPSHOT --- pom.xml | 14 +++- .../co/kepler/fastcraftplus/BukkitUtil.java | 76 +++++++++++++++++++ .../fastcraftplus/api/gui/GUIItemBuilder.java | 18 ++--- .../kepler/fastcraftplus/compat/Compat.java | 1 - .../fastcraftplus/compat/Compat_Bukkit.java | 2 - .../compat/Compat_FastCraftPlus.java | 1 - .../kepler/fastcraftplus/config/Language.java | 4 +- .../craftgui/GUIButtonRecipe.java | 2 +- .../fastcraftplus/craftgui/GUIFastCraft.java | 9 +++ .../recipes/CraftingInvWrapper.java | 19 ++++- .../fastcraftplus/recipes/FastRecipe.java | 8 +- .../fastcraftplus/recipes/RecipeUtil.java | 16 ++-- 12 files changed, 140 insertions(+), 30 deletions(-) create mode 100644 src/main/java/co/kepler/fastcraftplus/BukkitUtil.java diff --git a/pom.xml b/pom.xml index d3927d84..5d994f37 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ co.kepler.fastcraftplus fastcraftplus - 0.10 + 0.11 @@ -34,14 +34,24 @@ spigot-repo https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + sk89q-repo + http://maven.sk89q.com/repo/ + org.bukkit bukkit - 1.8-R0.1-SNAPSHOT + 1.7.2-R0.3 provided + + \ No newline at end of file diff --git a/src/main/java/co/kepler/fastcraftplus/BukkitUtil.java b/src/main/java/co/kepler/fastcraftplus/BukkitUtil.java new file mode 100644 index 00000000..bcd2f0cd --- /dev/null +++ b/src/main/java/co/kepler/fastcraftplus/BukkitUtil.java @@ -0,0 +1,76 @@ +package co.kepler.fastcraftplus; + +import org.bukkit.Bukkit; +import org.bukkit.inventory.meta.ItemMeta; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * Utility class for Bukkit. + */ +public class BukkitUtil { + private static String version = null; + private static Boolean supportsItemFlags = null; + + /** + * Get the server's version String. + * + * @return Return the server's version String. + */ + public static String serverVersion() { + if (version != null) return version; + version = Bukkit.getServer().getClass().getPackage().getName(); + return version = version.substring(version.lastIndexOf('.') + 1); + } + + /** + * ItemFlag replacement for backwards compatibility with old bukkit versions. + */ + public static class ItemFlag { + public static Enum + HIDE_ATTRIBUTES, + HIDE_ENCHANTS, + HIDE_DESTROYS, + HIDE_PLACED_ON, + HIDE_POTION_EFFECTS, + HIDE_UNBREAKABLE; + + private static boolean supportsItemFlags; + public static Class classItemFlag; + public static Method addItemFlags; + + static { + try { + classItemFlag = Class.forName("org.bukkit.inventory.ItemFlag"); + addItemFlags = ItemMeta.class.getMethod("addItemFlags"); + + HIDE_ATTRIBUTES = Enum.valueOf(classItemFlag, "HIDE_ATTRIBUTES"); + HIDE_ENCHANTS = Enum.valueOf(classItemFlag, "HIDE_ENCHANTS"); + HIDE_DESTROYS = Enum.valueOf(classItemFlag, "HIDE_DESTROYS"); + HIDE_PLACED_ON = Enum.valueOf(classItemFlag, "HIDE_PLACED_ON"); + HIDE_POTION_EFFECTS = Enum.valueOf(classItemFlag, "HIDE_POTION_EFFECTS"); + HIDE_UNBREAKABLE = Enum.valueOf(classItemFlag, "HIDE_UNBREAKABLE"); + + supportsItemFlags = true; + } catch (Exception e) { + supportsItemFlags = false; + } + } + + /** + * Add item flags to an ItemMeta, if possible. + * + * @param meta The ItemMeta to add flags to. + * @param itemFlag The flags to add. + */ + public static void addItemFlags(ItemMeta meta, Enum... itemFlag) { + if (!supportsItemFlags) return; + try { + addItemFlags.invoke(meta, (Object) itemFlag); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + } + } +} diff --git a/src/main/java/co/kepler/fastcraftplus/api/gui/GUIItemBuilder.java b/src/main/java/co/kepler/fastcraftplus/api/gui/GUIItemBuilder.java index 71d53ebf..81491ab6 100644 --- a/src/main/java/co/kepler/fastcraftplus/api/gui/GUIItemBuilder.java +++ b/src/main/java/co/kepler/fastcraftplus/api/gui/GUIItemBuilder.java @@ -1,8 +1,8 @@ package co.kepler.fastcraftplus.api.gui; +import co.kepler.fastcraftplus.BukkitUtil; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; -import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.material.MaterialData; @@ -56,13 +56,13 @@ public GUIItemBuilder(MaterialData materialData) { public ItemStack build() { if (hideInfo) { // Add item tags to hide the item's info. - meta.addItemFlags( - ItemFlag.HIDE_ATTRIBUTES, - ItemFlag.HIDE_ENCHANTS, - ItemFlag.HIDE_DESTROYS, - ItemFlag.HIDE_PLACED_ON, - ItemFlag.HIDE_POTION_EFFECTS, - ItemFlag.HIDE_UNBREAKABLE + BukkitUtil.ItemFlag.addItemFlags(meta, + BukkitUtil.ItemFlag.HIDE_ATTRIBUTES, + BukkitUtil.ItemFlag.HIDE_ENCHANTS, + BukkitUtil.ItemFlag.HIDE_DESTROYS, + BukkitUtil.ItemFlag.HIDE_PLACED_ON, + BukkitUtil.ItemFlag.HIDE_POTION_EFFECTS, + BukkitUtil.ItemFlag.HIDE_UNBREAKABLE ); } if (glowing) { @@ -73,7 +73,7 @@ public ItemStack build() { // Only hide the info if hideInfo is false if (!hideInfo) { - meta.addItemFlags(ItemFlag.HIDE_ENCHANTS); + BukkitUtil.ItemFlag.addItemFlags(meta, BukkitUtil.ItemFlag.HIDE_ENCHANTS); } } base.setItemMeta(meta); diff --git a/src/main/java/co/kepler/fastcraftplus/compat/Compat.java b/src/main/java/co/kepler/fastcraftplus/compat/Compat.java index b6b31928..b4c22bd6 100644 --- a/src/main/java/co/kepler/fastcraftplus/compat/Compat.java +++ b/src/main/java/co/kepler/fastcraftplus/compat/Compat.java @@ -2,7 +2,6 @@ import co.kepler.fastcraftplus.recipes.FastRecipe; import org.bukkit.entity.Player; -import org.bukkit.inventory.Recipe; import java.util.Set; diff --git a/src/main/java/co/kepler/fastcraftplus/compat/Compat_Bukkit.java b/src/main/java/co/kepler/fastcraftplus/compat/Compat_Bukkit.java index 3c90c8c2..5229df0e 100644 --- a/src/main/java/co/kepler/fastcraftplus/compat/Compat_Bukkit.java +++ b/src/main/java/co/kepler/fastcraftplus/compat/Compat_Bukkit.java @@ -3,7 +3,6 @@ import co.kepler.fastcraftplus.recipes.FastRecipe; import co.kepler.fastcraftplus.recipes.Ingredient; import co.kepler.fastcraftplus.recipes.RecipeUtil; -import com.sun.jmx.mbeanserver.Util; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -49,7 +48,6 @@ public Set getRecipes(Player player) { } // Return a set of FastRecipes - System.out.println("--------> " + result.size()); return result; } diff --git a/src/main/java/co/kepler/fastcraftplus/compat/Compat_FastCraftPlus.java b/src/main/java/co/kepler/fastcraftplus/compat/Compat_FastCraftPlus.java index d52d9972..c5d00e72 100644 --- a/src/main/java/co/kepler/fastcraftplus/compat/Compat_FastCraftPlus.java +++ b/src/main/java/co/kepler/fastcraftplus/compat/Compat_FastCraftPlus.java @@ -5,7 +5,6 @@ import co.kepler.fastcraftplus.recipes.RecipeUtil; import co.kepler.fastcraftplus.recipes.custom.CustomRecipe; import org.bukkit.entity.Player; -import org.bukkit.inventory.Recipe; import java.util.HashSet; import java.util.Set; diff --git a/src/main/java/co/kepler/fastcraftplus/config/Language.java b/src/main/java/co/kepler/fastcraftplus/config/Language.java index 780ba20c..347c9548 100644 --- a/src/main/java/co/kepler/fastcraftplus/config/Language.java +++ b/src/main/java/co/kepler/fastcraftplus/config/Language.java @@ -12,7 +12,6 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.nio.file.Files; import java.util.Collections; import java.util.HashMap; @@ -44,8 +43,7 @@ public Language(String language) { InputStream resStream = fastCraft.getResource(resPath); if (resStream != null) { // Load from internal lang file - InputStreamReader reader = new InputStreamReader(resStream); - lang = YamlConfiguration.loadConfiguration(reader); + lang = YamlConfiguration.loadConfiguration(resStream); } else { try { // Load from custom lang file diff --git a/src/main/java/co/kepler/fastcraftplus/craftgui/GUIButtonRecipe.java b/src/main/java/co/kepler/fastcraftplus/craftgui/GUIButtonRecipe.java index 7686d1bd..55a4b644 100644 --- a/src/main/java/co/kepler/fastcraftplus/craftgui/GUIButtonRecipe.java +++ b/src/main/java/co/kepler/fastcraftplus/craftgui/GUIButtonRecipe.java @@ -122,7 +122,7 @@ public boolean onClick(Layout layout, InventoryClickEvent invEvent) { if (ignoreClicks.contains(invEvent.getClick())) return false; // Craft the items, and return if unsuccessful - Set results = recipe.craft(gui.getPlayer()); + Set results = recipe.craft(gui); if (results == null) { gui.updateLayout(); return false; diff --git a/src/main/java/co/kepler/fastcraftplus/craftgui/GUIFastCraft.java b/src/main/java/co/kepler/fastcraftplus/craftgui/GUIFastCraft.java index 6fd0a821..41c9bcdb 100644 --- a/src/main/java/co/kepler/fastcraftplus/craftgui/GUIFastCraft.java +++ b/src/main/java/co/kepler/fastcraftplus/craftgui/GUIFastCraft.java @@ -222,6 +222,15 @@ public Player getPlayer() { return player; } + /** + * Get the location of this GUI. + * + * @return Returns the location of this GUI. + */ + public Location getLocation() { + return location; + } + /** * Show a tab in the GUI. * diff --git a/src/main/java/co/kepler/fastcraftplus/recipes/CraftingInvWrapper.java b/src/main/java/co/kepler/fastcraftplus/recipes/CraftingInvWrapper.java index b95da710..3d3844bb 100644 --- a/src/main/java/co/kepler/fastcraftplus/recipes/CraftingInvWrapper.java +++ b/src/main/java/co/kepler/fastcraftplus/recipes/CraftingInvWrapper.java @@ -1,6 +1,7 @@ package co.kepler.fastcraftplus.recipes; import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; @@ -19,13 +20,14 @@ public class CraftingInvWrapper implements CraftingInventory { private final Inventory craftingInv; private Recipe recipe; + private Location location; /** * Create a new instance of CraftingInvWrapper. * * @param owner The owner of the crafting inventory. */ - public CraftingInvWrapper(InventoryHolder owner) { + public CraftingInvWrapper(InventoryHolder owner, Location location) { craftingInv = Bukkit.createInventory(owner, InventoryType.WORKBENCH); } @@ -154,6 +156,16 @@ public void setContents(ItemStack[] itemStacks) throws IllegalArgumentException craftingInv.setContents(itemStacks); } + // @Override + public ItemStack[] getStorageContents() { + return craftingInv.getContents(); + } + + // @Override + public void setStorageContents(ItemStack[] itemStacks) throws IllegalArgumentException { + craftingInv.setContents(itemStacks); + } + @Override @Deprecated @SuppressWarnings("deprecation") @@ -290,4 +302,9 @@ public ListIterator iterator() { public ListIterator iterator(int i) { return craftingInv.iterator(i); } + + // @Override + public Location getLocation() { + return location; + } } diff --git a/src/main/java/co/kepler/fastcraftplus/recipes/FastRecipe.java b/src/main/java/co/kepler/fastcraftplus/recipes/FastRecipe.java index cc16f286..1fe10e93 100644 --- a/src/main/java/co/kepler/fastcraftplus/recipes/FastRecipe.java +++ b/src/main/java/co/kepler/fastcraftplus/recipes/FastRecipe.java @@ -1,5 +1,6 @@ package co.kepler.fastcraftplus.recipes; +import co.kepler.fastcraftplus.craftgui.GUIFastCraft; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -135,10 +136,11 @@ public boolean removeIngredients(ItemStack[] items) { /** * Craft this FastRecipe. * - * @param player The player crafting this recipe. + * @param gui The gui this recipe is being crafted in. * @return Returns true if the ingredients were removed from the player's inventory. */ - public Set craft(Player player) { + public Set craft(GUIFastCraft gui) { + Player player = gui.getPlayer(); ItemStack[] contents = player.getInventory().getContents(); // Remove items, and return false if unable to craft @@ -148,7 +150,7 @@ public Set craft(Player player) { ItemStack[] matrix = getMatrix(); Recipe recipe = getRecipe(); if (matrix != null && recipe != null) { - if (!RecipeUtil.callCraftItemEvent(player, recipe, matrix, getDisplayResult())) { + if (!RecipeUtil.callCraftItemEvent(player, recipe, matrix, getDisplayResult(), gui.getLocation())) { // If crafting cancelled return null; } diff --git a/src/main/java/co/kepler/fastcraftplus/recipes/RecipeUtil.java b/src/main/java/co/kepler/fastcraftplus/recipes/RecipeUtil.java index 9c17c7fb..6db1ea22 100644 --- a/src/main/java/co/kepler/fastcraftplus/recipes/RecipeUtil.java +++ b/src/main/java/co/kepler/fastcraftplus/recipes/RecipeUtil.java @@ -1,8 +1,10 @@ package co.kepler.fastcraftplus.recipes; +import co.kepler.fastcraftplus.BukkitUtil; import co.kepler.fastcraftplus.FastCraft; import org.bukkit.Achievement; import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.Event; @@ -32,8 +34,7 @@ public class RecipeUtil { private static Map craftingAchievements; static { - String version = Bukkit.getServer().getClass().getPackage().getName(); - version = version.substring(version.lastIndexOf('.') + 1); + String version = BukkitUtil.serverVersion(); String nms = "net.minecraft.server." + version + "."; String cb = "org.bukkit.craftbukkit." + version + "."; @@ -268,13 +269,13 @@ public static ItemStack[] getRecipeMatrix(Recipe recipe) { * @param result The item in the result slot of the crafting table. * @return Returns the called event. */ - public static PrepareItemCraftEvent callPrepareItemCraftEvent(Player player, Recipe recipe, - ItemStack[] matrix, ItemStack result) { + public static PrepareItemCraftEvent callPrepareItemCraftEvent(Player player, Recipe recipe, ItemStack[] matrix, + ItemStack result, Location location) { assert player != null : "Player must not be null"; assert recipe != null : "Recipe must not be null"; assert matrix != null : "Matrix must not be null"; - CraftingInvWrapper inv = new CraftingInvWrapper(player); + CraftingInvWrapper inv = new CraftingInvWrapper(player, location); inv.setRecipe(recipe); inv.setMatrix(matrix); inv.setResult(result); @@ -290,12 +291,13 @@ public static PrepareItemCraftEvent callPrepareItemCraftEvent(Player player, Rec * * @return Returns false if the event was cancelled. */ - public static boolean callCraftItemEvent(Player player, Recipe recipe, ItemStack[] matrix, ItemStack result) { + public static boolean callCraftItemEvent(Player player, Recipe recipe, ItemStack[] matrix, + ItemStack result, Location location) { assert player != null : "Player must not be null"; assert recipe != null : "Recipe must not be null"; assert matrix != null : "Matrix must not be null"; - CraftingInvWrapper inv = new CraftingInvWrapper(player); + CraftingInvWrapper inv = new CraftingInvWrapper(player, location); inv.setResult(recipe.getResult()); CraftItemEvent event = new CraftItemEvent(recipe, inv.getView(player),