From c5f910de21fc56d83655d3df9217aed98d6d3347 Mon Sep 17 00:00:00 2001 From: Evenprime Date: Tue, 10 May 2011 14:04:03 +0200 Subject: [PATCH] Additional implementations for the configuration GUI and custom actions --- .../bukkit/nocheat/actions/Action.java | 4 +- .../bukkit/nocheat/actions/CancelAction.java | 2 +- .../bukkit/nocheat/actions/CustomAction.java | 2 +- .../bukkit/nocheat/actions/LogAction.java | 8 +- .../bukkit/nocheat/checks/AirbuildCheck.java | 11 ++- .../bukkit/nocheat/checks/MovingCheck.java | 11 ++- .../bukkit/nocheat/checks/SpeedhackCheck.java | 11 ++- .../nocheat/config/CustomActionOption.java | 87 +++++++++++++++++++ .../nocheat/config/NoCheatConfiguration.java | 60 +++++-------- .../wizard/gui/ChildOptionGuiFactory.java | 80 ++++++++++++++++- .../nocheat/wizard/gui/ParentOptionGui.java | 11 +-- 11 files changed, 223 insertions(+), 64 deletions(-) create mode 100644 src/cc/co/evenprime/bukkit/nocheat/config/CustomActionOption.java diff --git a/src/cc/co/evenprime/bukkit/nocheat/actions/Action.java b/src/cc/co/evenprime/bukkit/nocheat/actions/Action.java index 002f1720..3643fe66 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/actions/Action.java +++ b/src/cc/co/evenprime/bukkit/nocheat/actions/Action.java @@ -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; } diff --git a/src/cc/co/evenprime/bukkit/nocheat/actions/CancelAction.java b/src/cc/co/evenprime/bukkit/nocheat/actions/CancelAction.java index 2af5361d..5c6909fd 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/actions/CancelAction.java +++ b/src/cc/co/evenprime/bukkit/nocheat/actions/CancelAction.java @@ -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"; diff --git a/src/cc/co/evenprime/bukkit/nocheat/actions/CustomAction.java b/src/cc/co/evenprime/bukkit/nocheat/actions/CustomAction.java index ca9c89e0..4575cf06 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/actions/CustomAction.java +++ b/src/cc/co/evenprime/bukkit/nocheat/actions/CustomAction.java @@ -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; } diff --git a/src/cc/co/evenprime/bukkit/nocheat/actions/LogAction.java b/src/cc/co/evenprime/bukkit/nocheat/actions/LogAction.java index 3f9cf78a..0752c9fe 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/actions/LogAction.java +++ b/src/cc/co/evenprime/bukkit/nocheat/actions/LogAction.java @@ -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; } diff --git a/src/cc/co/evenprime/bukkit/nocheat/checks/AirbuildCheck.java b/src/cc/co/evenprime/bukkit/nocheat/checks/AirbuildCheck.java index a8209164..0da4b1a1 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/checks/AirbuildCheck.java +++ b/src/cc/co/evenprime/bukkit/nocheat/checks/AirbuildCheck.java @@ -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")); diff --git a/src/cc/co/evenprime/bukkit/nocheat/checks/MovingCheck.java b/src/cc/co/evenprime/bukkit/nocheat/checks/MovingCheck.java index 5893ae71..082cd9ec 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/checks/MovingCheck.java +++ b/src/cc/co/evenprime/bukkit/nocheat/checks/MovingCheck.java @@ -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); diff --git a/src/cc/co/evenprime/bukkit/nocheat/checks/SpeedhackCheck.java b/src/cc/co/evenprime/bukkit/nocheat/checks/SpeedhackCheck.java index 98239682..b1386110 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/checks/SpeedhackCheck.java +++ b/src/cc/co/evenprime/bukkit/nocheat/checks/SpeedhackCheck.java @@ -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); diff --git a/src/cc/co/evenprime/bukkit/nocheat/config/CustomActionOption.java b/src/cc/co/evenprime/bukkit/nocheat/config/CustomActionOption.java new file mode 100644 index 00000000..2ff871ca --- /dev/null +++ b/src/cc/co/evenprime/bukkit/nocheat/config/CustomActionOption.java @@ -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; + + } + +} diff --git a/src/cc/co/evenprime/bukkit/nocheat/config/NoCheatConfiguration.java b/src/cc/co/evenprime/bukkit/nocheat/config/NoCheatConfiguration.java index e5c56d9d..4e15fb4e 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/config/NoCheatConfiguration.java +++ b/src/cc/co/evenprime/bukkit/nocheat/config/NoCheatConfiguration.java @@ -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 as = new LinkedList(); 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(); } diff --git a/src/cc/co/evenprime/bukkit/nocheat/wizard/gui/ChildOptionGuiFactory.java b/src/cc/co/evenprime/bukkit/nocheat/wizard/gui/ChildOptionGuiFactory.java index c11c36db..de79cc40 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/wizard/gui/ChildOptionGuiFactory.java +++ b/src/cc/co/evenprime/bukkit/nocheat/wizard/gui/ChildOptionGuiFactory.java @@ -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 diff --git a/src/cc/co/evenprime/bukkit/nocheat/wizard/gui/ParentOptionGui.java b/src/cc/co/evenprime/bukkit/nocheat/wizard/gui/ParentOptionGui.java index c78af84e..9c932256 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/wizard/gui/ParentOptionGui.java +++ b/src/cc/co/evenprime/bukkit/nocheat/wizard/gui/ParentOptionGui.java @@ -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);