diff --git a/src/main/java/com/gamingmesh/jobs/CMILib/ActionBarTitleMessages.java b/src/main/java/com/gamingmesh/jobs/CMILib/ActionBarTitleMessages.java new file mode 100644 index 00000000..d4f69731 --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/CMILib/ActionBarTitleMessages.java @@ -0,0 +1,234 @@ +package com.gamingmesh.jobs.CMILib; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.logging.Level; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import com.gamingmesh.jobs.Jobs; +import com.gamingmesh.jobs.CMILib.VersionChecker.Version; + +public class ActionBarTitleMessages { + private static Object packet; + private static Method getHandle; + private static Method sendPacket; + private static Field playerConnection; + private static Class nmsChatSerializer; + private static Class nmsIChatBaseComponent; + private static Class packetType; + + private static Constructor nmsPacketPlayOutTitle; + private static Class enumTitleAction; + private static Method fromString; + private static boolean simpleTitleMessages = false; + + private static Class ChatMessageclz; + private static Class sub; + private static Object[] consts; + + static { + if (Version.getCurrent().isHigher(Version.v1_7_R4)) { + try { + packetType = Class.forName(getPacketPlayOutChat()); + Class typeCraftPlayer = Class.forName(getCraftPlayerClasspath()); + Class typeNMSPlayer = Class.forName(getNMSPlayerClasspath()); + Class typePlayerConnection = Class.forName(getPlayerConnectionClasspath()); + nmsChatSerializer = Class.forName(getChatSerializerClasspath()); + nmsIChatBaseComponent = Class.forName(getIChatBaseComponentClasspath()); + getHandle = typeCraftPlayer.getMethod("getHandle"); + playerConnection = typeNMSPlayer.getField("playerConnection"); + sendPacket = typePlayerConnection.getMethod("sendPacket", Class.forName(getPacketClasspath())); + + if (Version.isCurrentHigher(Version.v1_11_R1)) { + ChatMessageclz = Class.forName(getChatMessageTypeClasspath()); + consts = ChatMessageclz.getEnumConstants(); + sub = consts[2].getClass(); + } + + } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | NoSuchFieldException ex) { + Bukkit.getLogger().log(Level.SEVERE, "Error {0}", ex); + } + // Title + try { + Class typePacketPlayOutTitle = Class.forName(getPacketPlayOutTitleClasspath()); + enumTitleAction = Class.forName(getEnumTitleActionClasspath()); + nmsPacketPlayOutTitle = typePacketPlayOutTitle.getConstructor(enumTitleAction, nmsIChatBaseComponent); + fromString = Class.forName(getClassMessageClasspath()).getMethod("fromString", String.class); + } catch (ClassNotFoundException | NoSuchMethodException | SecurityException ex) { + simpleTitleMessages = true; + } + } + } + + public static void send(CommandSender receivingPacket, String msg) { + if (receivingPacket instanceof Player) + send((Player) receivingPacket, msg); + else + receivingPacket.sendMessage(msg); + } + + public static void send(Player receivingPacket, String msg) { + if (receivingPacket == null) + return; + if (!receivingPacket.isOnline()) + return; + if (msg == null) + return; + try { + if (!Version.getCurrent().isHigher(Version.v1_7_R4) || nmsChatSerializer == null) { + receivingPacket.sendMessage(ChatColor.translateAlternateColorCodes('&', msg)); + return; + } + + Object serialized = nmsChatSerializer.getMethod("a", String.class).invoke(null, "{\"text\": \"" + ChatColor.translateAlternateColorCodes('&', msg) + "\"}"); + if (Version.isCurrentHigher(Version.v1_11_R1)) + packet = packetType.getConstructor(nmsIChatBaseComponent, sub).newInstance(serialized, consts[2]); + else if (Version.isCurrentHigher(Version.v1_7_R4)) { + packet = packetType.getConstructor(nmsIChatBaseComponent, byte.class).newInstance(serialized, (byte) 2); + } else { + packet = packetType.getConstructor(nmsIChatBaseComponent, int.class).newInstance(serialized, 2); + } + Object player = getHandle.invoke(receivingPacket); + Object connection = playerConnection.get(player); + sendPacket.invoke(connection, packet); + } catch (Exception ex) { +// Bukkit.getLogger().log(Level.SEVERE, "Error {0}", ex); + } + + try { + Object player = getHandle.invoke(receivingPacket); + Object connection = playerConnection.get(player); + sendPacket.invoke(connection, packet); + } catch (Exception ex) { +// Bukkit.getLogger().log(Level.SEVERE, "Error {0}", ex); + } + } + + public static void sendTitle(final Player receivingPacket, final Object title, final Object subtitle) { + sendTitle(receivingPacket, title, subtitle, 0, 20, 20); + } + + public static void sendTitle(final Player receivingPacket, final Object title, final Object subtitle, final int fadeIn, final int keep, final int fadeOut) { + Bukkit.getScheduler().runTaskAsynchronously(Jobs.getInstance(), new Runnable() { + @Override + public void run() { + + String t = title == null ? null : CMIChatColor.translateAlternateColorCodes((String) title); + String s = subtitle == null ? null : CMIChatColor.translateAlternateColorCodes((String) subtitle); + + if (simpleTitleMessages) { + receivingPacket.sendMessage(t); + receivingPacket.sendMessage(s); + return; + } + try { + switch (Version.getCurrent()) { + case v1_9_R1: + case v1_9_R2: + case v1_10_R1: + case v1_11_R1: + receivingPacket.sendTitle(t, s); + break; + case v1_12_R1: + case v1_13_R1: + case v1_13_R2: + case v1_14_R1: + case v1_14_R2: + case v1_15_R1: + case v1_15_R2: + receivingPacket.sendTitle(t, s, fadeIn, keep, fadeOut); + break; + case v1_7_R1: + case v1_7_R2: + case v1_7_R3: + case v1_7_R4: + case v1_8_R1: + case v1_8_R2: + case v1_8_R3: + if (title != null) { + Object packetTitle = nmsPacketPlayOutTitle.newInstance(enumTitleAction.getField("TITLE").get(null), + ((Object[]) fromString.invoke(null, t))[0]); + sendPacket(receivingPacket, packetTitle); + } + if (subtitle != null) { + if (title == null) { + Object packetTitle = nmsPacketPlayOutTitle.newInstance(enumTitleAction.getField("TITLE").get(null), ((Object[]) fromString.invoke(null, ""))[0]); + sendPacket(receivingPacket, packetTitle); + } + Object packetSubtitle = nmsPacketPlayOutTitle.newInstance(enumTitleAction.getField("SUBTITLE").get(null), + ((Object[]) fromString.invoke(null, s))[0]); + sendPacket(receivingPacket, packetSubtitle); + } + + break; + default: + break; + } + + } catch (SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchFieldException ex) { + simpleTitleMessages = true; + Bukkit.getLogger().log(Level.SEVERE, "Your server can't fully support title messages. They will be shown in chat instead."); + } + return; + } + }); + } + + private static void sendPacket(Player player, Object packet) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { + Object handle = getHandle.invoke(player); + Object connection = playerConnection.get(handle); + sendPacket.invoke(connection, packet); + } + + private static String getCraftPlayerClasspath() { + return "org.bukkit.craftbukkit." + Version.getCurrent() + ".entity.CraftPlayer"; + } + + private static String getPlayerConnectionClasspath() { + return "net.minecraft.server." + Version.getCurrent() + ".PlayerConnection"; + } + + private static String getNMSPlayerClasspath() { + return "net.minecraft.server." + Version.getCurrent() + ".EntityPlayer"; + } + + private static String getPacketClasspath() { + return "net.minecraft.server." + Version.getCurrent() + ".Packet"; + } + + private static String getIChatBaseComponentClasspath() { + return "net.minecraft.server." + Version.getCurrent() + ".IChatBaseComponent"; + } + + private static String getChatSerializerClasspath() { + if (!Version.isCurrentHigher(Version.v1_8_R2)) + return "net.minecraft.server." + Version.getCurrent() + ".ChatSerializer"; + return "net.minecraft.server." + Version.getCurrent() + ".IChatBaseComponent$ChatSerializer";// 1_8_R2 moved to IChatBaseComponent + } + + private static String getPacketPlayOutChat() { + return "net.minecraft.server." + Version.getCurrent() + ".PacketPlayOutChat"; + } + + private static String getPacketPlayOutTitleClasspath() { + return "net.minecraft.server." + Version.getCurrent() + ".PacketPlayOutTitle"; + } + + private static String getEnumTitleActionClasspath() { + return getPacketPlayOutTitleClasspath() + "$EnumTitleAction"; + } + + private static String getClassMessageClasspath() { + return "org.bukkit.craftbukkit." + Version.getCurrent() + ".util.CraftChatMessage"; + } + + private static String getChatMessageTypeClasspath() { + return "net.minecraft.server." + Version.getCurrent() + ".ChatMessageType"; + } +} diff --git a/src/main/java/com/gamingmesh/jobs/CMILib/CMIChatColor.java b/src/main/java/com/gamingmesh/jobs/CMILib/CMIChatColor.java new file mode 100644 index 00000000..9352bb82 --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/CMILib/CMIChatColor.java @@ -0,0 +1,154 @@ +package com.gamingmesh.jobs.CMILib; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.regex.Pattern; + +import org.bukkit.ChatColor; + +public enum CMIChatColor { + BLACK('0'), + DARK_BLUE('1'), + DARK_GREEN('2'), + DARK_AQUA('3'), + DARK_RED('4'), + DARK_PURPLE('5'), + GOLD('6'), + GRAY('7'), + DARK_GRAY('8'), + BLUE('9'), + GREEN('a'), + AQUA('b'), + RED('c'), + LIGHT_PURPLE('d'), + YELLOW('e'), + WHITE('f'), + MAGIC('k', false), + BOLD('l', false), + STRIKETHROUGH('m', false), + UNDERLINE('n', false), + ITALIC('o', false), + RESET('r', false, true); + + private char c; + private Boolean color = true; + private Boolean reset = false; + private Pattern pattern = null; + + CMIChatColor(char c) { + this(c, true); + } + + CMIChatColor(char c, Boolean color) { + this(c, color, false); + } + + CMIChatColor(char c, Boolean color, Boolean reset) { + this.c = c; + this.color = color; + this.reset = reset; + this.pattern = Pattern.compile("(?i)(&[" + c + "])"); + } + + public static String translateAlternateColorCodes(String text) { + return ChatColor.translateAlternateColorCodes('&', text); + } + + public static String colorize(String text) { + if (text == null) + return null; + return ChatColor.translateAlternateColorCodes('&', text); + } + + public static String deColorize(String text) { + if (text == null) + return null; + return text.replace("�", "&"); + } + + public static String stripColor(String text) { + if (text == null) + return null; + text = ChatColor.translateAlternateColorCodes('&', text); + return ChatColor.stripColor(text); + } + + public static String getLastColors(String text) { + if (text == null) + return null; + text = CMIChatColor.translateAlternateColorCodes(text); + return ChatColor.getLastColors(text); + } + + public String getColorCode() { + return "&" + c; + } + + public String getBukkitColorCode() { + return "�" + c; + } + + public char getChar() { + return c; + } + + public void setChar(char c) { + this.c = c; + } + + public Boolean isColor() { + return color; + } + + public Boolean isFormat() { + return !color && !reset; + } + + public Boolean isReset() { + return reset; + } + + public ChatColor getColor() { + return ChatColor.getByChar(this.getChar()); + } + + public static CMIChatColor getColor(String text) { + String or = CMIChatColor.deColorize(text); + text = CMIChatColor.deColorize(text).replace("&", ""); + + if (text.length() > 1) { + String formated = text.toLowerCase().replace("_", ""); + for (CMIChatColor one : CMIChatColor.values()) { + if (one.name().replace("_", "").equalsIgnoreCase(formated)) + return one; + } + } + + if (or.length() > 1 && String.valueOf(or.charAt(or.length() - 2)).equalsIgnoreCase("&")) { + text = text.substring(text.length() - 1, text.length()); + + for (CMIChatColor one : CMIChatColor.values()) { + if (String.valueOf(one.getChar()).equalsIgnoreCase(text)) + return one; + } + } + + return null; + } + + public static CMIChatColor getRandomColor() { + List ls = new ArrayList(); + for (CMIChatColor one : CMIChatColor.values()) { + if (!one.isColor()) + continue; + ls.add(one); + } + Collections.shuffle(ls); + return ls.get(0); + } + + public Pattern getPattern() { + return pattern; + } +} diff --git a/src/main/java/com/gamingmesh/jobs/CMILib/CMIEffectManager.java b/src/main/java/com/gamingmesh/jobs/CMILib/CMIEffectManager.java index 39992cd9..efe83761 100644 --- a/src/main/java/com/gamingmesh/jobs/CMILib/CMIEffectManager.java +++ b/src/main/java/com/gamingmesh/jobs/CMILib/CMIEffectManager.java @@ -12,7 +12,7 @@ import org.bukkit.Material; import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.CMILib.ItemManager.CMIMaterial; -import com.gamingmesh.jobs.stuff.VersionChecker.Version; +import com.gamingmesh.jobs.CMILib.VersionChecker.Version; public class CMIEffectManager { diff --git a/src/main/java/com/gamingmesh/jobs/CMILib/CMIItemStack.java b/src/main/java/com/gamingmesh/jobs/CMILib/CMIItemStack.java index 7881dc49..e8d2b70f 100644 --- a/src/main/java/com/gamingmesh/jobs/CMILib/CMIItemStack.java +++ b/src/main/java/com/gamingmesh/jobs/CMILib/CMIItemStack.java @@ -19,7 +19,7 @@ import org.bukkit.potion.PotionEffectType; import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.CMILib.ItemManager.CMIMaterial; -import com.gamingmesh.jobs.stuff.VersionChecker.Version; +import com.gamingmesh.jobs.CMILib.VersionChecker.Version; public class CMIItemStack { diff --git a/src/main/java/com/gamingmesh/jobs/CMILib/ItemManager.java b/src/main/java/com/gamingmesh/jobs/CMILib/ItemManager.java index 19f2de79..e56bcf98 100644 --- a/src/main/java/com/gamingmesh/jobs/CMILib/ItemManager.java +++ b/src/main/java/com/gamingmesh/jobs/CMILib/ItemManager.java @@ -23,7 +23,7 @@ import org.bukkit.inventory.meta.SkullMeta; import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.stuff.Debug; -import com.gamingmesh.jobs.stuff.VersionChecker.Version; +import com.gamingmesh.jobs.CMILib.VersionChecker.Version; public class ItemManager { diff --git a/src/main/java/com/gamingmesh/jobs/CMILib/ItemReflection.java b/src/main/java/com/gamingmesh/jobs/CMILib/ItemReflection.java index 40f5e6a7..1cc8fad5 100644 --- a/src/main/java/com/gamingmesh/jobs/CMILib/ItemReflection.java +++ b/src/main/java/com/gamingmesh/jobs/CMILib/ItemReflection.java @@ -99,9 +99,7 @@ public class ItemReflection { ff = "a"; break; case v1_11_R1: - case v1_11_R2: case v1_12_R1: - case v1_12_R2: ff = "b"; break; case v1_13_R2: diff --git a/src/main/java/com/gamingmesh/jobs/CMILib/RawMessage.java b/src/main/java/com/gamingmesh/jobs/CMILib/RawMessage.java new file mode 100644 index 00000000..9936bf07 --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/CMILib/RawMessage.java @@ -0,0 +1,587 @@ +package com.gamingmesh.jobs.CMILib; + +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map.Entry; +import java.util.Random; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +import com.gamingmesh.jobs.CMILib.VersionChecker.Version; + + +public class RawMessage { + + List parts = new ArrayList(); + List cleanParts = new ArrayList(); + String colorReplacerPlaceholder = "%#%"; + + private String unfinished = ""; + private String unfinishedClean = ""; + + private String combined = ""; + String combinedClean = ""; + private boolean breakLine = true; + +// private boolean colorizeEntireWithLast = true; + + public void clear() { + parts = new ArrayList(); + cleanParts = new ArrayList(); + combined = ""; + combinedClean = ""; + } + + public RawMessage add(String text) { + return add(text, null, null, null, null); + } + + public RawMessage add(String text, String hoverText) { + return add(text, hoverText, null, null, null); + } + + public RawMessage add(String text, List hoverText) { + + String hover = ""; + if (hoverText != null) + for (String one : hoverText) { + if (!hover.isEmpty()) + hover += "\n"; + hover += one; + } + + return add(text, hover.isEmpty() ? null : hover, null, null, null); + } + + public RawMessage add(String text, String hoverText, String command) { + return add(text, hoverText, command, null, null); + } + + public RawMessage add(String text, String hoverText, String command, String suggestion) { + return add(text, hoverText, command, suggestion, null); + } + + Set formats = new HashSet(); + CMIChatColor lastColor = null; + + Set savedFormats = new HashSet(); + CMIChatColor savedLastColor = null; + + CMIChatColor firstBlockColor = null; + + private String makeMessyText(String text) { + text = CMIChatColor.deColorize(text); + List splited = new ArrayList(); + + if (text.contains(" ")) { + for (String one : text.split(" ")) { + if (this.isBreakLine() && one.contains("\\n")) { + String[] split = one.split("\\\\n"); + for (int i = 0; i < split.length; i++) { + if (i < split.length - 1) { + splited.add(split[i] + "\n"); + } else { + splited.add(split[i]); + } + } + } else { + splited.add(one); + } + splited.add(" "); + } + if (text.length() > 1 && text.endsWith(" ")) + splited.add(" "); + if (text.startsWith(" ")) + splited.add(" "); + + if (!splited.isEmpty()) + splited.remove(splited.size() - 1); + } else + splited.add(text); + + String newText = ""; + + for (String one : splited) { + + String colorString = ""; + if (lastColor != null) + colorString += lastColor.getColorCode(); + for (CMIChatColor oneC : formats) { + colorString = colorString + oneC.getColorCode(); + } + + if (one.contains("&")) { + Pattern pattern = Pattern.compile("(&[0123456789abcdefklmnor])"); + Matcher match = pattern.matcher(one); + while (match.find()) { + String color = CMIChatColor.getLastColors(match.group(0)); + CMIChatColor c = CMIChatColor.getColor(color); + if (c != null) { + if (c.isFormat()) { + formats.add(c); + } else if (c.isReset()) { + formats.clear(); + lastColor = null; + firstBlockColor = null; + } else if (c.isColor()) { + lastColor = c; + formats.clear(); + firstBlockColor = c; + } + + if (c.isFormat()) { + } else if (c.isReset()) { + } else if (c.isColor()) { + String form = ""; + for (CMIChatColor oneC : formats) { + form += oneC.getColorCode(); + } + one = one.replace(c.getColorCode(), c.getColorCode() + form); + } + + } + } + } + + newText += colorString + one; + } + return newText; + } + + public RawMessage addText(String text) { + if (text == null) + return this; + if (breakLine) { + Random rand = new Random(); + String breakLine = rand.nextDouble() + "breakLine"; + text = text.replace("\\n", breakLine); + text = text.replace("\\", "\\\\"); + text = text.replace(breakLine, "\\n"); + } + + text = text.replace("\n", "\\n"); + unfinishedClean = text; + unfinished += "\"text\":\"" + ChatColor.translateAlternateColorCodes('&', makeMessyText(text)).replace(colorReplacerPlaceholder, "&") + "\""; + return this; + } + + public RawMessage addHoverText(List hoverText) { + String hover = ""; + if (hoverText != null) + for (String one : hoverText) { + if (!hover.isEmpty()) + hover += "\n"; + hover += one; + } + return addHoverText(hover); + } + + public RawMessage addHoverText(String hoverText) { + if (hoverText != null && !hoverText.isEmpty()) { + hoverText = hoverText.replace(" \n", " \\n"); + hoverText = hoverText.replace("\n", "\\n"); + unfinished += ",\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"" + ChatColor.translateAlternateColorCodes('&', hoverText) + "\"}]}}"; + } + return this; + } + + public RawMessage addCommand(String command) { + if (command != null) { + if (!command.startsWith("/")) + command = "/" + command; + unfinished += ",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"" + command + "\"}"; + } + return this; + } + + public RawMessage addSuggestion(String suggestion) { + if (suggestion != null) + unfinished += ",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"" + CMIChatColor.deColorize(suggestion) + "\"}"; + return this; + } + + public RawMessage addUrl(String url) { + if (url != null) { + if (!url.toLowerCase().startsWith("http://") || !url.toLowerCase().startsWith("https://")) + url = "http://" + url; + unfinished += ",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"" + url + "\"}"; + } + return this; + } + + public RawMessage build() { + if (unfinished.isEmpty()) + return this; + unfinished = unfinished.startsWith("{") ? unfinished : "{" + unfinished + "}"; + parts.add(unfinished); + cleanParts.add(ChatColor.translateAlternateColorCodes('&', unfinishedClean)); + + unfinished = ""; + unfinishedClean = ""; + + return this; + } + + public RawMessage add(String text, String hoverText, String command, String suggestion, String url) { + + if (text == null) + return this; + + if (breakLine) { + Random rand = new Random(); + String breakLine = rand.nextDouble() + "breakLine"; + text = text.replace("\\n", breakLine); + text = text.replace("\\", "\\\\"); + text = text.replace(breakLine, "\\n"); + } + + text = text.replace("\"", "\\\""); + + String f = "{\"text\":\"" + ChatColor.translateAlternateColorCodes('&', makeMessyText(text)).replace(colorReplacerPlaceholder, "&") + "\""; + +// if (firstBlockColor != null) { +// f += ",\"color\":\"" + firstBlockColor.name().toLowerCase() + "\""; +// } + + if (hoverText != null && !hoverText.isEmpty()) { + hoverText = hoverText.replace(" \n", " \\n"); + hoverText = hoverText.replace("\n", "\\n"); + f += ",\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"" + ChatColor.translateAlternateColorCodes('&', hoverText) + "\"}]}}"; + } + +// if (suggestion != null && command != null) { +// f += ",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"" + CMIChatColor.deColorize(suggestion) + "\"},\"clickEvent\":{\"action\":\"run_command\",\"value\":\"" + command + "\"\"}"; +// +// } else { + + if (suggestion != null) + f += ",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"" + CMIChatColor.deColorize(suggestion) + "\"}"; + if (url != null) { + if (!url.toLowerCase().startsWith("http://") || !url.toLowerCase().startsWith("https://")) + url = "http://" + url; + f += ",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"" + url + "\"}"; + } + + if (command != null) { + if (!command.startsWith("/")) + command = "/" + command; + f += ",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"" + command + "\"}"; + } +// } + + f += "}"; + + parts.add(f); + cleanParts.add(ChatColor.translateAlternateColorCodes('&', text)); +// firstBlockColor = null; + return this; + } + + public RawMessage addUrl(String text, String url) { + return addUrl(text, url, null); + } + + public RawMessage addUrl(String text, String url, String hoverText) { + if (text == null) + return this; + + text = text.replace("\\", "\\\\"); + text = text.replace("\"", "\\\""); + String f = "{\"text\":\"" + CMIChatColor.colorize(text).replace(colorReplacerPlaceholder, "&") + "\""; + if (firstBlockColor != null) { + f += ",\"color\":\"" + firstBlockColor.name().toLowerCase() + "\""; + } + if (hoverText != null && !hoverText.isEmpty()) { + hoverText = hoverText.startsWith(" ") ? hoverText.substring(1) : hoverText; + f += ",\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"" + ChatColor.translateAlternateColorCodes('&', hoverText).replace( + colorReplacerPlaceholder, "&") + "\"}]}}"; + } + + url = url.endsWith(" ") ? url.substring(0, url.length() - 1) : url; + url = url.startsWith(" ") ? url.substring(1) : url; + + if (url != null && !url.isEmpty()) { + if (!url.toLowerCase().startsWith("http://") && !url.toLowerCase().startsWith("https://")) + url = "http://" + url; + f += ",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"" + url + "\"}"; + } + f += "}"; + parts.add(f); + cleanParts.add(ChatColor.translateAlternateColorCodes('&', text)); +// firstBlockColor = null; + + return this; + } + + public RawMessage addItem(String text, ItemStack item, List extraLore, String command, String suggestion) { + if (text == null) + return this; + if (item == null) + return this; + + item = item.clone(); + + text = makeMessyText(text); + + String f = "{\"text\":\"" + ChatColor.translateAlternateColorCodes('&', text) + "\""; + + CMIItemStack cm = ItemManager.getItem(item); + + String ItemName = "&r&f" + cm.getDisplayName(); + String Enchants = getItemEnchants(item); + + if (!Enchants.isEmpty()) { + if (Version.isCurrentEqualOrHigher(Version.v1_13_R1)) { + Enchants = ",Enchantments:" + Enchants; + } else { + Enchants = ",ench:" + Enchants; + } + } + + List Lore = new ArrayList(); + +// if (CMIMaterial.isShulkerBox(item.getType())) { +// List items = CMI.getInstance().getShulkerBoxManager().getShulkerBoxContents(item); +// for (ItemStack one : items) { +// if (one == null) +// continue; +// CMIItemStack cim = ItemManager.getItem(one); +// if (cim == null) +// continue; +// Lore.add(CMIChatColor.translateAlternateColorCodes("&7" + cim.getRealName() + " x" + cim.getAmount())); +// } +// } + + if (item.hasItemMeta() && item.getItemMeta().hasLore()) + Lore.addAll(item.getItemMeta().getLore()); + if (extraLore != null) + Lore.addAll(extraLore); + + String itemName = cm.getBukkitName(); + + if (cm.getMojangName() != null) + itemName = cm.getMojangName(); + + if (itemName.equalsIgnoreCase("Air")) { + itemName = "Stone"; + ItemName = "Hand"; + } + + if (Version.isCurrentEqualOrHigher(Version.v1_13_R1)) { + itemName = org.bukkit.NamespacedKey.minecraft(cm.getType().name().toLowerCase()).getKey(); + } + + String loreS = convertLore(Lore); + if (!Lore.isEmpty()) { + loreS = ",Lore:[" + loreS + "]"; + } + f += ",\"hoverEvent\":{\"action\":\"show_item\",\"value\":\"{id:" + itemName + ",Count:1b,tag:{display:{Name:\\\"" + CMIChatColor.translateAlternateColorCodes(ItemName) + "\\\"" + loreS + "}" + + Enchants + "}}\"}"; + + if (suggestion != null) + f += ",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"" + suggestion + "\"}"; + if (command != null) { + if (!command.startsWith("/")) + command = "/" + command; + f += ",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"" + command + "\"}"; + } + f += "}"; + parts.add(f); + return this; + } + + private static String getItemEnchants(ItemStack item) { + String Enchants = ""; + if (item.getEnchantments().isEmpty()) + return Enchants; + + Enchants = ""; + for (Entry one : item.getEnchantments().entrySet()) { + if (!Enchants.isEmpty()) + Enchants += ","; + if (Version.isCurrentEqualOrHigher(Version.v1_13_R1)) + Enchants += "{id:" + one.getKey().getKey().getKey() + ",lvl:" + one.getValue() + "s}"; + else { + try { + Enchants += "{id:" + String.valueOf(one.getKey().getClass().getMethod("getId").invoke(one.getKey())) + ",lvl:" + one.getValue() + "}"; + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { + e.printStackTrace(); + } + } + } + if (!Enchants.isEmpty()) { + Enchants = "[" + Enchants; + Enchants += "]"; + } + return Enchants; + } + + private static String convertLore(List lore) { + String lr = ""; + for (String one : lore) { + if (!lr.isEmpty()) + lr += ","; + lr += "\\\"" + one + "\\\""; + } + return lr; + } + + public List softCombine() { + List ls = new ArrayList(); + String f = ""; + for (String part : parts) { + if (f.isEmpty()) + f = "[\"\","; + else { + if (f.length() > 30000) { + ls.add(f + "]"); + f = "[\"\"," + part; + continue; + } + f += ","; + } + f += part; + } + if (!f.isEmpty()) + f += "]"; + ls.add(f); + return ls; + } + + public RawMessage combine() { + String f = ""; + for (String part : parts) { + if (f.isEmpty()) + f = "[\"\","; + else + f += ","; + f += part; + } + if (!f.isEmpty()) + f += "]"; + combined = f; + return this; + } + + public RawMessage combineClean() { + String f = ""; + for (String part : cleanParts) { + f += part.replace("\\\"", "\""); + } + combinedClean = f; + return this; + } + + public RawMessage show(Player player) { + return show(player, true); + } + + public RawMessage show(Player player, boolean softCombined) { + if (player == null) + return this; + if (combined.isEmpty()) + combine(); + + if (!player.isOnline()) + return this; + +// CMI.getInstance().d("|" + combined + "|"); + + if (softCombined) { + for (String one : softCombine()) { + if (one.isEmpty()) + continue; +// CMI.getInstance().d("=" + one + "="); + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + player.getName() + " " + one); + } + } else { + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + player.getName() + " " + combined); + } + + return this; + } + + public int getFinalLenght() { + String f = ""; + for (String part : parts) { + if (f.isEmpty()) + f = "[\"\","; + else + f += ","; + f += part; + } + if (!f.isEmpty()) + f += "]"; + return f.length(); + } + + public RawMessage show(CommandSender sender) { + if (combined.isEmpty()) + combine(); + if (sender instanceof Player) + show((Player) sender); + else + sender.sendMessage(this.combineClean().combinedClean); + return this; + } + + public String getRaw() { + if (combined.isEmpty()) + combine(); + return combined; + } + + public String getShortRaw() { + String f = ""; + for (String part : parts) { + if (!f.isEmpty()) + f += ","; + f += part; + } + return f; + } + + public boolean isBreakLine() { + return breakLine; + } + + public void setBreakLine(boolean breakLine) { + this.breakLine = breakLine; + } + + public void setCombined(String combined) { + this.combined = combined; + } + +// Set formats = new HashSet(); +// CMIChatColor lastColor = null; +// +// Set savedFormats = new HashSet(); +// CMIChatColor savedLastColor = null; + public void resetColorFormats() { + formats.clear(); + lastColor = null; + } + + public void saveColorFormats() { + savedFormats.clear(); + savedFormats.addAll(formats); + savedLastColor = lastColor; + } + + public void loadColorFormats() { + formats.clear(); + formats.addAll(savedFormats); + lastColor = savedLastColor; + } +} diff --git a/src/main/java/com/gamingmesh/jobs/CMILib/VersionChecker.java b/src/main/java/com/gamingmesh/jobs/CMILib/VersionChecker.java new file mode 100644 index 00000000..01318422 --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/CMILib/VersionChecker.java @@ -0,0 +1,190 @@ +package com.gamingmesh.jobs.CMILib; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Arrays; +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; + +import com.gamingmesh.jobs.Jobs; + +public class VersionChecker { + Jobs plugin; + private static int resource = 4216; + + public VersionChecker(Jobs plugin) { + this.plugin = plugin; + version = Version.getCurrent(); + } + + private static Version version = Version.v1_13_R2; + + public Version getVersion() { + return Version.getCurrent(); + } + + public Integer convertVersion(String v) { + v = v.replaceAll("[^\\d.]", ""); + Integer version = 0; + if (v.contains(".")) { + String lVersion = ""; + for (String one : v.split("\\.")) { + String s = one; + if (s.length() == 1) + s = "0" + s; + lVersion += s; + } + + try { + version = Integer.parseInt(lVersion); + } catch (Exception e) { + e.printStackTrace(); + } + } else { + try { + version = Integer.parseInt(v); + } catch (Exception e) { + e.printStackTrace(); + } + } + return version; + } + + public enum Version { + v1_7_R1, + v1_7_R2, + v1_7_R3, + v1_7_R4, + v1_8_R1, + v1_8_R2, + v1_8_R3, + v1_9_R1, + v1_9_R2, + v1_10_R1, + v1_11_R1, + v1_12_R1, + v1_13_R1, + v1_13_R2, + v1_13_R3, + v1_14_R1, + v1_14_R2, + v1_15_R1, + v1_15_R2, + v1_16_R1, + v1_16_R2, + v1_17_R1, + v1_17_R2; + + private Integer value; + private String shortVersion; + private static Version current = null; + + Version() { + try { + this.value = Integer.valueOf(this.name().replaceAll("[^\\d.]", "")); + } catch (Exception e) { + } + shortVersion = this.name().substring(0, this.name().length() - 3); + } + + public Integer getValue() { + return value; + } + + public String getShortVersion() { + return shortVersion; + } + + public static Version getCurrent() { + if (current != null) + return current; + String[] v = Bukkit.getServer().getClass().getPackage().getName().split("\\."); + String vv = v[v.length - 1]; + for (Version one : values()) { + if (one.name().equalsIgnoreCase(vv)) { + current = one; + break; + } + } + return current; + } + + public boolean isLower(Version version) { + return getValue() < version.getValue(); + } + + public boolean isHigher(Version version) { + return getValue() > version.getValue(); + } + + public boolean isEqualOrLower(Version version) { + return getValue() <= version.getValue(); + } + + public boolean isEqualOrHigher(Version version) { + return getValue() >= version.getValue(); + } + + public static boolean isCurrentEqualOrHigher(Version v) { + return version.getValue() >= v.getValue(); + } + + public static boolean isCurrentHigher(Version v) { + return version.getValue() > v.getValue(); + } + + public static boolean isCurrentLower(Version v) { + return version.getValue() < v.getValue(); + } + + public static boolean isCurrentEqualOrLower(Version v) { + return version.getValue() <= v.getValue(); + } + } + + public void VersionCheck(final Player player) { + if (!Jobs.getGCManager().isShowNewVersion()) + return; + + Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { + @Override + public void run() { + String currentVersion = plugin.getDescription().getVersion(); + String newVersion = getNewVersion(); + if (newVersion == null || newVersion.equalsIgnoreCase(currentVersion)) + return; + List msg = Arrays.asList( + ChatColor.GREEN + "*********************** " + plugin.getDescription().getName() + " **************************", + ChatColor.GREEN + "* " + newVersion + " is now available! Your version: " + currentVersion, + ChatColor.GREEN + "* " + ChatColor.DARK_GREEN + plugin.getDescription().getWebsite(), + ChatColor.GREEN + "************************************************************"); + for (String one : msg) + if (player != null) + player.sendMessage(one); + else + Bukkit.getConsoleSender().sendMessage(one); + } + }); + } + + public String getNewVersion() { + try { + HttpURLConnection con = (HttpURLConnection) new URL("https://www.spigotmc.org/api/general.php").openConnection(); + con.setDoOutput(true); + con.setRequestMethod("POST"); + con.getOutputStream().write(("key=98BE0FE67F88AB82B4C197FAF1DC3B69206EFDCC4D3B80FC83A00037510B99B4&resource=" + resource).getBytes("UTF-8")); + String version = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine(); + if (version.length() <= 7) + return version; + } catch (Exception ex) { + Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Failed to check for " + plugin.getDescription().getName() + " update on spigot web page."); + } + return null; + } + +} diff --git a/src/main/java/com/gamingmesh/jobs/Jobs.java b/src/main/java/com/gamingmesh/jobs/Jobs.java index 06922854..5f029ef1 100644 --- a/src/main/java/com/gamingmesh/jobs/Jobs.java +++ b/src/main/java/com/gamingmesh/jobs/Jobs.java @@ -93,13 +93,10 @@ import com.gamingmesh.jobs.listeners.JobsPaymentListener; import com.gamingmesh.jobs.listeners.McMMOlistener; import com.gamingmesh.jobs.listeners.PistonProtectionListener; import com.gamingmesh.jobs.selection.SelectionManager; -import com.gamingmesh.jobs.stuff.ActionBar; import com.gamingmesh.jobs.stuff.CMIScoreboardManager; import com.gamingmesh.jobs.stuff.FurnaceBrewingHandling; import com.gamingmesh.jobs.stuff.Loging; -import com.gamingmesh.jobs.stuff.RawMessage; import com.gamingmesh.jobs.stuff.TabComplete; -import com.gamingmesh.jobs.stuff.VersionChecker; import com.gamingmesh.jobs.tasks.BufferedPaymentThread; import com.gamingmesh.jobs.tasks.DatabaseSaveThread; @@ -160,7 +157,7 @@ public class Jobs extends JavaPlugin { private static NMS nms = null; - private static ActionBar actionbar = null; + private static ActionBarTitleMessages actionbar = null; protected static VersionChecker versionCheckManager = null; @@ -276,10 +273,10 @@ public class Jobs extends JavaPlugin { } public void setActionBar() { - actionbar = new ActionBar(); + actionbar = new ActionBarTitleMessages(); } - public static ActionBar getActionBar() { + public static ActionBarTitleMessages getActionBar() { return actionbar; } diff --git a/src/main/java/com/gamingmesh/jobs/Reflections.java b/src/main/java/com/gamingmesh/jobs/Reflections.java index ab0f14bf..517d7423 100644 --- a/src/main/java/com/gamingmesh/jobs/Reflections.java +++ b/src/main/java/com/gamingmesh/jobs/Reflections.java @@ -90,10 +90,8 @@ public class Reflections { ff = "a"; break; case v1_11_R1: - case v1_11_R2: case v1_12_R1: case v1_13_R2: - case v1_12_R2: case v1_13_R1: ff = "b"; break; diff --git a/src/main/java/com/gamingmesh/jobs/actions/BlockActionInfo.java b/src/main/java/com/gamingmesh/jobs/actions/BlockActionInfo.java index 79583ebf..1106d7bf 100644 --- a/src/main/java/com/gamingmesh/jobs/actions/BlockActionInfo.java +++ b/src/main/java/com/gamingmesh/jobs/actions/BlockActionInfo.java @@ -24,7 +24,7 @@ import org.bukkit.block.Block; import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.container.ActionInfo; import com.gamingmesh.jobs.container.ActionType; -import com.gamingmesh.jobs.stuff.VersionChecker.Version; +import com.gamingmesh.jobs.CMILib.VersionChecker.Version; public class BlockActionInfo extends MaterialActionInfo implements ActionInfo { public BlockActionInfo(Block block, ActionType type) { diff --git a/src/main/java/com/gamingmesh/jobs/commands/JobsCommands.java b/src/main/java/com/gamingmesh/jobs/commands/JobsCommands.java index 04abc257..3a62b757 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/JobsCommands.java +++ b/src/main/java/com/gamingmesh/jobs/commands/JobsCommands.java @@ -28,6 +28,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import com.gamingmesh.jobs.Jobs; +import com.gamingmesh.jobs.CMILib.RawMessage; import com.gamingmesh.jobs.container.ActionType; import com.gamingmesh.jobs.container.Boost; import com.gamingmesh.jobs.container.CurrencyType; @@ -36,7 +37,6 @@ import com.gamingmesh.jobs.container.JobInfo; import com.gamingmesh.jobs.container.JobProgression; import com.gamingmesh.jobs.container.JobsPlayer; import com.gamingmesh.jobs.stuff.PageInfo; -import com.gamingmesh.jobs.stuff.RawMessage; public class JobsCommands implements CommandExecutor { private static final String label = "jobs"; diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/archive.java b/src/main/java/com/gamingmesh/jobs/commands/list/archive.java index 49275055..065c3b12 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/archive.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/archive.java @@ -7,11 +7,12 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import com.gamingmesh.jobs.Jobs; +import com.gamingmesh.jobs.CMILib.RawMessage; import com.gamingmesh.jobs.commands.Cmd; import com.gamingmesh.jobs.commands.JobCommand; import com.gamingmesh.jobs.container.JobProgression; import com.gamingmesh.jobs.container.JobsPlayer; -import com.gamingmesh.jobs.stuff.RawMessage; + public class archive implements Cmd { diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/browse.java b/src/main/java/com/gamingmesh/jobs/commands/list/browse.java index 3dbb02c2..5a98798e 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/browse.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/browse.java @@ -13,7 +13,7 @@ import com.gamingmesh.jobs.commands.JobCommand; import com.gamingmesh.jobs.container.Job; import com.gamingmesh.jobs.stuff.ChatColor; import com.gamingmesh.jobs.stuff.PageInfo; -import com.gamingmesh.jobs.stuff.RawMessage; +import com.gamingmesh.jobs.CMILib.RawMessage; public class browse implements Cmd { diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/editjobs.java b/src/main/java/com/gamingmesh/jobs/commands/list/editjobs.java index c6eead7f..8333558b 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/editjobs.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/editjobs.java @@ -22,9 +22,9 @@ import com.gamingmesh.jobs.container.JobInfo; import com.gamingmesh.jobs.stuff.ChatColor; import com.gamingmesh.jobs.stuff.Debug; import com.gamingmesh.jobs.stuff.PageInfo; -import com.gamingmesh.jobs.stuff.RawMessage; +import com.gamingmesh.jobs.CMILib.RawMessage; import com.gamingmesh.jobs.stuff.Util; -import com.gamingmesh.jobs.stuff.VersionChecker.Version; +import com.gamingmesh.jobs.CMILib.VersionChecker.Version; public class editjobs implements Cmd { diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/gtop.java b/src/main/java/com/gamingmesh/jobs/commands/list/gtop.java index 04b43fc6..4d19fae2 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/gtop.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/gtop.java @@ -11,7 +11,7 @@ import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.commands.Cmd; import com.gamingmesh.jobs.commands.JobCommand; import com.gamingmesh.jobs.container.TopList; -import com.gamingmesh.jobs.stuff.RawMessage; +import com.gamingmesh.jobs.CMILib.RawMessage; public class gtop implements Cmd { diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/itembonus.java b/src/main/java/com/gamingmesh/jobs/commands/list/itembonus.java index f735da38..206a0a40 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/itembonus.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/itembonus.java @@ -16,7 +16,7 @@ import com.gamingmesh.jobs.container.CurrencyType; import com.gamingmesh.jobs.container.Job; import com.gamingmesh.jobs.container.JobsPlayer; import com.gamingmesh.jobs.stuff.ChatColor; -import com.gamingmesh.jobs.stuff.RawMessage; +import com.gamingmesh.jobs.CMILib.RawMessage; public class itembonus implements Cmd { diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/join.java b/src/main/java/com/gamingmesh/jobs/commands/list/join.java index 849d7044..6d22d360 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/join.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/join.java @@ -10,7 +10,7 @@ import com.gamingmesh.jobs.commands.JobCommand; import com.gamingmesh.jobs.container.Job; import com.gamingmesh.jobs.container.JobProgression; import com.gamingmesh.jobs.container.JobsPlayer; -import com.gamingmesh.jobs.stuff.RawMessage; +import com.gamingmesh.jobs.CMILib.RawMessage; public class join implements Cmd { diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/quests.java b/src/main/java/com/gamingmesh/jobs/commands/list/quests.java index 96d694c0..a8c6c34e 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/quests.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/quests.java @@ -12,99 +12,99 @@ import com.gamingmesh.jobs.commands.JobCommand; import com.gamingmesh.jobs.container.JobProgression; import com.gamingmesh.jobs.container.JobsPlayer; import com.gamingmesh.jobs.container.QuestProgression; -import com.gamingmesh.jobs.stuff.RawMessage; +import com.gamingmesh.jobs.CMILib.RawMessage; import com.gamingmesh.jobs.stuff.TimeManage; public class quests implements Cmd { - @Override - @JobCommand(400) - public boolean perform(Jobs plugin, final CommandSender sender, final String[] args) { - JobsPlayer jPlayer = null; + @Override + @JobCommand(400) + public boolean perform(Jobs plugin, final CommandSender sender, final String[] args) { + JobsPlayer jPlayer = null; - if (args.length >= 1 && args[0].equals("next")) { + if (args.length >= 1 && args[0].equals("next")) { - jPlayer = Jobs.getPlayerManager().getJobsPlayer((Player) sender); - jPlayer.resetQuests(); - } else { - if (args.length >= 1) { - if (!Jobs.hasPermission(sender, "jobs.command.admin.quests", true)) { - return true; - } - jPlayer = Jobs.getPlayerManager().getJobsPlayer(args[0]); - } else if (sender instanceof Player) { - jPlayer = Jobs.getPlayerManager().getJobsPlayer((Player) sender); - } + jPlayer = Jobs.getPlayerManager().getJobsPlayer((Player) sender); + jPlayer.resetQuests(); + } else { + if (args.length >= 1) { + if (!Jobs.hasPermission(sender, "jobs.command.admin.quests", true)) { + return true; } - - if (jPlayer == null) { - if (args.length >= 1) - sender.sendMessage(Jobs.getLanguage().getMessage("general.error.noinfo")); - else - Jobs.getCommandManager().sendUsage(sender, "quests"); - return true; - } - - if (jPlayer.getQuestProgressions().isEmpty()) { - sender.sendMessage(Jobs.getLanguage().getMessage("command.quests.error.noquests")); - return true; - } - - sender.sendMessage(Jobs.getLanguage().getMessage("command.quests.toplineseparator", "[playerName]", jPlayer.getUserName(), "[questsDone]", jPlayer.getDoneQuests())); - if (sender instanceof Player) { - for (JobProgression jobProg : jPlayer.getJobProgression()) { - List list = jPlayer.getQuestProgressions(jobProg.getJob()); - for (QuestProgression q : list) { - String progressLine = Jobs.getCommandManager().jobProgressMessage(q.getQuest().getAmount(), q.getAmountDone()); - if (q.isComplited()) - progressLine = Jobs.getLanguage().getMessage("command.quests.output.completed"); - RawMessage rm = new RawMessage(); - String msg = Jobs.getLanguage().getMessage("command.quests.output.questLine", "[progress]", - progressLine, "[questName]", q.getQuest().getQuestName(), "[done]", q.getAmountDone(), "[required]", q.getQuest().getAmount()); - - List hoverMsgs = Jobs.getLanguage().getMessageList("command.quests.output.hover"); - List hoverList = new ArrayList<>(); - - for (int i = 0; i < hoverMsgs.size(); i++) { - String current = hoverMsgs.get(i); - current = current.replace("[jobName]", jobProg.getJob().getName()); - current = current.replace("[time]", TimeManage.to24hourShort(q.getValidUntil() - System.currentTimeMillis())); - if (current.contains("[desc]")) { - for (String one : q.getQuest().getDescription()) { - hoverList.add(one); - } - } else - hoverList.add(current); - } - - String hover = ""; - - for (String one : hoverList) { - if (!hover.isEmpty()) - hover += "\n"; - hover += one; - } - - /* - hover += "&f" + jobProg.getJob().getName(); - if (!q.getQuest().getDescription().isEmpty()) { - - for (String one : q.getQuest().getDescription()) { - hover += "\n&7"; - hover += one; - } - } - hover += "\n&7New quest in: &8" + - TimeManage.to24hourShort(q.getValidUntil() - - System.currentTimeMillis()); - */ - rm.add(msg, hover); - rm.show(sender); - } - } - } else - return true; - sender.sendMessage(Jobs.getLanguage().getMessage("general.info.separator")); - return true; + jPlayer = Jobs.getPlayerManager().getJobsPlayer(args[0]); + } else if (sender instanceof Player) { + jPlayer = Jobs.getPlayerManager().getJobsPlayer((Player) sender); + } } + + if (jPlayer == null) { + if (args.length >= 1) + sender.sendMessage(Jobs.getLanguage().getMessage("general.error.noinfo")); + else + Jobs.getCommandManager().sendUsage(sender, "quests"); + return true; + } + + if (jPlayer.getQuestProgressions().isEmpty()) { + sender.sendMessage(Jobs.getLanguage().getMessage("command.quests.error.noquests")); + return true; + } + + sender.sendMessage(Jobs.getLanguage().getMessage("command.quests.toplineseparator", "[playerName]", jPlayer.getUserName(), "[questsDone]", jPlayer.getDoneQuests())); + if (sender instanceof Player) { + for (JobProgression jobProg : jPlayer.getJobProgression()) { + List list = jPlayer.getQuestProgressions(jobProg.getJob()); + for (QuestProgression q : list) { + String progressLine = Jobs.getCommandManager().jobProgressMessage(q.getQuest().getAmount(), q.getAmountDone()); + if (q.isComplited()) + progressLine = Jobs.getLanguage().getMessage("command.quests.output.completed"); + RawMessage rm = new RawMessage(); + String msg = Jobs.getLanguage().getMessage("command.quests.output.questLine", "[progress]", + progressLine, "[questName]", q.getQuest().getQuestName(), "[done]", q.getAmountDone(), "[required]", q.getQuest().getAmount()); + + List hoverMsgs = Jobs.getLanguage().getMessageList("command.quests.output.hover"); + List hoverList = new ArrayList<>(); + + for (int i = 0; i < hoverMsgs.size(); i++) { + String current = hoverMsgs.get(i); + current = current.replace("[jobName]", jobProg.getJob().getName()); + current = current.replace("[time]", TimeManage.to24hourShort(q.getValidUntil() - System.currentTimeMillis())); + if (current.contains("[desc]")) { + for (String one : q.getQuest().getDescription()) { + hoverList.add(one); + } + } else + hoverList.add(current); + } + + String hover = ""; + + for (String one : hoverList) { + if (!hover.isEmpty()) + hover += "\n"; + hover += one; + } + + /* + hover += "&f" + jobProg.getJob().getName(); + if (!q.getQuest().getDescription().isEmpty()) { + + for (String one : q.getQuest().getDescription()) { + hover += "\n&7"; + hover += one; + } + } + hover += "\n&7New quest in: &8" + + TimeManage.to24hourShort(q.getValidUntil() - + System.currentTimeMillis()); + */ + rm.add(msg, hover); + rm.show(sender); + } + } + } else + return true; + sender.sendMessage(Jobs.getLanguage().getMessage("general.info.separator")); + return true; + } } diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/top.java b/src/main/java/com/gamingmesh/jobs/commands/list/top.java index 00984066..0db3ad08 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/top.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/top.java @@ -12,7 +12,7 @@ import com.gamingmesh.jobs.commands.Cmd; import com.gamingmesh.jobs.commands.JobCommand; import com.gamingmesh.jobs.container.Job; import com.gamingmesh.jobs.container.TopList; -import com.gamingmesh.jobs.stuff.RawMessage; +import com.gamingmesh.jobs.CMILib.RawMessage; public class top implements Cmd { diff --git a/src/main/java/com/gamingmesh/jobs/config/BossBarManager.java b/src/main/java/com/gamingmesh/jobs/config/BossBarManager.java index 6cd1f3f5..4f154173 100644 --- a/src/main/java/com/gamingmesh/jobs/config/BossBarManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/BossBarManager.java @@ -17,7 +17,7 @@ import com.gamingmesh.jobs.container.BossBarInfo; import com.gamingmesh.jobs.container.Job; import com.gamingmesh.jobs.container.JobProgression; import com.gamingmesh.jobs.container.JobsPlayer; -import com.gamingmesh.jobs.stuff.VersionChecker.Version; +import com.gamingmesh.jobs.CMILib.VersionChecker.Version; public class BossBarManager { diff --git a/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java b/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java index e9ca3a26..8d8b361e 100644 --- a/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java @@ -59,7 +59,7 @@ import com.gamingmesh.jobs.container.JobPermission; import com.gamingmesh.jobs.container.Quest; import com.gamingmesh.jobs.resources.jfep.Parser; import com.gamingmesh.jobs.stuff.ChatColor; -import com.gamingmesh.jobs.stuff.VersionChecker.Version; +import com.gamingmesh.jobs.CMILib.VersionChecker.Version; public class ConfigManager { private Jobs plugin; diff --git a/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java b/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java index 362363ed..276a4d15 100644 --- a/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java @@ -40,7 +40,7 @@ import com.gamingmesh.jobs.container.CurrencyLimit; import com.gamingmesh.jobs.container.CurrencyType; import com.gamingmesh.jobs.container.LocaleReader; import com.gamingmesh.jobs.container.Schedule; -import com.gamingmesh.jobs.stuff.VersionChecker.Version; +import com.gamingmesh.jobs.CMILib.VersionChecker.Version; public class GeneralConfigManager { private Jobs plugin; diff --git a/src/main/java/com/gamingmesh/jobs/economy/BufferedEconomy.java b/src/main/java/com/gamingmesh/jobs/economy/BufferedEconomy.java index e4944c74..e51df960 100644 --- a/src/main/java/com/gamingmesh/jobs/economy/BufferedEconomy.java +++ b/src/main/java/com/gamingmesh/jobs/economy/BufferedEconomy.java @@ -26,10 +26,13 @@ import java.util.concurrent.LinkedBlockingQueue; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; + import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.api.JobsPaymentEvent; import com.gamingmesh.jobs.container.JobsPlayer; -import com.gamingmesh.jobs.stuff.VersionChecker.Version; +import com.gamingmesh.jobs.CMILib.ActionBarTitleMessages; +import com.gamingmesh.jobs.CMILib.VersionChecker.Version; import com.gamingmesh.jobs.tasks.BufferedPaymentTask; public class BufferedEconomy { @@ -144,7 +147,8 @@ public class BufferedEconomy { if (!Jobs.getActionbarToggleList().containsKey(ServerTaxesAccountname) && Jobs.getGCManager().ActionBarsMessageByDefault) Jobs.getActionbarToggleList().put(ServerTaxesAccountname, true); if (Jobs.getActionbarToggleList().containsKey(ServerTaxesAccountname) && Jobs.getActionbarToggleList().get(ServerTaxesAccountname)) { - Jobs.getActionBar().send(Bukkit.getPlayer(ServerAccountname), Jobs.getLanguage().getMessage("message.taxes", "[amount]", (int) (TotalAmount * 100) + Jobs.getActionBar(); + ActionBarTitleMessages.send(Bukkit.getPlayer(ServerAccountname), Jobs.getLanguage().getMessage("message.taxes", "[amount]", (int) (TotalAmount * 100) / 100.0)); } } @@ -177,7 +181,8 @@ public class BufferedEconomy { if (Jobs.getGCManager().UseServerAccount) { if (!hasMoney) { - Jobs.getActionBar().send(payment.getOfflinePlayer().getPlayer(), Jobs.getLanguage().getMessage("economy.error.nomoney")); + Jobs.getActionBar(); + ActionBarTitleMessages.send(payment.getOfflinePlayer().getPlayer(), Jobs.getLanguage().getMessage("economy.error.nomoney")); continue; } if (Jobs.getGCManager().isEconomyAsync()) { @@ -192,7 +197,7 @@ public class BufferedEconomy { } try { // Action bar stuff - Jobs.getActionBar().ShowActionBar(payment); + ShowActionBar(payment); if (payment.getOfflinePlayer().isOnline() && Jobs.getVersionCheckManager().getVersion().isHigher(Version.v1_8_R3)) { JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(payment.getOfflinePlayer().getUniqueId()); Jobs.getBBManager().ShowJobProgression(jPlayer); @@ -205,4 +210,39 @@ public class BufferedEconomy { } } + + public void ShowActionBar(BufferedPayment payment) { + if (!payment.getOfflinePlayer().isOnline()) { + return; + } + String playername = payment.getOfflinePlayer().getName(); + if ((!Jobs.getActionbarToggleList().containsKey(playername)) && (Jobs.getGCManager().ActionBarsMessageByDefault)) { + Jobs.getActionbarToggleList().put(playername, Boolean.valueOf(true)); + } + if (playername == null) { + return; + } + if (!Jobs.getActionbarToggleList().containsKey(playername)) { + return; + } + Boolean show = Jobs.getActionbarToggleList().get(playername); + Player abp = Bukkit.getPlayer(payment.getOfflinePlayer().getUniqueId()); + if ((abp != null) && (show.booleanValue())) { + String Message = Jobs.getLanguage().getMessage("command.toggle.output.paid.main"); + if (payment.getAmount() != 0.0D) { + Message = Message + " " + Jobs.getLanguage().getMessage("command.toggle.output.paid.money", new Object[] { "[amount]", String.format(Jobs.getGCManager().getDecimalPlacesMoney(), + new Object[] { Double.valueOf(payment + .getAmount()) }) }); + } + if (payment.getPoints() != 0.0D) { + Message = Message + " " + Jobs.getLanguage().getMessage("command.toggle.output.paid.points", new Object[] { "[points]", String.format(Jobs.getGCManager().getDecimalPlacesPoints(), + new Object[] { Double.valueOf(payment.getPoints()) }) }); + } + if (payment.getExp() != 0.0D) { + Message = Message + " " + Jobs.getLanguage().getMessage("command.toggle.output.paid.exp", new Object[] { "[exp]", String.format(Jobs.getGCManager().getDecimalPlacesExp(), new Object[] { + Double.valueOf(payment.getExp()) }) }); + } + ActionBarTitleMessages.send(abp, Message); + } + } } diff --git a/src/main/java/com/gamingmesh/jobs/stuff/ActionBar.java b/src/main/java/com/gamingmesh/jobs/stuff/ActionBar.java deleted file mode 100644 index b0ab42e0..00000000 --- a/src/main/java/com/gamingmesh/jobs/stuff/ActionBar.java +++ /dev/null @@ -1,153 +0,0 @@ -package com.gamingmesh.jobs.stuff; - -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.logging.Level; - -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -import com.gamingmesh.jobs.Jobs; -import com.gamingmesh.jobs.economy.BufferedPayment; -import com.gamingmesh.jobs.stuff.VersionChecker.Version; - -/** -* -* @author hamzaxx -*/ -public class ActionBar { - private Version version = Version.v1_11_R1; - private Object packet; - private Method getHandle; - private Method sendPacket; - private Field playerConnection; - private Class nmsChatSerializer; - private Class nmsIChatBaseComponent; - private Class packetType; - - private Class ChatMessageclz; - private Class sub; - private Object[] consts; - - public ActionBar() { - try { - version = Jobs.getVersionCheckManager().getVersion(); - packetType = Class.forName(getPacketPlayOutChat()); - Class typeCraftPlayer = Class.forName(getCraftPlayerClasspath()); - Class typeNMSPlayer = Class.forName(getNMSPlayerClasspath()); - Class typePlayerConnection = Class.forName(getPlayerConnectionClasspath()); - nmsChatSerializer = Class.forName(getChatSerializerClasspath()); - nmsIChatBaseComponent = Class.forName(getIChatBaseComponentClasspath()); - getHandle = typeCraftPlayer.getMethod("getHandle"); - playerConnection = typeNMSPlayer.getField("playerConnection"); - sendPacket = typePlayerConnection.getMethod("sendPacket", Class.forName(getPacketClasspath())); - - if (Jobs.getVersionCheckManager().getVersion().isHigher(Version.v1_11_R1)) { - ChatMessageclz = Class.forName(getChatMessageTypeClasspath()); - consts = ChatMessageclz.getEnumConstants(); - sub = consts[2].getClass(); - } - - } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | NoSuchFieldException ex) { - Bukkit.getLogger().log(Level.SEVERE, "Error {0}", ex); - } - - } - - public void ShowActionBar(BufferedPayment payment) { - - if (!payment.getOfflinePlayer().isOnline()) - return; - - String playername = payment.getOfflinePlayer().getName(); - if (!Jobs.getActionbarToggleList().containsKey(playername) && Jobs.getGCManager().ActionBarsMessageByDefault) - Jobs.getActionbarToggleList().put(playername, true); - - if (playername == null) - return; - - if (!Jobs.getActionbarToggleList().containsKey(playername)) - return; - - Boolean show = Jobs.getActionbarToggleList().get(playername); - Player abp = Bukkit.getPlayer(payment.getOfflinePlayer().getUniqueId()); - - if (abp != null && show) { - String Message = Jobs.getLanguage().getMessage("command.toggle.output.paid.main"); - if (payment.getAmount() != 0D) - Message = Message + " " + Jobs.getLanguage().getMessage("command.toggle.output.paid.money", "[amount]", String.format(Jobs.getGCManager().getDecimalPlacesMoney(), payment - .getAmount())); - if (payment.getPoints() != 0D) - Message = Message + " " + Jobs.getLanguage().getMessage("command.toggle.output.paid.points", "[points]", String.format(Jobs.getGCManager().getDecimalPlacesPoints(), payment.getPoints())); - if (payment.getExp() != 0D) - Message = Message + " " + Jobs.getLanguage().getMessage("command.toggle.output.paid.exp", "[exp]", String.format(Jobs.getGCManager().getDecimalPlacesExp(), payment.getExp())); - send(abp, Message); - } - } - - public void send(CommandSender receivingPacket, String msg) { - try { - if (msg == null || nmsChatSerializer == null || msg.isEmpty()) - return; - - if (receivingPacket == null) - return; - - if (version.isLower(Version.v1_8_R1) || !(receivingPacket instanceof Player)) { - receivingPacket.sendMessage(ChatColor.translateAlternateColorCodes('&', msg)); - return; - } - - Object serialized = nmsChatSerializer.getMethod("a", String.class).invoke(null, "{\"text\": \"" + ChatColor.translateAlternateColorCodes('&', msg) + "\"}"); - if (Jobs.getVersionCheckManager().getVersion().isHigher(Version.v1_11_R1)) - packet = packetType.getConstructor(nmsIChatBaseComponent, sub).newInstance(serialized, consts[2]); - else if (version.isHigher(Version.v1_7_R4)) { - packet = packetType.getConstructor(nmsIChatBaseComponent, byte.class).newInstance(serialized, (byte) 2); - } else { - packet = packetType.getConstructor(nmsIChatBaseComponent, int.class).newInstance(serialized, 2); - } - Object player = getHandle.invoke(receivingPacket); - Object connection = playerConnection.get(player); - sendPacket.invoke(connection, packet); - } catch (SecurityException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | InstantiationException | NoSuchMethodException ex) { - Bukkit.getLogger().log(Level.SEVERE, "Error {0}", ex); - } - } - - private String getCraftPlayerClasspath() { - return "org.bukkit.craftbukkit." + version.name() + ".entity.CraftPlayer"; - } - - private String getPlayerConnectionClasspath() { - return "net.minecraft.server." + version.name() + ".PlayerConnection"; - } - - private String getNMSPlayerClasspath() { - return "net.minecraft.server." + version.name() + ".EntityPlayer"; - } - - private String getPacketClasspath() { - return "net.minecraft.server." + version.name() + ".Packet"; - } - - private String getIChatBaseComponentClasspath() { - return "net.minecraft.server." + version.name() + ".IChatBaseComponent"; - } - - private String getChatSerializerClasspath() { - if (!Jobs.getVersionCheckManager().getVersion().isHigher(Version.v1_8_R2)) - return "net.minecraft.server." + version.name() + ".ChatSerializer"; - return "net.minecraft.server." + version.name() + ".IChatBaseComponent$ChatSerializer";// 1_8_R2 moved to IChatBaseComponent - } - - private String getPacketPlayOutChat() { - return "net.minecraft.server." + version.name() + ".PacketPlayOutChat"; - } - - private String getChatMessageTypeClasspath() { - return "net.minecraft.server." + version.name() + ".ChatMessageType"; - } -} \ No newline at end of file diff --git a/src/main/java/com/gamingmesh/jobs/stuff/CMIScoreboardManager.java b/src/main/java/com/gamingmesh/jobs/stuff/CMIScoreboardManager.java index d6546891..e93bfa9a 100644 --- a/src/main/java/com/gamingmesh/jobs/stuff/CMIScoreboardManager.java +++ b/src/main/java/com/gamingmesh/jobs/stuff/CMIScoreboardManager.java @@ -19,7 +19,7 @@ import org.bukkit.scoreboard.Scoreboard; import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.container.ScoreboardInfo; -import com.gamingmesh.jobs.stuff.VersionChecker.Version; +import com.gamingmesh.jobs.CMILib.VersionChecker.Version; public class CMIScoreboardManager { diff --git a/src/main/java/com/gamingmesh/jobs/stuff/RawMessage.java b/src/main/java/com/gamingmesh/jobs/stuff/RawMessage.java deleted file mode 100644 index ebff0b39..00000000 --- a/src/main/java/com/gamingmesh/jobs/stuff/RawMessage.java +++ /dev/null @@ -1,118 +0,0 @@ -package com.gamingmesh.jobs.stuff; - -import java.util.ArrayList; -import java.util.List; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class RawMessage { - - private List parts = new ArrayList<>(); - private List cleanParts = new ArrayList<>(); - private String combined = ""; - private String combinedClean = ""; - - public void clear() { - parts = new ArrayList<>(); - cleanParts = new ArrayList<>(); - combined = ""; - combinedClean = ""; - } - - public RawMessage add(String text) { - return add(text, null, null, null, null); - } - - public RawMessage add(String text, String hoverText) { - return add(text, hoverText, null, null, null); - } - - public RawMessage add(String text, String hoverText, String command) { - return add(text, hoverText, command, null, null); - } - - public RawMessage add(String text, String hoverText, String command, String suggestion) { - return add(text, hoverText, command, suggestion, null); - } - - public RawMessage add(String text, String hoverText, String command, String suggestion, String url) { - if (text == null) - return this; - String f = "{\"text\":\"" + ChatColor.translateAlternateColorCodes('&', text) + "\""; - String last = ChatColor.getLastColors(ChatColor.translateAlternateColorCodes('&', text)); - if (last != null && !last.isEmpty()) { - ChatColor color = ChatColor.getByChar(last.replace("�", "")); - if (color != null) { - f += ",\"color\":\"" + color.name().toLowerCase() + "\""; - } - } - if (hoverText != null) - f += ",\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"" + ChatColor.translateAlternateColorCodes('&', hoverText) + "\"}]}}"; - if (suggestion != null) - f += ",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"" + suggestion + "\"}"; - - if (url != null) { - f += ",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"" + url + "\"}"; - } - - if (command != null) { - if (!command.startsWith("/")) - command = "/" + command; - f += ",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"" + command + "\"}"; - } - f += "}"; - parts.add(f); - cleanParts.add(ChatColor.translateAlternateColorCodes('&', text)); - return this; - } - - public RawMessage combine() { - String f = ""; - for (String part : parts) { - if (f.isEmpty()) - f = "[\"\","; - else - f += ","; - f += part; - } - if (!f.isEmpty()) - f += "]"; - combined = f; - return this; - } - - public RawMessage combineClean() { - String f = ""; - for (String part : cleanParts) { - f += part; - } - combinedClean = f; - return this; - } - - public RawMessage show(Player player) { - if (combined.isEmpty()) - combine(); - Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + player.getName() + " " + combined); - return this; - } - - public RawMessage show(CommandSender sender) { - if (combined.isEmpty()) - combine(); - if (sender instanceof Player) - show((Player) sender); - else - sender.sendMessage(this.combineClean().combinedClean); - return this; - } - - public String getRaw() { - if (combined.isEmpty()) - combine(); - return combined; - } - -} diff --git a/src/main/java/com/gamingmesh/jobs/stuff/VersionChecker.java b/src/main/java/com/gamingmesh/jobs/stuff/VersionChecker.java deleted file mode 100644 index 8e1d97d1..00000000 --- a/src/main/java/com/gamingmesh/jobs/stuff/VersionChecker.java +++ /dev/null @@ -1,164 +0,0 @@ -package com.gamingmesh.jobs.stuff; - -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; -import java.util.Arrays; -import java.util.List; - -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; - -import com.gamingmesh.jobs.Jobs; - -public class VersionChecker { - private Jobs plugin; - private int resource = 4216; - private static Version version = null; - - public VersionChecker(Jobs plugin) { - this.plugin = plugin; - version = getCurrent(); - } - - public Version getVersion() { - return version; - } - - public enum Version { - v1_7_R1(171, "v1_7"), - v1_7_R2(172, "v1_7"), - v1_7_R3(173, "v1_7"), - v1_7_R4(174, "v1_7"), - v1_8_R1(181, "v1_8"), - v1_8_R2(182, "v1_8"), - v1_8_R3(183, "v1_8"), - v1_9_R1(191, "v1_9"), - v1_9_R2(192, "v1_9"), - v1_10_R1(1101, "v1_10"), - v1_11_R1(1111, "v1_11"), - v1_11_R2(1112, "v1_11"), - v1_12_R1(1121, "v1_12"), - v1_12_R2(1122, "v1_12"), - v1_13_R1(1131, "v1_13"), - v1_13_R2(1132, "v1_13"), - v1_14_R1(1141, "v1_14"), - v1_14_R2(1142, "v1_14"), - v1_15_R1(1151, "v1_15"), - v1_15_R2(1152, "v1_15"); - - private Integer value; - private String shortVersion; - - Version(Integer value, String shortVersion) { - this.value = value; - this.shortVersion = shortVersion; - } - - public Integer getValue() { - return value; - } - - public String getShortVersion() { - return shortVersion; - } - - public static Version getCurrent() { - String[] v = Bukkit.getServer().getClass().getPackage().getName().split("\\."); - String vv = v[v.length - 1]; - for (Version one : values()) { - if (one.name().equalsIgnoreCase(vv)) - return one; - } - return null; - } - - public boolean isLower(Version version) { - return getValue() < version.getValue(); - } - - public boolean isHigher(Version version) { - return getValue() > version.getValue(); - } - - public boolean isEqualOrLower(Version version) { - return getValue() <= version.getValue(); - } - - public boolean isEqualOrHigher(Version version) { - return getValue() >= version.getValue(); - } - - public static boolean isCurrentEqualOrHigher(Version version) { - return VersionChecker.version.getValue() >= version.getValue(); - } - } - - public static Version getCurrent() { - String[] v = Bukkit.getServer().getClass().getPackage().getName().split("\\."); - String vv = v[v.length - 1]; - for (Version one : Version.values()) { - if (one.name().equalsIgnoreCase(vv)) { - return one; - } - } - return null; - } - - public boolean isLower(Version version) { - return VersionChecker.version.getValue() < version.getValue(); - } - - public boolean isLowerEquals(Version version) { - return VersionChecker.version.getValue() <= version.getValue(); - } - - public boolean isHigher(Version version) { - return VersionChecker.version.getValue() > version.getValue(); - } - - public boolean isHigherEquals(Version version) { - return VersionChecker.version.getValue() >= version.getValue(); - } - - public void VersionCheck(final Player player) { - if (!Jobs.getGCManager().isShowNewVersion()) - return; - - Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { - @Override - public void run() { - String currentVersion = plugin.getDescription().getVersion(); - String newVersion = getNewVersion(); - if (newVersion == null || newVersion.equalsIgnoreCase(currentVersion)) - return; - List msg = Arrays.asList( - "&a*********************** &e" + plugin.getDescription().getName() + "&a **************************", - "&a* &e" + newVersion + " is now available! Your version: &e" + currentVersion, - "&a* &2" + plugin.getDescription().getWebsite(), - "&a* Or get the dev. builds from &2https://github.com/Zrips/Jobs/releases", - "&a************************************************************"); - for (String one : msg) - if (player != null) - player.sendMessage(org.bukkit.ChatColor.translateAlternateColorCodes('&', one)); - } - }); - } - - public String getNewVersion() { - try { - HttpURLConnection con = (HttpURLConnection) new URL("https://www.spigotmc.org/api/general.php").openConnection(); - con.setDoOutput(true); - con.setRequestMethod("POST"); - con.getOutputStream().write(("key=98BE0FE67F88AB82B4C197FAF1DC3B69206EFDCC4D3B80FC83A00037510B99B4&resource=" + resource).getBytes("UTF-8")); - String version = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine(); - if (version.length() <= 9) - return version; - } catch (Exception ex) { - Jobs.consoleMsg("&cFailed to check for " + plugin.getDescription().getName() + " update on spigot web page."); - } - return null; - } - -}