From 614fa4668d3d9d175ab9c4d12fb51af7eb4bf764 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Wed, 2 Sep 2015 18:20:57 +0800 Subject: [PATCH] update... Signed-off-by: 502647092 --- .classpath | 19 ++- pom.xml | 43 ++++++- .../SimpleEssential/SimpleEssential.java | 20 ++-- .../SimpleEssential/config/Config.java | 55 --------- .../SimpleEssential/config/ConfigLoader.java | 108 ------------------ .../SimpleEssential/config/FileConfig.java | 108 ------------------ .../SimpleEssential/config/Language.java | 63 ++-------- .../SimpleEssential/config/PlayerConfig.java | 43 ++----- .../SimpleEssential/utils/VersionChecker.java | 108 ------------------ 9 files changed, 83 insertions(+), 484 deletions(-) delete mode 100644 src/main/java/cn/citycraft/SimpleEssential/config/Config.java delete mode 100644 src/main/java/cn/citycraft/SimpleEssential/config/ConfigLoader.java delete mode 100644 src/main/java/cn/citycraft/SimpleEssential/config/FileConfig.java delete mode 100644 src/main/java/cn/citycraft/SimpleEssential/utils/VersionChecker.java diff --git a/.classpath b/.classpath index 2c705bb..bea39b1 100644 --- a/.classpath +++ b/.classpath @@ -1,7 +1,16 @@ - - + + + + + + + + + + + @@ -12,5 +21,11 @@ + + + + + + diff --git a/pom.xml b/pom.xml index e469850..9fdb91b 100644 --- a/pom.xml +++ b/pom.xml @@ -7,12 +7,12 @@ SimpleEssential ${project.name} - - - src/main/resources - true - - + + + src/main/resources + true + + maven-compiler-plugin @@ -22,6 +22,27 @@ 1.7 + + org.apache.maven.plugins + maven-shade-plugin + 2.3 + + true + + + cn.citycraft:PluginHelper + + + + + + package + + shade + + + + @@ -29,6 +50,10 @@ spigot-repo https://hub.spigotmc.org/nexus/content/groups/public/ + + citycraft-repo + http://ci.citycraft.cn:8800/jenkins/plugin/repository/everything/ + @@ -37,6 +62,12 @@ jar 1.8.3-R0.1-SNAPSHOT + + cn.citycraft + PluginHelper + jar + 1.0 + UTF-8 diff --git a/src/main/java/cn/citycraft/SimpleEssential/SimpleEssential.java b/src/main/java/cn/citycraft/SimpleEssential/SimpleEssential.java index 4c334a1..0a358ca 100644 --- a/src/main/java/cn/citycraft/SimpleEssential/SimpleEssential.java +++ b/src/main/java/cn/citycraft/SimpleEssential/SimpleEssential.java @@ -28,13 +28,13 @@ import cn.citycraft.SimpleEssential.command.CommandTpaccept; import cn.citycraft.SimpleEssential.command.CommandTpdeny; import cn.citycraft.SimpleEssential.command.CommandTphere; import cn.citycraft.SimpleEssential.command.CommandWorkBench; -import cn.citycraft.SimpleEssential.config.Config; import cn.citycraft.SimpleEssential.config.Language; import cn.citycraft.SimpleEssential.inventory.InventoryControl; import cn.citycraft.SimpleEssential.listen.PlayerInventoryViewListen; import cn.citycraft.SimpleEssential.listen.PlayerLocationListen; import cn.citycraft.SimpleEssential.teleport.TeleportControl; -import cn.citycraft.SimpleEssential.utils.VersionChecker; +import cn.citycraft.config.FileConfig; +import cn.citycraft.utils.VersionChecker; /** * 简单基础插件 @@ -42,7 +42,7 @@ import cn.citycraft.SimpleEssential.utils.VersionChecker; * @author 蒋天蓓 2015年8月11日下午3:29:32 */ public class SimpleEssential extends JavaPlugin { - + FileConfig config; /** * 传送控制 */ @@ -64,14 +64,14 @@ public class SimpleEssential extends JavaPlugin { } private void initTeleportControl() { - int tpdelay = Config.getInstance().getInt("Teleport.delay", 3); - String tpcontorlname = Config.getMessage("Teleport.name"); + int tpdelay = config.getInt("Teleport.delay", 3); + String tpcontorlname = config.getMessage("Teleport.name"); tpcontrol = new TeleportControl(this, tpcontorlname, tpdelay); } @Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - for (BaseCommand command : commandlist) { + for (BaseCommand command : commandlist) if (command.isValidTrigger(label)) { if (!command.hasPermission(sender)) { sender.sendMessage(Language.getMessage("Base.no-permission")); @@ -81,16 +81,14 @@ public class SimpleEssential extends JavaPlugin { sender.sendMessage(Language.getMessage("Base.playercommand")); return true; } - if (args.length >= command.getMinimumArguments()) { + if (args.length >= command.getMinimumArguments()) try { command.execute(sender, label, args); return true; } catch (CommandException e) { sender.sendMessage(e.getMessage()); } - } } - } return false; } @@ -110,8 +108,8 @@ public class SimpleEssential extends JavaPlugin { @Override public void onLoad() { - Config.load(this, "1.0"); - Language.load(this, "1.0"); + config = new FileConfig(this); + Language.load(this); } /** diff --git a/src/main/java/cn/citycraft/SimpleEssential/config/Config.java b/src/main/java/cn/citycraft/SimpleEssential/config/Config.java deleted file mode 100644 index b1228ee..0000000 --- a/src/main/java/cn/citycraft/SimpleEssential/config/Config.java +++ /dev/null @@ -1,55 +0,0 @@ -package cn.citycraft.SimpleEssential.config; - -import java.io.File; -import java.io.IOException; - -import org.bukkit.plugin.Plugin; - -public class Config extends ConfigLoader { - private static String CONFIG_NAME = "config.yml"; - private static FileConfig instance; - private static File file; - - public Config(Plugin p) { - super(p, CONFIG_NAME); - file = new File(p.getDataFolder(), CONFIG_NAME); - instance = super.getInstance(); - } - - public Config(Plugin p, String ver) { - super(p, CONFIG_NAME, ver); - instance = super.getInstance(); - } - - public static void load(Plugin p) { - new Config(p); - } - - public static void load(Plugin p, String ver) { - new Config(p, ver); - } - - public static FileConfig getInstance() { - return instance; - } - - public static String getMessage(String path) { - String message = instance.getString(path); - if (message != null && message.length() != 0) - message = message.replaceAll("&", "§"); - return message; - } - - public static String[] getStringArray(String path) { - return instance.getStringList(path).toArray(new String[0]); - } - - public static void save() { - try { - instance.save(file); - } catch (IOException e) { - saveError(file); - e.printStackTrace(); - } - } -} diff --git a/src/main/java/cn/citycraft/SimpleEssential/config/ConfigLoader.java b/src/main/java/cn/citycraft/SimpleEssential/config/ConfigLoader.java deleted file mode 100644 index e853cfb..0000000 --- a/src/main/java/cn/citycraft/SimpleEssential/config/ConfigLoader.java +++ /dev/null @@ -1,108 +0,0 @@ -package cn.citycraft.SimpleEssential.config; - -import java.io.File; -import java.io.IOException; - -import org.bukkit.plugin.Plugin; - -public class ConfigLoader extends FileConfig { - protected static FileConfig config; - protected static boolean tip = true; - protected static Plugin plugin; - - public ConfigLoader(Plugin p, File file) { - ConfigLoader.plugin = p; - config = loadConfig(p, file, null, true); - } - - public ConfigLoader(Plugin p, File file, boolean res) { - ConfigLoader.plugin = p; - config = loadConfig(p, file, null, res); - } - - public ConfigLoader(Plugin p, File file, String ver) { - ConfigLoader.plugin = p; - config = loadConfig(p, file, ver, true); - } - - public ConfigLoader(Plugin p, File file, String ver, boolean res) { - ConfigLoader.plugin = p; - config = loadConfig(p, file, ver, res); - } - - public ConfigLoader(Plugin p, String filename) { - ConfigLoader.plugin = p; - config = loadConfig(p, new File(p.getDataFolder(), filename), null, true); - } - - public ConfigLoader(Plugin p, String filename, boolean res) { - ConfigLoader.plugin = p; - config = loadConfig(p, new File(p.getDataFolder(), filename), null, res); - } - - public ConfigLoader(Plugin p, String filename, String ver) { - ConfigLoader.plugin = p; - config = loadConfig(p, new File(p.getDataFolder(), filename), ver, true); - } - - public ConfigLoader(Plugin p, String filename, String ver, boolean res) { - ConfigLoader.plugin = p; - config = loadConfig(p, new File(p.getDataFolder(), filename), ver, true); - } - - public static FileConfig getInstance() { - return config; - } - - public FileConfig loadConfig(Plugin p, File file, String ver, boolean res) { - tip = res; - if (!file.getParentFile().exists()) { - file.getParentFile().mkdirs(); - p.getLogger().info("创建新的文件夹" + file.getParentFile().getAbsolutePath() + "..."); - } - if (!file.exists()) { - fileCreate(p, file, res); - } else { - if (ver != null) { - FileConfig configcheck = init(file); - String version = configcheck.getString("version"); - if (version == null || !version.equals(ver)) { - p.getLogger().warning("配置文件: " + file.getName() + " 版本过低 正在升级..."); - try { - configcheck.save(new File(file.getParent(), file.getName() + ".backup")); - p.getLogger() - .warning( - "配置文件: " + file.getName() + " 已备份为 " + file.getName() - + ".backup !"); - } catch (IOException e) { - p.getLogger().warning("配置文件: " + file.getName() + "备份失败!"); - } - p.saveResource(file.getName(), true); - p.getLogger().info("配置文件: " + file.getName() + "升级成功!"); - } - } - } - if (tip) - p.getLogger().info("载入配置文件: " + file.getName() + (ver != null ? " 版本: " + ver : "")); - return init(file); - } - - private void fileCreate(Plugin p, File file, boolean res) { - if (res) { - p.saveResource(file.getName(), false); - } else { - try { - p.getLogger().info("创建新的配置文件" + file.getAbsolutePath() + "..."); - file.createNewFile(); - } catch (IOException e) { - p.getLogger().info("配置文件" + file.getName() + "创建失败..."); - e.printStackTrace(); - } - } - } - - public static void saveError(File file) { - plugin.getLogger().info("配置文件" + file.getName() + "保存错误..."); - } - -} diff --git a/src/main/java/cn/citycraft/SimpleEssential/config/FileConfig.java b/src/main/java/cn/citycraft/SimpleEssential/config/FileConfig.java deleted file mode 100644 index a9a9f96..0000000 --- a/src/main/java/cn/citycraft/SimpleEssential/config/FileConfig.java +++ /dev/null @@ -1,108 +0,0 @@ -package cn.citycraft.SimpleEssential.config; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.Reader; -import java.io.Writer; -import java.util.logging.Level; - -import org.apache.commons.lang.Validate; -import org.bukkit.Bukkit; -import org.bukkit.configuration.Configuration; -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; - -import com.google.common.base.Charsets; -import com.google.common.io.Files; - -/** - * An implementation of {@link Configuration} which saves all files in Yaml. Note that this - * implementation is not synchronized. - */ -public class FileConfig extends YamlConfiguration { - - public static FileConfig init(File file) { - return FileConfig.loadConfiguration(file); - } - - public static FileConfig loadConfiguration(File file) { - Validate.notNull(file, "File cannot be null"); - FileConfig config = new FileConfig(); - 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; - } - - protected final DumperOptions yamlOptions = new DumperOptions(); - - protected final Representer yamlRepresenter = new YamlRepresenter(); - - protected final Yaml yaml = new Yaml(new YamlConstructor(), yamlRepresenter, yamlOptions); - - @Override - public void load(File file) throws FileNotFoundException, IOException, InvalidConfigurationException { - Validate.notNull(file, "File cannot be null"); - final FileInputStream stream = new FileInputStream(file); - load(new InputStreamReader(stream, Charsets.UTF_8)); - } - - @Override - public void load(Reader reader) throws IOException, InvalidConfigurationException { - BufferedReader input = (reader instanceof BufferedReader) ? (BufferedReader) reader - : new BufferedReader(reader); - StringBuilder builder = new StringBuilder(); - try { - String line; - while ((line = input.readLine()) != null) { - builder.append(line); - builder.append('\n'); - } - } finally { - input.close(); - } - loadFromString(builder.toString()); - } - - @Override - public void save(File file) throws IOException { - Validate.notNull(file, "File cannot be null"); - Files.createParentDirs(file); - String data = saveToString(); - Writer writer = new OutputStreamWriter(new FileOutputStream(file), Charsets.UTF_8); - try { - writer.write(data); - } finally { - writer.close(); - } - } - - @Override - public String saveToString() { - yamlOptions.setIndent(options().indent()); - yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); - yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); - String header = buildHeader(); - String dump = yaml.dump(getValues(false)); - if (dump.equals(BLANK_CONFIG)) { - dump = ""; - } - return header + dump; - } -} diff --git a/src/main/java/cn/citycraft/SimpleEssential/config/Language.java b/src/main/java/cn/citycraft/SimpleEssential/config/Language.java index eb20926..3d8d4eb 100644 --- a/src/main/java/cn/citycraft/SimpleEssential/config/Language.java +++ b/src/main/java/cn/citycraft/SimpleEssential/config/Language.java @@ -1,65 +1,26 @@ package cn.citycraft.SimpleEssential.config; -import java.io.File; -import java.io.IOException; - import org.bukkit.plugin.Plugin; -public class Language extends ConfigLoader { +import cn.citycraft.config.FileConfig; + +public class Language { private static String CONFIG_NAME = "language.yml"; - private static FileConfig instance; - private static File file; - - public Language(Plugin p) { - super(p, CONFIG_NAME); - file = new File(p.getDataFolder(), CONFIG_NAME); - instance = super.getInstance(); - } - - public Language(Plugin p, String ver) { - super(p, CONFIG_NAME, ver); - instance = super.getInstance(); - } - - public static void load(Plugin p) { - new Language(p); - } - - public static void load(Plugin p, String ver) { - new Language(p, ver); - } - - public static FileConfig getInstance() { - return instance; - } - - public static String getMessage(String path, Object... paramVarArgs) { - String message = instance.getString(path); - try { - if (message != null) - message = String.format(message.replaceAll("&", "§"), paramVarArgs); - } catch (Exception e) { - } - return message; - } + private static FileConfig config; public static String getMessage(String path) { - String message = instance.getString(path); - if (message != null && message.length() != 0) - message = message.replaceAll("&", "§"); - return message; + return config.getMessage(path); + } + + public static String getMessage(String path, Object... args) { + return String.format(config.getMessage(path), args); } public static String[] getStringArray(String path) { - return instance.getStringList(path).toArray(new String[0]); + return config.getStringList(path).toArray(new String[0]); } - public static void save() { - try { - instance.save(file); - } catch (IOException e) { - saveError(file); - e.printStackTrace(); - } + public static void load(Plugin p) { + config = new FileConfig(p, CONFIG_NAME); } } diff --git a/src/main/java/cn/citycraft/SimpleEssential/config/PlayerConfig.java b/src/main/java/cn/citycraft/SimpleEssential/config/PlayerConfig.java index cdacc45..0656317 100644 --- a/src/main/java/cn/citycraft/SimpleEssential/config/PlayerConfig.java +++ b/src/main/java/cn/citycraft/SimpleEssential/config/PlayerConfig.java @@ -1,48 +1,21 @@ package cn.citycraft.SimpleEssential.config; import java.io.File; -import java.io.IOException; -import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; -public class PlayerConfig extends ConfigLoader { +import cn.citycraft.config.FileConfig; + +public class PlayerConfig { private static String CONFIG_FOLDER = "userdate"; - private static FileConfig instance; - private static File file; - - public PlayerConfig(Plugin p, String player) { - super(p, CONFIG_FOLDER + File.separator + player + ".yml", false); - file = new File(p.getDataFolder(), CONFIG_FOLDER + File.separator - + player + ".yml"); - instance = super.getInstance(); - } - - public static FileConfig getInstance(Plugin p, Player player) { - new PlayerConfig(p, player.getName()); - return instance; - } - - public static FileConfig getInstance(Plugin p, String player) { - new PlayerConfig(p, player); - return instance; - } - - public static String getMessage(String path) { - String message = instance.getString(path).replaceAll("&", "§"); - return message; - } + private static FileConfig config; public static String[] getStringArray(String path) { - return instance.getStringList(path).toArray(new String[0]); + return config.getStringList(path).toArray(new String[0]); } - public static void save() { - try { - instance.save(file); - } catch (IOException e) { - saveError(file); - e.printStackTrace(); - } + public void load(Plugin p, String player) { + config = new FileConfig(p, new File(p.getDataFolder(), CONFIG_FOLDER + "/" + player)); } + } diff --git a/src/main/java/cn/citycraft/SimpleEssential/utils/VersionChecker.java b/src/main/java/cn/citycraft/SimpleEssential/utils/VersionChecker.java deleted file mode 100644 index 1c1b82a..0000000 --- a/src/main/java/cn/citycraft/SimpleEssential/utils/VersionChecker.java +++ /dev/null @@ -1,108 +0,0 @@ -package cn.citycraft.SimpleEssential.utils; - -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.net.URL; - -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.plugin.Plugin; - -import com.google.common.base.Charsets; - -/** - * 自动更新类 - * - * @author 蒋天蓓 2015年8月14日下午4:01:15 - */ -public class VersionChecker implements Listener { - Plugin plugin; - public String checkurl = "https://coding.net/u/502647092/p/%s/git/raw/%s/src/main/resources/plugin.yml"; - public String branch = "master"; - - /** - * @param plugin - * - 插件 - */ - public VersionChecker(Plugin plugin) { - this.plugin = plugin; - plugin.getServer().getPluginManager().registerEvents(this, plugin); - this.versioncheck(null); - } - - /** - * @param plugin - * - 插件 - * @param branch - * - 分支名称 - */ - public VersionChecker(Plugin plugin, String branch) { - this.plugin = plugin; - plugin.getServer().getPluginManager().registerEvents(this, plugin); - this.branch = branch; - this.versioncheck(null); - } - - /** - * 获取插件更新链接 - * - * @param pluginName - * - 插件名称 - * @param branch - * - 插件分支 - * @return 更新链接 - */ - public String getCheckUrl(String pluginName, String branch) { - return String.format(checkurl, pluginName, branch); - } - - @EventHandler - public void onPlayerJoin(PlayerJoinEvent e) { - if (e.getPlayer().isOp()) { - this.versioncheck(e.getPlayer()); - } - } - - /** - * 开始更新 - * - * @param player - * - 获取更新的玩家(null则默认为控制台) - */ - public void versioncheck(final Player player) { - Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { - @Override - public void run() { - String readURL = getCheckUrl(plugin.getName(), branch); - FileConfiguration config; - String currentVersion = plugin.getDescription().getVersion(); - try { - URL url = new URL(readURL); - BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), Charsets.UTF_8)); - config = YamlConfiguration.loadConfiguration(br); - String newVersion = config.getString("version"); - br.close(); - if (!newVersion.equals(currentVersion)) { - String[] msg = new String[] { ChatColor.GREEN + plugin.getName() + " 插件最新版本 v" + newVersion, - ChatColor.RED + "服务器运行版本: v" + currentVersion, - ChatColor.GOLD + "插件更新网站: " + ChatColor.BLUE + plugin.getDescription().getWebsite() }; - if (player != null) { - player.sendMessage(msg); - } else { - plugin.getServer().getConsoleSender().sendMessage(msg); - } - } - } catch (Exception e) { - plugin.getLogger().warning("版本更新检查失败 异常: " + e.getMessage()); - } - } - }); - } - -}