1
0
mirror of https://e.coding.net/circlecloud/DataStorage.git synced 2024-12-04 04:08:53 +00:00

feat: 使用新的类库

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2017-03-24 20:47:14 +08:00
parent f3bca9a69c
commit e28c3a00fb
7 changed files with 233 additions and 284 deletions

View File

@ -1,31 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<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"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<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>

View File

@ -1,23 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>DataStorage</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

Binary file not shown.

91
pom.xml
View File

@ -1,12 +1,11 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.citycraft</groupId>
<groupId>pw.yumc</groupId>
<artifactId>DataStorage</artifactId>
<version>1.0.1</version>
<name>DataStorage</name>
<version>1.1</version>
<build>
<finalName>${project.name}</finalName>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
@ -14,79 +13,65 @@
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<minimizeJar>true</minimizeJar>
<artifactSet>
<includes>
<include>cn.citycraft:PluginHelper</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>cn.citycraft.PluginHelper</pattern>
<shadedPattern>${project.groupId}.${project.artifactId}</shadedPattern>
</relocation>
</relocations>
</configuration>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<minimizeJar>true</minimizeJar>
<artifactSet>
<includes>
<include>pw.yumc:YumCore</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>pw.yumc.YumCore</pattern>
<shadedPattern>${project.groupId}.${project.artifactId}</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<ciManagement>
<system>Jenkins</system>
<url>http://ci.yumc.pw/job/${project.artifactId}/</url>
</ciManagement>
<properties>
<jenkins.url>http://ci.citycraft.cn:8080</jenkins.url>
<update.description>§d更新命令以及参数...</update.description>
<env.BUILD_NUMBER>Debug</env.BUILD_NUMBER>
<env.BUILD_NUMBER>DEV</env.BUILD_NUMBER>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
<repository>
<id>citycraft-repo</id>
<url>${jenkins.url}/plugin/repository/everything/</url>
<id>yumc-repo</id>
<url>http://repo.yumc.pw/content/groups/public/</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>jtb</id>
<name>YUMC</name>
<url>http://repo.yumc.pw/content/repositories/yumcenter/</url>
</repository>
</distributionManagement>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<type>jar</type>
<version>1.8.8-R0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.citycraft</groupId>
<artifactId>PluginHelper</artifactId>
<type>jar</type>
<version>1.0</version>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>PlaceholderAPI</artifactId>
<version>1.8.6</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/PlaceholderAPI.jar</systemPath>
<groupId>pw.yumc</groupId>
<artifactId>YumCore</artifactId>
<version>1.7</version>
</dependency>
</dependencies>
</project>

View File

