33import com .crystalneko .csnktools .csnktools .CTTool .CTScoreboard ;
44import com .crystalneko .csnktools .csnktools .CTTool .loginmsg ;
55import com .crystalneko .csnktools .csnktools .CTcommand .csnktools ;
6+ import com .crystalneko .csnktools .csnktools .CTcommand .csnktoolsTabCompleter ;
7+
68import org .bukkit .Bukkit ;
9+ import org .bukkit .configuration .ConfigurationSection ;
710import org .bukkit .configuration .file .FileConfiguration ;
811import org .bukkit .configuration .file .YamlConfiguration ;
912import org .bukkit .entity .Player ;
1013import org .bukkit .event .Listener ;
1114import org .bukkit .plugin .java .JavaPlugin ;
1215import org .bukkit .scoreboard .Scoreboard ;
1316import org .bukkit .scheduler .*;
14- import java .io .File ;
17+
18+ import java .io .*;
19+ import java .net .HttpURLConnection ;
20+ import java .net .URL ;
21+ import java .nio .file .Files ;
22+ import java .nio .file .Path ;
23+ import java .nio .file .Paths ;
1524import java .util .HashMap ;
1625import java .util .Map ;
1726
27+
1828public final class CSNKTools extends JavaPlugin implements Listener {
29+
1930 private File configFile ;
2031 private BukkitScheduler scheduler ;
2132 private FileConfiguration config ;
@@ -30,6 +41,7 @@ public final class CSNKTools extends JavaPlugin implements Listener {
3041 public void onEnable () {
3142 int pluginId = 19702 ; // <-- Replace with the id of your plugin!
3243 Metrics metrics = new Metrics (this , pluginId );
44+
3345 // 创建一个名为config.yml的配置文件
3446 configFile = new File (getDataFolder (), "config.yml" );
3547 // 获取配置文件实例
@@ -42,26 +54,34 @@ public void onEnable() {
4254 String configFileName = "config.yml" ;
4355 File configFile = new File (getDataFolder (), configFileName );
4456 File oldConfigFile = new File (getDataFolder (), "Config.yml" );
45-
4657 if (oldConfigFile .exists () && !configFile .exists ()) {
4758 oldConfigFile .renameTo (configFile );
4859 }
60+
61+ //加载配置
62+ createConfigFile ();
63+
64+ //检查配置
65+ //checkConfigFile();
66+
4967 // 创建语言文件
5068 createLanguageFiles ();
5169
5270 // 加载语言文件
5371 loadLanguageFile ();
72+
73+
5474 //加载消息
5575 String enableplugin = getMessage ("Console.enable" );
5676 Bukkit .getConsoleSender ().sendMessage (enableplugin );
57- // 判断插件配置是否启用
58- if (config .getBoolean ("Enable" )) {
77+ //检查更新
78+ checkUpdates ();
79+ // 注册命令执行器
80+ getCommand ("csnktools" ).setExecutor (new csnktools (this ));
81+ // 注册 Tab 补全
82+ getCommand ("csnktools" ).setTabCompleter (new csnktoolsTabCompleter ());
5983 // 加载类readconfig
6084 readconfig ();
61- } else {
62- String disableplugin = getMessage ("Console.disable" );
63- Bukkit .getConsoleSender ().sendMessage (disableplugin );
64- }
6585 }
6686
6787 public void readconfig () {
@@ -116,6 +136,8 @@ private void loadLanguageFile() {
116136 }
117137
118138 languageConfig = YamlConfiguration .loadConfiguration (languageFile );
139+
140+
119141 }
120142
121143 // 获取翻译内容的方法
@@ -124,6 +146,224 @@ public String getMessage(String key) {
124146 }
125147
126148
149+ public void checkUpdates () {
150+ String remoteUrl = "https://w.csk.asia/res/version.txt" ; // 远程网站中存储版本号的文件地址
151+
152+ try {
153+ URL url = new URL (remoteUrl );
154+ HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
155+ connection .setRequestMethod ("GET" );
156+
157+ int responseCode = connection .getResponseCode ();
158+ if (responseCode == HttpURLConnection .HTTP_OK ) {
159+ BufferedReader reader = new BufferedReader (new InputStreamReader (connection .getInputStream ()));
160+ String remoteVersion = reader .readLine ();
161+
162+ reader .close ();
163+
164+ // 将远程版本号与插件版本进行比较
165+ String pluginVersion = getDescription ().getVersion (); // 获取插件的版本号
166+ if (!pluginVersion .equals (remoteVersion )) {
167+ // 版本不同,发出更新提醒
168+ String checkupdate_update = getMessage ("System.check-update.update" );
169+ getLogger ().info (checkupdate_update );
170+ } else {
171+ // 版本相同,无需更新
172+ String checkupdate_isupdate = getMessage ("System.check-update.is-update" );
173+ getLogger ().info (checkupdate_isupdate );
174+ }
175+ } else {
176+ // 请求失败
177+ String checkupdate_warning = getMessage ("System.check-update.warning" );
178+ getLogger ().warning (checkupdate_warning + responseCode );
179+ }
180+
181+ connection .disconnect ();
182+ } catch (IOException e ) {
183+ // 发生异常
184+ String checkupdate_warning2 = getMessage ("System.check-update.warning2" );
185+ getLogger ().warning (checkupdate_warning2 + e .getMessage ());
186+ }
187+ }
188+
189+ public void checkUpdates2 (Player player ) {
190+ String remoteUrl = "https://w.csk.asia/res/version.txt" ; // 远程网站中存储版本号的文件地址
191+
192+ try {
193+ URL url = new URL (remoteUrl );
194+ HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
195+ connection .setRequestMethod ("GET" );
196+
197+ int responseCode = connection .getResponseCode ();
198+ if (responseCode == HttpURLConnection .HTTP_OK ) {
199+ BufferedReader reader = new BufferedReader (new InputStreamReader (connection .getInputStream ()));
200+ String remoteVersion = reader .readLine ();
201+ reader .close ();
202+
203+ // 将远程版本号与插件版本进行比较
204+ String pluginVersion = getDescription ().getVersion (); // 获取插件的版本号
205+ if (!pluginVersion .equals (remoteVersion )) {
206+ // 版本不同,发出更新提醒
207+ String checkupdate_update = getMessage ("System.check-update.update" );
208+ if (checkupdate_update != null ) {
209+ player .sendMessage (checkupdate_update );
210+ }
211+ } else {
212+ // 版本相同,无需更新
213+ String checkupdate_isupdate = getMessage ("System.check-update.is-update" );
214+ if (checkupdate_isupdate != null ) {
215+ player .sendMessage (checkupdate_isupdate );
216+ }
217+ }
218+ } else {
219+ // 请求失败
220+ String checkupdate_warning = getMessage ("System.check-update.warning" );
221+ player .sendMessage (checkupdate_warning + responseCode );
222+ }
223+
224+ connection .disconnect ();
225+ } catch (IOException e ) {
226+ // 发生异常
227+ String checkupdate_warning2 = getMessage ("System.check-update.warning2" );
228+ player .sendMessage (checkupdate_warning2 + e .getMessage ());
229+ }
230+ }
231+
232+
233+
234+ public void createConfigFile (){
235+ // 创建一个名为config.yml的配置文件
236+ configFile = new File (getDataFolder (), "config.yml" );
237+ // 获取配置文件实例
238+ config = getConfig ();
239+ // 如果配置文件不存在,则从插件资源中复制默认的配置文件
240+ if (!configFile .exists ()) {
241+ saveResource ("Config.yml" , false );
242+ }
243+ // 检查并修复配置文件名的大小写
244+ String configFileName = "config.yml" ;
245+ File configFile = new File (getDataFolder (), configFileName );
246+ File oldConfigFile = new File (getDataFolder (), "Config.yml" );
247+ if (oldConfigFile .exists () && !configFile .exists ()) {
248+ oldConfigFile .renameTo (configFile );
249+ }
250+ }
251+
252+ //这一段无法正常运行,因此被注释了
253+ /* private void checkConfigFile() {
254+ //---------------------------------------------------------------------------------------
255+ // 创建一个名为config.yml的配置文件
256+ configFile = new File(getDataFolder(), "config.yml");
257+ // 获取配置文件实例
258+ config = getConfig();
259+ // 如果配置文件不存在,则从插件资源中复制默认的配置文件
260+ if (!configFile.exists()) {
261+ saveResource("Config.yml", false);
262+ }
263+ // 检查并修复配置文件名的大小写
264+ String configFileName = "config.yml";
265+ File configFile = new File(getDataFolder(), configFileName);
266+ File oldConfigFile = new File(getDataFolder(), "Config.yml");
267+ if (oldConfigFile.exists() && !configFile.exists()) {
268+ oldConfigFile.renameTo(configFile);
269+ }
270+ //---------------------------------------------------------------------------------------
271+ String sourceFileName = "config.yml";
272+ String targetFileName = "newconfig.yml";
273+
274+ // 获取插件的数据文件夹路径
275+ File dataFolder = getDataFolder();
276+
277+ // 创建源文件和目标文件对象
278+ File sourceFile = new File(dataFolder, sourceFileName);
279+ File targetFile = new File(dataFolder, targetFileName);
280+
281+ try {
282+ // 复制文件
283+ Files.copy(sourceFile.toPath(), targetFile.toPath());
284+
285+ System.out.println("文件复制成功!");
286+ } catch (IOException e) {
287+ e.printStackTrace();
288+ }
289+
290+ //----------------------------------------------------------------------------------------
291+
292+ // 创建配置文件对象
293+ File dconfigFile = new File(getDataFolder(), "config.yml");
294+
295+ // 检查配置文件是否存在
296+ if (dconfigFile.exists()) {
297+ // 删除配置文件
298+ boolean deleted = dconfigFile.delete();
299+
300+ // 检查删除操作是否成功
301+ if (deleted) {
302+ getLogger().info("配置文件已成功删除。");
303+ } else {
304+ getLogger().warning("删除配置文件失败。");
305+ }
306+ } else {
307+ getLogger().warning("配置文件不存在。");
308+ }
309+
310+ //----------------------------------------------------------------------------------------------
311+
312+ // 创建一个名为config.yml的配置文件
313+ configFile = new File(getDataFolder(), "config.yml");
314+ // 获取配置文件实例
315+ config = getConfig();
316+ // 如果配置文件不存在,则从插件资源中复制默认的配置文件
317+ if (!configFile.exists()) {
318+ saveResource("Config.yml", false);
319+ }
320+ // 检查并修复配置文件名的大小写
321+ String configFileName11 = "config.yml";
322+ File configFile11 = new File(getDataFolder(), configFileName11);
323+ File oldConfigFile11 = new File(getDataFolder(), "Config.yml");
324+ if (oldConfigFile11.exists() && !configFile11.exists()) {
325+ oldConfigFile11.renameTo(configFile11);
326+ }
327+
328+ //---------------------------------------------------------------------------------------------------------
329+
330+ File configFile1 = new File(getDataFolder(), "config.yml");
331+ File configFile2 = new File(getDataFolder(), "newconfig.yml");
332+ // 加载配置文件1
333+ FileConfiguration config1 = YamlConfiguration.loadConfiguration(configFile1);
334+
335+ // 加载配置文件2
336+ FileConfiguration config2 = YamlConfiguration.loadConfiguration(configFile2);
337+
338+ // 遍历配置文件2的所有键值对,将它们合并到配置文件1中
339+ for (String key : config2.getKeys(true)) {
340+ if (!config1.contains(key)) {
341+ // 如果配置文件1中没有该键,则将键值对添加到配置文件1中
342+ config1.set(key, config2.get(key));
343+ } else {
344+ // 如果配置文件1中已经存在该键,则使用配置文件2中的值覆盖配置文件1中的值
345+ config1.set(key, config2.get(key));
346+ }
347+ }
348+
349+ try {
350+ // 保存合并后的配置文件1
351+ config1.save(configFile1);
352+ getLogger().info("成功合并并保存配置文件1.");
353+ } catch (IOException e) {
354+ getLogger().warning("无法保存合并后的配置文件1.");
355+ e.printStackTrace();
356+ }
357+
358+
359+
360+
361+ }*/
362+
363+
364+
365+
366+
127367
128368
129369 @ Override
0 commit comments