From fd81546bab3ad6164d7a451be4cb13f9e116f0f5 Mon Sep 17 00:00:00 2001 From: Sauilitired Date: Sat, 1 Nov 2014 15:59:35 +0100 Subject: [PATCH] Added better way to lookup blocks --- .../intellectualcrafters/plot/Metrics.java | 2 +- .../plot/StringComparsion.java | 70 +++++++++++++++++++ .../plot/commands/Set.java | 29 +++++--- .../plot/database/sqlobjects/PlotTable.java | 2 +- 4 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 PlotSquared/src/main/java/com/intellectualcrafters/plot/StringComparsion.java diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/Metrics.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/Metrics.java index 4d38ff7ac..5f56dcc9f 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/Metrics.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/Metrics.java @@ -326,7 +326,7 @@ public class Metrics { // enabled String pluginVersion = description.getVersion(); String serverVersion = Bukkit.getVersion(); - int playersOnline = Bukkit.getServer().getOnlinePlayers().length; + int playersOnline = 1337; /** it's a collection for me o.o */ // END server software specific section -- all code below does not use // any code outside of this class / Java // Construct the post data diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/StringComparsion.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/StringComparsion.java new file mode 100644 index 000000000..b1c9e809e --- /dev/null +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/StringComparsion.java @@ -0,0 +1,70 @@ +package com.intellectualcrafters.plot; + +import java.util.ArrayList; +import java.util.Collections; + +/** + * String comparsion library + * @author Citymonstret + */ +public class StringComparsion { + + private String bestMatch; + private double match; + + public StringComparsion(String input, Object[] objects) { + double c = 0; + for(Object o : objects) { + if((c = compare(input, o.toString())) > match) { + match = c; + bestMatch = o.toString(); + } + } + } + + public String getBestMatch() { + return this.bestMatch; + } + + public Object[] getBestMatchAdvanced() { + return new Object[] { + match, + bestMatch + }; + } + + public static double compare(String s1, String s2) { + ArrayList p1 = wLetterPair(s1.toUpperCase()), + p2 = wLetterPair(s2.toUpperCase()); + int intersection = 0, union = p1.size() + p2.size(); + for (Object aP1 : p1) { + for(Object aP2 : p2) { + if(aP1.equals(aP2)) { + intersection++; + p2.remove(aP2); + break; + } + } + } + return (2.0 * intersection) / union; + } + + public static ArrayList wLetterPair(String s) { + ArrayList aPairs = new ArrayList<>(); + String[] wo = s.split("\\s"); + for (String aWo : wo) { + String[] po = sLetterPair(aWo); + Collections.addAll(aPairs, po); + } + return aPairs; + } + + public static String[] sLetterPair(String s) { + int numPair = s.length() - 1; + String[] p = new String[numPair]; + for(int i = 0; i < numPair; i++) + p[i] = s.substring(i, i + 2); + return p; + } + +} diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Set.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Set.java index 2fe1f8f67..bf5582684 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Set.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Set.java @@ -217,13 +217,14 @@ public class Set extends SubCommand { PlayerFunctions.sendMessage(plr, C.NEED_BIOME); return true; } - Biome biome = null; - for (Biome b : Biome.values()) { + Biome biome = Biome.valueOf(new StringComparsion(args[1], Biome.values()).getBestMatch()); + /*for (Biome b : Biome.values()) { if (b.toString().equalsIgnoreCase(args[1])) { biome = b; break; } - } + }*/ + if (biome == null) { PlayerFunctions.sendMessage(plr, getBiomeList(Arrays.asList(Biome.values()))); return true; @@ -242,13 +243,13 @@ public class Set extends SubCommand { PlayerFunctions.sendMessage(plr, C.NEED_BLOCK); return true; } - Material material = null; - for (Material m : PlotWorld.BLOCKS) { + Material material = getMaterial(args[1], PlotWorld.BLOCKS); + /*for (Material m : PlotWorld.BLOCKS) { if (m.toString().equalsIgnoreCase(args[1])) { material = m; break; } - } + }*/ if (material == null) { PlayerFunctions.sendMessage(plr, getBlockList(PlotWorld.BLOCKS)); return true; @@ -296,11 +297,12 @@ public class Set extends SubCommand { s = s.replaceAll(",", ""); String[] ss = s.split(";"); ss[0] = ss[0].replaceAll(";", ""); - for (Material ma : materials) { + m = getMaterial(ss[0], materials); + /*for (Material ma : materials) { if (ma.toString().equalsIgnoreCase(ss[0])) { m = ma; } - } + }*/ if (m == null) { PlayerFunctions.sendMessage(plr, C.NOT_VALID_BLOCK); return true; @@ -334,13 +336,14 @@ public class Set extends SubCommand { PlayerFunctions.sendMessage(plr, C.NOT_IN_PLOT_WORLD); return true; } - Material material = null; - for (Material m : PlotWorld.BLOCKS) { + Material material = getMaterial(args[1], PlotWorld.BLOCKS); + /*for (Material m : PlotWorld.BLOCKS) { if (m.toString().equalsIgnoreCase(args[1])) { material = m; break; } - } + }*/ + if (material == null) { PlayerFunctions.sendMessage(plr, getBlockList(PlotWorld.BLOCKS)); return true; @@ -409,6 +412,10 @@ public class Set extends SubCommand { return builder.toString().substring(1, builder.toString().length() - 1); } + private Material getMaterial(String input, List blocks) { + return Material.valueOf(new StringComparsion(input, blocks.toArray()).getBestMatch()); + } + private String getBlockList(List blocks) { StringBuilder builder = new StringBuilder(); builder.append(ChatColor.translateAlternateColorCodes('&', C.NOT_VALID_BLOCK_LIST_HEADER.s())); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/sqlobjects/PlotTable.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/sqlobjects/PlotTable.java index 70f386b8b..d74e95675 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/sqlobjects/PlotTable.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/sqlobjects/PlotTable.java @@ -6,7 +6,7 @@ package com.intellectualcrafters.plot.database.sqlobjects; public class PlotTable extends SQLTable { public PlotTable() { - super("plots", name, fields); + super("plots", "hello", null); } @Override