commit d2dff8b7b442a3038f3ba30ba51bc518cb06db0f Author: yuyu <124714592@qq.com> Date: Sun Jun 30 17:37:32 2024 +0800 基本完成版版 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/SrDisassemble113.iml b/.idea/SrDisassemble113.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/SrDisassemble113.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..569a41b --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..269d90a --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,17 @@ + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:3306 + + + + + + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..861936a --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/lib.xml b/.idea/libraries/lib.xml new file mode 100644 index 0000000..fa8838a --- /dev/null +++ b/.idea/libraries/lib.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/lib__2_.xml b/.idea/libraries/lib__2_.xml new file mode 100644 index 0000000..0ae6450 --- /dev/null +++ b/.idea/libraries/lib__2_.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/lib__3_.xml b/.idea/libraries/lib__3_.xml new file mode 100644 index 0000000..1fad679 --- /dev/null +++ b/.idea/libraries/lib__3_.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/lib__4_.xml b/.idea/libraries/lib__4_.xml new file mode 100644 index 0000000..81811af --- /dev/null +++ b/.idea/libraries/lib__4_.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..132404b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..96976c1 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..cfb1f60 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/SrDisassemble.iml b/SrDisassemble.iml new file mode 100644 index 0000000..a589521 --- /dev/null +++ b/SrDisassemble.iml @@ -0,0 +1,13 @@ + + + + + + + SPIGOT + + 1 + + + + \ No newline at end of file diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml new file mode 100644 index 0000000..1144558 --- /dev/null +++ b/dependency-reduced-pom.xml @@ -0,0 +1,72 @@ + + + 4.0.0 + com.yuyu + SrDisassemble + SrDisassemble + 1.0-SNAPSHOT + + + + true + src/main/resources + + + + + maven-compiler-plugin + 3.8.1 + + ${java.version} + ${java.version} + + + + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + + + + + spigotmc-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + sonatype + https://oss.sonatype.org/content/groups/public/ + + + + + org.spigotmc + spigot-api + 1.12.2-R0.1-SNAPSHOT + provided + + + junit + junit + 4.10 + test + + + hamcrest-core + org.hamcrest + + + + + + 1.8 + UTF-8 + + diff --git a/lib/GermPlugin-Snapshot-4.3.9-8.jar b/lib/GermPlugin-Snapshot-4.3.9-8.jar new file mode 100644 index 0000000..11e8bd2 Binary files /dev/null and b/lib/GermPlugin-Snapshot-4.3.9-8.jar differ diff --git a/lib/SrItem-1.1.4-2.jar b/lib/SrItem-1.1.4-2.jar new file mode 100644 index 0000000..70b38f1 Binary files /dev/null and b/lib/SrItem-1.1.4-2.jar differ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..edd273b --- /dev/null +++ b/pom.xml @@ -0,0 +1,93 @@ + + + 4.0.0 + + com.yuyu + SrDisassemble + 1.0-SNAPSHOT + jar + + SrDisassemble + + + 1.8 + UTF-8 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + + + + src/main/resources + true + + + + + + + spigotmc-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + sonatype + https://oss.sonatype.org/content/groups/public/ + + + + + + org.projectlombok + lombok + 1.18.22 + compile + + + org.spigotmc + spigot-api + 1.12.2-R0.1-SNAPSHOT + provided + + + mysql + mysql-connector-java + 8.0.28 + + + org.mybatis + mybatis + 3.5.7 + + + junit + junit + 4.10 + test + + + diff --git a/src/main/java/com/yuyu/srdisassemble/SrDisassemble.java b/src/main/java/com/yuyu/srdisassemble/SrDisassemble.java new file mode 100644 index 0000000..48e630e --- /dev/null +++ b/src/main/java/com/yuyu/srdisassemble/SrDisassemble.java @@ -0,0 +1,38 @@ +package com.yuyu.srdisassemble; + +import com.sakurarealm.sritem.bukkit.command.CommandBase; +import com.sakurarealm.sritem.bukkit.command.OpenCommand; +import com.yuyu.srdisassemble.command.DisCommand; +import com.yuyu.srdisassemble.config.ConfigManager; +import lombok.Getter; +import org.bukkit.ChatColor; +import org.bukkit.plugin.java.JavaPlugin; + +public final class SrDisassemble extends JavaPlugin { + + @Getter + private static SrDisassemble instance;//用于获取实例 + @Getter + private static ConfigManager configManager;//用于鉴定 + + + @Override + public void onEnable() { + // Plugin startup logic + getLogger().info(ChatColor.GOLD+"分解插件开始运行!"); + this.configManager = new ConfigManager(this); + + //注册指令 + CommandBase commandBase = new CommandBase(); + this.getCommand("srdisassemble").setExecutor(commandBase); + commandBase.registerSubCommand("open",new DisCommand()); + commandBase.registerSubCommand("reload",new DisCommand()); + + + } + + @Override + public void onDisable() { + // Plugin shutdown logic + } +} diff --git a/src/main/java/com/yuyu/srdisassemble/command/DisCommand.java b/src/main/java/com/yuyu/srdisassemble/command/DisCommand.java new file mode 100644 index 0000000..b4232c9 --- /dev/null +++ b/src/main/java/com/yuyu/srdisassemble/command/DisCommand.java @@ -0,0 +1,46 @@ +package com.yuyu.srdisassemble.command; + +import com.sakurarealm.sritem.bukkit.command.SubCommand; +import com.yuyu.srdisassemble.SrDisassemble; +import com.yuyu.srdisassemble.disassemeble.DisassembleManager; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +/** + * @author 峰。 + * @version 1.0 + * @project SrDisassemble + * @date 2024/6/11 16:11:24 + * @description + */ +public class DisCommand implements SubCommand { + @Override + public void onCommand(CommandSender commandSender, String[] strings) { + if (strings.length == 1) { + if (strings[0].equalsIgnoreCase("reload")){ + commandSender.sendMessage(ChatColor.RED+"分解配置文件重载!"); + //重载文件 + SrDisassemble.getConfigManager().reload(); + return; + } + } + + + if (strings.length < 2){ + commandSender.sendMessage(ChatColor.RED+"正确的格式为:/disassemble open 姓名 "); + return; + } + + Player player = Bukkit.getPlayer(strings[1]); + if (player == null){ + commandSender.sendMessage(ChatColor.RED+"玩家"+strings[1]+"不存在!"); + return; + } + + //为玩家打开GUI列表 + DisassembleManager.getInstance().open(player); + + } +} diff --git a/src/main/java/com/yuyu/srdisassemble/config/ConfigManager.java b/src/main/java/com/yuyu/srdisassemble/config/ConfigManager.java new file mode 100644 index 0000000..596180a --- /dev/null +++ b/src/main/java/com/yuyu/srdisassemble/config/ConfigManager.java @@ -0,0 +1,156 @@ +package com.yuyu.srdisassemble.config; + +import com.yuyu.srdisassemble.data.JdbcSqlClass; +import com.yuyu.srdisassemble.pojo.CanDisItem; +import com.yuyu.srdisassemble.pojo.DisProduct; +import org.bukkit.ChatColor; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.Plugin; + +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.logging.Logger; + +/** + * @author 峰。 + * @version 1.0 + * @project SrDisassemble + * @date 2024/6/11 16:11:01 + * @description 配置文件相关 + */ +public class ConfigManager { + private Logger logger; + private HashMap disProductHashMap; + private Plugin plugin; + private String username; + private String password; + private String driver; + private String url; + public ConfigManager(Plugin plugin) { + this.plugin = plugin; + this.logger = plugin.getLogger(); + this.loadDataBase(); + + plugin.saveResource("disProduct.yml",false); + plugin.saveResource("disToItem.yml",false); + plugin.saveResource("ItemDetail.yml",false); + + File disProduct = new File(plugin.getDataFolder(), "disProduct.yml"); + File disToItem = new File(plugin.getDataFolder(), "disToItem.yml"); + File ItemDetail = new File(plugin.getDataFolder(), "ItemDetail.yml"); + + FileConfiguration disProductConfig = YamlConfiguration.loadConfiguration(disProduct); + FileConfiguration disToItemProduct = YamlConfiguration.loadConfiguration(disToItem); + FileConfiguration itemDetailProduct = YamlConfiguration.loadConfiguration(ItemDetail); + this.disProductHashMap = new HashMap<>(); + + //先读取可分解的产物 + List disProductConfigStringList = disProductConfig.getStringList("SrDisassemble"); + for (String disProductConfigString : disProductConfigStringList) { + HashMap disProductHashMap = new HashMap<>(); + //循环读取每个需要分解的产物 + int anInt = disToItemProduct.getInt("SrDisassemble." + disProductConfigString + ".disTime"); + String tagetIndex = disToItemProduct.getString("SrDisassemble." + disProductConfigString + ".index"); + CanDisItem canDisItem = new CanDisItem(disProductConfigString, anInt,disProductConfigString,tagetIndex); + List stringList = disToItemProduct.getStringList("SrDisassemble." + disProductConfigString +".disProduct"); + for (String product : stringList) { + Double proMax = itemDetailProduct.getDouble("SrDisassemble."+disProductConfigString+"."+product+".probabilityMax"); + Double proMin = itemDetailProduct.getDouble("SrDisassemble."+disProductConfigString+"."+product+".probabilityMin"); + String index = itemDetailProduct.getString("SrDisassemble."+disProductConfigString+"."+product+".index"); + DisProduct disPojo = new DisProduct(product, index, proMax, proMin); + logger.info(ChatColor.GREEN+disPojo.toString()); + disProductHashMap.put(product,disPojo); + } + canDisItem.setDisProductHashMap(disProductHashMap); + + this.disProductHashMap.put(disProductConfigString,canDisItem); + } + + } + + public void loadDataBase(){ + plugin.saveResource("datasource.yml",false); + + File datasource = new File(plugin.getDataFolder(), "datasource.yml"); + + FileConfiguration datasourceConfig = YamlConfiguration.loadConfiguration(datasource); + + JdbcSqlClass.setUser(datasourceConfig.getString("database.sql.username")); + JdbcSqlClass.setPassword(datasourceConfig.getString("database.sql.password")); + JdbcSqlClass.setDriver(datasourceConfig.getString("database.sql.driver")); + JdbcSqlClass.setUrl(datasourceConfig.getString("database.sql.url")); + + logger.info(ChatColor.GREEN+"username:"+JdbcSqlClass.getUser()+"\npassword:"+JdbcSqlClass.getPassword() + +"\ndriver:"+JdbcSqlClass.getDriver()+"\nurl:"+JdbcSqlClass.getUrl()); + + JdbcSqlClass.initTable(); + JdbcSqlClass.setChartseUtf8(); + + //获取到datasource.yml的信息后建立数据库连接 +// SqlData.initTable(); + } + + + public void reload() { + //重载前先清零 + disProductHashMap.clear(); + loadDataBase(); + + plugin.saveResource("disProduct.yml",false); + plugin.saveResource("disToItem.yml",false); + plugin.saveResource("ItemDetail.yml",false); + + File disProduct = new File(plugin.getDataFolder(), "disProduct.yml"); + File disToItem = new File(plugin.getDataFolder(), "disToItem.yml"); + File ItemDetail = new File(plugin.getDataFolder(), "ItemDetail.yml"); + + FileConfiguration disProductConfig = YamlConfiguration.loadConfiguration(disProduct); + FileConfiguration disToItemProduct = YamlConfiguration.loadConfiguration(disToItem); + FileConfiguration itemDetailProduct = YamlConfiguration.loadConfiguration(ItemDetail); + this.disProductHashMap = new HashMap<>(); + + //先读取可分解的产物 + List disProductConfigStringList = disProductConfig.getStringList("SrDisassemble"); + for (String disProductConfigString : disProductConfigStringList) { + HashMap disProductHashMap = new HashMap<>(); + //循环读取每个需要分解的产物 + int anInt = disToItemProduct.getInt("SrDisassemble." + disProductConfigString + ".disTime"); + String tagetIndex = disToItemProduct.getString("SrDisassemble." + disProductConfigString + ".index"); + CanDisItem canDisItem = new CanDisItem(disProductConfigString, anInt,disProductConfigString,tagetIndex); + List stringList = disToItemProduct.getStringList("SrDisassemble." + disProductConfigString +".disProduct"); + for (String product : stringList) { + Double proMax = itemDetailProduct.getDouble("SrDisassemble."+disProductConfigString+"."+product+".probabilityMax"); + Double proMin = itemDetailProduct.getDouble("SrDisassemble."+disProductConfigString+"."+product+".probabilityMin"); + String index = itemDetailProduct.getString("SrDisassemble."+disProductConfigString+"."+product+".index"); + DisProduct disPojo = new DisProduct(product, index, proMax, proMin); + logger.info(ChatColor.GREEN+disPojo.toString()); + disProductHashMap.put(product,disPojo); + } + canDisItem.setDisProductHashMap(disProductHashMap); + + this.disProductHashMap.put(disProductConfigString,canDisItem); + } + } + + public HashMap getDisProductHashMap() { + return disProductHashMap; + } + + public String getUsername() { + return username; + } + + public String getPassword() { + return password; + } + + public String getDriver() { + return driver; + } + + public String getUrl() { + return url; + } +} diff --git a/src/main/java/com/yuyu/srdisassemble/data/DataSource.java b/src/main/java/com/yuyu/srdisassemble/data/DataSource.java new file mode 100644 index 0000000..855010e --- /dev/null +++ b/src/main/java/com/yuyu/srdisassemble/data/DataSource.java @@ -0,0 +1,42 @@ +package com.yuyu.srdisassemble.data; + +import com.yuyu.srdisassemble.SrDisassemble; +import org.apache.ibatis.datasource.DataSourceFactory; +import org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory; + +import java.util.Properties; + +public class DataSource implements DataSourceFactory { + private static UnpooledDataSourceFactory unpooledDataSourceFactory = new UnpooledDataSourceFactory(); + + + public DataSource(){ + + + } + + + @Override + public void setProperties(Properties props) { + props.setProperty("driver", SrDisassemble.getConfigManager().getDriver()); + props.setProperty("url", SrDisassemble.getConfigManager().getUrl()); + + + props.setProperty("username", SrDisassemble.getConfigManager().getUsername()); + props.setProperty("password", SrDisassemble.getConfigManager().getPassword()); + + + } + + @Override + public javax.sql.DataSource getDataSource() { + Properties props = new Properties(); + props.setProperty("driver", SrDisassemble.getConfigManager().getDriver()); + props.setProperty("url", SrDisassemble.getConfigManager().getUrl()); + props.setProperty("username", SrDisassemble.getConfigManager().getUsername()); + props.setProperty("password", SrDisassemble.getConfigManager().getPassword()); + + unpooledDataSourceFactory.setProperties(props); + return unpooledDataSourceFactory.getDataSource(); + } +} diff --git a/src/main/java/com/yuyu/srdisassemble/data/JdbcSqlClass.java b/src/main/java/com/yuyu/srdisassemble/data/JdbcSqlClass.java new file mode 100644 index 0000000..5743de6 --- /dev/null +++ b/src/main/java/com/yuyu/srdisassemble/data/JdbcSqlClass.java @@ -0,0 +1,316 @@ +package com.yuyu.srdisassemble.data; + +import com.yuyu.srdisassemble.SrDisassemble; +import com.yuyu.srdisassemble.pojo.DisassembleData; +import org.bukkit.ChatColor; + +import java.sql.*; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + +/** + * @author 峰。 + * @version 1.0 + * @project SrDisassemble + * @date 2024/6/19 16:05:42 + * @description JDBC + */ +public class JdbcSqlClass { + private static String driver; + private static String url; + private static String user; + private static String password; + private static Connection con; + + + public static void openConnection() { + //注册驱动 + try { + Class.forName(JdbcSqlClass.driver); + + //获取连接 + Connection conn = DriverManager.getConnection(JdbcSqlClass.url, JdbcSqlClass.user, JdbcSqlClass.password); + JdbcSqlClass.con = conn; + } catch (Exception e) { + throw new RuntimeException(e); + } + + + } + + public static Statement getConnection() { + try { + //注册驱动 + Class.forName(JdbcSqlClass.driver); + + //获取执行者对象 + Statement stmt = con.createStatement(); + + + return stmt; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static void initTable() { + try { + con = DriverManager.getConnection(JdbcSqlClass.url, JdbcSqlClass.user, JdbcSqlClass.password); + + Statement connection = getConnection(); + + //执行Sql语句 + String createTable = " create table if not exists srdisassemble\t( id BIGINT auto_increment not null PRIMARY KEY comment 'ID'," + + " playername varchar(255) null comment '玩家姓名'," + + " disassembleTime varchar(255) null comment '分解的时间'," + + " target varchar(255) null comment '分解的目标'," + + " targetIndex varchar(255) null comment '分解目标的索引'," + + " guiname varchar(255) null comment '打开gui的名称'," + + " amount int null comment '分解的数量')"; + + boolean rs = connection.execute(createTable); + + if (rs) { + SrDisassemble.getInstance().getLogger().info(ChatColor.GREEN + "initTable执行完毕"); + } + + + String sql = "select column_name\n" + + "from INFORMATION_SCHEMA.COLUMNS\n" + + "where TABLE_NAME = 'srdisassemble'"; + + //手动注入所有列 + List columns = new ArrayList<>(); + columns.add("id"); + columns.add("playername"); + columns.add("disassembleTime"); + columns.add("target"); + columns.add("targetIndex"); + columns.add("guiname"); + columns.add("amount"); + + try { + ResultSet resultSet = connection.executeQuery(sql); + List lists = new ArrayList<>(); + int index = 0; + while (resultSet.next()) { + String columnName = resultSet.getString("column_name"); + lists.add(columnName); + index++; + } + List missColumn = new ArrayList<>(); + for (String columnName : columns) { + if (!lists.contains(columnName)) { + missColumn.add(columnName); + } + } + //新增列 + for (String columnName : missColumn) { + addColumn(columnName); + } + + } catch (SQLException e) { + throw new RuntimeException(e); + } + + + connection.close(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + + /** + * 用于新增列 + * + * @return + */ + public static int addColumn(String columns) { + String sql = null; + if (columns.equals("id")) { + sql = "ALTER TABLE srdisassemble\n" + + "ADD id BIGINT auto_increment not null PRIMARY KEY comment 'ID'"; + } else if (columns.equals("playername")) { + sql = "ALTER TABLE srdisassemble\n" + + "ADD playername varchar(255) null comment '玩家姓名'"; + } else if (columns.equals("disassembleTime")) { + sql = "ALTER TABLE srdisassemble\n" + + "ADD disassembleTime varchar(255) null comment '分解的时间'"; + } else if (columns.equals("target")) { + sql = "ALTER TABLE srdisassemble\n" + + "ADD target varchar(255) null comment '分解的目标'"; + } else if (columns.equals("targetIndex")) { + sql = "ALTER TABLE srdisassemble\n" + + "ADD targetIndex varchar(255) null comment '分解目标的索引'"; + } else if (columns.equals("guiname")) { + sql = "ALTER TABLE srdisassemble\n" + + "ADD guiname varchar(255) null comment '打开gui的名称'"; + } else if (columns.equals("amount")) { + sql = "ALTER TABLE srdisassemble\n" + + "ADD amount int null comment '分解的数量'"; + } + + if (sql != null) { + Statement stmt = getConnection(); + try { + int i = stmt.executeUpdate(sql); + return i; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + return 0; + } + + public static void setChartseUtf8() { + try { + Statement connection = getConnection(); + + //执行Sql语句 + + String sql = "ALTER TABLE srdisassemble CONVERT TO CHARACTER SET utf8mb4;"; + boolean rs = connection.execute(sql); + + if (rs) { + SrDisassemble.getInstance().getLogger().info(ChatColor.GREEN + "修改字符集成功"); + } + + connection.close(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static int insertData(DisassembleData data) { + try { + Statement connection = getConnection(); + + //执行Sql语句 + String sql = "insert into srdisassemble(playername, disassembleTime, target, targetIndex, guiname) values('" + + data.getPlayername() + "','" + data.getDisassembleTime().toString() + "','" + data.getTarget() + "','" + data.getTargetIndex() + "','" + data.getGuiName() + "')"; + int i = connection.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS); + ResultSet generatedKeys = connection.getGeneratedKeys(); + int id = 0; + if (generatedKeys.next()) { + id = generatedKeys.getInt(1); +// SrDisassemble.getInstance().getLogger().info(ChatColor.GREEN+"数据插入成功"); + } + + connection.close(); + connection.close(); + return id; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static void deleteData(int id) { + try { + Statement connection = getConnection(); + + //执行Sql语句 + + String sql = "delete from srdisassemble where id=" + id; + int i = connection.executeUpdate(sql); + + + connection.close(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static DisassembleData getDisassembleData(int id) { + try { + Statement connection = getConnection(); + + //执行Sql语句 + + String sql = "select * from srdisassemble where id=" + id; + ResultSet rs = connection.executeQuery(sql); + + DisassembleData data = new DisassembleData(); + while (rs.next()) { + data.setId(rs.getInt("id")); + data.setPlayername(rs.getString("playername")); + data.setDisassembleTime(rs.getTimestamp("disassembleTime").toLocalDateTime()); + data.setTarget(rs.getString("target")); + data.setTargetIndex(rs.getString("targetIndex")); + data.setGuiName(rs.getString("guiname")); + } + + rs.close(); + connection.close(); + connection.close(); + + return data; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static DisassembleData getDataByGuiAndPlay(String playerName, String guiName) { + try { + Statement connection = getConnection(); + + //执行Sql语句 + + List disassembleDataList = new ArrayList<>(); + String sql = "select * from srdisassemble where playername= '" + playerName + "' and guiname= '" + guiName + "'"; + ResultSet rs = connection.executeQuery(sql); + + DisassembleData data = new DisassembleData(); + + while (rs.next()) { + data.setId(rs.getInt("id")); + data.setPlayername(rs.getString("playername")); + data.setDisassembleTime(LocalDateTime.parse(rs.getString("disassembleTime"))); + data.setTarget(rs.getString("target")); + data.setTargetIndex(rs.getString("targetIndex")); + data.setGuiName(rs.getString("guiname")); + } + + rs.close(); + connection.close(); + connection.close(); + + return data; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static String getDriver() { + return driver; + } + + public static void setDriver(String driver) { + JdbcSqlClass.driver = driver; + } + + public static String getUrl() { + return url; + } + + public static void setUrl(String url) { + JdbcSqlClass.url = url; + } + + public static String getUser() { + return user; + } + + public static void setUser(String user) { + JdbcSqlClass.user = user; + } + + public static String getPassword() { + return password; + } + + public static void setPassword(String password) { + JdbcSqlClass.password = password; + } +} diff --git a/src/main/java/com/yuyu/srdisassemble/data/SqlData.java b/src/main/java/com/yuyu/srdisassemble/data/SqlData.java new file mode 100644 index 0000000..c1ffd30 --- /dev/null +++ b/src/main/java/com/yuyu/srdisassemble/data/SqlData.java @@ -0,0 +1,63 @@ +package com.yuyu.srdisassemble.data; + +import com.yuyu.srdisassemble.data.mapper.SqlDataMapper; +import com.yuyu.srdisassemble.pojo.DisassembleData; +import com.yuyu.srdisassemble.utils.MybatisUtil; +import org.apache.ibatis.session.SqlSession; + +import java.util.List; + +/** + * @author 峰。 + * @version 1.0 + * @project SrDisassemble + * @date 2024/6/17 16:57:26 + * @description + */ +public class SqlData { + /** + * 插件启动时加载表 + */ + public static void initTable(){ + SqlSession sqlSession = MybatisUtil.getSqlSession(); +// sqlSession.getConfiguration().addMapper(SqlDataMapper.class); + SqlDataMapper mapper = sqlSession.getMapper(SqlDataMapper.class); + + mapper.createTable(); + mapper.setChartseUtf8(); + + sqlSession.commit(); + sqlSession.close(); + } + + /** + * 新增数据 + * @param data + */ + public static void addDisassemble(DisassembleData data){ + try (SqlSession sqlSession = MybatisUtil.getSqlSession()){ + SqlDataMapper mapper = sqlSession.getMapper(SqlDataMapper.class); + mapper.addDisassembleEvent(data); + } + } + + public static List getDisassembleData(String playerName){ + try (SqlSession sqlSession = MybatisUtil.getSqlSession()){ + SqlDataMapper mapper = sqlSession.getMapper(SqlDataMapper.class); + List disassembleData = mapper.selectDisassembleEvent(playerName); + return disassembleData; + } + } + + public static List getDisassembleData(String playerName, String target){ + try (SqlSession sqlSession = MybatisUtil.getSqlSession()){ + SqlDataMapper mapper = sqlSession.getMapper(SqlDataMapper.class); + List disassembleData = mapper.selectDisassembleEventAndTarget(playerName,target); + return disassembleData; + } + } + + public static void deleteDisassemble(String playerName){ + + } +} diff --git a/src/main/java/com/yuyu/srdisassemble/data/mapper/SqlDataMapper.java b/src/main/java/com/yuyu/srdisassemble/data/mapper/SqlDataMapper.java new file mode 100644 index 0000000..6c336e2 --- /dev/null +++ b/src/main/java/com/yuyu/srdisassemble/data/mapper/SqlDataMapper.java @@ -0,0 +1,18 @@ +package com.yuyu.srdisassemble.data.mapper; + +import com.yuyu.srdisassemble.pojo.DisassembleData; +import org.apache.ibatis.annotations.Param; + +import java.util.Date; +import java.util.List; + +public interface SqlDataMapper { + + + void createTable(); + void setChartseUtf8(); + void addDisassembleEvent(@Param("data") DisassembleData data); + List selectDisassembleEvent(@Param("playername") String playername); + List selectDisassembleEventAndTarget(@Param("playername")String playername, @Param("target") String target); + void deleteDisassemble(@Param("playername") String playername, @Param("target") String target,@Param("disassembleTime") Date disassembleTime); +} diff --git a/src/main/java/com/yuyu/srdisassemble/disassemeble/DisassembleManager.java b/src/main/java/com/yuyu/srdisassemble/disassemeble/DisassembleManager.java new file mode 100644 index 0000000..936bdbd --- /dev/null +++ b/src/main/java/com/yuyu/srdisassemble/disassemeble/DisassembleManager.java @@ -0,0 +1,75 @@ +package com.yuyu.srdisassemble.disassemeble; + +import com.germ.germplugin.GermPlugin; +import com.germ.germplugin.api.dynamic.gui.GermGuiScreen; +import com.germ.germplugin.api.dynamic.gui.GuiManager; +import com.sakurarealm.sritem.germ.GermGuiService; +import com.yuyu.srdisassemble.SrDisassemble; +import lombok.Getter; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; + +import java.io.File; +import java.util.List; + +/** + * @author 峰。 + * @version 1.0 + * @project SrDisassemble + * @date 2024/6/11 16:42:02 + * @description + */ +public class DisassembleManager { + @Getter + private final static DisassembleManager instance = new DisassembleManager();//用于获取实例 + + public DisassembleManager() { + } + + public static ConfigurationSection getGermGui(String directoryPath, String guiName) { + if (directoryPath != null && !directoryPath.startsWith(File.separator)) + directoryPath = File.separator + directoryPath; + + return YamlConfiguration.loadConfiguration( + new File(GermGuiService.germPluginFilePath.getAbsolutePath() + + File.separator + "gui" + directoryPath + File.separator + + guiName + ".yml") + ).getConfigurationSection(guiName); + } + + //获取分解gui的背包 + public static ConfigurationSection getDisassemblePlayerInventory() { + return DisassembleManager.getGermGui( "disassemble","player_inventory-disassemble"); + } + + //获取分解gui + public static ConfigurationSection getDisassembleGui() { + return DisassembleManager.getGermGui( "disassemble","disassemble"); + } + + + public static ConfigurationSection getTemporarilyBox(){ + return DisassembleManager.getGermGui( "disassemble","TemporarilyBox"); + } + + //用于打开gui + public void open(Player player) { +// SassembleGuiService sassembleGuiService = (SassembleGuiService) TemporarilyBox.getGermGuiScreen(player.getName(), "disassemble"); +// if (sassembleGuiService == null) { + //获取分解gui + SassembleGuiService sassembleGuiService = new SassembleGuiService(getDisassembleGui(), "disassemble", player); +// TemporarilyBox.addGuiScreen(player.getName(), "disassemble", sassembleGuiService); +// } + +// GermGuiScreen packscreen = TemporarilyBox.getGermGuiScreen(player.getName(), "player_inventory-disassemble"); +// if (packscreen == null) { + GermGuiScreen packscreen = GermGuiScreen.getGermGuiScreen("player_inventory-disassemble", this.getDisassemblePlayerInventory()); +// TemporarilyBox.addGuiScreen(player.getName(), "player_inventory-disassemble",packscreen); +// } + + sassembleGuiService.openGui(player); + packscreen.openChildGui(player); + } + +} diff --git a/src/main/java/com/yuyu/srdisassemble/disassemeble/SassembleGuiService.java b/src/main/java/com/yuyu/srdisassemble/disassemeble/SassembleGuiService.java new file mode 100644 index 0000000..d3f55d1 --- /dev/null +++ b/src/main/java/com/yuyu/srdisassemble/disassemeble/SassembleGuiService.java @@ -0,0 +1,314 @@ +package com.yuyu.srdisassemble.disassemeble; + +import com.germ.germplugin.api.dynamic.gui.*; +import com.sakurarealm.sritem.api.ItemStackHelper; +import com.sakurarealm.sritem.api.SrItemAPI; +import com.sakurarealm.sritem.api.builder.SrItemHandler; +import com.yuyu.srdisassemble.SrDisassemble; +import com.yuyu.srdisassemble.data.JdbcSqlClass; +import com.yuyu.srdisassemble.data.SqlData; +import com.yuyu.srdisassemble.pojo.CanDisItem; +import com.yuyu.srdisassemble.pojo.DisProduct; +import com.yuyu.srdisassemble.pojo.DisassembleData; +import com.yuyu.srdisassemble.utils.MinUtil; +import org.bukkit.Color; +import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +import java.util.HashMap; +import java.util.List; +import java.util.Random; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +/** + * @author 峰。 + * @version 1.0 + * @project SrDisassemble + * @date 2024/6/11 17:01:05 + * @description 用于给分解gui提供服务的类 + */ +public class SassembleGuiService extends GermGuiScreen { + private DisassembleData data; + private String name; + private Random random ; + private boolean disassemble = true; + private Player playerHost; + private GermGuiScreen temporarilyBoxScreen; + + public SassembleGuiService(ConfigurationSection configurationSection, String guiName,Player playerHost) { + super(guiName, configurationSection); + this.playerHost = playerHost; + this.data = JdbcSqlClass.getDataByGuiAndPlay(playerHost.getName(), guiName); + this.registerDisassemble(); + this.random = new Random(); + this.name = playerHost.getName(); + this.getTemporarilyBox(); + this.boolGui(); + this.setClosedHandler(((player, germGuiScreen) -> { + //窗口关闭事件 + this.returnItemStacks(player); + this.saveTemporarilyBox(); + this.close(); + })); + } + + /** + * 获取暂存箱对象 + */ + public void getTemporarilyBox(){ + this.temporarilyBoxScreen = TemporarilyBox.getGermGuiScreen(playerHost.getName(), "TemporarilyBox"); + if (temporarilyBoxScreen == null) { + temporarilyBoxScreen = GermGuiScreen.getGermGuiScreen("TemporarilyBox", DisassembleManager.getTemporarilyBox()); + TemporarilyBox.addGuiScreen(playerHost.getName(),"TemporarilyBox",temporarilyBoxScreen); + } + } + + /** + * 用于关闭暂存箱 + */ + public void closeTemporaryBox(){ + this.temporarilyBoxScreen.close(); + } + + /** + * gui关闭时保存暂存箱 + */ + public void saveTemporarilyBox(){ + TemporarilyBox.addGuiScreen(playerHost.getName(), "TemporarilyBox",this.temporarilyBoxScreen); + } + + public void boolGui(){ + if (data.getId() != 0 && data.getTargetIndex() != null) { + GermGuiSlot disassembleSlot = (GermGuiSlot) this.getGuiPart("disassembleSlot"); + disassembleSlot.setInteract(false); + disassembleSlot.setItemStack(SrItemAPI.getItem(data.getTargetIndex(), + new HashMap<>(),null,true,true)); + + GermGuiGif fire = (GermGuiGif) this.getGuiPart("fire"); + GermGuiTexture men1 = (GermGuiTexture) this.getGuiPart("men1"); + GermGuiTexture doorPause = (GermGuiTexture) this.getGuiPart("doorPause"); + GermGuiGif door = (GermGuiGif) this.getGuiPart("door"); + + men1.setEnable(false); + fire.setEnable(true); + doorPause.setEnable(true); + + ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); + + +// executor.schedule(() ->{ +// //打开暂存箱 +// temporarilyBoxScreen.openChildGui(playerHost); +// +// },100,TimeUnit.MILLISECONDS); + executor.scheduleAtFixedRate(() -> { + int calculateMinutesDifference = MinUtil.calculateMinutesDifference(data.getDisassembleTime()); + if (calculateMinutesDifference <= 0) { + //执行语句说明分解完成 + this.disassembleComplete(); + if ((boolean)disassembleSlot.getInteract() == true && disassemble){ + JdbcSqlClass.deleteData(data.getId()); + + //赋予产物后把火关掉,门打开 + //TODO(此处后面可以让美术大佬做一个开炉门的gif + fire.setEnable(false); + door.setEnable(false); + men1.setEnable(true); + doorPause.setEnable(false); + this.openTemporarilyBox(); + executor.shutdownNow(); + } + + } + + },0, 1 , TimeUnit.MINUTES); + //0延迟,minTime分钟后执行 + }else { + this.openTemporarilyBox(); + } + } + + + public void openTemporarilyBox(){ + ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); + executor.schedule(() -> { + this.temporarilyBoxScreen.openChildGui(this.playerHost); + },100,TimeUnit.MILLISECONDS); + + } + + /** + * 注册分解事件 + */ + public void registerDisassemble() { + //获取画布 + GermGuiCanvas canvas = (GermGuiCanvas)this.getGuiPart("canvas"); + //通过画布获取按键 + GermGuiButton confirmButton = (GermGuiButton) canvas.getGuiPart("confirmButton"); + GermGuiButton cancleButton = (GermGuiButton) canvas.getGuiPart("cancleButton"); + GermGuiButton germGuiButton =(GermGuiButton) this.getGuiPart("youhu"); + GermGuiSlot disassembleSlot = (GermGuiSlot) this.getGuiPart("disassembleSlot"); + + confirmButton.registerCallbackHandler((player, btn) -> { + this.disassemble(player); + canvas.setEnable(false); + },GermGuiButton.EventType.LEFT_CLICK); + + //取消事件 + cancleButton.registerCallbackHandler((player, btn) -> { + //取消事件,关闭画布gui + canvas.setEnable(false); + },GermGuiButton.EventType.LEFT_CLICK); + + //油壶按钮绑定的事件触发画布显示 + germGuiButton.registerCallbackHandler((player, btn) -> { + if ((boolean)disassembleSlot.getInteract()) { + canvas.setEnable(true); + } + },GermGuiButton.EventType.LEFT_CLICK); + } + + public void disassemble(Player player){ + + GermGuiSlot disassembleSlot = (GermGuiSlot) this.getGuiPart("disassembleSlot"); + if ((boolean)disassembleSlot.getInteract() == false){ + //正在分解某种物品 + return; + } + ItemStack itemStack = disassembleSlot.getItemStack(); + if (itemStack.getType() == Material.AIR || itemStack == null) { + player.sendMessage(Color.RED+"在熔炉物品框中放上可分解的物品!"); + return; + } + SrItemHandler srItemHandler = ItemStackHelper.getSrItemHandler(itemStack); + String title = srItemHandler.getTitle(); + //这个是item的名字 + String itemName = title.substring(title.indexOf('l') + 1); + HashMap disProductHashMap = SrDisassemble.getConfigManager().getDisProductHashMap(); + CanDisItem canDisItem = disProductHashMap.get(itemName); + if (!disProductHashMap.containsKey(itemName)){ + player.sendMessage(Color.RED+"放入的物品不可分解!"); + return; + } + disassembleSlot.setInteract(false);//关闭交互 + //上方if语句通过表示该物品可以分解 + //假如启动按钮是油壶, + GermGuiGif door = (GermGuiGif) this.getGuiPart("door"); + door.setCycle(1); + if ((boolean)door.getEnable() == true){ + return; + } + GermGuiTexture men1 = (GermGuiTexture) this.getGuiPart("men1"); + men1.setEnable(false); + GermGuiGif fire = (GermGuiGif) this.getGuiPart("fire"); + fire.setEnable(true); + door.setEnable(true); + this.data = new DisassembleData(player.getName(), + MinUtil.calculateTimeAfterMinutes(canDisItem.getDisTime()), + canDisItem.getTarget(), canDisItem.getTargetIndex(), this.getGuiName(),itemStack.getAmount()); + //此处向数据库中插入数据,并且注册定时器计算时间 + int i = JdbcSqlClass.insertData(data); + data.setId(i); + + //下面写注册逻辑,完成的时间已经记录在data里面了!!! + + this.closeTemporaryBox(); + + int minTime = MinUtil.calculateMinutesDifference(data.getDisassembleTime()); + + ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); + +// executor.schedule(() ->{ +// //打开暂存箱 +// temporarilyBoxScreen.openChildGui(playerHost); +// +// },1,TimeUnit.SECONDS); + + executor.scheduleAtFixedRate(() -> { + int calculateMinutesDifference = MinUtil.calculateMinutesDifference(data.getDisassembleTime()); + if (calculateMinutesDifference <= 0) { + //执行语句说明分解完成 + this.disassembleComplete(); + if ((boolean)disassembleSlot.getInteract() == true && disassemble){ + JdbcSqlClass.deleteData(data.getId()); + + //赋予产物后把火关掉,门打开 + //TODO(此处后面可以让美术大佬做一个开炉门的gif + fire.setEnable(false); + door.setEnable(false); + men1.setEnable(true); + this.openTemporarilyBox(); + executor.shutdownNow(); + } + } + + },1, 1, TimeUnit.MINUTES); + //1分钟延迟,minTime分钟后执行 + + + + +// //此处写分解逻辑 +// int itemAmount = itemStack.getAmount(); +// //分解物品返回原胚,理论上是只能单次分解一个然后得到一个。 +// HashMap stringDisProductHashMap = disProductHashMap.get(itemName); +// float v = random.nextFloat(); +// ItemStack product = null; +// for (DisProduct disProduct : stringDisProductHashMap.values()) { +// if (disProduct.getProMax() > v && disProduct.getProMin() <= v){ +// product = SrItemAPI.getItem(disProduct.getIndexName(),new HashMap<>(),null,true,true); +// break; +// } +// } +// disassembleSlot.setInteract(true); +// //赋予产物 +// disassembleSlot.setItemStack(product); + } + + /** + * 此方法用于获取产物 + */ + public void disassembleComplete(){ + GermGuiSlot disassembleSlot = (GermGuiSlot) this.getGuiPart("disassembleSlot"); + ItemStack itemStack = disassembleSlot.getItemStack(); + //此处写分解逻辑 + int itemAmount = itemStack.getAmount(); + SrItemHandler srItemHandler = ItemStackHelper.getSrItemHandler(itemStack); + String title = srItemHandler.getTitle(); + //这个是item的名字 + String itemName = title.substring(title.indexOf('l') + 1); + + HashMap disProductHashMap = SrDisassemble.getConfigManager().getDisProductHashMap(); + //分解物品返回原胚,理论上是只能单次分解一个然后得到一个。 + CanDisItem canDisItem = disProductHashMap.get(itemName); + HashMap disProducts = canDisItem.getDisProductHashMap(); + float v = random.nextFloat(); + ItemStack product = null; + for (DisProduct disProduct : disProducts.values()) { + if (disProduct.getProMax() > v && disProduct.getProMin() <= v){ + product = SrItemAPI.getItem(disProduct.getIndexName(),new HashMap<>(),null,true,true); + break; + } + } + disassembleSlot.setInteract(true); + //赋予产物 + disassembleSlot.setItemStack(product); + } + + //遍历gui的所有物品槽,并且返回到玩家背包中 + synchronized private void returnItemStacks(Player player) { + this.disassemble = false; + this.temporarilyBoxScreen.close(); + List allGuiParts = this.getAllGuiParts(GermGuiSlot.class); + for (GermGuiSlot guiPart : allGuiParts) { + if ((guiPart.getItemStack().getType() != Material.AIR || guiPart.getItemStack() == null) && (boolean)guiPart.getInteract()) { + player.getInventory().addItem(guiPart.getItemStack()); + } + } + } + +} diff --git a/src/main/java/com/yuyu/srdisassemble/disassemeble/TemporarilyBox.java b/src/main/java/com/yuyu/srdisassemble/disassemeble/TemporarilyBox.java new file mode 100644 index 0000000..e82274d --- /dev/null +++ b/src/main/java/com/yuyu/srdisassemble/disassemeble/TemporarilyBox.java @@ -0,0 +1,45 @@ +package com.yuyu.srdisassemble.disassemeble; + +import com.germ.germplugin.api.dynamic.gui.GermGuiScreen; + +import java.util.HashMap; + +/** + * @author 峰。 + * @version 1.0 + * @project SrDisassemble + * @date 2024/6/21 18:50:18 + * @description 用于存放暂存箱 + */ +public class TemporarilyBox { + public static HashMap> temporarilyBoxHashMap = new HashMap<>(); + + public static GermGuiScreen getGermGuiScreen(String playname,String guiName) { + if(temporarilyBoxHashMap.containsKey(playname)) { + HashMap stringGermGuiScreenHashMap = temporarilyBoxHashMap.get(playname); + if(stringGermGuiScreenHashMap.containsKey(guiName)) { + return stringGermGuiScreenHashMap.get(guiName); + }else { + return null; + } + }else { + return null; + } + } + + public static void deleteGuiScreen(String playname,String guiName) { + if(temporarilyBoxHashMap.containsKey(playname)) { + temporarilyBoxHashMap.get(playname).remove(guiName); + } + } + + public static void addGuiScreen(String playname,String guiName,GermGuiScreen guiScreen) { + if(temporarilyBoxHashMap.containsKey(playname)) { + temporarilyBoxHashMap.get(playname).put(guiName, guiScreen); + }else { + HashMap stringGermGuiScreenHashMap = new HashMap<>(); + stringGermGuiScreenHashMap.put(guiName, guiScreen); + temporarilyBoxHashMap.put(playname, stringGermGuiScreenHashMap); + } + } +} diff --git a/src/main/java/com/yuyu/srdisassemble/pojo/CanDisItem.java b/src/main/java/com/yuyu/srdisassemble/pojo/CanDisItem.java new file mode 100644 index 0000000..183cc62 --- /dev/null +++ b/src/main/java/com/yuyu/srdisassemble/pojo/CanDisItem.java @@ -0,0 +1,72 @@ +package com.yuyu.srdisassemble.pojo; + +import java.util.HashMap; +import java.util.List; + +/** + * @author 峰。 + * @version 1.0 + * @project SrDisassemble + * @date 2024/6/19 21:49:48 + * @description 可分解的物品 + */ +public class CanDisItem { + private String itemName; + private int disTime; + private String target; + private String targetIndex; + private HashMap disProductHashMap; + + public void setTarget(String target) { + this.target = target; + } + + public void setTargetIndex(String targetIndex) { + this.targetIndex = targetIndex; + } + + public String getTarget() { + return target; + } + + public String getTargetIndex() { + return targetIndex; + } + + public String getItemName() { + return itemName; + } + + public int getDisTime() { + return disTime; + } + + public HashMap getDisProductHashMap() { + return disProductHashMap; + } + + public void setItemName(String itemName) { + this.itemName = itemName; + } + + public void setDisTime(int disTime) { + this.disTime = disTime; + } + + public void setDisProductHashMap(HashMap disProductHashMap) { + this.disProductHashMap = disProductHashMap; + } + + public CanDisItem(String itemName, int disTime,String target,String targetIndex) { + this.itemName = itemName; + this.disTime = disTime; + this.target = target; + this.targetIndex = targetIndex; + } + + public CanDisItem(String itemName, int disTime, HashMap disProductHashMap) { + this.itemName = itemName; + this.disTime = disTime; + this.disProductHashMap = disProductHashMap; + } +} diff --git a/src/main/java/com/yuyu/srdisassemble/pojo/DisProduct.java b/src/main/java/com/yuyu/srdisassemble/pojo/DisProduct.java new file mode 100644 index 0000000..0c8426e --- /dev/null +++ b/src/main/java/com/yuyu/srdisassemble/pojo/DisProduct.java @@ -0,0 +1,67 @@ +package com.yuyu.srdisassemble.pojo; + +import com.sakurarealm.sritem.api.SrItemAPI; +import org.bukkit.inventory.ItemStack; + +import java.util.HashMap; + +/** + * @BelongsProject: SrAppraise + * @BelongsPackage: com.yuyu.srappraise.pojo + * @FileName: AppraiseProduct + * @Author: 峰。 + * @Date: 2024/4/23-14:09 + * @Version: 1.0 + * @Description: 分解的产物 + */ +public class DisProduct { + //物品的名称 + private final String ItemName; + //物品的索引 + private final String indexName; + //最大的概率 + private final double ProMax; + //最小的概率 + private final double ProMin; + //获取ItemStack + private final ItemStack itemStack; + + public String getItemName() { + return ItemName; + } + + public String getIndexName() { + return indexName; + } + + public double getProMax() { + return ProMax; + } + + public double getProMin() { + return ProMin; + } + + public DisProduct(String itemName, String indexName, double proMax, double proMin) { + ItemName = itemName; + this.indexName = indexName; + ProMax = proMax; + ProMin = proMin; + this.itemStack = SrItemAPI.getItem(indexName,new HashMap<>(),null,true,true); + } + + public ItemStack getItemStack() { + return itemStack; + } + + @Override + public String toString() { + return "DisProduct{" + + "ItemName='" + ItemName + '\'' + + ", indexName='" + indexName + '\'' + + ", ProMax=" + ProMax + + ", ProMin=" + ProMin + + ", itemStack=" + itemStack + + '}'; + } +} diff --git a/src/main/java/com/yuyu/srdisassemble/pojo/DisassembleData.java b/src/main/java/com/yuyu/srdisassemble/pojo/DisassembleData.java new file mode 100644 index 0000000..f3dcd6c --- /dev/null +++ b/src/main/java/com/yuyu/srdisassemble/pojo/DisassembleData.java @@ -0,0 +1,98 @@ +package com.yuyu.srdisassemble.pojo; + +import java.time.LocalDateTime; + +/** + * @author 峰。 + * @version 1.0 + * @project SrDisassemble + * @date 2024/6/17 18:08:55 + * @description 用于新增和接收分解的信息 + */ +public class DisassembleData { + private int id; + private String playername; + private LocalDateTime disassembleTime; + private String target; + private String targetIndex; + private String guiName; + private int size; + + public DisassembleData() { + } + + + public DisassembleData(String playername, LocalDateTime disassembleTime, String target, String targetIndex, String guiName, int size) { + this.playername = playername; + this.disassembleTime = disassembleTime; + this.target = target; + this.targetIndex = targetIndex; + this.guiName = guiName; + this.size = size; + } + + public DisassembleData(int id, String playername, LocalDateTime disassembleTime, String target, String targetIndex, String guiName) { + this.id = id; + this.playername = playername; + this.disassembleTime = disassembleTime; + this.target = target; + this.targetIndex = targetIndex; + this.guiName = guiName; + } + + public String getGuiName() { + return guiName; + } + + public void setId(int id) { + this.id = id; + } + + public void setPlayername(String playername) { + this.playername = playername; + } + + public void setDisassembleTime(LocalDateTime disassembleTime) { + this.disassembleTime = disassembleTime; + } + + public void setTarget(String target) { + this.target = target; + } + + public void setTargetIndex(String targetIndex) { + this.targetIndex = targetIndex; + } + + public void setGuiName(String guiName) { + this.guiName = guiName; + } + + public int getId() { + return id; + } + + public int getSize() { + return size; + } + + public void setSize(int size) { + this.size = size; + } + + public String getPlayername() { + return playername; + } + + public LocalDateTime getDisassembleTime() { + return disassembleTime; + } + + public String getTarget() { + return target; + } + + public String getTargetIndex() { + return targetIndex; + } +} diff --git a/src/main/java/com/yuyu/srdisassemble/utils/MinUtil.java b/src/main/java/com/yuyu/srdisassemble/utils/MinUtil.java new file mode 100644 index 0000000..78e7c52 --- /dev/null +++ b/src/main/java/com/yuyu/srdisassemble/utils/MinUtil.java @@ -0,0 +1,54 @@ +package com.yuyu.srdisassemble.utils; + +import java.time.Duration; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +/** + * @author 峰。 + * @version 1.0 + * @project SrDisassemble + * @date 2024/6/19 21:24:25 + * @description 用于计算时间 + */ +public class MinUtil { + + + /** + * 计算得出产物的时间 + * @param minutesToAdd + * @return + */ + public static LocalDateTime calculateTimeAfterMinutes(int minutesToAdd) { + LocalDateTime currentTime = LocalDateTime.now(); + // 使用plusMinutes方法添加分钟数 + LocalDateTime futureTime = currentTime.plusMinutes(minutesToAdd); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); + futureTime.format(formatter); + return futureTime; + } + + /** + * 计算得出产物需要的时间,注意此处返回的可能是负数 + * @param givenTime + * @return + */ + public static int calculateMinutesDifference(LocalDateTime givenTime) { + // 获取当前时间 + LocalDateTime currentTime = LocalDateTime.now(); + + // 计算两个时间之间的Duration + Duration duration = Duration.between(currentTime, givenTime); + + // 获取Duration中的分钟数(注意:Duration的toMinutes方法返回的是总分钟数,包括天数、小时等转换过来的分钟) + int minutes =(int) duration.toMinutes(); + + // 如果givenTime在currentTime之后,则duration是负数,我们需要取绝对值 + // 但实际上我们可能想要返回负数来表示givenTime在未来 + // 这里取决于你的需求,是返回绝对值还是实际值 + // 如果要返回绝对值,则使用Math.abs(minutes) + + // 返回分钟数 + return minutes; + } +} diff --git a/src/main/java/com/yuyu/srdisassemble/utils/MybatisUtil.java b/src/main/java/com/yuyu/srdisassemble/utils/MybatisUtil.java new file mode 100644 index 0000000..112dc78 --- /dev/null +++ b/src/main/java/com/yuyu/srdisassemble/utils/MybatisUtil.java @@ -0,0 +1,51 @@ +package com.yuyu.srdisassemble.utils; + +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; + +import java.io.IOException; +import java.io.InputStream; + +public class MybatisUtil { + private static final SqlSessionFactory factory; + + static { + + + InputStream inputStream = null; + try { + inputStream = Resources.getResourceAsStream("mybatis-config.xml"); + factory = new SqlSessionFactoryBuilder().build(inputStream); + } catch (IOException e) { + throw new RuntimeException("Failed to initialize MyBatis SqlSessionFactory", e); + } finally { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + public MybatisUtil( ) { + + + + + } + + public static SqlSession getSqlSession() { + return factory.openSession(); + } + + public static void close(SqlSession sqlSession) { + if (sqlSession != null) { + sqlSession.close(); + } + } + +} \ No newline at end of file diff --git a/src/main/resources/ItemDetail.yml b/src/main/resources/ItemDetail.yml new file mode 100644 index 0000000..c14d2d5 --- /dev/null +++ b/src/main/resources/ItemDetail.yml @@ -0,0 +1,14 @@ +#产物的详情 +SrDisassemble: + 巴尔伐楼伽的怨牙: #这一行填可以鉴定的物品 + 环齿原胚: #这下面填鉴定物品可以得到的结果 + probabilityMax: 0.4 #概率,相当于一个转盘,多少到多少是这个产物,必定会有结果,比如[0,0.4],左闭右开的范围区间,若随机到0则为此产物,0.4则跳过 + probabilityMin: 0 #概率,相当于一个转盘,多少到多少是这个产物,必定会有结果,比如[0,0.4],左闭右开的范围区间,若随机到0则为此产物,0.4则跳过 + index: public.material.巴尔伐楼伽的怨牙 #这里填材料的索引名 + DisTime: 2 #分解需要的时间,单位为分钟 + 保密试作品HC-03原胚: #这下面填鉴定物品可以得到的结果 + probabilityMax: 1 #概率 + probabilityMin: 0.4 #概率 + index: public.test.保密试作品HC-03原胚 #这里填材料的索引名 + DisTime: 2 #分解需要的时间,单位为分钟 + diff --git a/src/main/resources/SqlDataMapper.xml b/src/main/resources/SqlDataMapper.xml new file mode 100644 index 0000000..c8e2b18 --- /dev/null +++ b/src/main/resources/SqlDataMapper.xml @@ -0,0 +1,64 @@ + + + + + + + create table if not exists srdisassemble + ( + id BIGINT auto_increment comment 'ID', + playername varchar(255) null comment '玩家姓名', + disassembleTime datetime null comment '分解的时间', + target varchar(255) null comment '分解的目标', + targetIndex varchar(255) null comment '分解目标的索引' + guiname varchar(255) null comment '打开gui的名称' + ); + + + + ALTER TABLE srdisassemble CONVERT TO CHARACTER SET utf8mb4; + + + + insert into srdisassemble(playername,disassembleTime,target,targetIndex) + values( + #{data.playername}, + #{data.disassembleTime}, + #{data.target}, + #{data.targetIndex} + ) + + + + + + + + delete from srdisassemble where + id = #{id} + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/datasource.yml b/src/main/resources/datasource.yml new file mode 100644 index 0000000..c6a75e2 --- /dev/null +++ b/src/main/resources/datasource.yml @@ -0,0 +1,6 @@ +database: + sql: + user: root + password: + driver: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/mc_service diff --git a/src/main/resources/disProduct.yml b/src/main/resources/disProduct.yml new file mode 100644 index 0000000..40f36f3 --- /dev/null +++ b/src/main/resources/disProduct.yml @@ -0,0 +1,3 @@ +#可分解的物品 +SrDisassemble: + 巴尔伐楼伽的怨牙 \ No newline at end of file diff --git a/src/main/resources/disToItem.yml b/src/main/resources/disToItem.yml new file mode 100644 index 0000000..fa51ea6 --- /dev/null +++ b/src/main/resources/disToItem.yml @@ -0,0 +1,8 @@ +#可分解的物品对应的产物 +SrDisassemble: + 巴尔伐楼伽的怨牙: + disProduct: + - 环齿原胚 + - 保密试作品HC-03原胚 + disTime: 2 #分解的时间,单位为分钟 + index: public.material.巴尔伐楼伽的怨牙 diff --git a/src/main/resources/mybatis-config.xml b/src/main/resources/mybatis-config.xml new file mode 100644 index 0000000..ce4e51e --- /dev/null +++ b/src/main/resources/mybatis-config.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..0d8a641 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,11 @@ +name: SrDisassemble +version: '${project.version}' +main: com.yuyu.srdisassemble.SrDisassemble + +depend: + - SrItem +commands: + srdisassemble: + description: srdisassemble command + aliases: [ srd ] + permission: srdisassemble.command