From af115811338c60d38111d8a738b8103b5ae7db1e Mon Sep 17 00:00:00 2001 From: marcocorriero Date: Tue, 14 Nov 2017 11:34:49 +0100 Subject: [PATCH 1/9] Forgot to cancel the event im a such stupid xD --- .../me/totalfreedom/totalfreedommod/PvpMonitor.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/PvpMonitor.java b/src/main/java/me/totalfreedom/totalfreedommod/PvpMonitor.java index d387a933..6798d459 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/PvpMonitor.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/PvpMonitor.java @@ -43,12 +43,16 @@ public class PvpMonitor extends FreedomService { // Checks 4 cases if (player.getGameMode() == GameMode.CREATIVE && plugin.esb.getEssentialsUser(player.getName()).isGodModeEnabled()) { // This checks if player is on creative and god mode on. player.sendMessage(ChatColor.RED + "Hey! You cannot PVP with God Mode and creative!"); + event.setCancelled(true); } else if (player.getGameMode() == GameMode.CREATIVE && !plugin.esb.getEssentialsUser(player.getName()).isGodModeEnabled()) { // This checks if player is on creative and god mode off. player.sendMessage(ChatColor.RED + "Hey! You cannot PVP in creative!"); + event.setCancelled(true); } else if (player.getGameMode() == GameMode.SURVIVAL && plugin.esb.getEssentialsUser(player.getName()).isGodModeEnabled()) { // This checks if player is on survival with god mode on. player.sendMessage(ChatColor.RED + "Hey! You can't PVP with godmode!"); + event.setCancelled(true); } else if (player.getGameMode() == GameMode.ADVENTURE && plugin.esb.getEssentialsUser(player.getName()).isGodModeEnabled()) { // This checks if player is on Adventure with god mode on. player.sendMessage(ChatColor.RED + "Hey! You can't PVP with godmode!"); + event.setCancelled(true); } } @@ -58,7 +62,7 @@ public class PvpMonitor extends FreedomService { ProjectileSource ps = ((Projectile) damager).getShooter(); Player player = (Player) ps; - + // Bypasses the block if Player is actually a Supered-Admin. if (plugin.al.isAdmin((player))) { return; @@ -66,12 +70,16 @@ public class PvpMonitor extends FreedomService { if (player.getGameMode() == GameMode.CREATIVE && plugin.esb.getEssentialsUser(player.getName()).isGodModeEnabled()) { // This checks if player is on creative and god mode on. player.sendMessage(ChatColor.RED + "Hey! You cannot PVP with God Mode and creative!"); + event.setCancelled(true); } else if (player.getGameMode() == GameMode.CREATIVE && !plugin.esb.getEssentialsUser(player.getName()).isGodModeEnabled()) { // This checks if player is on creative and god mode off. player.sendMessage(ChatColor.RED + "Hey! You cannot PVP in creative!"); + event.setCancelled(true); } else if (player.getGameMode() == GameMode.SURVIVAL && plugin.esb.getEssentialsUser(player.getName()).isGodModeEnabled()) { // This checks if player is on survival with god mode on. player.sendMessage(ChatColor.RED + "Hey! You can't PVP with godmode!"); + event.setCancelled(true); } else if (player.getGameMode() == GameMode.ADVENTURE && plugin.esb.getEssentialsUser(player.getName()).isGodModeEnabled()) { // This checks if player is on Adventure with god mode on. player.sendMessage(ChatColor.RED + "Hey! You can't PVP with godmode!"); + event.setCancelled(true); } } } From 25ad80be604720a8ff2ad77bc252a799ac689d33 Mon Sep 17 00:00:00 2001 From: marcocorriero Date: Thu, 16 Nov 2017 14:48:54 +0100 Subject: [PATCH 2/9] Makes Trail loggeable to coreprotect. --- .../totalfreedommod/fun/Trailer.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java b/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java index 78e26274..8893e79f 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java @@ -1,12 +1,12 @@ package me.totalfreedom.totalfreedommod.fun; import java.util.HashSet; -import java.util.List; import java.util.Random; import java.util.Set; import me.totalfreedom.totalfreedommod.FreedomService; import me.totalfreedom.totalfreedommod.TotalFreedomMod; import me.totalfreedom.totalfreedommod.util.DepreciationAggregator; +import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.entity.Player; @@ -14,11 +14,15 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.player.PlayerMoveEvent; + public class Trailer extends FreedomService { + public byte data; + + private final Random random = new Random(); - private final Set trailPlayers = new HashSet<>(); // player name + public final Set trailPlayers = new HashSet<>(); // player name public Trailer(TotalFreedomMod plugin) { @@ -60,8 +64,23 @@ public class Trailer extends FreedomService return; } + final Location location = fromBlock.getLocation(); + fromBlock.setType(Material.WOOL); DepreciationAggregator.setData_Block(fromBlock, (byte) random.nextInt(16)); + final byte data = DepreciationAggregator.getData_Block(fromBlock); + Material material = Material.getMaterial(String.valueOf(fromBlock.getType())); + for (int x = -1; x <= 1; x++) + { + for (int z = -1; z <= 1; z++) + { + final Location trail_pos; + trail_pos = new Location(event.getPlayer().getWorld(), fromBlock.getX() + x, fromBlock.getY(), fromBlock.getZ() + z); + if (trailPlayers.contains(event.getPlayer().getName())) { + plugin.cpb.getCoreProtectAPI().logPlacement(event.getPlayer().getName(), trail_pos, material, data); + } + } + } } public void remove(Player player) From 3babdf88a21a098587ad46d753beee9e74f3476d Mon Sep 17 00:00:00 2001 From: marcocorriero Date: Thu, 16 Nov 2017 14:54:53 +0100 Subject: [PATCH 3/9] Update Trailer.java --- src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java b/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java index 8893e79f..5ad7568f 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java @@ -22,7 +22,7 @@ public class Trailer extends FreedomService private final Random random = new Random(); - public final Set trailPlayers = new HashSet<>(); // player name + private final Set trailPlayers = new HashSet<>(); // player name public Trailer(TotalFreedomMod plugin) { From cba204d9f3ecb3eb15a5c455c164f8add64e52ab Mon Sep 17 00:00:00 2001 From: marcocorriero Date: Fri, 17 Nov 2017 14:12:21 +0100 Subject: [PATCH 4/9] Applying requested changes --- .../java/me/totalfreedom/totalfreedommod/fun/Trailer.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java b/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java index 5ad7568f..2397aa55 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java @@ -18,9 +18,7 @@ import org.bukkit.event.player.PlayerMoveEvent; public class Trailer extends FreedomService { - public byte data; - - + private byte data; private final Random random = new Random(); private final Set trailPlayers = new HashSet<>(); // player name @@ -65,10 +63,9 @@ public class Trailer extends FreedomService } final Location location = fromBlock.getLocation(); - fromBlock.setType(Material.WOOL); DepreciationAggregator.setData_Block(fromBlock, (byte) random.nextInt(16)); - final byte data = DepreciationAggregator.getData_Block(fromBlock); + private byte data = DepreciationAggregator.getData_Block(fromBlock); Material material = Material.getMaterial(String.valueOf(fromBlock.getType())); for (int x = -1; x <= 1; x++) { From a10a0cbef964a4ea3e86c124f3ce032e6c251d82 Mon Sep 17 00:00:00 2001 From: marcocorriero Date: Wed, 22 Nov 2017 20:48:45 +0100 Subject: [PATCH 5/9] Update Trailer.java --- .../java/me/totalfreedom/totalfreedommod/fun/Trailer.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java b/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java index 2397aa55..e44344ac 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java @@ -14,11 +14,8 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.player.PlayerMoveEvent; - public class Trailer extends FreedomService { - - private byte data; private final Random random = new Random(); private final Set trailPlayers = new HashSet<>(); // player name @@ -65,7 +62,7 @@ public class Trailer extends FreedomService final Location location = fromBlock.getLocation(); fromBlock.setType(Material.WOOL); DepreciationAggregator.setData_Block(fromBlock, (byte) random.nextInt(16)); - private byte data = DepreciationAggregator.getData_Block(fromBlock); + byte data = DepreciationAggregator.getData_Block(fromBlock); Material material = Material.getMaterial(String.valueOf(fromBlock.getType())); for (int x = -1; x <= 1; x++) { From a1a0894eb527ebdeb5e674f7664b1286b1a798c4 Mon Sep 17 00:00:00 2001 From: Lemon Date: Mon, 27 Nov 2017 11:58:40 +0500 Subject: [PATCH 6/9] Log IP's downloading logfiles & uploading schematics --- .../httpd/module/Module_logfile.java | 234 ++++++++++++++++++ .../httpd/module/Module_logs.java | 2 + .../httpd/module/Module_schematic.java | 7 +- 3 files changed, 241 insertions(+), 2 deletions(-) create mode 100644 src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_logfile.java diff --git a/src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_logfile.java b/src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_logfile.java new file mode 100644 index 00000000..7fda63a7 --- /dev/null +++ b/src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_logfile.java @@ -0,0 +1,234 @@ +package me.totalfreedom.totalfreedommod.httpd.module; + +import me.totalfreedom.totalfreedommod.TotalFreedomMod; +import me.totalfreedom.totalfreedommod.admin.Admin; +import me.totalfreedom.totalfreedommod.httpd.HTMLGenerationTools; +import me.totalfreedom.totalfreedommod.httpd.HTTPDPageBuilder; +import me.totalfreedom.totalfreedommod.httpd.HTTPDaemon; +import me.totalfreedom.totalfreedommod.httpd.NanoHTTPD; +import me.totalfreedom.totalfreedommod.httpd.NanoHTTPD.Response; +import me.totalfreedom.totalfreedommod.util.FLog; +import java.io.File; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.StringEscapeUtils; +import org.apache.commons.lang3.StringUtils; + +public class Module_logfile extends HTTPDModule +{ + + private static final File LOG_FOLDER = new File("./logs/"); + private static final String[] LOG_FILTER = new String[] + { + "log", + "gz" + }; + + public Module_logfile(TotalFreedomMod plugin, NanoHTTPD.HTTPSession session) + { + super(plugin, session); + } + + @Override + public Response getResponse() + { + try + { + return new HTTPDPageBuilder(body(), title(), null, null).getResponse(); + } + catch (ResponseOverrideException ex) + { + return ex.getResponse(); + } + } + + public String title() + { + return "TotalFreedomMod :: Logfiles"; + } + + public String body() throws ResponseOverrideException + { + if (!LOG_FOLDER.exists()) + { + FLog.warning("The logfile module failed to find the logs folder."); + return HTMLGenerationTools.paragraph("Can't find the logs folder."); + + } + + final StringBuilder out = new StringBuilder(); + final String remoteAddress = socket.getInetAddress().getHostAddress(); + final String[] args = StringUtils.split(uri, "/"); + final ModuleMode mode = ModuleMode.getMode(getArg(args, 1)); + + switch (mode) + { + case LIST: + { + if (!isAuthorized(remoteAddress)) + { + + out.append(HTMLGenerationTools.paragraph("Log files access denied: Your IP, " + remoteAddress + ", is not registered to a superadmin on this server.")); + FLog.info("An unregistered IP (" + remoteAddress + ") has tried to access the log files"); + } + else + { + Collection LogFiles = FileUtils.listFiles(LOG_FOLDER, LOG_FILTER, false); + + final List LogFilesFormatted = new ArrayList<>(); + for (File logfile : LogFiles) + { + String filename = StringEscapeUtils.escapeHtml4(logfile.getName()); + + LogFilesFormatted.add("
  • " + filename + "
  • "); + + } + + Collections.sort(LogFilesFormatted, new Comparator() + { + @Override + public int compare(String a, String b) + { + return a.toLowerCase().compareTo(b.toLowerCase()); + } + }); + + out + .append(HTMLGenerationTools.heading("Logfiles:", 1)) + .append("
      ") + .append(StringUtils.join(LogFilesFormatted, "\r\n")) + .append("
    "); + } + break; + } + case DOWNLOAD: + { + if (!isAuthorized(remoteAddress)) + { + out.append(HTMLGenerationTools.paragraph("Log files access denied: Your IP, " + remoteAddress + ", is not registered to a superadmin on this server.")); + FLog.info("An unregistered IP (" + remoteAddress + ") has tried to download a log file"); + } + else + { + try + { + FLog.info("The IP \"" + remoteAddress + "\" is downloading log file:" + params.get("logFileName")); + throw new ResponseOverrideException(downloadLogFile(params.get("logFileName"))); + } + catch (LogFileTransferException ex) + { + out.append(HTMLGenerationTools.paragraph("Error downloading logfile: " + ex.getMessage())); + } + } + break; + } + default: + { + out.append(HTMLGenerationTools.paragraph("Invalid request mode.")); + break; + } + } + + return out.toString(); + } + + private Response downloadLogFile(String LogFilesName) throws LogFileTransferException + { + if (LogFilesName == null) + { + throw new LogFileTransferException("Invalid logfile requested: " + LogFilesName); + } + + final File targetFile = new File(LOG_FOLDER.getPath(), LogFilesName); + if (!targetFile.exists()) + { + throw new LogFileTransferException("Logfile not found: " + LogFilesName); + } + + Response response = HTTPDaemon.serveFileBasic(targetFile); + + response.addHeader("Content-Disposition", "attachment; filename=" + targetFile.getName() + ";"); + + return response; + } + + private boolean isAuthorized(String remoteAddress) + { + Admin entry = plugin.al.getEntryByIp(remoteAddress); + return entry != null && entry.isActive(); + } + + private static class LogFileTransferException extends Exception + { + + public LogFileTransferException() + { + } + + public LogFileTransferException(String string) + { + super(string); + } + } + + private static class ResponseOverrideException extends Exception + { + + private final Response response; + + public ResponseOverrideException(Response response) + { + this.response = response; + } + + public Response getResponse() + { + return response; + } + } + + private static String getArg(String[] args, int index) + { + String out = (args.length == index + 1 ? args[index] : null); + return (out == null ? null : (out.trim().isEmpty() ? null : out.trim())); + } + + private static enum ModuleMode + { + + LIST("list"), + DOWNLOAD("download"), + INVALID(null); + // + private final String modeName; + + private ModuleMode(String modeName) + { + this.modeName = modeName; + } + + @Override + public String toString() + { + return this.modeName; + } + + public static ModuleMode getMode(String needle) + { + for (ModuleMode mode : values()) + { + final String haystack = mode.toString(); + if (haystack != null && haystack.equalsIgnoreCase(needle)) + { + return mode; + } + } + return INVALID; + } + } + +} diff --git a/src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_logs.java b/src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_logs.java index 71692a6f..73f184bb 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_logs.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_logs.java @@ -4,6 +4,7 @@ import java.io.File; import me.totalfreedom.totalfreedommod.TotalFreedomMod; import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.httpd.NanoHTTPD; +import me.totalfreedom.totalfreedommod.util.FLog; public class Module_logs extends Module_file { @@ -18,6 +19,7 @@ public class Module_logs extends Module_file { if (ConfigEntry.LOGS_SECRET.getString().equals(params.get("password"))) { + FLog.info(session.getSocket().getInetAddress() + " is downloading latest.log."); return serveFile("latest.log", params, new File("./logs")); } else diff --git a/src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_schematic.java b/src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_schematic.java index 3fc39a5d..d7da1ffc 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_schematic.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/httpd/module/Module_schematic.java @@ -2,6 +2,7 @@ package me.totalfreedom.totalfreedommod.httpd.module; import java.io.File; import java.io.IOException; +import java.net.InetAddress; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -137,7 +138,8 @@ public class Module_schematic extends HTTPDModule { try { - uploadSchematic(); + uploadSchematic(remoteAddress); + out.append(HTMLGenerationTools.paragraph("Schematic uploaded successfully.")); } catch (SchematicTransferException ex) @@ -162,7 +164,7 @@ public class Module_schematic extends HTTPDModule return out.toString(); } - private boolean uploadSchematic() throws SchematicTransferException + private boolean uploadSchematic(String remoteAddress) throws SchematicTransferException { Map files = getFiles(); @@ -203,6 +205,7 @@ public class Module_schematic extends HTTPDModule try { FileUtils.copyFile(tempFile, targetFile); + FLog.info(remoteAddress + " uploaded schematic: " + targetFile.getName()); } catch (IOException ex) { From 336a2de2317f9dbb89b82c91cbf09bb5cd5c8131 Mon Sep 17 00:00:00 2001 From: Lemon Date: Mon, 27 Nov 2017 12:04:53 +0500 Subject: [PATCH 7/9] eh --- .../totalfreedommod/util/FUtil.java | 102 +++++++++--------- 1 file changed, 54 insertions(+), 48 deletions(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java b/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java index b3f2147e..f5ffa34e 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java @@ -20,8 +20,6 @@ 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; @@ -34,39 +32,38 @@ import org.bukkit.scheduler.BukkitTask; public class FUtil { - private static final Random RANDOM; - public static final String SAVED_FLAGS_FILENAME = "savedflags.dat"; - 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", "marcocorriero"); - 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 static final Random RANDOM = new 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 DEVELOPERS = Arrays.asList("Madgeek1450", "Prozza", "WickedGamingUK", "aggelosQQ", "OxLemonxO", "Commodore64x", "Wild1145", "marcocorriero"); + 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_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); + private static Iterator CHAT_COLOR_ITERATOR; + + static + { + for (ChatColor chatColor : CHAT_COLOR_POOL) + { + CHAT_COLOR_NAMES.put(chatColor.name().toLowerCase().replace("_", ""), chatColor); + } + } + + private FUtil() { } @@ -147,7 +144,7 @@ private 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"); } @@ -348,7 +345,7 @@ private 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 @@ -359,29 +356,38 @@ private 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 FUtil.CHAT_COLOR_POOL.get(FUtil.RANDOM.nextInt(FUtil.CHAT_COLOR_POOL.size())); + public static ChatColor randomChatColor() + { + return CHAT_COLOR_POOL.get(RANDOM.nextInt(CHAT_COLOR_POOL.size())); } - public static ChatColor rainbowChatColor() { - if (FUtil.color.hasNext()) { - return FUtil.color.next(); + public static String rainbowify(String string) + { + CHAT_COLOR_ITERATOR = CHAT_COLOR_POOL.iterator(); + + final StringBuilder newString = new StringBuilder(); + final char[] chars = string.toCharArray(); + + for (char c : chars) + { + if (!CHAT_COLOR_ITERATOR.hasNext()) + { + CHAT_COLOR_ITERATOR = CHAT_COLOR_POOL.iterator(); //Restart from first colour if there are no more colours in iterator. + } + newString.append(CHAT_COLOR_ITERATOR.next()).append(c); } - FUtil.color = FUtil.CHAT_RAINBOW.iterator(); - return FUtil.color.next(); + + return newString.toString(); } public static String colorize(String string) From 7c42f78426461f0c124d20b4b326c737f5f3d688 Mon Sep 17 00:00:00 2001 From: Lemon Date: Mon, 27 Nov 2017 12:06:04 +0500 Subject: [PATCH 8/9] Add rainbowtag --- .../command/Command_rainbowtag.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/main/java/me/totalfreedom/totalfreedommod/command/Command_rainbowtag.java diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_rainbowtag.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_rainbowtag.java new file mode 100644 index 00000000..b4a13d98 --- /dev/null +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_rainbowtag.java @@ -0,0 +1,48 @@ +package me.totalfreedom.totalfreedommod.command; + +import java.util.Iterator; +import me.totalfreedom.totalfreedommod.rank.Rank; +import me.totalfreedom.totalfreedommod.util.FUtil; +import org.apache.commons.lang3.StringUtils; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +@CommandPermissions(level = Rank.OP, source = SourceType.ONLY_IN_GAME) +@CommandParameters(description = "Gives you a rainbow tag", usage = "/ ") +public class Command_rainbowtag extends FreedomCommand +{ + + @Override + public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) + { + if (args.length < 1) + { + return false; + } + + final String tag = ChatColor.stripColor(FUtil.colorize(StringUtils.join(args, " "))); + + if(tag.length() > 20) + { + msg("That tag is too long (Max is 20 characters)."); + return true; + } + + for (String word : Command_tag.FORBIDDEN_WORDS) + { + if (tag.contains(word)) + { + msg("That tag contains a forbidden word."); + return true; + } + } + + plugin.pl.getPlayer(playerSender).setTag(FUtil.rainbowify(tag)); + + msg("Set tag to " + tag); + + return true; + } +} From 019950152b6914b2b1c526f3254dd30d56296b92 Mon Sep 17 00:00:00 2001 From: Lemon Date: Mon, 27 Nov 2017 12:06:39 +0500 Subject: [PATCH 9/9] Add rainbownick --- .../command/Command_rainbownick.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/main/java/me/totalfreedom/totalfreedommod/command/Command_rainbownick.java diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_rainbownick.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_rainbownick.java new file mode 100644 index 00000000..83ad4801 --- /dev/null +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_rainbownick.java @@ -0,0 +1,60 @@ +package me.totalfreedom.totalfreedommod.command; + +import me.totalfreedom.totalfreedommod.rank.Rank; +import me.totalfreedom.totalfreedommod.util.FUtil; +import org.apache.commons.lang3.StringUtils; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +@CommandPermissions(level = Rank.OP, source = SourceType.ONLY_IN_GAME) +@CommandParameters(description = "Essentials Interface Command - Rainbowify your nickname.", usage = "/ ") +public class Command_rainbownick extends FreedomCommand +{ + + @Override + public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) + { + if (args.length != 1) + { + return false; + } + + final String nickPlain = ChatColor.stripColor(FUtil.colorize(args[0].trim())); + + if (!nickPlain.matches("^[a-zA-Z_0-9" + ChatColor.COLOR_CHAR + "]+$")) + { + msg("That nickname contains invalid characters."); + return true; + } + + if (nickPlain.length() < 4 || nickPlain.length() > 30) + { + msg("Your nickname must be between 4 and 30 characters long."); + return true; + } + + for (Player player : Bukkit.getOnlinePlayers()) + { + if (player == playerSender) + { + continue; + } + if (player.getName().equalsIgnoreCase(nickPlain) || ChatColor.stripColor(player.getDisplayName()).trim().equalsIgnoreCase(nickPlain)) + { + msg("That nickname is already in use."); + return true; + } + } + + final String newNick = FUtil.rainbowify(ChatColor.stripColor(FUtil.colorize(nickPlain))); + + plugin.esb.setNickname(sender.getName(), newNick); + + msg("Your nickname is now: " + newNick); + + return true; + } +}