diff --git a/src/main/java/me/libraryaddict/disguise/LibsDisguises.java b/src/main/java/me/libraryaddict/disguise/LibsDisguises.java index e3f0403e..1c4e4d8c 100644 --- a/src/main/java/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/main/java/me/libraryaddict/disguise/LibsDisguises.java @@ -80,17 +80,6 @@ public class LibsDisguises extends JavaPlugin { "reloads gracefully!"); } - String version = ProtocolLibrary.getPlugin().getDescription().getVersion(); - - String requiredProtocolLib = "4.5.1"; - - if (DisguiseUtilities.isOlderThan(requiredProtocolLib, version)) { - getLogger().severe("!! Attention please !!"); - getLogger().severe("Update your ProtocolLib! You are running " + version + - " but the minimum version you should be on is " + requiredProtocolLib + "!"); - getLogger().severe("!! Attention please !!"); - } - try { Class cl = Class.forName("org.bukkit.Server$Spigot"); } @@ -131,6 +120,19 @@ public class LibsDisguises extends JavaPlugin { return; } + String requiredProtocolLib = DisguiseUtilities.getProtocolLibRequiredVersion(); + String version = ProtocolLibrary.getPlugin().getDescription().getVersion(); + + if (DisguiseUtilities.isOlderThan(requiredProtocolLib, version)) { + getLogger().severe("!! May I have your attention please !!"); + getLogger().severe("Update your ProtocolLib! You are running " + version + + " but the minimum version you should be on is " + requiredProtocolLib + "!"); + getLogger() + .severe("https://ci.dmulloy2.net/job/ProtocolLib/lastSuccessfulBuild/artifact/target/ProtocolLib" + + ".jar"); + getLogger().severe("!! May I have your attention please !!"); + } + // If this is a release build, even if jenkins build.. if (isReleaseBuild()) { // If downloaded from spigot, forcibly set release build to true diff --git a/src/main/java/me/libraryaddict/disguise/commands/LibsDisguisesCommand.java b/src/main/java/me/libraryaddict/disguise/commands/LibsDisguisesCommand.java index f521e010..ab51280b 100644 --- a/src/main/java/me/libraryaddict/disguise/commands/LibsDisguisesCommand.java +++ b/src/main/java/me/libraryaddict/disguise/commands/LibsDisguisesCommand.java @@ -34,6 +34,7 @@ public class LibsDisguisesCommand implements CommandExecutor, TabCompleter { getCommands().add(new LDMetaInfo()); getCommands().add(new LDDebugPlayer()); getCommands().add(new LDUploadLogs()); + getCommands().add(new LDUpdateProtocolLib()); } protected ArrayList filterTabs(ArrayList list, String[] origArgs) { diff --git a/src/main/java/me/libraryaddict/disguise/commands/libsdisguises/LDUpdateProtocolLib.java b/src/main/java/me/libraryaddict/disguise/commands/libsdisguises/LDUpdateProtocolLib.java new file mode 100644 index 00000000..d1fc8eac --- /dev/null +++ b/src/main/java/me/libraryaddict/disguise/commands/libsdisguises/LDUpdateProtocolLib.java @@ -0,0 +1,107 @@ +package me.libraryaddict.disguise.commands.libsdisguises; + +import com.comphenix.protocol.ProtocolLibrary; +import me.libraryaddict.disguise.LibsDisguises; +import me.libraryaddict.disguise.utilities.DisguiseUtilities; +import me.libraryaddict.disguise.utilities.LibsPremium; +import me.libraryaddict.disguise.utilities.plugin.PluginInformation; +import me.libraryaddict.disguise.utilities.translations.LibsMsg; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitRunnable; + +import java.io.File; +import java.io.InputStream; +import java.lang.reflect.Method; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * Created by libraryaddict on 30/06/2020. + */ +public class LDUpdateProtocolLib implements LDCommand { + private final AtomicBoolean updateInProgress = new AtomicBoolean(false); + + @Override + public List getTabComplete() { + return Arrays.asList("updateprotocollib"); + } + + @Override + public boolean hasPermission(CommandSender sender) { + return sender.isOp(); + } + + @Override + public String getPermission() { + return null; + } + + @Override + public void onCommand(CommandSender sender, String[] args) { + if (updateInProgress.get()) { + sender.sendMessage(ChatColor.RED + "Update already in progress"); + return; + } + + sender.sendMessage(ChatColor.RED + "Please hold, now downloading.."); + + new BukkitRunnable() { + @Override + public void run() { + File protocolLibFile = null; + + try { + Method getFile = JavaPlugin.class.getDeclaredMethod("getFile"); + getFile.setAccessible(true); + File theirFile = (File) getFile.invoke(ProtocolLibrary.getPlugin()); + File dest = new File("plugins/update/" + theirFile.getName()); + + // We're connecting to spigot's API + URL url = new URL( + "https://ci.dmulloy2.net/job/ProtocolLib/lastSuccessfulBuild/artifact/target/ProtocolLib" + + ".jar"); + // Creating a connection + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestProperty("User-Agent", "libraryaddict/LibsDisguises"); + + // Get the input stream, what we receive + try (InputStream input = con.getInputStream()) { + Files.copy(input, dest.toPath(), StandardCopyOption.REPLACE_EXISTING); + } + + new BukkitRunnable() { + @Override + public void run() { + sender.sendMessage(ChatColor.RED + "Update success! Restart server to finish update!"); + } + }.runTask(LibsDisguises.getInstance()); + } + catch (Exception ex) { + ex.printStackTrace(); + + new BukkitRunnable() { + @Override + public void run() { + sender.sendMessage(ChatColor.RED + "Update failed, " + ex.getMessage()); + } + }.runTask(LibsDisguises.getInstance()); + } + finally { + updateInProgress.set(false); + } + } + }.runTaskAsynchronously(LibsDisguises.getInstance()); + } + + @Override + public LibsMsg getHelp() { + return LibsMsg.LD_COMMAND_UPDATEPROTOCOLLIB; + } +} diff --git a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index 05cdee97..ad7130fb 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -35,6 +35,8 @@ import me.libraryaddict.disguise.utilities.reflection.NmsVersion; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; import me.libraryaddict.disguise.utilities.translations.LibsMsg; import me.libraryaddict.disguise.utilities.watchers.CompileMethods; +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.api.chat.ComponentBuilder; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.math.RandomUtils; import org.apache.logging.log4j.util.Strings; @@ -152,6 +154,7 @@ public class DisguiseUtilities { private static boolean invalidFile; @Getter private static char[] alphabet = "0123456789abcdefghijklmnopqrstuvwxyz".toCharArray(); + private static Pattern hexColor; public static void setPlayerVelocity(Player player) { if (player == null) { @@ -163,6 +166,10 @@ public class DisguiseUtilities { } } + public static String getProtocolLibRequiredVersion() { + return NmsVersion.v1_16.isSupported() ? "4.6.0" : "4.5.1"; + } + /** * Returns if this velocity is due to a PlayerVelocityEvent */ @@ -972,7 +979,10 @@ public class DisguiseUtilities { runningPaper = Class.forName("com.destroystokyo.paper.VersionHistoryManager$VersionData") != null; } catch (Exception ex) { + } + if (NmsVersion.v1_16.isSupported()) { + hexColor = Pattern.compile("<#[0-9a-fA-F]{6}>"); } GsonBuilder gsonBuilder = new GsonBuilder(); @@ -2067,7 +2077,7 @@ public class DisguiseUtilities { public static int[] getNumericVersion(String version) { int[] v = new int[0]; - for (String split : version.split("\\.-")) { + for (String split : version.split("\\.|-")) { if (!split.matches("[0-9]+")) { return v; } @@ -2079,6 +2089,33 @@ public class DisguiseUtilities { return v; } + public static BaseComponent[] getColoredChat(String string) { + if (hexColor == null) { + return new ComponentBuilder().appendLegacy(string).create(); + } + + Matcher match = hexColor.matcher(string); + + ComponentBuilder builder = new ComponentBuilder(); + int lastMatch = 0; + + while (match.find()) { + if (match.start() > lastMatch) { + builder.appendLegacy(string.substring(lastMatch, match.start())); + } + + lastMatch = match.end(); + + builder.color(net.md_5.bungee.api.ChatColor.of(match.group().substring(1, 8))); + } + + if (lastMatch < string.length()) { + builder.appendLegacy(string.substring(lastMatch)); + } + + return builder.create(); + } + public static boolean isOlderThan(String requiredVersion, String theirVersion) { int[] ourVersion = getNumericVersion(requiredVersion); int[] theirs = getNumericVersion(theirVersion); @@ -2401,8 +2438,13 @@ public class DisguiseUtilities { WrappedDataWatcher watcher = new WrappedDataWatcher(); - Object name = NmsVersion.v1_13.isSupported() ? Optional.of(WrappedChatComponent.fromText(newNames[i])) : - newNames[i]; + Object name; + + if (NmsVersion.v1_13.isSupported()) { + name = Optional.of(WrappedChatComponent.fromText(newNames[i])); + } else { + name = newNames[i]; + } WrappedDataWatcher.WrappedDataWatcherObject obj = ReflectionManager.createDataWatcherObject( NmsVersion.v1_13.isSupported() ? MetaIndex.ENTITY_CUSTOM_NAME : diff --git a/src/main/java/me/libraryaddict/disguise/utilities/listeners/DisguiseListener.java b/src/main/java/me/libraryaddict/disguise/utilities/listeners/DisguiseListener.java index 21b4168d..15c441a6 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/listeners/DisguiseListener.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/listeners/DisguiseListener.java @@ -19,6 +19,7 @@ import me.libraryaddict.disguise.utilities.modded.ModdedManager; import me.libraryaddict.disguise.utilities.translations.LibsMsg; import org.apache.commons.lang.math.RandomUtils; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Entity; @@ -323,6 +324,20 @@ public class DisguiseListener implements Listener { p.removeMetadata("ld_loggedin", LibsDisguises.getInstance()); plugin.getUpdateChecker().notifyUpdate(p); + if (p.isOp()) { + String requiredProtocolLib = DisguiseUtilities.getProtocolLibRequiredVersion(); + String version = ProtocolLibrary.getPlugin().getDescription().getVersion(); + + if (DisguiseUtilities.isOlderThan(requiredProtocolLib, version)) { + p.sendMessage(ChatColor.RED + "Update your ProtocolLib! You are running " + version + + " but the minimum version you should be on is " + requiredProtocolLib + "!"); + p.sendMessage(ChatColor.RED + "https://ci.dmulloy2.net/job/ProtocolLib/lastSuccessfulBuild/artifact/target" + + "/ProtocolLib" + + ".jar"); + p.sendMessage(ChatColor.RED + "Use /ld updateprotocollib - To update to the latest development build"); + } + } + if (DisguiseConfig.isSaveGameProfiles() && DisguiseConfig.isUpdateGameProfiles() && DisguiseUtilities.hasGameProfile(p.getName())) { WrappedGameProfile profile = WrappedGameProfile.fromPlayer(p); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/reflection/NmsVersion.java b/src/main/java/me/libraryaddict/disguise/utilities/reflection/NmsVersion.java index 2118f0c3..ef960c9f 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/reflection/NmsVersion.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/reflection/NmsVersion.java @@ -14,6 +14,6 @@ public enum NmsVersion { * If this nms version isn't newer than the running version */ public boolean isSupported() { - return ReflectionManager.getVersion().ordinal() >= ordinal(); + return ReflectionManager.getVersion() != null && ReflectionManager.getVersion().ordinal() >= ordinal(); } } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java b/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java index 5a18aad4..e5b33edf 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java @@ -327,6 +327,8 @@ public enum LibsMsg { USING_DEFAULT_CONFIG(ChatColor.DARK_GREEN + "Using the default config!"), LIBS_SCOREBOARD_ISSUES(ChatColor.GREEN + "Too many issues found, hidden %s"), LIBS_SCOREBOARD_NO_ISSUES(ChatColor.GREEN + "No issues found in player disguise scoreboard name teams"), + LD_COMMAND_UPDATEPROTOCOLLIB(ChatColor.BLUE + "/libsdisguises updateprotocollib - " + ChatColor.AQUA + + "Updates ProtocolLib to the latest development version"), LD_COMMAND_HELP(ChatColor.BLUE + "/libsdisguises help - " + ChatColor.AQUA + "Returns this!"), LD_COMMAND_COUNT(ChatColor.BLUE + "/libsdisguises count - " + ChatColor.AQUA + "Tells you how many active disguises there are"), @@ -357,15 +359,6 @@ public enum LibsMsg { SELF_DISGUISE_HIDDEN(ChatColor.GREEN + "Self disguise hidden as it's too tall.."); private final String string; - private static final Pattern hexColor; - - static { - if (NmsVersion.v1_16.isSupported()) { - hexColor = Pattern.compile("<#[0-9a-fA-F]{6}>"); - } else { - hexColor = null; - } - } LibsMsg(String string) { this.string = string; @@ -378,30 +371,7 @@ public enum LibsMsg { public BaseComponent[] getChat(Object... strings) { String string = get(strings); - if (hexColor == null) { - return new ComponentBuilder().appendLegacy(string).create(); - } - - Matcher match = hexColor.matcher(string); - - ComponentBuilder builder = new ComponentBuilder(); - int lastMatch = 0; - - while (match.find()) { - if (match.start() > lastMatch) { - builder.appendLegacy(string.substring(lastMatch, match.start())); - } - - lastMatch = match.end(); - - builder.color(net.md_5.bungee.api.ChatColor.of(match.group().substring(1, 8))); - } - - if (lastMatch < string.length()) { - builder.appendLegacy(string.substring(lastMatch)); - } - - return builder.create(); + return DisguiseUtilities.getColoredChat(string); } public String get(Object... strings) {