Permalink
Browse files

Added GREATERVAR and LESSVAR action types.

  • Loading branch information...
me4502 committed Jul 31, 2017
1 parent 54bf15d commit 2f9fce66862975b420bf2e7a87dd3aa19a3d1dc8
@@ -1,5 +1,6 @@
package com.sk89q.craftbook.mechanics.items;
import com.sk89q.craftbook.bukkit.CraftBookPlugin;
import org.apache.commons.lang.StringUtils;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
@@ -32,7 +33,9 @@ public CommandItemAction(String name, ActionType type, String value, ActionRunSt
SETVAR,
MATHVAR,
ISVAR
ISVAR,
GREATERVAR,
LESSVAR
}
/**
@@ -86,11 +89,40 @@ public boolean runAction(CommandItemDefinition definition, Event event, Player p
VariableManager.instance.setVariable(mvar, mnamespace, val);
return true;
case ISVAR:
String[] isparts = RegexUtil.EQUALS_PATTERN.split(newVal,2);
case ISVAR: {
String[] isparts = RegexUtil.EQUALS_PATTERN.split(newVal, 2);
String isnamespace = VariableManager.getNamespace(isparts[0]);
String isvar = VariableManager.getVariableName(isparts[0]);
return VariableManager.instance.getVariable(isvar, isnamespace).equals(isparts[1]);
}
case GREATERVAR: {
String[] isparts = RegexUtil.GREATER_THAN_PATTERN.split(newVal, 2);
String isnamespace = VariableManager.getNamespace(isparts[0]);
String isvar = VariableManager.getVariableName(isparts[0]);
double variable = 0;
double test = 0;
try {
variable = Double.parseDouble(VariableManager.instance.getVariable(isvar, isnamespace));
test = Double.parseDouble(isparts[1]);
} catch(NumberFormatException e) {
CraftBookPlugin.logger().warning("Variable " + isparts[0] + " is not a number!");
}
return variable > test;
}
case LESSVAR: {
String[] isparts = RegexUtil.LESS_THAN_PATTERN.split(newVal, 2);
String isnamespace = VariableManager.getNamespace(isparts[0]);
String isvar = VariableManager.getVariableName(isparts[0]);
double variable = 0;
double test = 0;
try {
variable = Double.parseDouble(VariableManager.instance.getVariable(isvar, isnamespace));
test = Double.parseDouble(isparts[1]);
} catch(NumberFormatException e) {
CraftBookPlugin.logger().warning("Variable " + isparts[0] + " is not a number!");
}
return variable < test;
}
default:
return true;
}
@@ -13,6 +13,8 @@
public static final Pattern COMMA_PATTERN = Pattern.compile(",", Pattern.LITERAL);
public static final Pattern RIGHT_BRACKET_PATTERN = Pattern.compile("]", Pattern.LITERAL);
public static final Pattern LEFT_BRACKET_PATTERN = Pattern.compile("[", Pattern.LITERAL);
public static final Pattern GREATER_THAN_PATTERN = Pattern.compile(">", Pattern.LITERAL);
public static final Pattern LESS_THAN_PATTERN = Pattern.compile("<", Pattern.LITERAL);
public static final Pattern MINUS_PATTERN = Pattern.compile("-", Pattern.LITERAL);
public static final Pattern PLUS_PATTERN = Pattern.compile("+", Pattern.LITERAL);
public static final Pattern COMMENT_PATTERN = Pattern.compile("#", Pattern.LITERAL);

0 comments on commit 2f9fce6

Please sign in to comment.