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) {
try {
if (Quests.bukkitVersion >= 1132) {
if (Quests.bukkitVersion.contains("1.13.2")) {
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);
} else if (Quests.bukkitVersion >= 112 ) {
} else if (Quests.bukkitVersion.contains("1.12")) {
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);
} else if (Quests.bukkitVersion >= 110) {
} else if (Quests.bukkitVersion.contains("1.10")) {
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);
} else if (Quests.bukkitVersion >= 19) {
} else if (Quests.bukkitVersion.contains("1.9")) {
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);
} else if (Quests.bukkitVersion >= 183) {
} else if (Quests.bukkitVersion.contains("1.8.3")) {
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);
} else if (Quests.bukkitVersion >= 1710) {
} else if (Quests.bukkitVersion.contains("1.7.10")) {
showEffect_R4(player, npc, effectType);
} else if (Quests.bukkitVersion >= 179) {
} else if (Quests.bukkitVersion.contains("1.7.9")) {
showEffect_R3(player, npc, effectType);
}
} catch (Exception e) {

View File

@ -111,7 +111,7 @@ import ro.nicuch.citizensbooks.CitizensBooksPlugin;
public class Quests extends JavaPlugin implements ConversationAbandonedListener {
public static int bukkitVersion = 0;
public static String bukkitVersion = "0";
// Dependencies
public static Economy economy = null;
public static Permission permission = null;
@ -174,7 +174,7 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
@Override
public void onEnable() {
bukkitVersion = Integer.valueOf(Bukkit.getServer().getBukkitVersion().split("-")[0].replace(".", ""));
bukkitVersion = Bukkit.getServer().getBukkitVersion().split("-")[0];
pListener = new PlayerListener(this);
effListener = new NpcEffectThread(this);
npcListener = new NpcListener(this);
@ -268,7 +268,7 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
if (!outDir.exists()) {
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;
import java.lang.reflect.InvocationTargetException;
import me.blackvein.quests.Quester;
import me.blackvein.quests.Quests;
@ -24,101 +25,135 @@ import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
public class LocaleQuery {
private static Class<?> craftMagicNumbers = null;
private final Quests plugin;
public LocaleQuery(Quests plugin){
this.plugin = plugin;
setup();
}
public void sendMessage(Player player, String message, Material material) {
if (plugin.translateItems) {
String key = queryByType(material);
if (Quests.bukkitVersion <= 1122) {
if (key.startsWith("tile.") || key.startsWith("item.")) {
key = key + ".name";
}
}
if (key != null) {
String msg = message.replace("<item>", "\",{\"translate\":\"" + key + "\"},\"");
player.chat("/tellraw " + player.getName() + " [\"" + msg + "\"]");
return;
}
}
player.sendMessage(message.replace("<item>", Quester.prettyItemString(material.name())));
}
public void sendMessage(Player player, String message, Material material, Enchantment enchantment) {
if (plugin.translateItems) {
String key = queryByType(material);
if (Quests.bukkitVersion <= 1122) {
if (key.startsWith("tile.") || key.startsWith("item.")) {
key = key + ".name";
}
}
String key2 = "";
if (Quests.bukkitVersion <= 1122) {
key2 = "enchantment." + MiscUtil.getProperEnchantmentName(enchantment).replace("_", ".")
.replace("environmental", "all").replace("protection", "protect");
} else {
key2 = "enchantment.minecraft." + enchantment.toString().toLowerCase();
}
if (key != null && !key.equals("")) {
String msg = message.replace("<item>", "\",{\"translate\":\"" + key + "\"},\"")
.replace("<enchantment>", "\",{\"translate\":\"" + key2 + "\"},\"");
player.chat("/tellraw " + player.getName() + " [\"" + msg + "\"]");
return;
}
}
player.sendMessage(message.replace("<item>", Quester.prettyItemString(material.name()))
.replace("<enchantment>", Quester.prettyEnchantmentString(enchantment)));
}
public void sendMessage(Player player, String message, EntityType type) {
if (plugin.translateItems) {
String key = "";
if (Quests.bukkitVersion <= 1122) {
key = "entity." + MiscUtil.getProperMobName(type) + ".name";
} else {
key = "entity.minecraft." + type.toString().toLowerCase();
}
if (!key.equals("")) {
String msg = message.replace("<mob>", "\",{\"translate\":\"" + key + "\"},\"");
player.chat("/tellraw " + player.getName() + " [\"" + msg + "\"]");
return;
}
}
player.sendMessage(message.replace("<mob>", Quester.prettyMobString(type)));
}
/**
* Creates a new LocaleQuery of the specified material
* @param material the item with the material
* @return the new LocaleQuery
* @throws IllegalArgumentException if an item with that material could not be found
*/
public String queryByType(Material material) throws IllegalArgumentException{
try {
Object item = MethodUtils.invokeExactStaticMethod(craftMagicNumbers,"getItem", material);
if (item == null) {
throw new IllegalArgumentException("An item with that material could not be found! (Perhaps you have specified a block?)");
}
private static Class<?> craftMagicNumbers = null;
private final Quests plugin;
static boolean oldVersion = isBelow113();
public LocaleQuery(Quests plugin){
this.plugin = plugin;
setup();
}
public void sendMessage(Player player, String message, Material material) {
if (plugin.translateItems) {
String key = queryByType(material);
if (oldVersion) {
if (key.startsWith("tile.") || key.startsWith("item.")) {
key = key + ".name";
}
}
if (key != null) {
String msg = message.replace("<item>", "\",{\"translate\":\"" + key + "\"},\"");
player.chat("/tellraw " + player.getName() + " [\"" + msg + "\"]");
return;
}
}
player.sendMessage(message.replace("<item>", Quester.prettyItemString(material.name())));
}
public void sendMessage(Player player, String message, Material material, Enchantment enchantment) {
if (plugin.translateItems) {
String key = queryByType(material);
if (oldVersion) {
if (key.startsWith("tile.") || key.startsWith("item.")) {
key = key + ".name";
}
}
String key2 = "";
if (oldVersion) {
key2 = "enchantment." + MiscUtil.getProperEnchantmentName(enchantment).replace("_", ".")
.replace("environmental", "all").replace("protection", "protect");
} else {
key2 = "enchantment.minecraft." + enchantment.toString().toLowerCase();
}
if (key != null && !key.equals("")) {
String msg = message.replace("<item>", "\",{\"translate\":\"" + key + "\"},\"")
.replace("<enchantment>", "\",{\"translate\":\"" + key2 + "\"},\"");
player.chat("/tellraw " + player.getName() + " [\"" + msg + "\"]");
return;
}
}
player.sendMessage(message.replace("<item>", Quester.prettyItemString(material.name()))
.replace("<enchantment>", Quester.prettyEnchantmentString(enchantment)));
}
public void sendMessage(Player player, String message, EntityType type) {
if (plugin.translateItems) {
String key = "";
if (oldVersion) {
key = "entity." + MiscUtil.getProperMobName(type) + ".name";
} else {
key = "entity.minecraft." + type.toString().toLowerCase();
}
if (!key.equals("")) {
String msg = message.replace("<mob>", "\",{\"translate\":\"" + key + "\"},\"");
player.chat("/tellraw " + player.getName() + " [\"" + msg + "\"]");
return;
}
}
player.sendMessage(message.replace("<mob>", Quester.prettyMobString(type)));
}
/**
* Creates a new LocaleQuery of the specified material
* @param material the item with the material
* @return the new LocaleQuery
* @throws IllegalArgumentException if an item with that material could not be found
*/
public String queryByType(Material material) throws IllegalArgumentException{
try {
Object item = MethodUtils.invokeExactStaticMethod(craftMagicNumbers,"getItem", material);
if (item == null) {
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");
return name;
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
public void setup() {
String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
try {
craftMagicNumbers = Class.forName("org.bukkit.craftbukkit.{v}.util.CraftMagicNumbers".replace("{v}", version));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
String name = (String) MethodUtils.invokeExactMethod(item, "getName");
return name;
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
public void setup() {
String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
try {
craftMagicNumbers = Class.forName("org.bukkit.craftbukkit.{v}.util.CraftMagicNumbers".replace("{v}", version));
} catch (ClassNotFoundException e) {
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;
}
}