@ -1,139 +0,0 @@
package cn.citycraft.DataStorage;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import cn.citycraft.PluginHelper.config.FileConfig;
import cn.citycraft.PluginHelper.pluginapi.PluginAPI;
import cn.citycraft.PluginHelper.sql.DataBase;
import cn.citycraft.PluginHelper.sql.KeyValue;
import cn.citycraft.PluginHelper.sql.MySQLCore;
import cn.citycraft.PluginHelper.sql.SQLiteCore;
import me.clip.placeholderapi.PlaceholderHook;
public class DataStorage extends JavaPlugin {
FileConfig config;
DataBase db;
String kfields = "key";
String tableName;
String vfields = "value";
@Override
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) {
if (args.length > 1) {
final String subcmd = args[0];
final String key = args[1];
final KeyValue kv = new KeyValue(kfields, key);
int value = 0;
int dbValue = 0;
try {
value = Integer.valueOf(args[2]);
dbValue = Integer.valueOf(db.dbSelectFirst(tableName, vfields, kv));
} catch (final Exception e) {
}
if (args.length > 2 && value != 0) {
switch (subcmd) {
case "add":
if (db.dbUpdate(tableName, new KeyValue(vfields, value + dbValue), kv) != 0) {
sender.sendMessage("§a成功给了 §3" + key + " §a添加了 §e" + value + " §a目前的值为 §d" + (value + dbValue));
} else {
sender.sendMessage("§c操作失败 请查看后台错误 (添加删除需要初始化)!");
}
return true;
case "del":
if (db.dbUpdate(tableName, new KeyValue(vfields, dbValue - value), kv) != 0) {
sender.sendMessage("§a成功给了 §3" + key + " §c减少了 §e" + value + " §a目前的值为 §d" + (dbValue - value));
} else {
sender.sendMessage("§c操作失败 请查看后台错误 (添加删除需要初始化)!");
}
return true;
case "create":
if (db.dbInsert(tableName, kv.add(vfields, value)) != 0) {
sender.sendMessage("§a成功创建了 §3" + key + " §a初始值为 §d" + value);
} else {
sender.sendMessage("§c操作失败 请查看后台错误!");
}
return true;
}
}
if (args.length > 1) {
switch (subcmd) {
case "remove":
if (db.dbDelete(tableName, kv) != 0) {
sender.sendMessage("§a成功移除了 §3" + key);
} else {
sender.sendMessage("§c操作失败 请查看后台错误 (添加删除需要初始化)!");
}
return true;
case "test":
if (sender instanceof Player) {
sender.sendMessage("§6语句测试: §3" + PluginAPI.PlaceholderAPI((Player) sender, key));
} else {
sender.sendMessage("§c控制台无法测试语句!");
}
return true;
default:
break;
}
}
}
final String[] help = new String[] { "§6========= [§b" + this.getName() + "§6] 插件帮助列表=========",
"§6/dd §acreate key value §6- §b创建键key设置值为value!",
"§6/dd §aadd key value §6- §b键key的值添加value!",
"§6/dd §adel key value §6- §b键key的值减少value!",
"§6/dd §aremove key §6- §b移除key!" };
sender.sendMessage(help);
return true;
}
@Override
public void onDisable() {
if (db != null) {
db.close();
}
PluginAPI.PlaceholderUnhook("dd");
}
@Override
public void onEnable() {
final ConfigurationSection cfg = config.getConfigurationSection("Data.MySQL");
if (config.getMessage("Data.FileSystem").equalsIgnoreCase("MySQL")) {
db = new DataBase(new MySQLCore(cfg));
} else {
db = new DataBase(new SQLiteCore(this, cfg));
}
tableName = config.getMessage("Data.MySQL.tablename");
if (db.testConnect()) {
this.getLogger().info("数据库连接成功,检查数据表是否存在...");
if (!db.isTableExists(tableName)) {
this.getLogger().info("数据表不存在,新建表" + tableName + "...");
if (!db.createTables(tableName, new KeyValue(kfields, "VARCHAR(30) PRIMARY KEY").add(vfields, "INT"), "UNIQUE (`key`)")) {
this.getLogger().info("数据表" + tableName + "创建失败,请尝试手动创建并重启服务器...");
}
}
} else {
this.getLogger().warning("数据库连接失败!");
this.getServer().getPluginManager().disablePlugin(this);
}
final boolean result = PluginAPI.PlaceholderHook("dd", new PlaceholderHook() {
@Override
public String onPlaceholderRequest(final Player arg0, final String arg1) {
return db.dbSelectFirst(tableName, vfields, new KeyValue(kfields, arg1));
}
});
if (result) {
this.getLogger().info("发现 PlaceholderAPI 已关联...");
} else {
Bukkit.getConsoleSender().sendMessage("§c发现 PlaceholderAPI 但是关联失败了!!!失败了!!!");
}
}
@Override
public void onLoad() {
config = new FileConfig(this);
}
}

View File

