Require Bukkit 1.8 as minimum version

This commit is contained in:
filoghost 2020-06-05 19:59:13 +02:00
parent e303d6969c
commit 500a2699f3
8 changed files with 179 additions and 291 deletions

View File

@ -41,9 +41,9 @@
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>${spigot-api.version}</version>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>${bukkit.version}</version>
<scope>provided</scope>
</dependency>

View File

@ -45,6 +45,8 @@ import me.filoghost.chestcommands.task.RefreshMenusTask;
import me.filoghost.chestcommands.util.BukkitUtils;
import me.filoghost.chestcommands.util.CaseInsensitiveMap;
import me.filoghost.chestcommands.util.ErrorLogger;
import me.filoghost.chestcommands.util.Utils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
@ -84,6 +86,15 @@ public class ChestCommands extends JavaPlugin {
settings = new Settings(new PluginConfig(this, "config.yml"));
lang = new Lang(new PluginConfig(this, "lang.yml"));
if (!Utils.isClassLoaded("org.bukkit.inventory.ItemFlag")) { // ItemFlag was added in 1.8
if (Bukkit.getVersion().contains("(MC: 1.8)")) {
criticalShutdown("ChestCommands requires a more recent version of Bukkit 1.8 to run.");
} else {
criticalShutdown("ChestCommands requires at least Bukkit 1.8 to run.");
}
return;
}
if (!EconomyBridge.setupEconomy()) {
getLogger().warning("Vault with a compatible economy plugin was not found! Icons with a PRICE or commands that give money will not work.");
}
@ -266,7 +277,7 @@ public class ChestCommands extends JavaPlugin {
public static void closeAllMenus() {
for (Player player : BukkitUtils.getOnlinePlayers()) {
for (Player player : Bukkit.getOnlinePlayers()) {
if (player.getOpenInventory() != null) {
if (player.getOpenInventory().getTopInventory().getHolder() instanceof MenuInventoryHolder || player.getOpenInventory().getBottomInventory().getHolder() instanceof MenuInventoryHolder) {
player.closeInventory();
@ -316,4 +327,24 @@ public class ChestCommands extends JavaPlugin {
ChestCommands.lastReloadErrors = lastReloadErrors;
}
private static void criticalShutdown(String... errorMessage) {
String separator = "****************************************************************************";
StringBuffer output = new StringBuffer("\n ");
output.append("\n" + separator);
for (String line : errorMessage) {
output.append("\n " + line);
}
output.append("\n ");
output.append("\n This plugin has been disabled.");
output.append("\n" + separator);
output.append("\n ");
System.out.println(output);
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {}
instance.setEnabled(false);
}
}

View File

@ -14,7 +14,7 @@
*/
package me.filoghost.chestcommands.internal;
import me.filoghost.chestcommands.util.BukkitUtils;
import org.bukkit.Bukkit;
public class CachedGetters {
@ -27,7 +27,7 @@ public class CachedGetters {
if (lastOnlinePlayersRefresh == 0 || now - lastOnlinePlayersRefresh > 1000) {
// getOnlinePlayers() could be expensive if called frequently
lastOnlinePlayersRefresh = now;
onlinePlayers = BukkitUtils.getOnlinePlayers().size();
onlinePlayers = Bukkit.getOnlinePlayers().size();
}
return onlinePlayers;

View File

@ -14,13 +14,13 @@
*/
package me.filoghost.chestcommands.task;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import me.filoghost.chestcommands.internal.ExtendedIconMenu;
import me.filoghost.chestcommands.internal.MenuInventoryHolder;
import me.filoghost.chestcommands.util.BukkitUtils;
public class RefreshMenusTask implements Runnable {
@ -29,7 +29,7 @@ public class RefreshMenusTask implements Runnable {
@Override
public void run() {
for (Player player : BukkitUtils.getOnlinePlayers()) {
for (Player player : Bukkit.getOnlinePlayers()) {
InventoryView view = player.getOpenInventory();
if (view == null) {

View File

@ -1,45 +1,13 @@
package me.filoghost.chestcommands.util;
import com.google.common.collect.ImmutableList;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
public final class BukkitUtils {
private static final Method LEGACY_GET_ONLINE_PLAYERS;
static {
try {
Method method = Bukkit.class.getDeclaredMethod("getOnlinePlayers");
LEGACY_GET_ONLINE_PLAYERS = method.getReturnType() == Player[].class ? method : null;
} catch (NoSuchMethodException e) {
// This should NEVER happen!
throw new IllegalStateException("Missing Bukkit.getOnlinePlayers() method!");
}
}
private BukkitUtils() {}
private BukkitUtils() {
}
public static Collection<? extends Player> getOnlinePlayers() {
try {
if (LEGACY_GET_ONLINE_PLAYERS == null) {
return Bukkit.getOnlinePlayers();
} else {
Player[] playersArray = (Player[]) LEGACY_GET_ONLINE_PLAYERS.invoke(null);
return ImmutableList.copyOf(playersArray);
}
} catch (Exception e) {
e.printStackTrace();
return Collections.emptyList();
}
}
public static String addYamlExtension(String input) {
if (input == null) {

View File

@ -24,60 +24,11 @@ import org.bukkit.inventory.meta.ItemMeta;
import me.filoghost.chestcommands.exception.FormatException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public final class ItemUtils {
private static final boolean USE_ITEM_FLAGS_API;
private static final boolean USE_ITEM_FLAGS_REFLECTION;
// Reflection stuff
private static Class<?> nbtTagCompoundClass;
private static Class<?> nbtTagListClass;
private static Class<?> nmsItemstackClass;
private static Method asNmsCopyMethod;
private static Method asCraftMirrorMethod;
private static Method hasTagMethod;
private static Method getTagMethod;
private static Method setTagMethod;
private static Method nbtSetMethod;
static {
if (Utils.isClassLoaded("org.bukkit.inventory.ItemFlag")) {
// We can use the new Bukkit API (1.8.3+)
USE_ITEM_FLAGS_API = true;
USE_ITEM_FLAGS_REFLECTION = false;
} else {
USE_ITEM_FLAGS_API = false;
// Try to get the NMS methods and classes
boolean success;
try {
nbtTagCompoundClass = NMSUtils.getNMSClass("NBTTagCompound");
nbtTagListClass = NMSUtils.getNMSClass("NBTTagList");
nmsItemstackClass = NMSUtils.getNMSClass("ItemStack");
asNmsCopyMethod = NMSUtils.getCraftBukkitClass("inventory.CraftItemStack").getMethod("asNMSCopy", ItemStack.class);
asCraftMirrorMethod = NMSUtils.getCraftBukkitClass("inventory.CraftItemStack").getMethod("asCraftMirror", nmsItemstackClass);
hasTagMethod = nmsItemstackClass.getMethod("hasTag");
getTagMethod = nmsItemstackClass.getMethod("getTag");
setTagMethod = nmsItemstackClass.getMethod("setTag", nbtTagCompoundClass);
nbtSetMethod = nbtTagCompoundClass.getMethod("set", String.class, NMSUtils.getNMSClass("NBTBase"));
success = true;
} catch (Exception e) {
new IllegalStateException("Could not enable the attribute remover for this version." +
"Attributes will show up on items.", e).printStackTrace();
success = false;
}
USE_ITEM_FLAGS_REFLECTION = success;
}
}
private ItemUtils() {
}
@ -86,46 +37,13 @@ public final class ItemUtils {
return null;
}
if (USE_ITEM_FLAGS_API) {
ItemMeta meta = item.getItemMeta();
if (Utils.isNullOrEmpty(meta.getItemFlags())) {
// Add them only if necessary
// Add them only if no flag was already set
meta.addItemFlags(ItemFlag.values());
item.setItemMeta(meta);
}
return item;
} else if (USE_ITEM_FLAGS_REFLECTION) {
try {
Object nmsItemstack = asNmsCopyMethod.invoke(null, item);
if (nmsItemstack == null) {
return item;
}
Object nbtCompound;
if ((Boolean) hasTagMethod.invoke(nmsItemstack)) {
nbtCompound = getTagMethod.invoke(nmsItemstack);
} else {
nbtCompound = nbtTagCompoundClass.newInstance();
setTagMethod.invoke(nmsItemstack, nbtCompound);
}
if (nbtCompound == null) {
return item;
}
Object nbtList = nbtTagListClass.newInstance();
nbtSetMethod.invoke(nbtCompound, "AttributeModifiers", nbtList);
return (ItemStack) asCraftMirrorMethod.invoke(null, nmsItemstack);
} catch (Throwable t) {
// Ignore
}
}
// On failure just return the item
return item;
}
public static Color parseColor(String input) throws FormatException {

View File

@ -1,29 +0,0 @@
package me.filoghost.chestcommands.util;
import org.bukkit.Bukkit;
public final class NMSUtils {
private static final String NMS_VERSION;
static {
String packageName = Bukkit.getServer().getClass().getPackage().getName();
NMS_VERSION = packageName.substring(packageName.lastIndexOf('.') + 1);
}
private NMSUtils() {
}
public static String getNMSVersion() {
return NMS_VERSION;
}
public static Class<?> getNMSClass(String name) throws ClassNotFoundException {
return Class.forName("net.minecraft.server." + NMSUtils.getNMSVersion() + "." + name);
}
public static Class<?> getCraftBukkitClass(String name) throws ClassNotFoundException {
return Class.forName("org.bukkit.craftbukkit." + NMSUtils.getNMSVersion() + "." + name);
}
}

View File

@ -22,7 +22,7 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.test.skip>true</maven.test.skip>
<spigot-api.version>1.8.8-R0.1-SNAPSHOT</spigot-api.version>
<bukkit.version>1.8-R0.1-SNAPSHOT</bukkit.version>
</properties>
<modules>