Created
October 29, 2011 11:48
-
-
Save xZise/1324361 to your computer and use it in GitHub Desktop.
Method parser test classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.tahkeh.loginmessage.methods.AliasMethod; | |
import com.tahkeh.loginmessage.methods.MethodParser; | |
import com.tahkeh.loginmessage.methods.PrintMethod; | |
import de.xzise.MinecraftUtil; | |
import de.xzise.XLogger; | |
public class AltCls { | |
private static MethodParser parser; | |
public static void main(String[] args) { | |
XLogger logger = new XLogger("test", "cls"); | |
parser = new MethodParser(logger); | |
parser.loadDefaults(); | |
parser.registerMethod("onlist", new OnlistMethod(logger), 0, 2, 3); | |
parser.registerMethod("foo", new AliasMethod("Online: onlist($0;,$0;,$1;)", 2), 2); | |
parser.registerMethod("foo", new AliasMethod("Online: print(Jay)", 0), 0); | |
MethodParser.createRedirected(parser, "bar", "onlist", 0); | |
// Online list | |
call("Online2 1 Y: onlist !"); // SHOULD Work | |
call("Online2 2 Y: onlist(\"a\",f)!"); // SHOULD Work | |
call("Online2 3 Y: onlist(\"a\",f,e)!"); // SHOULD Work | |
call("Online2 4 Y: onlist()!"); // SHOULD Work | |
call("Online2 5 Y: onlist"); // Work | |
call("Online2 6 Y: onlist <<-- Jay!"); // Work | |
call("Online2 7 Y: onlist()<<-- Jay!"); // Work | |
call("Online2 8 Y: onlist(\"a\",15)!"); // Work | |
call("Online2 9 N: onlist<<-- Jay!"); // Not work | |
call("Online2 10 N: onlist!"); // Not work | |
call("Print2 1: Hello world → print(\"Hello world\")"); | |
call("Print2 2: Brackets: () → print(\"Brackets: ()\")"); | |
call("Print2 3: Specific escape: ) → print(Specific escape: \\))"); | |
call("Print2 4: Specific escape: , ha! → print(Specific escape: \\,,ha!)"); | |
call("Print2 5: Online: Jay bar! → call(call(\"foo\", \"print(\"bar\")\"))!"); | |
call("ifeq 1 Y: ifequals(a,a,true,false)"); | |
call("ifeq 1 N: ifequals(a,A,true,false)"); | |
call("ifeq 1 Y: ifequalsignorecase(a,A,true,false)"); | |
call("print(foo(a,f)) 1: foo(a,f)"); | |
call("print(foo) 2: foo()"); | |
call("print(bar) 2: bar()"); | |
} | |
private static void call(String s) { | |
System.out.println(parser.parseLine(null, "", s)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.Iterator; | |
import java.util.List; | |
import org.bukkit.Bukkit; | |
import org.bukkit.ChatColor; | |
import org.bukkit.entity.Player; | |
import com.tahkeh.loginmessage.Message; | |
import com.tahkeh.loginmessage.methods.Method; | |
import de.xzise.MinecraftUtil; | |
import de.xzise.XLogger; | |
/** | |
* Simple online list method. | |
*/ | |
public class OnlistMethod implements Method { | |
public final static ChatColor NAME_COLOR = ChatColor.WHITE; | |
public final static ChatColor COMMA_COLOR = ChatColor.WHITE; | |
/** | |
* Tries to convert a string into an integer. If the string is invalid it | |
* returns <code>null</code>. | |
* | |
* @param string | |
* The string to be parsed. | |
* @param radix | |
* The radix of the integer. | |
* @return The value if the string is valid, otherwise <code>null</code>. | |
* @since 1.3 | |
*/ | |
// TODO: Move to BPU 1.3 | |
public static Integer tryAndGetInteger(String string, int radix) { | |
try { | |
return Integer.parseInt(string, radix); | |
} catch (NumberFormatException e) { | |
return null; | |
} | |
} | |
/** | |
* Parses an integer between 0-16 or 0-F. | |
* @param string the inputed string. | |
* @return the integer or null, if it couldn't parsed. | |
*/ | |
public static Integer parseInt(String string) { | |
string = string.trim(); | |
if (string.length() == 2) { | |
return MinecraftUtil.tryAndGetInteger(string); | |
} else if (string.length() == 1) { | |
return tryAndGetInteger(string, 16); | |
} else { | |
return null; | |
} | |
} | |
private final XLogger logger; | |
public OnlistMethod(XLogger logger) { | |
this.logger = logger; | |
} | |
private ChatColor getColor(String parameter, String name) { | |
Integer colorValue = parseInt(parameter); | |
if (colorValue == null || colorValue < 0 || colorValue > 15) { | |
this.logger.warning("OnList parameter '" + name + "' is not a valid integer ('" + parameter + "'). Valid integers are: 1-16 or 0-F"); | |
return null; | |
} else { | |
ChatColor color = ChatColor.getByCode(colorValue); | |
if (color == null) { | |
this.logger.warning("OnList parameter '" + name + "' is not a valid chat color (" + colorValue + ")."); | |
} | |
return color; | |
} | |
} | |
@Override | |
public String call(Player player, String event, String... parameters) { | |
ChatColor nameColor = NAME_COLOR; | |
ChatColor commaColor = COMMA_COLOR; | |
ChatColor endColor = COMMA_COLOR; | |
boolean endColorSet = false; | |
switch (parameters.length) { | |
case 3: | |
endColor = getColor(parameters[2], "end color"); | |
endColorSet = true; | |
case 2: | |
nameColor = getColor(parameters[0], "name color"); | |
commaColor = getColor(parameters[1], "comma color"); | |
break; | |
case 0: | |
break; | |
default: | |
return null; | |
} | |
if (nameColor != null && commaColor != null && endColor != null) { | |
if (!endColorSet) { | |
endColor = commaColor; | |
} | |
return this.call(player, event, nameColor, commaColor, endColor); | |
} else { | |
return null; | |
} | |
} | |
private String call(Player triggerPlayer, String event, ChatColor nameColor, ChatColor commaColor, ChatColor endColor) { | |
StringBuilder builder = new StringBuilder(); | |
// List<Player> allPlayers = Arrays.asList(Bukkit.getServer().getOnlinePlayers()); | |
// if (Message.isLeaveEvent(event)) { | |
// allPlayers.remove(triggerPlayer); | |
// } | |
// for (Iterator<Player> playerIt = allPlayers.iterator(); playerIt.hasNext();) { | |
// Player player = playerIt.next(); | |
// builder.append(nameColor).append(player.getName()); | |
// if (playerIt.hasNext()) { | |
// builder.append(commaColor).append(", "); | |
// } | |
// } | |
List<String> allPlayers = Arrays.asList("foo", "leet", "bar"); | |
for (Iterator<String> playerIt = allPlayers.iterator(); playerIt.hasNext();) { | |
builder.append(nameColor).append(playerIt.next()); | |
if (playerIt.hasNext()) { | |
builder.append(commaColor).append(", "); | |
} | |
} | |
return builder.append(endColor).toString(); | |
} | |
@Override | |
public boolean recursive() { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment