Additional implementations for the configuration GUI and custom

actions
This commit is contained in:
Evenprime 2011-05-10 14:04:03 +02:00
parent fe9a868169
commit c5f910de21
11 changed files with 223 additions and 64 deletions

View File

@ -9,9 +9,9 @@ package cc.co.evenprime.bukkit.nocheat.actions;
public abstract class Action {
public final int firstAfter;
public final boolean repeat;
public final int repeat;
public Action(int firstAfter, boolean repeat) {
public Action(int firstAfter, int repeat) {
this.firstAfter = firstAfter;
this.repeat = repeat;
}

View File

@ -9,7 +9,7 @@ public class CancelAction extends Action {
public final static CancelAction cancel = new CancelAction();
private CancelAction() { super(1, true); }
private CancelAction() { super(1, 1); }
public String getName() {
return "cancel";

View File

@ -10,7 +10,7 @@ public class CustomAction extends Action {
public final String command;
public CustomAction(int firstAfter, boolean repeat, String command) {
public CustomAction(int firstAfter, int repeat, String command) {
super(firstAfter, repeat);
this.command = command;
}

View File

@ -11,13 +11,13 @@ public class LogAction extends Action {
public final Level level;
public final static LogAction loglow = new LogAction(1, false, Level.INFO);
public final static LogAction logmed = new LogAction(1, false, Level.WARNING);
public final static LogAction loghigh = new LogAction(1, false, Level.SEVERE);
public final static LogAction loglow = new LogAction(1, 0, Level.INFO);
public final static LogAction logmed = new LogAction(1, 0, Level.WARNING);
public final static LogAction loghigh = new LogAction(1, 0, Level.SEVERE);
public final static LogAction[] log = { loglow, logmed, loghigh };
public LogAction(int firstAfter, boolean repeat, Level level) {
public LogAction(int firstAfter, int repeat, Level level) {
super(firstAfter, repeat);
this.level = level;
}

View File

@ -30,10 +30,7 @@ import cc.co.evenprime.bukkit.nocheat.listeners.AirbuildBlockListener;
public class AirbuildCheck extends Check {
// How should airbuild violations be treated?
private final Action actions[][] = {
{ LogAction.loglow, CancelAction.cancel },
{ LogAction.logmed, CancelAction.cancel },
{ LogAction.loghigh, CancelAction.cancel } };
private Action actions[][];
private int limits[];
@ -134,6 +131,12 @@ public class AirbuildCheck extends Check {
limits[0] = config.getIntegerValue("airbuild.limits.low");
limits[1] = config.getIntegerValue("airbuild.limits.med");
limits[2] = config.getIntegerValue("airbuild.limits.high");
actions = new Action[3][];
actions[0] = config.getActionValue("airbuild.action.low");
actions[1] = config.getActionValue("airbuild.action.med");
actions[2] = config.getActionValue("airbuild.action.high");
setActive(config.getBooleanValue("active.airbuild"));

View File

@ -65,10 +65,7 @@ public class MovingCheck extends Check {
private String summaryMessage;
// How should moving violations be treated?
private final Action actions[][] = {
{ LogAction.loglow, CancelAction.cancel },
{ LogAction.logmed, CancelAction.cancel },
{ LogAction.loghigh, CancelAction.cancel } };
private Action actions[][];
public long statisticTotalEvents = 1; // Prevent accidental division by 0 at some point
@ -659,6 +656,12 @@ public class MovingCheck extends Check {
logMessage = config.getStringValue("moving.logmessage");
summaryMessage = config.getStringValue("moving.summarymessage");
actions = new Action[3][];
actions[0] = config.getActionValue("moving.action.low");
actions[1] = config.getActionValue("moving.action.med");
actions[2] = config.getActionValue("moving.action.high");
setActive(config.getBooleanValue("active.moving"));
} catch (ConfigurationException e) {
setActive(false);

View File

@ -41,10 +41,7 @@ public class SpeedhackCheck extends Check {
private String logMessage;
// How should speedhack violations be treated?
private Action actions[][] = {
{ LogAction.loglow, CancelAction.cancel },
{ LogAction.logmed, CancelAction.cancel },
{ LogAction.loghigh, CancelAction.cancel } };
private Action actions[][];
public void check(PlayerMoveEvent event) {
@ -160,6 +157,12 @@ public class SpeedhackCheck extends Check {
logMessage = config.getStringValue("speedhack.logmessage");
actions = new Action[3][];
actions[0] = config.getActionValue("speedhack.action.low");
actions[1] = config.getActionValue("speedhack.action.med");
actions[2] = config.getActionValue("speedhack.action.high");
setActive(config.getBooleanValue("active.speedhack"));
} catch (ConfigurationException e) {
setActive(false);

View File

@ -0,0 +1,87 @@
package cc.co.evenprime.bukkit.nocheat.config;
import cc.co.evenprime.bukkit.nocheat.actions.CustomAction;
public class CustomActionOption extends ChildOption {
private int firstAfter;
private int repeat;
private String command;
public CustomActionOption(String identifier, String command) {
super(identifier);
this.parseCommand(command);
}
private void parseCommand(String command) {
if(command.matches("\\[[0-9]*,[0-9]*\\] .*")) {
String s[] = command.split(" ", 2);
String s2[] = s[0].replace("[", "").replace("]", "").split(",");
this.firstAfter = Integer.parseInt(s2[0]);
this.repeat = Integer.parseInt(s2[1]);
this.command = s[1];
}
else if(command.matches("\\[[0-9]*\\] .*")) {
String s[] = command.split(" ", 2);
this.firstAfter = Integer.parseInt(s[0].replace("[", "").replace("]", ""));
this.repeat = 1;
this.command = s[1];
}
else
{
this.command = command;
this.firstAfter = 1;
this.repeat = 1;
}
}
@Override
public String getValue() {
if(firstAfter <= 1) {
return command;
}
else if(repeat <= 0) {
return "["+firstAfter+"] "+ command;
}
else {
return "["+firstAfter+","+repeat+"] "+ command;
}
}
public CustomAction getCustomActionValue() {
return new CustomAction(firstAfter, repeat, command);
}
public String getCommandValue() {
return command;
}
public void setCommandValue(String command) {
this.command = command;
}
public void setRepeatValue(int value) {
this.repeat = value;
}
public int getRepeatValue() {
return repeat;
}
public int getFirstAfterValue() {
return firstAfter;
}
public void setFirsttAfterValue(int value) {
this.firstAfter = value;
}
}

View File

@ -208,6 +208,21 @@ public class NoCheatConfiguration {
ParentOption bogusitemsNode = new ParentOption("bogusitems");
root.add(bogusitemsNode);
}
/*** CUSTOMACTIONS section ***/
{
ParentOption customActionsNode = new ParentOption("customactions");
root.add(customActionsNode);
customActionsNode.add(new CustomActionOption("mycommand",
CONFIG.getString("customactions.mycommand", "[4,8] TESTCOMMAND")));
customActionsNode.add(new CustomActionOption("mycommand2",
CONFIG.getString("customactions.mycommand2", "TESTCOMMAND2")));
customActionsNode.add(new CustomActionOption("mycommand3",
CONFIG.getString("customactions.mycommand3", "[7] TESTCOMMAND3")));
}
if(!configurationFile.exists()) {
writeConfigFile(configurationFile, this);
@ -246,9 +261,7 @@ public class NoCheatConfiguration {
}
}
private Action[] stringToActions(String string, Action[] def) {
if(string == null) return def;
public static Action[] stringToActions(String string) {
List<Action> as = new LinkedList<Action>();
String[] parts = string.split(" ");
@ -280,8 +293,7 @@ public class NoCheatConfiguration {
}
}
return as.toArray(def);
return as.toArray(new Action[as.size()]);
}
private String actionsToString(Action[] actions) {
@ -297,37 +309,6 @@ public class NoCheatConfiguration {
return s.toString().trim();
}
/**
* Convert a string into a log level
* @param string
* @return
*/
private static Level stringToLevel(String string, Level def) {
if(string == null) {
return def;
}
if(string.trim().equals("info") || string.trim().equals("low")) return Level.INFO;
if(string.trim().equals("warn") || string.trim().equals("med")) return Level.WARNING;
if(string.trim().equals("severe")|| string.trim().equals("high")) return Level.SEVERE;
return Level.OFF;
}
private static String levelToString(Level level) {
if(level == null) {
return "off";
}
if(level.equals(Level.INFO)) return "low";
else if(level.equals(Level.WARNING)) return "med";
else if(level.equals(Level.SEVERE)) return "high";
return "off";
}
/**
* Write configuration file to specific filename
* @param f
@ -336,6 +317,7 @@ public class NoCheatConfiguration {
try {
if(f.getParentFile() != null)
f.getParentFile().mkdirs();
f.createNewFile();
BufferedWriter w = new BufferedWriter(new FileWriter(f));
@ -347,7 +329,11 @@ public class NoCheatConfiguration {
}
}
public Action[] getActionValue(String optionName) throws ConfigurationException {
return stringToActions(getStringOption(optionName).getValue());
}
public int getIntegerValue(String optionName) throws ConfigurationException {
return getIntegerOption(optionName).getIntegerValue();
}

View File

@ -1,17 +1,21 @@
package cc.co.evenprime.bukkit.nocheat.wizard.gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.InputVerifier;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import cc.co.evenprime.bukkit.nocheat.config.BooleanOption;
import cc.co.evenprime.bukkit.nocheat.config.ChildOption;
import cc.co.evenprime.bukkit.nocheat.config.CustomActionOption;
import cc.co.evenprime.bukkit.nocheat.config.LevelOption;
import cc.co.evenprime.bukkit.nocheat.config.TextFieldOption;
@ -28,6 +32,9 @@ public class ChildOptionGuiFactory {
else if(option instanceof LevelOption) {
return createLogLevel((LevelOption)option);
}
else if(option instanceof CustomActionOption) {
return createCustomAction((CustomActionOption)option);
}
throw new RuntimeException("Unknown ChildOption " + option);
}
@ -54,6 +61,75 @@ public class ChildOptionGuiFactory {
return comboBox;
}
private static JPanel createCustomAction(final CustomActionOption option) {
final JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
final JTextField command = new JTextField(option.getCommandValue());
command.setColumns(55);
command.setInputVerifier(new InputVerifier() {
@Override
public boolean verify(JComponent arg0) {
option.setCommandValue(command.getText());
return true;
}
});
final JTextField repeat = new JTextField(String.valueOf(option.getRepeatValue()));
repeat.setColumns(3);
repeat.setInputVerifier(new InputVerifier() {
@Override
public boolean verify(JComponent arg0) {
int value;
try {
value = Integer.parseInt(repeat.getText());
option.setRepeatValue(value);
return true;
}
catch(Exception e) {
JOptionPane.showMessageDialog(repeat, "Illegal value for this field");
repeat.setText(String.valueOf(option.getRepeatValue()));
return false;
}
}
});
final JTextField firstAfter = new JTextField(String.valueOf(option.getFirstAfterValue()));
firstAfter.setColumns(3);
firstAfter.setInputVerifier(new InputVerifier() {
@Override
public boolean verify(JComponent arg0) {
int value;
try {
value = Integer.parseInt(firstAfter.getText());
option.setFirsttAfterValue(value);
return true;
}
catch(Exception e) {
JOptionPane.showMessageDialog(firstAfter, "Illegal value for this field");
firstAfter.setText(String.valueOf(option.getFirstAfterValue()));
return false;
}
}
});
panel.add(firstAfter);
panel.add(repeat);
panel.add(command);
return panel;
}
private static JCheckBox createBoolean(final BooleanOption option) {
final JCheckBox checkBox = new JCheckBox();
@ -73,11 +149,11 @@ public class ChildOptionGuiFactory {
private static JTextField createTextField(final TextFieldOption option) {
final JTextField textField = new JTextField(option.getValue());
if(option.hasPreferredLength()) {
textField.setColumns(option.getPreferredLength());
}
textField.setInputVerifier(new InputVerifier() {
@Override

View File

@ -48,7 +48,7 @@ public class ParentOptionGui extends JPanel {
c.gridx = 0;
c.gridy = children.size();
c.gridwidth = 3; // Spans over both columns
c.gridwidth = 3; // Spans over three columns
c.anchor = GridBagConstraints.WEST;
c.ipadx = 5;
c.ipady = 15;
@ -63,7 +63,7 @@ public class ParentOptionGui extends JPanel {
c.gridx = 0;
c.gridy = children.size();
c.gridwidth = 1; // Spans over both columns
c.gridwidth = 1;
c.anchor = GridBagConstraints.WEST;
c.ipadx = 10;
c.weightx = 0;
@ -72,10 +72,11 @@ public class ParentOptionGui extends JPanel {
c.gridx = 1;
c.gridy = children.size();
c.gridwidth = 1; // Spans over both columns
c.gridwidth = 1;
c.anchor = GridBagConstraints.WEST;
c.ipadx = 5;
c.weightx = 0;
c.fill = GridBagConstraints.HORIZONTAL;
JComponent child = ChildOptionGuiFactory.create((ChildOption)option);
@ -83,11 +84,11 @@ public class ParentOptionGui extends JPanel {
c.gridx = 2;
c.gridy = children.size();
c.gridwidth = 1; // Spans over both columns
c.gridwidth = 1;
c.anchor = GridBagConstraints.WEST;
c.ipadx = 5;
c.weightx = 1;
c.fill = GridBagConstraints.HORIZONTAL;
this.add(Box.createHorizontalGlue(), c);