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); } } } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_rainbownick.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_rainbownick.java index d4cba746..de59c98f 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_rainbownick.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_rainbownick.java @@ -1,32 +1,26 @@ 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.Bukkit; import org.bukkit.ChatColor; -import me.totalfreedom.totalfreedommod.util.FUtil; -import me.totalfreedom.totalfreedommod.TotalFreedomMod; import org.bukkit.command.Command; -import org.bukkit.entity.Player; import org.bukkit.command.CommandSender; -import me.totalfreedom.totalfreedommod.rank.Rank; +import org.bukkit.entity.Player; @CommandPermissions(level = Rank.OP, source = SourceType.ONLY_IN_GAME) -@CommandParameters(description = "Essentials Interface Command - Rainbowify your nickname.", usage = "/ < | off>") +@CommandParameters(description = "Essentials Interface Command - Rainbowify your nickname.", usage = "/ ") public class Command_rainbownick extends FreedomCommand { - public boolean run(final CommandSender sender, final Player playerSender, final Command cmd, final String commandLabel, final String[] args, final boolean senderIsConsole) + @Override + public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) { if (args.length != 1) { return false; } - if ("off".equals(args[0])) - { - ((TotalFreedomMod) this.plugin).esb.setNickname(sender.getName(), null); - this.msg("Nickname cleared."); - return true; - } final String nickPlain = ChatColor.stripColor(FUtil.colorize(args[0].trim())); @@ -35,13 +29,14 @@ public class Command_rainbownick extends FreedomCommand msg("That nickname contains invalid characters."); return true; } - + if (nickPlain.length() < 4 || nickPlain.length() > 30) { - this.msg("Your nickname must be between 4 and 30 characters long."); + msg("Your nickname must be between 4 and 30 characters long."); return true; } - for (final Player player : Bukkit.getOnlinePlayers()) + + for (Player player : Bukkit.getOnlinePlayers()) { if (player == playerSender) { @@ -49,20 +44,17 @@ public class Command_rainbownick extends FreedomCommand } if (player.getName().equalsIgnoreCase(nickPlain) || ChatColor.stripColor(player.getDisplayName()).trim().equalsIgnoreCase(nickPlain)) { - this.msg("That nickname is already in use."); + msg("That nickname is already in use."); return true; } } - final StringBuilder newNick = new StringBuilder(); - final char[] charArray; - final char[] chars = charArray = nickPlain.toCharArray(); - for (final char c : charArray) - { - newNick.append(FUtil.rainbowChatColor()).append(c); - } - newNick.append(ChatColor.WHITE); - ((TotalFreedomMod) this.plugin).esb.setNickname(sender.getName(), newNick.toString()); - this.msg("Your nickname is now: " + newNick.toString()); + + final String newNick = FUtil.rainbowify(ChatColor.stripColor(FUtil.colorize(nickPlain))); + + plugin.esb.setNickname(sender.getName(), newNick); + + msg("Your nickname is now: " + newNick); + return true; } -} +} \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_rainbowtag.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_rainbowtag.java index cdc4bf49..4fcb68bc 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_rainbowtag.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_rainbowtag.java @@ -1,51 +1,48 @@ package me.totalfreedom.totalfreedommod.command; -import java.util.Arrays; -import me.totalfreedom.totalfreedommod.player.FPlayer; import java.util.Iterator; -import java.util.List; -import me.totalfreedom.totalfreedommod.TotalFreedomMod; -import org.bukkit.ChatColor; +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.entity.Player; import org.bukkit.command.CommandSender; -import me.totalfreedom.totalfreedommod.rank.Rank; +import org.bukkit.entity.Player; @CommandPermissions(level = Rank.OP, source = SourceType.ONLY_IN_GAME) -@CommandParameters(description = "Gives you a tag with Rainbow", usage = "/ ", aliases = "tn") +@CommandParameters(description = "Gives you a rainbow tag", usage = "/ ") public class Command_rainbowtag extends FreedomCommand { - public static final List FORBIDDEN_WORDS = Arrays.asList(new String[] - { - "admin", "owner", "moderator", "developer", "console", "SRA", "TCA", "SA" - }); - - public boolean run(final CommandSender sender, final Player playerSender, final Command cmd, final String commandLabel, final String[] args, final boolean senderIsConsole) + @Override + public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) { if (args.length < 1) { return false; } - final StringBuilder tag = new StringBuilder(); - for (final char c : ChatColor.stripColor(FUtil.colorize(StringUtils.join((Object[]) args, " "))).toCharArray()) + + final String tag = ChatColor.stripColor(FUtil.colorize(StringUtils.join(args, " "))); + + if(tag.length() > 20) { - tag.append(FUtil.rainbowChatColor()).append(c); + msg("That tag is too long (Max is 20 characters)."); + return true; } - final String tagStr = tag.toString(); - for (final String word : FORBIDDEN_WORDS) + + for (String word : Command_tag.FORBIDDEN_WORDS) { - if (tagStr.contains(word)) + if (tag.contains(word)) { - this.msg("That tag contains a forbidden word."); + msg("That tag contains a forbidden word."); return true; } } - final FPlayer data = ((TotalFreedomMod) this.plugin).pl.getPlayer(playerSender); - data.setTag(tagStr); - this.msg("Set tag to " + (Object) tag); + + plugin.pl.getPlayer(playerSender).setTag(FUtil.rainbowify(tag)); + + msg("Set tag to " + tag); + return true; } -} +} \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java b/src/main/java/me/totalfreedom/totalfreedommod/fun/Trailer.java index 78e26274..e44344ac 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; @@ -16,7 +16,6 @@ import org.bukkit.event.player.PlayerMoveEvent; public class Trailer extends FreedomService { - private final Random random = new Random(); private final Set trailPlayers = new HashSet<>(); // player name @@ -60,8 +59,22 @@ public class Trailer extends FreedomService return; } + final Location location = fromBlock.getLocation(); fromBlock.setType(Material.WOOL); DepreciationAggregator.setData_Block(fromBlock, (byte) random.nextInt(16)); + 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) 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) { diff --git a/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java b/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java index 277e02f4..c2b332d7 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,46 +32,36 @@ import org.bukkit.scheduler.BukkitTask; public class FUtil { - private static final Random RANDOM; + + private static final Random RANDOM = new 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; - private static final ChatColor[] BLOCKED = new ChatColor[] - { - ChatColor.MAGIC, - ChatColor.STRIKETHROUGH, - ChatColor.ITALIC, - ChatColor.UNDERLINE, - ChatColor.BLACK - }; + // 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 { - 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); + static + { + for (ChatColor chatColor : CHAT_COLOR_POOL) + { + 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 static final Pattern REGEX = Pattern.compile(ChatColor.COLOR_CHAR + "[" + StringUtils.join(BLOCKED, "") + "]", Pattern.CASE_INSENSITIVE); - private FUtil() { @@ -156,7 +144,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"); } @@ -357,7 +345,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 @@ -368,29 +356,38 @@ 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 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) @@ -398,22 +395,6 @@ public class FUtil return ChatColor.translateAlternateColorCodes('&', string); } - public static String StrictColorize(String string) - { - String string2 = ChatColor.translateAlternateColorCodes('&', string); - final Matcher matcher = REGEX.matcher(string2); - if (matcher.find()) - { - final String filteredcolorize = matcher.replaceAll("&"); - if(matcher.find(REGEX.matcher(ChatColor.BLACK))) - return filteredcolorize; - } - else - { - return string2; - } - } - public static Date getUnixDate(long unix) { return new Date(unix * 1000); @@ -440,4 +421,4 @@ public class FUtil return packageName.substring(packageName.lastIndexOf('.') + 1); } -} +} \ No newline at end of file