Skip to content

Commit

Permalink
Added compatibility for Bukkit 1.7.2-R0.3
Browse files Browse the repository at this point in the history
Updated for Bukkit 1.9.2-R0.1-SNAPSHOT
  • Loading branch information
BenWoodworth committed Apr 5, 2016
1 parent 7be0a98 commit 56086d7
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 30 deletions.
14 changes: 12 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>co.kepler.fastcraftplus</groupId>
<artifactId>fastcraftplus</artifactId>
<version>0.10</version>
<version>0.11</version>

<build>
<plugins>
Expand Down Expand Up @@ -34,14 +34,24 @@
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>

<repository>
<id>sk89q-repo</id>
<url>http://maven.sk89q.com/repo/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.8-R0.1-SNAPSHOT</version>
<version>1.7.2-R0.3</version>
<scope>provided</scope>

<!-- Bukkit versions:
<version>1.7.2-R0.3</version>
<version>1.9.2-R0.1-SNAPSHOT</version>
-->
</dependency>
</dependencies>
</project>
76 changes: 76 additions & 0 deletions src/main/java/co/kepler/fastcraftplus/BukkitUtil.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/co/kepler/fastcraftplus/compat/Compat.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import co.kepler.fastcraftplus.recipes.FastRecipe;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Recipe;

import java.util.Set;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,7 +48,6 @@ public Set<FastRecipe> getRecipes(Player player) {
}

// Return a set of FastRecipes
System.out.println("--------> " + result.size());
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/co/kepler/fastcraftplus/config/Language.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ItemStack> results = recipe.craft(gui.getPlayer());
Set<ItemStack> results = recipe.craft(gui);
if (results == null) {
gui.updateLayout();
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -290,4 +302,9 @@ public ListIterator<ItemStack> iterator() {
public ListIterator<ItemStack> iterator(int i) {
return craftingInv.iterator(i);
}

// @Override
public Location getLocation() {
return location;
}
}
8 changes: 5 additions & 3 deletions src/main/java/co/kepler/fastcraftplus/recipes/FastRecipe.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<ItemStack> craft(Player player) {
public Set<ItemStack> craft(GUIFastCraft gui) {
Player player = gui.getPlayer();
ItemStack[] contents = player.getInventory().getContents();

// Remove items, and return false if unable to craft
Expand All @@ -148,7 +150,7 @@ public Set<ItemStack> 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;
}
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/co/kepler/fastcraftplus/recipes/RecipeUtil.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -32,8 +34,7 @@ public class RecipeUtil {
private static Map<Material, Achievement> 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 + ".";

Expand Down Expand Up @@ -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);
Expand All @@ -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),
Expand Down

0 comments on commit 56086d7

Please sign in to comment.