Fixes #419 , among other things

This commit is contained in:
boy0001
2015-07-17 20:48:13 +10:00
parent 1564b58a3d
commit b2fbd74d4b
15 changed files with 226 additions and 141 deletions

View File

@ -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());
}

View File

@ -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<array.length;i++)
{
sd += Math.pow(Math.abs(array[i] - av), 2);
}
return Math.sqrt(sd/array.length);
}
public static double getSD(int[] array, double av) {
double sd = 0;
for (int i=0; i<array.length;i++)
{
sd += Math.pow(Math.abs(array[i] - av), 2);
}
return Math.sqrt(sd/array.length);
}
public static int mod(int x, int y) {
if (isPowerOfTwo(y)) {
return x & (y - 1);
}
return x % y;
}
public static int unsignedmod(int x, int y) {
if (isPowerOfTwo(y)) {
return x & (y - 1);
}
return x % y;
}
public static boolean isPowerOfTwo(int x) {
return (x & (x - 1)) == 0;
}
public static int mod(int x) {
if (x < 0) {
return (x % 16) + 16;
}
return x % 16;
}
}

View File

@ -1,16 +1,35 @@
package com.intellectualcrafters.plot.util;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class StringMan {
public static String replaceFromMap(String string, Map<String, String> replacements) {
StringBuilder sb = new StringBuilder(string);
int size = string.length();
for (Entry<String, String> 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<String, String> replacements = new HashMap<>();
for (int i = 0; i < pairs.length; i+=2) {
replacements.put(pairs[i] + "", pairs[i+1] + "");
}
return replaceFromMap(string, replacements);
}
}