diff --git a/src/main/java/net/h31ix/anticheat/Anticheat.java b/src/main/java/net/h31ix/anticheat/Anticheat.java index 13ed61f..629ce48 100644 --- a/src/main/java/net/h31ix/anticheat/Anticheat.java +++ b/src/main/java/net/h31ix/anticheat/Anticheat.java @@ -19,7 +19,10 @@ package net.h31ix.anticheat; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; @@ -85,6 +88,34 @@ public class Anticheat extends JavaPlugin cleanup(); } + private void save(InputStream in, File file) + { + if (!file.exists()) + { + file.getParentFile().mkdirs(); + try + { + OutputStream out = new FileOutputStream(file); + byte[] buf = new byte[1024]; + int len; + while((len=in.read(buf))>0) + { + out.write(buf,0,len); + } + out.close(); + in.close(); + } + catch (Exception e) + { + e.printStackTrace(); + } + if (verbose) + { + getLogger().log(Level.INFO, file.getName()+" created."); + } + } + } + private void setupXray() { final XRayTracker xtracker = manager.getXRayTracker(); @@ -208,7 +239,7 @@ public class Anticheat extends JavaPlugin private void setupConfig() { config = manager.getConfiguration(); - checkConfig(); + checkConfigs(); verbose = config.verboseStartup(); if (verbose) { @@ -229,32 +260,11 @@ public class Anticheat extends JavaPlugin } } - public void checkConfig() + public void checkConfigs() { - if (!new File(getDataFolder() + "/config.yml").exists()) - { - saveDefaultConfig(); - if (verbose) - { - getLogger().log(Level.INFO, "Config file created."); - } - } - if (!new File(getDataFolder() + "/lang.yml").exists()) - { - saveResource("lang.yml", false); - if (verbose) - { - getLogger().log(Level.INFO, "Lang file created."); - } - } - if (!new File(getDataFolder() + "/magic.yml").exists()) - { - saveResource("magic.yml", false); - if (verbose) - { - getLogger().log(Level.INFO, "Magic file created."); - } - } + save(getResource("config.yml"), new File(getDataFolder() + "/config.yml")); + save(getResource("lang.yml"), new File(getDataFolder() + "/lang.yml")); + save(getResource("magic.yml"), new File(getDataFolder() + "/magic.yml")); } public static Anticheat getPlugin() diff --git a/src/main/java/net/h31ix/anticheat/util/Configuration.java b/src/main/java/net/h31ix/anticheat/util/Configuration.java index ebf2aa3..03439d7 100644 --- a/src/main/java/net/h31ix/anticheat/util/Configuration.java +++ b/src/main/java/net/h31ix/anticheat/util/Configuration.java @@ -25,10 +25,9 @@ import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import net.h31ix.anticheat.manage.AnticheatManager; +import net.h31ix.anticheat.util.yaml.CommentedConfiguration; import org.bukkit.Bukkit; import org.bukkit.World; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; public class Configuration { @@ -37,10 +36,10 @@ public class Configuration private File levelFile = null; private File langFile = null; private File magicFile = null; - private FileConfiguration config; - private FileConfiguration level; - private FileConfiguration lang; - private FileConfiguration magic; + private CommentedConfiguration config; + private CommentedConfiguration level; + private CommentedConfiguration lang; + private CommentedConfiguration magic; private boolean logConsole; private boolean logXRay; private boolean alertXRay; @@ -151,11 +150,11 @@ public class Configuration public final void load() { - micromanage.getPlugin().checkConfig(); - config = YamlConfiguration.loadConfiguration(configFile); - level = YamlConfiguration.loadConfiguration(levelFile); - lang = YamlConfiguration.loadConfiguration(langFile); - magic = YamlConfiguration.loadConfiguration(magicFile); + micromanage.getPlugin().checkConfigs(); + config = CommentedConfiguration.loadConfig(configFile); + level = CommentedConfiguration.loadConfig(levelFile); + lang = CommentedConfiguration.loadConfig(langFile); + magic = CommentedConfiguration.loadConfig(magicFile); language = new Language(lang); updateFolder = Bukkit.getUpdateFolder(); @@ -248,7 +247,7 @@ public class Configuration return levelFile; } - public FileConfiguration getMagic() + public CommentedConfiguration getMagic() { return magic; } diff --git a/src/main/java/net/h31ix/anticheat/util/yaml/CommentedConfiguration.java b/src/main/java/net/h31ix/anticheat/util/yaml/CommentedConfiguration.java new file mode 100644 index 0000000..b329d66 --- /dev/null +++ b/src/main/java/net/h31ix/anticheat/util/yaml/CommentedConfiguration.java @@ -0,0 +1,117 @@ +/* + * AntiCheat for Bukkit. + * Copyright (C) 2012 AntiCheat Team | http://gravitydevelopment.net + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.h31ix.anticheat.util.yaml; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; +import org.bukkit.Bukkit; +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.configuration.file.YamlConstructor; +import org.bukkit.configuration.file.YamlRepresenter; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.representer.Representer; + +public class CommentedConfiguration extends YamlConfiguration +{ + private Map comments = new HashMap(); + + private final DumperOptions yamlOptions = new DumperOptions(); + private final Representer yamlRepresenter = new YamlRepresenter(); + private final Yaml yaml = new Yaml(new YamlConstructor(), yamlRepresenter, yamlOptions); + + @Override + public String saveToString() { + yamlOptions.setIndent(options().indent()); + yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + + String dump = yaml.dump(getValues(false)); + + if (dump.equals(BLANK_CONFIG)) { + dump = ""; + } else { + StringBuilder builder = new StringBuilder(); + int i = 0; + for(String string : dump.split("\n")) { + i++; + while(comments.containsKey(i)) { + builder.append(comments.get(i)); + builder.append("\n"); + i++; + } + builder.append(string); + builder.append("\n"); + } + dump = builder.toString(); + } + + return dump; + } + + @Override + public void load(InputStream stream) throws IOException, InvalidConfigurationException { + + InputStreamReader reader = new InputStreamReader(stream); + StringBuilder builder = new StringBuilder(); + BufferedReader input = new BufferedReader(reader); + + try { + String line; + int i = 0; + while ((line = input.readLine()) != null) { + i++; + if(line.contains(COMMENT_PREFIX)) { + comments.put(i, line); + } + builder.append(line); + builder.append('\n'); + } + } finally { + input.close(); + } + + loadFromString(builder.toString()); + } + + public static CommentedConfiguration loadConfig(File file) { + + CommentedConfiguration config = new CommentedConfiguration(); + try { + config.load(file); + } catch (FileNotFoundException ex) { + } catch (IOException ex) { + Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex); + } catch (InvalidConfigurationException ex) { + Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file , ex); + } + + return config; + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 2f64f6b..3d6867c 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,37 +1,37 @@ -#AntiCheat configuration file -#Please report any bugs: http://dev.bukkit.org/server-mods/anticheat/ +# AntiCheat configuration file +# Please report any bugs: http://dev.bukkit.org/server-mods/anticheat/ XRay: Log xray stats: true Alert when xray is found: false - +# Events occur when a player's hack level changes. You may configure reactions to these events here. +# Valid events = NONE,WARN,KICK,BAN,COMMAND[command] +# Use commands like so: COMMAND[ban &player 30] or COMMAND[kick &player hacking] or COMMAND[jail &player 10] +# &player will be replaced with player's name, and any command on your server can be used. Events: - #Valid events = NONE,WARN,KICK,BAN,COMMAND[command] - #Use commands like so: COMMAND[ban &player 30] or COMMAND[kick &player hacking] or COMMAND[jail &player 10] - #&player will be replaced with player's name, and any command on your server can be used. Level Medium: WARN Level High: KICK - #How many warnings should the player get before entering medium? + # How many warnings should the player get before entering medium? Medium threshold: 20 - #How many warnings should the player get before entering high? + # How many warnings should the player get before entering high? High threshold: 50 - System: - #Turning auto-update off is a _BAD_ idea. You will no longer be protected by the latest hacks/cheats if you do so. - #AntiCheat will never bother you about updates if this is on, but if you turn it off you will have to manually update. + # Turning auto-update off is a _BAD_ idea. You will no longer be protected by the latest hacks/cheats if you do so. + # AntiCheat will never bother you about updates if this is on, but if you turn it off you will have to manually update. Auto update: true + # Should AntiCheat log ALL failed checks to console? Log to console: false - #Should AntiCheat log to files? - #0 = off, 1 = log when an event takes place, 2 = more detailed logs, 3 = most detailed logs + # Should AntiCheat log to files? + # 0 = off, 1 = log only when an event takes place, 2 = more detailed logs, 3 = most detailed logs File log level: 1 - #Should AntiCheat display extra (debug) information when starting? + # Should AntiCheat display extra debug information when starting? Verbose startup: false - #Should AntiCheat block when players spam messages in chat? + # Should AntiCheat block players spamming messages in chat? Block chat spam: true - #Should AntiCheat block when players spam commands in chat? + # Should AntiCheat block players spamming commands in chat? Block command spam: true - #If silent mode is on, players will not be stopped when they try to hack, - #and AntiCheat will do everything possible to keep them in the dark, but their level will still raise, - #alerts will still be sent to console and to admins online, and events will still take place. + # If silent mode is on, players will not be stopped when they try to hack, + # and AntiCheat will do everything possible to keep them unaware of their rising hack level. + # However, alerts will still be sent to console and to admins online, and events will still take place. Silent mode: false - #Should ops be exempt from all checks? + # Should ops be exempt from all checks? Exempt op: false \ No newline at end of file diff --git a/src/main/resources/lang.yml b/src/main/resources/lang.yml index ef8a340..47ef071 100644 --- a/src/main/resources/lang.yml +++ b/src/main/resources/lang.yml @@ -1,3 +1,5 @@ +# AntiCheat language file +# Please report any bugs: http://dev.bukkit.org/server-mods/anticheat/ alert: medium_alert: - "&player has just entered the medium hack level." diff --git a/src/main/resources/magic.yml b/src/main/resources/magic.yml index fc957da..795f9d6 100644 --- a/src/main/resources/magic.yml +++ b/src/main/resources/magic.yml @@ -1,6 +1,6 @@ -#Magic number configuration file for AntiCheat -#Documentation for these values is available at http://gravitydevelopment.net/docs/anticheat/net/h31ix/anticheat/util/Magic.html - +# AntiCheat configuration file +# Please report any bugs: http://dev.bukkit.org/server-mods/anticheat/ +# Documentation for these values is available at http://gravitydevelopment.net/docs/anticheat/net/h31ix/anticheat/util/Magic.html ENTERED_EXITED_TIME: 1000 SNEAK_TIME: 250 TELEPORT_TIME: 1000