From d878fd2458cff9bdfa47b4b4e10a57b1491a6eeb Mon Sep 17 00:00:00 2001 From: Lemon Date: Fri, 13 Oct 2017 23:38:05 +0500 Subject: [PATCH] Patches --- .../util/DepreciationAggregator.java | 3 +- .../totalfreedommod/util/FSync.java | 15 +++ .../totalfreedommod/util/FUtil.java | 83 ++++++++------ .../totalfreedommod/util/History.java | 104 ++++++++++++++++++ .../totalfreedommod/util/UUIDFetcher.java | 68 ++++++++++++ 5 files changed, 238 insertions(+), 35 deletions(-) create mode 100644 src/main/java/me/totalfreedom/totalfreedommod/util/History.java create mode 100644 src/main/java/me/totalfreedom/totalfreedommod/util/UUIDFetcher.java diff --git a/src/main/java/me/totalfreedom/totalfreedommod/util/DepreciationAggregator.java b/src/main/java/me/totalfreedom/totalfreedommod/util/DepreciationAggregator.java index a404deab..eba8053d 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/util/DepreciationAggregator.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/util/DepreciationAggregator.java @@ -1,6 +1,7 @@ package me.totalfreedom.totalfreedommod.util; import java.util.HashSet; +import java.util.UUID; import org.bukkit.Material; import org.bukkit.OfflinePlayer; import org.bukkit.Server; @@ -13,7 +14,7 @@ import org.bukkit.material.MaterialData; public class DepreciationAggregator { - public static Block getTargetBlock(LivingEntity entity, HashSet transparent, int maxDistance) + public static Block getTargetBlock(LivingEntity entity, HashSet transparent, int maxDistance) { return entity.getTargetBlock(transparent, maxDistance); } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/util/FSync.java b/src/main/java/me/totalfreedom/totalfreedommod/util/FSync.java index a67831b9..7a4b9e3a 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/util/FSync.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/util/FSync.java @@ -24,6 +24,21 @@ public class FSync }.runTask(plugin); } + public static void playerMsg(final CommandSender sender, final String message) + { + final TotalFreedomMod plugin = TotalFreedomMod.plugin(); + new BukkitRunnable() + { + + @Override + public void run() + { + sender.sendMessage(message); + } + + }.runTask(plugin); + } + public static void playerKick(final Player player, final String reason) { final TotalFreedomMod plugin = TotalFreedomMod.plugin(); diff --git a/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java b/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java index 5a327297..e812fe8b 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java @@ -11,6 +11,7 @@ import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; @@ -19,6 +20,8 @@ import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import me.totalfreedom.totalfreedommod.config.ConfigEntry; +import static me.totalfreedom.totalfreedommod.util.FUtil.CHAT_RAINBOW; +import static me.totalfreedom.totalfreedommod.util.FUtil.FOUNDER; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.bukkit.Bukkit; @@ -31,37 +34,39 @@ import org.bukkit.scheduler.BukkitTask; public class FUtil { - - private static final Random RANDOM = new Random(); - // + private static final Random RANDOM; public static final String SAVED_FLAGS_FILENAME = "savedflags.dat"; - // See https://github.com/TotalFreedom/License - None of the listed names may be removed. - public static final List DEVELOPERS = Arrays.asList("Madgeek1450", "Prozza", "Wild1145", "WickedGamingUK", "aggelosQQ"); - public static String DATE_STORAGE_FORMAT = "EEE, d MMM yyyy HH:mm:ss Z"; - public static final Map CHAT_COLOR_NAMES = new HashMap<>(); - public static final List CHAT_COLOR_POOL = Arrays.asList( - ChatColor.DARK_BLUE, - ChatColor.DARK_GREEN, - ChatColor.DARK_AQUA, - ChatColor.DARK_RED, - ChatColor.DARK_PURPLE, - ChatColor.GOLD, - ChatColor.BLUE, - ChatColor.GREEN, - ChatColor.AQUA, - ChatColor.RED, - ChatColor.LIGHT_PURPLE, - ChatColor.YELLOW); - - static - { - for (ChatColor chatColor : CHAT_COLOR_POOL) - { - CHAT_COLOR_NAMES.put(chatColor.name().toLowerCase().replace("_", ""), chatColor); + public static final List DEVELOPERS; + public static final List FOUNDER; + public static String DATE_STORAGE_FORMAT; + public static final Map CHAT_COLOR_NAMES; + public static final Map CHAT_RAINBOW_NAMES; + public static List BLOCKED_CODES; + public static final List CHAT_COLOR_POOL; + public static final List CHAT_RAINBOW; + private static Iterator color; + + static { + RANDOM = new Random(); + DEVELOPERS = Arrays.asList("Madgeek1450", "Prozza", "WickedGamingUK", "aggelosQQ", "OxLemonxO", "Commodore64x", "Wild1145"); + FOUNDER = Arrays.asList("markbyron"); + FUtil.DATE_STORAGE_FORMAT = "EEE, d MMM yyyy HH:mm:ss Z"; + CHAT_COLOR_NAMES = new HashMap(); + CHAT_RAINBOW_NAMES = new HashMap(); + FUtil.BLOCKED_CODES = new ArrayList(); + CHAT_COLOR_POOL = Arrays.asList(ChatColor.DARK_BLUE, ChatColor.DARK_GREEN, ChatColor.DARK_AQUA, ChatColor.DARK_RED, ChatColor.DARK_PURPLE, ChatColor.GOLD, ChatColor.BLUE, ChatColor.GREEN, ChatColor.AQUA, ChatColor.RED, ChatColor.LIGHT_PURPLE, ChatColor.YELLOW); + CHAT_RAINBOW = Arrays.asList(ChatColor.DARK_RED, ChatColor.RED, ChatColor.GOLD, ChatColor.YELLOW, ChatColor.GREEN, ChatColor.DARK_GREEN, ChatColor.AQUA, ChatColor.DARK_AQUA, ChatColor.BLUE, ChatColor.DARK_BLUE, ChatColor.DARK_PURPLE, ChatColor.LIGHT_PURPLE); + for (final ChatColor chatColor : FUtil.CHAT_COLOR_POOL) { + FUtil.CHAT_COLOR_NAMES.put(chatColor.name().toLowerCase().replace("_", ""), chatColor); } + for (final ChatColor chatColor : FUtil.CHAT_RAINBOW) { + FUtil.CHAT_RAINBOW_NAMES.put(chatColor.name().toLowerCase().replace("_", ""), chatColor); + } + FUtil.color = FUtil.CHAT_RAINBOW.iterator(); } + - private FUtil() +private FUtil() { } @@ -142,7 +147,7 @@ public class FUtil final File[] coreDumps = new File(".").listFiles(new FileFilter() { @Override - public boolean accept(File file) + public boolean accept(File file) { return file.getName().startsWith("java.core"); } @@ -343,7 +348,7 @@ public class FUtil //getField: Borrowed from WorldEdit @SuppressWarnings("unchecked") - public static T getField(Object from, String name) + public static T getField(Object from, String name) { Class checkClass = from.getClass(); do @@ -354,19 +359,29 @@ public class FUtil field.setAccessible(true); return (T) field.get(from); - } + + +} catch (NoSuchFieldException | IllegalAccessException ex) { } } while (checkClass.getSuperclass() != Object.class - && ((checkClass = checkClass.getSuperclass()) != null)); + +&& ((checkClass = checkClass.getSuperclass()) != null)); return null; } - public static ChatColor randomChatColor() - { - return CHAT_COLOR_POOL.get(RANDOM.nextInt(CHAT_COLOR_POOL.size())); + public static ChatColor randomChatColor() { + return FUtil.CHAT_COLOR_POOL.get(FUtil.RANDOM.nextInt(FUtil.CHAT_COLOR_POOL.size())); + } + + public static ChatColor rainbowChatColor() { + if (FUtil.color.hasNext()) { + return FUtil.color.next(); + } + FUtil.color = FUtil.CHAT_RAINBOW.iterator(); + return FUtil.color.next(); } public static String colorize(String string) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/util/History.java b/src/main/java/me/totalfreedom/totalfreedommod/util/History.java new file mode 100644 index 00000000..f610adc0 --- /dev/null +++ b/src/main/java/me/totalfreedom/totalfreedommod/util/History.java @@ -0,0 +1,104 @@ +package me.totalfreedom.totalfreedommod.util; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.Date; +import java.util.UUID; +import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import me.totalfreedom.totalfreedommod.TotalFreedomMod; + +public class History +{ + + public static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + public static void reportHistory(final CommandSender sender, final String username) + { + new BukkitRunnable() + { + @Override + public void run() + { + UUID uuid = UUIDFetcher.fetch(username); + if (uuid != null) + { + Gson gson = new GsonBuilder().create(); + String compactUuid = uuid.toString().replace("-", ""); + try + { + URL url = new URL("https://api.mojang.com/user/profiles/" + compactUuid + "/names"); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); + FName[] oldNames = gson.fromJson(reader, FName[].class); + if (oldNames == null) + { + FSync.playerMsg(sender, ChatColor.RED + "Player not found!"); + return; + } + reader.close(); + conn.disconnect(); + Arrays.sort(oldNames); + printHistory(sender, oldNames); + } + catch (Exception ex) + { + FSync.playerMsg(sender, ChatColor.RED + "Error, see logs for more details."); + FLog.severe(ex); + } + } + else + { + FSync.playerMsg(sender, ChatColor.RED + "Player not found!"); + } + } + }.runTaskAsynchronously(TotalFreedomMod.plugin()); + } + + private static void printHistory(CommandSender sender, FName[] oldNames) + { + if (oldNames.length == 1) + { + FSync.playerMsg(sender, ChatColor.GREEN + oldNames[0].getName() + ChatColor.GOLD + " has never changed their name."); + return; + } + FSync.playerMsg(sender, ChatColor.GOLD + "Original name: " + ChatColor.GREEN + oldNames[0].getName()); + for (int i = 1; i < oldNames.length; i++) + { + Date date = new Date(oldNames[i].getChangedToAt()); + String formattedDate = df.format(date); + FSync.playerMsg(sender, ChatColor.BLUE + formattedDate + ChatColor.GOLD + " changed to " + ChatColor.GREEN + oldNames[i].getName()); + } + } + + private static class FName implements Comparable + { + + private String name; + private long changedToAt; + + @Override + public int compareTo(FName other) + { + return Long.compare(this.changedToAt, other.changedToAt); + } + + public String getName() + { + return name; + } + + public long getChangedToAt() + { + return changedToAt; + } + } +} \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/totalfreedommod/util/UUIDFetcher.java b/src/main/java/me/totalfreedom/totalfreedommod/util/UUIDFetcher.java new file mode 100644 index 00000000..5af74391 --- /dev/null +++ b/src/main/java/me/totalfreedom/totalfreedommod/util/UUIDFetcher.java @@ -0,0 +1,68 @@ +package me.totalfreedom.totalfreedommod.util; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.UUID; + +// UUIDFetcher retrieves UUIDs from usernames via web requests to Mojang. +public class UUIDFetcher +{ + + private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft"; + + public static UUID fetch(String name) + { + try + { + Gson gson = new GsonBuilder().create(); + UUID uuid; + String body = gson.toJson(name); + URL url = new URL(PROFILE_URL); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setUseCaches(false); + connection.setDoInput(true); + connection.setDoOutput(true); + OutputStream stream = connection.getOutputStream(); + stream.write(body.getBytes()); + stream.flush(); + stream.close(); + FetchedUuid[] id = gson.fromJson( + new InputStreamReader(connection.getInputStream()), + FetchedUuid[].class); + + if (id.length == 0 || id[0].getID() == null) + { + return null; + } + + String idd = id[0].getID(); + uuid = UUID.fromString(idd.substring(0, 8) + "-" + idd.substring(8, 12) + + "-" + idd.substring(12, 16) + "-" + idd.substring(16, 20) + "-" + + idd.substring(20, 32)); + return uuid; + } + catch (IOException ex) + { + FLog.severe(ex); + } + return null; + } + + private class FetchedUuid + { + + private String id; + + public String getID() + { + return id; + } + } +} \ No newline at end of file