1
0
mirror of https://e.coding.net/circlecloud/SimpleEssential.git synced 2024-11-17 01:18:47 +00:00

update...

Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
502647092 2015-09-02 18:20:57 +08:00
parent 64dbc4529b
commit 614fa4668d
9 changed files with 83 additions and 484 deletions

View File

@ -1,7 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
@ -12,5 +21,11 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

43
pom.xml
View File

@ -7,12 +7,12 @@
<name>SimpleEssential</name>
<build>
<finalName>${project.name}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
@ -22,6 +22,27 @@
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<minimizeJar>true</minimizeJar>
<artifactSet>
<includes>
<include>cn.citycraft:PluginHelper</include>
</includes>
</artifactSet>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
@ -29,6 +50,10 @@
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
<repository>
<id>citycraft-repo</id>
<url>http://ci.citycraft.cn:8800/jenkins/plugin/repository/everything/</url>
</repository>
</repositories>
<dependencies>
<dependency>
@ -37,6 +62,12 @@
<type>jar</type>
<version>1.8.3-R0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.citycraft</groupId>
<artifactId>PluginHelper</artifactId>
<type>jar</type>
<version>1.0</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -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);
}
/**

View File

@ -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();
}
}
}

View File

@ -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() + "保存错误...");
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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));
}
}

View File

@ -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());
}
}
});
}
}