Improve Bukkit version comparison, fixes #516

This commit is contained in:
BuildTools 2018-11-28 17:33:44 -05:00
parent 9a6465e6a4
commit c06879b2ec
3 changed files with 146 additions and 111 deletions

View File

@ -70,29 +70,29 @@ public class NpcEffectThread implements Runnable {
*/ */
private void showEffect(Player player, NPC npc, String effectType) { private void showEffect(Player player, NPC npc, String effectType) {
try { try {
if (Quests.bukkitVersion >= 1132) { if (Quests.bukkitVersion.contains("1.13.2")) {
showEffect_1_13_R2(player, npc, effectType); showEffect_1_13_R2(player, npc, effectType);
} else if (Quests.bukkitVersion >= 113) { } else if (Quests.bukkitVersion.contains("1.13")) {
showEffect_1_13_R1(player, npc, effectType); showEffect_1_13_R1(player, npc, effectType);
} else if (Quests.bukkitVersion >= 112 ) { } else if (Quests.bukkitVersion.contains("1.12")) {
showEffect_1_12_R1(player, npc, effectType); showEffect_1_12_R1(player, npc, effectType);
} else if (Quests.bukkitVersion >= 111) { } else if (Quests.bukkitVersion.contains("1.11")) {
showEffect_1_11_R1(player, npc, effectType); showEffect_1_11_R1(player, npc, effectType);
} else if (Quests.bukkitVersion >= 110) { } else if (Quests.bukkitVersion.contains("1.10")) {
showEffect_1_10_R1(player, npc, effectType); showEffect_1_10_R1(player, npc, effectType);
} else if (Quests.bukkitVersion >= 194) { } else if (Quests.bukkitVersion.contains("1.9.4")) {
showEffect_1_9_R2(player, npc, effectType); showEffect_1_9_R2(player, npc, effectType);
} else if (Quests.bukkitVersion >= 19) { } else if (Quests.bukkitVersion.contains("1.9")) {
showEffect_1_9_R1(player, npc, effectType); showEffect_1_9_R1(player, npc, effectType);
} else if (Quests.bukkitVersion >= 184) { } else if (Quests.bukkitVersion.contains("1.8.4")) {
showEffect_1_8_R3(player, npc, effectType); showEffect_1_8_R3(player, npc, effectType);
} else if (Quests.bukkitVersion >= 183) { } else if (Quests.bukkitVersion.contains("1.8.3")) {
showEffect_1_8_R2(player, npc, effectType); showEffect_1_8_R2(player, npc, effectType);
} else if (Quests.bukkitVersion >= 18) { } else if (Quests.bukkitVersion.contains("1.8")) {
showEffect_1_8_R1(player, npc, effectType); showEffect_1_8_R1(player, npc, effectType);
} else if (Quests.bukkitVersion >= 1710) { } else if (Quests.bukkitVersion.contains("1.7.10")) {
showEffect_R4(player, npc, effectType); showEffect_R4(player, npc, effectType);
} else if (Quests.bukkitVersion >= 179) { } else if (Quests.bukkitVersion.contains("1.7.9")) {
showEffect_R3(player, npc, effectType); showEffect_R3(player, npc, effectType);
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -111,7 +111,7 @@ import ro.nicuch.citizensbooks.CitizensBooksPlugin;
public class Quests extends JavaPlugin implements ConversationAbandonedListener { public class Quests extends JavaPlugin implements ConversationAbandonedListener {
public static int bukkitVersion = 0; public static String bukkitVersion = "0";
// Dependencies // Dependencies
public static Economy economy = null; public static Economy economy = null;
public static Permission permission = null; public static Permission permission = null;
@ -174,7 +174,7 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
@Override @Override
public void onEnable() { public void onEnable() {
bukkitVersion = Integer.valueOf(Bukkit.getServer().getBukkitVersion().split("-")[0].replace(".", "")); bukkitVersion = Bukkit.getServer().getBukkitVersion().split("-")[0];
pListener = new PlayerListener(this); pListener = new PlayerListener(this);
effListener = new NpcEffectThread(this); effListener = new NpcEffectThread(this);
npcListener = new NpcListener(this); npcListener = new NpcListener(this);
@ -268,7 +268,7 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
if (!outDir.exists()) { if (!outDir.exists()) {
if (!outDir.mkdirs()) { if (!outDir.mkdirs()) {
getLogger().log(Level.SEVERE, "Failed to make directories for " + outFile.getName() + "(canWrite= " + outFile.canWrite()); getLogger().log(Level.SEVERE, "Failed to make directories for " + outFile.getName() + " (canWrite= " + outFile.canWrite() + ")");
} }
} }

View File

@ -13,6 +13,7 @@
package me.blackvein.quests.util; package me.blackvein.quests.util;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import me.blackvein.quests.Quester; import me.blackvein.quests.Quester;
import me.blackvein.quests.Quests; import me.blackvein.quests.Quests;
@ -24,101 +25,135 @@ import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class LocaleQuery { public class LocaleQuery {
private static Class<?> craftMagicNumbers = null; private static Class<?> craftMagicNumbers = null;
private final Quests plugin; private final Quests plugin;
static boolean oldVersion = isBelow113();
public LocaleQuery(Quests plugin){ public LocaleQuery(Quests plugin){
this.plugin = plugin; this.plugin = plugin;
setup(); setup();
} }
public void sendMessage(Player player, String message, Material material) { public void sendMessage(Player player, String message, Material material) {
if (plugin.translateItems) { if (plugin.translateItems) {
String key = queryByType(material); String key = queryByType(material);
if (Quests.bukkitVersion <= 1122) { if (oldVersion) {
if (key.startsWith("tile.") || key.startsWith("item.")) { if (key.startsWith("tile.") || key.startsWith("item.")) {
key = key + ".name"; key = key + ".name";
} }
} }
if (key != null) { if (key != null) {
String msg = message.replace("<item>", "\",{\"translate\":\"" + key + "\"},\""); String msg = message.replace("<item>", "\",{\"translate\":\"" + key + "\"},\"");
player.chat("/tellraw " + player.getName() + " [\"" + msg + "\"]"); player.chat("/tellraw " + player.getName() + " [\"" + msg + "\"]");
return; return;
} }
} }
player.sendMessage(message.replace("<item>", Quester.prettyItemString(material.name()))); player.sendMessage(message.replace("<item>", Quester.prettyItemString(material.name())));
} }
public void sendMessage(Player player, String message, Material material, Enchantment enchantment) { public void sendMessage(Player player, String message, Material material, Enchantment enchantment) {
if (plugin.translateItems) { if (plugin.translateItems) {
String key = queryByType(material); String key = queryByType(material);
if (Quests.bukkitVersion <= 1122) { if (oldVersion) {
if (key.startsWith("tile.") || key.startsWith("item.")) { if (key.startsWith("tile.") || key.startsWith("item.")) {
key = key + ".name"; key = key + ".name";
} }
} }
String key2 = ""; String key2 = "";
if (Quests.bukkitVersion <= 1122) { if (oldVersion) {
key2 = "enchantment." + MiscUtil.getProperEnchantmentName(enchantment).replace("_", ".") key2 = "enchantment." + MiscUtil.getProperEnchantmentName(enchantment).replace("_", ".")
.replace("environmental", "all").replace("protection", "protect"); .replace("environmental", "all").replace("protection", "protect");
} else { } else {
key2 = "enchantment.minecraft." + enchantment.toString().toLowerCase(); key2 = "enchantment.minecraft." + enchantment.toString().toLowerCase();
} }
if (key != null && !key.equals("")) { if (key != null && !key.equals("")) {
String msg = message.replace("<item>", "\",{\"translate\":\"" + key + "\"},\"") String msg = message.replace("<item>", "\",{\"translate\":\"" + key + "\"},\"")
.replace("<enchantment>", "\",{\"translate\":\"" + key2 + "\"},\""); .replace("<enchantment>", "\",{\"translate\":\"" + key2 + "\"},\"");
player.chat("/tellraw " + player.getName() + " [\"" + msg + "\"]"); player.chat("/tellraw " + player.getName() + " [\"" + msg + "\"]");
return; return;
} }
} }
player.sendMessage(message.replace("<item>", Quester.prettyItemString(material.name())) player.sendMessage(message.replace("<item>", Quester.prettyItemString(material.name()))
.replace("<enchantment>", Quester.prettyEnchantmentString(enchantment))); .replace("<enchantment>", Quester.prettyEnchantmentString(enchantment)));
} }
public void sendMessage(Player player, String message, EntityType type) { public void sendMessage(Player player, String message, EntityType type) {
if (plugin.translateItems) { if (plugin.translateItems) {
String key = ""; String key = "";
if (Quests.bukkitVersion <= 1122) { if (oldVersion) {
key = "entity." + MiscUtil.getProperMobName(type) + ".name"; key = "entity." + MiscUtil.getProperMobName(type) + ".name";
} else { } else {
key = "entity.minecraft." + type.toString().toLowerCase(); key = "entity.minecraft." + type.toString().toLowerCase();
} }
if (!key.equals("")) { if (!key.equals("")) {
String msg = message.replace("<mob>", "\",{\"translate\":\"" + key + "\"},\""); String msg = message.replace("<mob>", "\",{\"translate\":\"" + key + "\"},\"");
player.chat("/tellraw " + player.getName() + " [\"" + msg + "\"]"); player.chat("/tellraw " + player.getName() + " [\"" + msg + "\"]");
return; return;
} }
} }
player.sendMessage(message.replace("<mob>", Quester.prettyMobString(type))); player.sendMessage(message.replace("<mob>", Quester.prettyMobString(type)));
} }
/** /**
* Creates a new LocaleQuery of the specified material * Creates a new LocaleQuery of the specified material
* @param material the item with the material * @param material the item with the material
* @return the new LocaleQuery * @return the new LocaleQuery
* @throws IllegalArgumentException if an item with that material could not be found * @throws IllegalArgumentException if an item with that material could not be found
*/ */
public String queryByType(Material material) throws IllegalArgumentException{ public String queryByType(Material material) throws IllegalArgumentException{
try { try {
Object item = MethodUtils.invokeExactStaticMethod(craftMagicNumbers,"getItem", material); Object item = MethodUtils.invokeExactStaticMethod(craftMagicNumbers,"getItem", material);
if (item == null) { if (item == null) {
throw new IllegalArgumentException("An item with that material could not be found! (Perhaps you have specified a block?)"); throw new IllegalArgumentException("An item with that material could not be found! (Perhaps you have specified a block?)");
} }
String name = (String) MethodUtils.invokeExactMethod(item, "getName"); String name = (String) MethodUtils.invokeExactMethod(item, "getName");
return name; return name;
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
} }
return null; return null;
} }
public void setup() { public void setup() {
String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
try { try {
craftMagicNumbers = Class.forName("org.bukkit.craftbukkit.{v}.util.CraftMagicNumbers".replace("{v}", version)); craftMagicNumbers = Class.forName("org.bukkit.craftbukkit.{v}.util.CraftMagicNumbers".replace("{v}", version));
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public static boolean isBelow113() {
String ver = Quests.bukkitVersion;
if (Quests.bukkitVersion.matches("^[0-9]+$")) {
switch(ver) {
case "1.12" :
return true;
case "1.11" :
return true;
case "1.10" :
return true;
case "1.9.4" :
return true;
case "1.9" :
return true;
case "1.8.4" :
return true;
case "1.8.3" :
return true;
case "1.8" :
return true;
case "1.7.10" :
return true;
case "1.7.9" :
return true;
default:
// Bukkit version is 1.13+
return false;
}
}
Bukkit.getLogger().severe("Invalid Bukkit version " + ver);
return false;
}
} }