@ -0,0 +1,157 @@
package pw.yumc.DataStorage;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.PlaceholderHook;
import pw.yumc.YumCore.commands.CommandSub;
import pw.yumc.YumCore.commands.annotation.Cmd;
import pw.yumc.YumCore.commands.annotation.Help;
import pw.yumc.YumCore.commands.interfaces.Executor;
import pw.yumc.YumCore.config.FileConfig;
import pw.yumc.YumCore.sql.DataBase;
import pw.yumc.YumCore.sql.core.KeyValue;
public class DataStorage extends JavaPlugin implements Executor {
FileConfig config;
DataBase db;
String kfields = "key";
String tableName;
String vfields = "value";
@Override
public void onLoad() {
config = new FileConfig();
}
@Override
public void onEnable() {
initDataBase();
hookPAPI();
new CommandSub("dd", this);
}
private void initDataBase() {
db = DataBase.create(this, config.getConfigurationSection("Data"));
tableName = config.getMessage("Data.MySQL.tablename");
if (db.testConnect()) {
this.getLogger().info("数据库连接成功,检查数据表是否存在...");
if (!db.isTableExists(tableName)) {
this.getLogger().info("数据表不存在,新建表" + tableName + "...");
if (!db.createTables(tableName, new KeyValue(kfields, "VARCHAR(30) PRIMARY KEY").add(vfields, "INT"), "UNIQUE (`key`)")) {
this.getLogger().info("数据表" + tableName + "创建失败,请尝试手动创建并重启服务器...");
}
}
} else {
this.getLogger().warning("数据库连接失败!");
this.getServer().getPluginManager().disablePlugin(this);
}
}
private void hookPAPI() {
final boolean result = PlaceholderAPI.registerPlaceholderHook("dd", new PlaceholderHook() {
@Override
public String onPlaceholderRequest(final Player arg0, final String arg1) {
return db.dbSelectFirst(tableName, vfields, new KeyValue(kfields, arg1));
}
});
if (result) {
this.getLogger().info("发现 PlaceholderAPI 已关联...");
} else {
Bukkit.getConsoleSender().sendMessage("§6[§bDataStorage§6] §c发现 PlaceholderAPI 但是关联失败了!!!失败了!!!");
}
}
@Cmd
@Help(value = "创建一个键值对", possibleArguments = "<键> <值>")
public void create(CommandSender sender, String key, Integer value) {
try {
if (createValue(key, value)) {
sender.sendMessage("§a成功创建了 §3" + key + " §a初始值为 §d" + value);
return;
}
} catch (SQLException e) {
e.printStackTrace();
}
sender.sendMessage("§c操作失败 请查看后台错误 (添加删除需要初始化)!");
}
@Cmd
@Help(value = "移除一个键值对", possibleArguments = "<键>")
public void remove(CommandSender sender, String key, Integer value) {
try {
if (removeValue(key)) {
sender.sendMessage("§a成功移除了 §3" + key);
return;
}
} catch (SQLException e) {
e.printStackTrace();
}
sender.sendMessage("§c操作失败 请查看后台错误 (添加删除需要初始化)!");
}
@Cmd
@Help(value = "键key的值添加value", possibleArguments = "<键> <值>")
public void add(CommandSender sender, String key, Integer value) {
try {
if (incValue(key, value)) {
sender.sendMessage("§a成功给了 §3" + key + " §a增加了 §e" + value + " §a目前的值为 §d" + getValue(key));
return;
}
} catch (SQLException e) {
e.printStackTrace();
}
sender.sendMessage("§c操作失败 请查看后台错误 (添加删除需要初始化)!");
}
@Cmd
@Help(value = "键key的值添加value", possibleArguments = "<键> <值>")
public void del(CommandSender sender, String key, Integer value) {
try {
if (decValue(key, value)) {
sender.sendMessage("§a成功给了 §3" + key + " §c减少了 §e" + value + " §a目前的值为 §d" + getValue(key));
return;
}
} catch (SQLException e) {
e.printStackTrace();
}
sender.sendMessage("§c操作失败 请查看后台错误 (添加删除需要初始化)!");
}
private boolean createValue(String key, int value) throws SQLException {
return db.getDataBaseCore().update("INSERT INTO " + tableName + " (" + kfields + ", " + vfields + ") VALUES ('" + key + "', '" + value + "')") != 0;
}
private boolean removeValue(String key) throws SQLException {
return db.getDataBaseCore().update("DELETE FROM " + tableName + " WHERE " + kfields + " = '" + key + "'") != 0;
}
private boolean incValue(String key, int value) throws SQLException {
return db.getDataBaseCore().update("UPDATE " + tableName + " SET " + vfields + " = " + vfields + " + " + value + " WHERE " + kfields + " = '" + key + "'") != 0;
}
private boolean decValue(String key, int value) throws SQLException {
return db.getDataBaseCore().update("UPDATE " + tableName + " SET " + vfields + " = " + vfields + " + " + value + " WHERE " + kfields + " = '" + key + "'") != 0;
}
private int getValue(String key) throws SQLException {
ResultSet result = db.getDataBaseCore().query("SELECT " + vfields + " FROM " + tableName + " WHERE " + kfields + " = '" + key + "' LIMIT 1");
if (result.next()) { return result.getInt(1); }
return 0;
}
@Override
public void onDisable() {
if (db != null) {
db.close();
}
PlaceholderAPI.unregisterPlaceholderHook("dd");
}
}

View File

@ -3,7 +3,7 @@ description: ${project.description}
main: ${project.groupId}.${project.artifactId}.${project.artifactId}
version: ${project.version}-Build#${env.BUILD_NUMBER}
author: 喵♂呜
website: ${jenkins.url}/job/${project.artifactId}/
website: ${ciManagement.url}
depend: [PlaceholderAPI]
commands:
${project.artifactId}: