From b2fbd74d4be1696ac187a7264d16364da491beb4 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Fri, 17 Jul 2015 20:48:13 +1000 Subject: [PATCH] Fixes #419 , among other things --- .../intellectualcrafters/plot/BukkitMain.java | 4 +- .../plot/commands/DebugExec.java | 4 +- .../plot/commands/MainCommand.java | 8 +- .../plot/commands/Set.java | 5 +- .../plot/commands/list.java | 2 +- .../intellectualcrafters/plot/config/C.java | 33 +++++-- .../plot/generator/BukkitHybridUtils.java | 96 +++++++------------ .../plot/listeners/worldedit/WEListener.java | 1 - .../plot/object/Plot.java | 16 ++++ .../plot/object/PlotAnalysis.java | 46 +++++++++ .../plot/object/Rating.java | 1 + .../plot/util/MainUtil.java | 46 +-------- .../plot/util/MathMan.java | 62 ++++++++++++ .../plot/util/StringMan.java | 31 ++++-- src/main/resources/plugin.yml | 12 ++- 15 files changed, 226 insertions(+), 141 deletions(-) create mode 100644 src/main/java/com/intellectualcrafters/plot/util/MathMan.java diff --git a/src/main/java/com/intellectualcrafters/plot/BukkitMain.java b/src/main/java/com/intellectualcrafters/plot/BukkitMain.java index 18a19eae7..be8caf093 100644 --- a/src/main/java/com/intellectualcrafters/plot/BukkitMain.java +++ b/src/main/java/com/intellectualcrafters/plot/BukkitMain.java @@ -68,6 +68,7 @@ import com.intellectualcrafters.plot.util.InventoryUtil; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.PlayerManager; import com.intellectualcrafters.plot.util.SetupUtils; +import com.intellectualcrafters.plot.util.StringMan; import com.intellectualcrafters.plot.util.TaskManager; import com.intellectualcrafters.plot.util.bukkit.BukkitChunkManager; import com.intellectualcrafters.plot.util.bukkit.BukkitEconHandler; @@ -175,10 +176,9 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain { if (message == null) { return; } - message = message.replaceAll("\u00B2", "2"); if (THIS != null && Bukkit.getServer().getConsoleSender() != null) { try { - message = ChatColor.translateAlternateColorCodes('&', message); + message = C.color(message); if (!Settings.CONSOLE_COLOR) { message = ChatColor.stripColor(message); } diff --git a/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java b/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java index 91d971378..cb52c03fc 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java @@ -68,11 +68,9 @@ public class DebugExec extends SubCommand { @Override public void run() { List result = new ArrayList<>(); - result.add(Math.round(value.air * 100) / 100d); result.add(Math.round(value.changes * 100) / 100d); - result.add(Math.round(value.complexity * 100) / 100d); - result.add(Math.round(value.data * 100) / 100d); result.add(Math.round(value.faces * 100) / 100d); + result.add(Math.round(value.data * 100) / 100d); result.add(Math.round(value.air * 100) / 100d); result.add(Math.round(value.variety * 100) / 100d); Flag flag = new Flag(FlagManager.getFlag("analysis"), result); diff --git a/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java b/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java index f761f7f93..afdc56dd2 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java @@ -88,7 +88,7 @@ public class MainCommand { final int start = page * perPage; for (int x = start; x < max; x++) { cmd = commands.get(x); - String s = t(C.HELP_ITEM.s()); + String s = C.HELP_ITEM.s(); if (cmd.alias.size() > 0) { s = s.replace("%alias%", cmd.alias.get(0)); } @@ -99,14 +99,10 @@ public class MainCommand { help.add(s); } if (help.size() < 2) { - help.add(t(C.NO_COMMANDS.s())); + help.add(C.NO_COMMANDS.s()); } return help; } - - private static String t(final String s) { - return MainUtil.colorise('&', s); - } public static boolean onCommand(final PlotPlayer player, final String cmd, final String... args) { if ((args.length < 1) || ((args.length >= 1) && (args[0].equalsIgnoreCase("help") || args[0].equalsIgnoreCase("he")))) { diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Set.java b/src/main/java/com/intellectualcrafters/plot/commands/Set.java index f69d530ea..28f147336 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Set.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Set.java @@ -225,6 +225,7 @@ public class Set extends SubCommand { if (component.equalsIgnoreCase(args[0])) { if (!Permissions.hasPermission(plr, "plots.set." + component)) { MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.set." + component); + return false; } PlotBlock[] blocks; try { @@ -313,7 +314,7 @@ public class Set extends SubCommand { } private String getString(final String s) { - return MainUtil.colorise('&', C.BLOCK_LIST_ITEM.s().replaceAll("%mat%", s)); + return StringMan.replaceAll(C.BLOCK_LIST_ITEM.s(), "%mat%", s); } private String getArgumentList(final List newValues) { @@ -326,7 +327,7 @@ public class Set extends SubCommand { private String getBiomeList(final String[] biomes) { final StringBuilder builder = new StringBuilder(); - builder.append(MainUtil.colorise('&', C.NEED_BIOME.s())); + builder.append(C.NEED_BIOME.s()); for (final String b : biomes) { builder.append(getString(b)); } diff --git a/src/main/java/com/intellectualcrafters/plot/commands/list.java b/src/main/java/com/intellectualcrafters/plot/commands/list.java index cb58d14dc..ae89bcc52 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/list.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/list.java @@ -523,7 +523,7 @@ public class list extends SubCommand { final StringBuilder builder = new StringBuilder(); String prefix = ""; for (final String s : strings) { - builder.append(prefix + MainUtil.colorise('&', s)); + builder.append(prefix + s); prefix = " | "; } return builder.toString(); diff --git a/src/main/java/com/intellectualcrafters/plot/config/C.java b/src/main/java/com/intellectualcrafters/plot/config/C.java index 41fa40b73..c64226eea 100644 --- a/src/main/java/com/intellectualcrafters/plot/config/C.java +++ b/src/main/java/com/intellectualcrafters/plot/config/C.java @@ -24,12 +24,15 @@ import com.intellectualcrafters.configuration.ConfigurationSection; import com.intellectualcrafters.configuration.file.YamlConfiguration; import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.util.StringMan; + +import org.apache.commons.lang.StringEscapeUtils; import org.bukkit.ChatColor; import java.io.File; import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; /** @@ -543,20 +546,34 @@ public enum C { this(d, true, cat.toLowerCase()); } - public static String format(C c, Object... args) { - String m = c.s; - for (int i = args.length - 1 ; i >= 0; i--) { - if (args[i] == null) { - continue; - } - m = m.replaceAll("%s" + i, args[i].toString()); + public static String format(String m, Object... args) { + if (args.length == 0) { + return m; } + Map map = new HashMap(); if (args.length > 0) { - m = m.replaceAll("%s", args[0].toString()); + for (int i = args.length - 1 ; i >= 0; i--) { + if (args[i] == null) { + args[i] = ""; + } + map.put("%s" + i, args[i].toString()); + } + map.put("%s", args[0].toString()); } + map.putAll(replacements); + m = StringMan.replaceFromMap(m, map); return m; } + public static String format(C c, Object... args) { + return format(c.s, args); + } + + + public static String color(String string) { + return StringMan.replaceFromMap(string, replacements); + } + public static void load(File file) { try { if (!file.exists()) { diff --git a/src/main/java/com/intellectualcrafters/plot/generator/BukkitHybridUtils.java b/src/main/java/com/intellectualcrafters/plot/generator/BukkitHybridUtils.java index 61c2b9b1f..486d51ebb 100644 --- a/src/main/java/com/intellectualcrafters/plot/generator/BukkitHybridUtils.java +++ b/src/main/java/com/intellectualcrafters/plot/generator/BukkitHybridUtils.java @@ -31,6 +31,7 @@ import com.intellectualcrafters.plot.object.PlotWorld; import com.intellectualcrafters.plot.object.RunnableVal; import com.intellectualcrafters.plot.util.ChunkManager; import com.intellectualcrafters.plot.util.MainUtil; +import com.intellectualcrafters.plot.util.MathMan; import com.intellectualcrafters.plot.util.TaskManager; import com.intellectualcrafters.plot.util.bukkit.BukkitUtil; @@ -43,47 +44,6 @@ public class BukkitHybridUtils extends HybridUtils { public int task; private long last; - public double getMean(int[] array) { - double count = 0; - for (int i : array) { - count += i; - } - return count / array.length; - } - - public double getMean(double[] array) { - double count = 0; - for (double i : array) { - count += i; - } - return count / array.length; - } - - public double getSD(double[] array, double av) { - double sd = 0; - for (int i=0; i whenDone) { // TODO Auto-generated method stub @@ -143,8 +103,8 @@ public class BukkitHybridUtils extends HybridUtils { short[][] result = gen.generateExtBlockSections(world, r, chunk.getX(), chunk.getZ(), base); int X = chunk.getX(); int Z = chunk.getZ(); - int xb = ((X - cbx) << 4) - bx; - int zb = ((Z - cbz) << 4) - bz; + int xb = ((X) << 4) - bx; + int zb = ((Z) << 4) - bz; for (int i = 0; i < result.length; i++) { if (result[i] == null) { for (int j = 0; j < 4096; j++) { @@ -192,14 +152,13 @@ public class BukkitHybridUtils extends HybridUtils { else { // check verticies // modifications_adjacent - if (x > 0 && z > 0 && y > 0 && x < width - 1 && z < length - 1 && y < 255) { - if (oldblocks[y - 1][x][z] == 0) faces[i]++; - if (oldblocks[y][x - 1][z] == 0) faces[i]++; - if (oldblocks[y][x][z - 1] == 0) faces[i]++; - if (oldblocks[y + 1][x][z] == 0) faces[i]++; - if (oldblocks[y][x + 1][z] == 0) faces[i]++; - if (oldblocks[y][x][z + 1] == 0) faces[i]++; + if (newblocks[y - 1][x][z] == 0) faces[i]++; + if (newblocks[y][x - 1][z] == 0) faces[i]++; + if (newblocks[y][x][z - 1] == 0) faces[i]++; + if (newblocks[y + 1][x][z] == 0) faces[i]++; + if (newblocks[y][x + 1][z] == 0) faces[i]++; + if (newblocks[y][x][z + 1] == 0) faces[i]++; } Material material = Material.getMaterial(now); @@ -223,12 +182,18 @@ public class BukkitHybridUtils extends HybridUtils { // run whenDone PlotAnalysis analysis = new PlotAnalysis(); - analysis.changes = getMean(changes); - analysis.faces = getMean(faces); - analysis.data = getMean(data); - analysis.air = getMean(air); - analysis.variety = getMean(variety); - analysis.complexity = getSD(changes, analysis.changes) + getSD(faces, analysis.faces) + getSD(data, analysis.data) + getSD(air, analysis.air) + getSD(variety, analysis.variety); + analysis.changes = MathMan.getMean(changes); + analysis.faces = MathMan.getMean(faces); + analysis.data = MathMan.getMean(data); + analysis.air = MathMan.getMean(air); + analysis.variety = MathMan.getMean(variety); + analysis.complexity = + + (analysis.changes + MathMan.getSD(changes, analysis.changes)) * PlotAnalysis.CHANGES_MODIFIER + + (analysis.faces + MathMan.getSD(changes, analysis.faces)) * PlotAnalysis.FACES_MODIFIER + + (analysis.data + MathMan.getSD(changes, analysis.data)) * PlotAnalysis.DATA_MODIFIER + + (analysis.air + MathMan.getSD(changes, analysis.air)) * PlotAnalysis.AIR_MODIFIER + + (analysis.variety + MathMan.getSD(changes, analysis.variety)) * PlotAnalysis.VARIETY_MODIFIER + ; whenDone.value = analysis; whenDone.run(); } @@ -252,22 +217,27 @@ public class BukkitHybridUtils extends HybridUtils { processed_chunks.add(chunk); int X = chunk.getX(); int Z = chunk.getZ(); - short[][] current = new short[16][]; int minX; int minZ; int maxX; int maxZ; - if (X == cbx) minX = mod(bx); + if (X == cbx) minX = MathMan.mod(bx); else minX = 0; - if (Z == cbz) minZ = mod(bz); + if (Z == cbz) minZ = MathMan.mod(bz); else minZ = 0; - if (X == ctx) maxX = mod(tx); + if (X == ctx) maxX = MathMan.mod(tx); else maxX = 16; - if (Z == ctz) maxZ = mod(tz); + if (Z == ctz) maxZ = MathMan.mod(tz); else maxZ = 16; - int xb = ((X - cbx) << 4) - bx; - int zb = ((Z - cbz) << 4) - bz; + System.out.print("VALUES ===================="); + System.out.print(X + "," + Z + " | " + cbx + "," + cbz + " | " + bx + "," + bz); + System.out.print("VALUES ===================="); + + int xb = ((X) << 4) - bx; + int zb = ((Z) << 4) - bz; + + System.out.print(xb + "," + zb + " | " + minX + "," + minZ + " | " + maxX + "," + maxZ); for (int x = minX; x <= maxX; x++) { for (int z = minZ; z <= maxZ; z++) { diff --git a/src/main/java/com/intellectualcrafters/plot/listeners/worldedit/WEListener.java b/src/main/java/com/intellectualcrafters/plot/listeners/worldedit/WEListener.java index d88226c61..d9417a806 100644 --- a/src/main/java/com/intellectualcrafters/plot/listeners/worldedit/WEListener.java +++ b/src/main/java/com/intellectualcrafters/plot/listeners/worldedit/WEListener.java @@ -77,7 +77,6 @@ public class WEListener implements Listener { return max; } catch (NumberFormatException e) { - e.printStackTrace(); return 0; } } diff --git a/src/main/java/com/intellectualcrafters/plot/object/Plot.java b/src/main/java/com/intellectualcrafters/plot/object/Plot.java index 4bf029e18..3b3f8b043 100644 --- a/src/main/java/com/intellectualcrafters/plot/object/Plot.java +++ b/src/main/java/com/intellectualcrafters/plot/object/Plot.java @@ -24,12 +24,16 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map.Entry; import java.util.UUID; +import sun.awt.SunHints.Value; + import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.database.DBFunc; import com.intellectualcrafters.plot.flag.Flag; +import com.intellectualcrafters.plot.flag.FlagManager; import com.intellectualcrafters.plot.util.ChunkManager; import com.intellectualcrafters.plot.util.MainUtil; @@ -260,6 +264,18 @@ public class Plot implements Cloneable { MainUtil.clear(this, false, whenDone); } + /** + * This will return null if the plot hasn't been analyzed + * @return analysis of plot + */ + public PlotAnalysis getComplexity() { + return PlotAnalysis.getAnalysis(this); + } + + public void analyze(RunnableVal whenDone) { + PlotAnalysis.analyzePlot(this, whenDone); + } + /** * Delete a plot * @see PS#removePlot(String, PlotId, boolean) diff --git a/src/main/java/com/intellectualcrafters/plot/object/PlotAnalysis.java b/src/main/java/com/intellectualcrafters/plot/object/PlotAnalysis.java index 873f3daaf..190952465 100644 --- a/src/main/java/com/intellectualcrafters/plot/object/PlotAnalysis.java +++ b/src/main/java/com/intellectualcrafters/plot/object/PlotAnalysis.java @@ -1,5 +1,12 @@ package com.intellectualcrafters.plot.object; +import java.util.List; + +import com.intellectualcrafters.plot.flag.Flag; +import com.intellectualcrafters.plot.flag.FlagManager; +import com.intellectualcrafters.plot.generator.BukkitHybridUtils; +import com.intellectualcrafters.plot.util.TaskManager; + public class PlotAnalysis { public double changes; public double faces; @@ -7,4 +14,43 @@ public class PlotAnalysis { public double air; public double variety; public double complexity; + + public static double CHANGES_MODIFIER = 32; + public static double FACES_MODIFIER = 32; + public static double DATA_MODIFIER = 32; + public static double AIR_MODIFIER = 32; + public static double VARIETY_MODIFIER = 32; + + public static PlotAnalysis getAnalysis(Plot plot) { + Flag flag = FlagManager.getPlotFlag(plot, "analysis"); + if (flag != null) { + PlotAnalysis analysis = new PlotAnalysis(); + List values = (List) flag.getValue(); + analysis.changes = values.get(0); + analysis.faces = values.get(1); + analysis.data = values.get(3); + analysis.air = values.get(4); + analysis.variety = values.get(5); + return analysis; + } + return null; + } + + public static void analyzePlot(Plot plot, RunnableVal whenDone) { + PlotAnalysis analysis = getAnalysis(plot); + if (analysis != null) { + whenDone.value = analysis; + whenDone.run(); + return; + } + BukkitHybridUtils.manager.analyzePlot(plot, whenDone); + } + + /** + * + * @param whenDone + */ + public static void calcOptimalModifiers(Runnable whenDone) { + + } } diff --git a/src/main/java/com/intellectualcrafters/plot/object/Rating.java b/src/main/java/com/intellectualcrafters/plot/object/Rating.java index 6f04e54fc..7afb9325d 100644 --- a/src/main/java/com/intellectualcrafters/plot/object/Rating.java +++ b/src/main/java/com/intellectualcrafters/plot/object/Rating.java @@ -15,6 +15,7 @@ public class Rating { private HashMap ratingMap; public Rating(int value) { + ratingMap = new HashMap<>(); if (Settings.RATING_CATEGORIES != null && Settings.RATING_CATEGORIES.size() > 1) { for (int i = 0 ; i < Settings.RATING_CATEGORIES.size(); i++) { ratingMap.put(Settings.RATING_CATEGORIES.get(i), (value % 10) - 1); diff --git a/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java b/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java index 5619250fd..4215b6757 100644 --- a/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java +++ b/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java @@ -1309,17 +1309,6 @@ public class MainUtil { return sendMessage(plr, msg, true); } - public static String colorise(final char alt, final String message) { - final char[] b = message.toCharArray(); - for (int i = 0; i < (b.length - 1); i++) { - if ((b[i] == alt) && ("0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(b[(i + 1)]) > -1)) { - b[i] = '\u00A7'; - b[(i + 1)] = Character.toLowerCase(b[(i + 1)]); - } - } - return new String(b); - } - public static void sendConsoleMessage(String msg) { sendMessage(null, msg); } @@ -1329,13 +1318,11 @@ public class MainUtil { } public static boolean sendMessage(final PlotPlayer plr, String msg, final boolean prefix) { - msg = colorise('&', msg); - final String prefixStr = colorise('&', C.PREFIX.s()); if ((msg.length() > 0) && !msg.equals("")) { if (plr == null) { - PS.log(prefixStr + msg); + PS.log(C.PREFIX.s() + msg); } else { - sendMessageWrapped(plr, prefixStr + msg); + plr.sendMessage(C.PREFIX.s() + C.color(msg)); } } return true; @@ -1408,27 +1395,6 @@ public class MainUtil { return lines.toArray(new String[lines.size()]); } - /** - * \\previous\\ - * - * @param plr - * @param msg Was used to wrap the chat client length (Packets out--) - */ - public static void sendMessageWrapped(final PlotPlayer plr, final String msg) { - // if (msg.length() > 65) { - // final String[] ss = wordWrap(msg, 65); - // final StringBuilder b = new StringBuilder(); - // for (final String p : ss) { - // b.append(p).append(p.equals(ss[ss.length - 1]) ? "" : "\n "); - // } - // msg = b.toString(); - // } - // if (msg.endsWith("\n")) { - // msg = msg.substring(0, msg.length() - 2); - // } - plr.sendMessage(msg); - } - /** * Send a message to the player * @@ -1441,14 +1407,10 @@ public class MainUtil { if (c.s().length() > 1) { String msg = c.s(); if ((args != null) && (args.length > 0)) { - for (final String str : args) { - if (msg.contains("%s")) { - msg = msg.replaceFirst("%s", str); - } - } + msg = C.format(c, args); } if (plr == null) { - PS.log(colorise('&', msg)); + PS.log(msg); } else { sendMessage(plr, msg, c.usePrefix()); } diff --git a/src/main/java/com/intellectualcrafters/plot/util/MathMan.java b/src/main/java/com/intellectualcrafters/plot/util/MathMan.java new file mode 100644 index 000000000..7f062797f --- /dev/null +++ b/src/main/java/com/intellectualcrafters/plot/util/MathMan.java @@ -0,0 +1,62 @@ +package com.intellectualcrafters.plot.util; + +public class MathMan { + public static double getMean(int[] array) { + double count = 0; + for (int i : array) { + count += i; + } + return count / array.length; + } + + public static double getMean(double[] array) { + double count = 0; + for (double i : array) { + count += i; + } + return count / array.length; + } + + public static double getSD(double[] array, double av) { + double sd = 0; + for (int i=0; i replacements) { StringBuilder sb = new StringBuilder(string); + int size = string.length(); for (Entry entry : replacements.entrySet()) { + if (size == 0) { + break; + } String key = entry.getKey(); String value = entry.getValue(); - + int start = sb.indexOf(key, 0); + while (start > -1) { + int end = start + key.length(); + int nextSearchStart = start + value.length(); + sb.replace(start, end, value); + size -= end - start; + start = sb.indexOf(key, nextSearchStart); + } + } + return sb.toString(); + } + + public static String replaceAll(String string, Object... pairs) { + StringBuilder sb = new StringBuilder(string); + for (int i = 0; i < pairs.length; i+=2) { + String key = pairs[i] + ""; + String value = pairs[i + 1] + ""; int start = sb.indexOf(key, 0); while (start > -1) { int end = start + key.length(); @@ -21,12 +40,4 @@ public class StringMan { } return sb.toString(); } - - public static String replaceAll(String string, Object... pairs) { - HashMap replacements = new HashMap<>(); - for (int i = 0; i < pairs.length; i+=2) { - replacements.put(pairs[i] + "", pairs[i+1] + ""); - } - return replaceFromMap(string, replacements); - } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 37969fb2b..8709a14cb 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -15,12 +15,18 @@ commands: permission: plots.use permission-message: "You are lacking the permission node 'plots.use'" permissions: - plots.use: - default: true + plotsquared_user_attributes: + default: false + plotsquared_user_attributes.disabletitles: + default: false + plotsquared_user_attributes.*: + default: false plots.admin: children: plots.*: true default: op + plots.use: + default: true plots.permpack.basicflags: default: op children: @@ -123,4 +129,4 @@ permissions: plots.undeny: true plots.kick: true plots.worldedit.bypass: - default: false + default: false \ No newline at end of file