Skip to content

Commit

Permalink
1.4.1 Release
Browse files Browse the repository at this point in the history
Update GUI
Add Update-Check
  • Loading branch information
jingwenMC committed Aug 18, 2020
1 parent 47748fb commit ff02784
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 2 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>top.jingwenmc</groupId>
<artifactId>MCDNDC</artifactId>
<version>1.4.0-SNAPSHOT</version>
<version>1.4.1-RELEASE</version>
<packaging>jar</packaging>

<name>MCDNDC</name>
Expand Down Expand Up @@ -88,5 +88,10 @@
<version>1.15.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
</dependencies>
</project>
2 changes: 2 additions & 0 deletions src/main/java/top/jingwenmc/mcdndc/commands/mcdndc.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class mcdndc implements CommandExecutor {
public static void reloadPluginConf()
{
wordkeeper.map.clear();
main.getInstance().getConfigAccessor().saveDefaultConfig();
main.getInstance().getLangAccessor().saveDefaultConfig();
main.getInstance().getConfigAccessor().reloadConfig();
main.getInstance().getLangAccessor().reloadConfig();
main.getInstance().getTask().cancel();
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/top/jingwenmc/mcdndc/main.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ public void run() {
}.runTaskTimer(this,configAccessor.getConfig().getInt("interval"),configAccessor.getConfig().getInt("interval"));
getServer().getPluginManager().registerEvents(this,this);
getServer().getPluginManager().registerEvents(new GuiUtil(),this);
getServer().getPluginManager().registerEvents(new UpdateUtil(),this);
for(Player p : Bukkit.getOnlinePlayers())
{
//in case of reload confirm
getServer().getPluginManager().callEvent(new PlayerJoinEvent(p,""));
}
registerModules();
MessageUtil.sendConsole("console.post_load");
UpdateUtil.checkUpdateAsync();
}
@Override
public void onDisable() {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/top/jingwenmc/mcdndc/util/GuiUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public static void showWordsGui(Player player,int page)
ItemStack itemStack = new ItemStack(randomMaterial());
ItemMeta itemMeta = itemStack.getItemMeta();
List<String> wordList = main.getInstance().getConfigAccessor().getConfig().getStringList("words");
//System.out.println((wordList.size()+44)/45);
int maxPage = (wordList.size()+44)/45;
boolean hasNext = true;
boolean hasPrev = true;
Expand Down Expand Up @@ -209,11 +208,13 @@ public void run() {
event.getPlayer().sendMessage(MessageUtil.getMessage("gui.edit_cancel"));
event.getPlayer().sendMessage(MessageUtil.getMessage("gui.edit_not_null"));
map.remove(event.getPlayer().getName());
showWordsGui(event.getPlayer(),1);
return;
}
if (event.getMessage().equalsIgnoreCase("cancel")) {
event.getPlayer().sendMessage(MessageUtil.getMessage("gui.edit_cancel"));
map.remove(event.getPlayer().getName());
showWordsGui(event.getPlayer(),1);
return;
}
List<String> list = main.getInstance().getConfigAccessor().getConfig().getStringList("words");
Expand All @@ -224,6 +225,7 @@ public void run() {
main.getInstance().getConfigAccessor().saveConfig();
event.getPlayer().sendMessage(MessageUtil.getMessage("gui.edit_success"));
map.remove(event.getPlayer().getName());
showWordsGui(event.getPlayer(),1);
}
}.runTask(main.getInstance());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package top.jingwenmc.mcdndc.util;

public enum UpdateCheckResult {
LATEST_RELEASE,SNAPSHOT,NEW_RELEASE,UNEXPECTED
}
140 changes: 140 additions & 0 deletions src/main/java/top/jingwenmc/mcdndc/util/UpdateUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package top.jingwenmc.mcdndc.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.scheduler.BukkitRunnable;
import top.jingwenmc.mcdndc.events.NewGameEvent;
import top.jingwenmc.mcdndc.main;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class UpdateUtil implements Listener {
static String ver_now = "0";
static String ver_new = "0";
public static void checkUpdateAsync()
{
new BukkitRunnable() {
@Override
public void run() {
System.out.println(MessageUtil.getPrefix()+MessageUtil.getMessage("update.check"));
UpdateCheckResult result = updateExists();
if (result.equals(UpdateCheckResult.SNAPSHOT)) {
System.out.println(MessageUtil.getPrefix()+MessageUtil.getMessage("update.dev_l1"));
System.out.println(MessageUtil.getPrefix()+MessageUtil.getMessage("update.dev_l2"));
System.out.println(MessageUtil.getPrefix()+MessageUtil.getMessage("update.dev_l3"));
System.out.println(MessageUtil.getPrefix()+MessageUtil.getMessage("update.dev_l4")+"https://github.com/jingwenMC/MCDNDC/actions");
System.out.println(MessageUtil.getPrefix()+MessageUtil.getMessage("update.dev_l5")+"https://github.com/jingwenMC/MCDNDC/releases");
System.out.println(MessageUtil.getPrefix()+MessageUtil.getMessage("update.dev_l6")+"https://github.com/jingwenMC/MCDNDC/issues");
System.out.println(MessageUtil.getPrefix()+MessageUtil.getMessage("update.dev_l7"));
} else if (result.equals(UpdateCheckResult.LATEST_RELEASE)) {
System.out.println(MessageUtil.getPrefix()+MessageUtil.getMessage("update.latest"));
} else if (result.equals(UpdateCheckResult.NEW_RELEASE)) {
System.out.println(MessageUtil.getPrefix()+MessageUtil.getMessage("update.new_ver"));
System.out.println(MessageUtil.getPrefix() + "v" + ver_now + " -> v" + ver_new);
System.out.println(MessageUtil.getPrefix()+MessageUtil.getMessage("update.download_addr")+"https://github.com/jingwenMC/MCDNDC/releases/latest");
for (Player p : Bukkit.getOnlinePlayers()) {
if (p.isOp()) {
p.sendMessage(MessageUtil.getPrefix()+MessageUtil.getMessage("update.new_ver"));
p.sendMessage(MessageUtil.getPrefix() + "v" + ver_now + " -> v" + ver_new);
p.sendMessage(MessageUtil.getPrefix()+MessageUtil.getMessage("update.download_addr")+"https://github.com/jingwenMC/MCDNDC/releases/latest");
}
}
}
else
{
System.out.println(MessageUtil.getPrefix()+MessageUtil.getMessage("update.error"));
}
}
}.runTaskAsynchronously(main.getInstance());
}
public static UpdateCheckResult updateExists()
{
String version_now = main.getInstance().getDescription().getVersion();
if(version_now.contains("SNAPSHOT"))
{
return UpdateCheckResult.SNAPSHOT;
}
else if(version_now.contains("RELEASE"))
{
String str = getFromUrl("https://api.github.com/repos/jingwenMC/MCDNDC/releases/latest");
if(str==null)return UpdateCheckResult.UNEXPECTED;
JSONObject object = JSON.parseObject(str);
String rawVersion = object.getString("tag_name");
String version = rawVersion.substring(1);
String[] split_ver = version.split("-");
String[] split_now_ver = version_now.split("-");
ver_new = split_ver[0];
ver_now = split_now_ver[0];
if(ver_new.equalsIgnoreCase(ver_now))
{
return UpdateCheckResult.LATEST_RELEASE;
}
else return UpdateCheckResult.NEW_RELEASE;
}
return UpdateCheckResult.UNEXPECTED;
}
private static String getFromUrl(String url) {
try {
URLConnection connection = new URL(url).openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(20000);
InputStream stream = connection.getInputStream();
return streamToString(stream);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static String streamToString(InputStream source) {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
try {
while ((length = source.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
} catch (IOException e) {
e.printStackTrace();
}
return result.toString();
}
@EventHandler
public void onNewGame(NewGameEvent event)
{
checkUpdateAsync();
}
}

/* This file's some part is under the following license:
*
* Copyright (c) 2020 Spazzinq
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
13 changes: 13 additions & 0 deletions src/main/resources/lang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ zh_CN:
used: '你使用了右键换词'
title: '&b切换词语'
lore: '&b右键点击以切换词语'
update:
error: '发生错误,请检查你的Internet连接或联系开发者'
new_ver: '有更新的版本!'
download_addr: '下载地址:'
latest: '恭喜!当前版本已经是最新版本.'
check: '正在检查更新...'
dev_l1: '警告:'
dev_l2: '当前版本为开发版,可能会不稳定'
dev_l3: '将不会自动检查更新'
dev_l4: '开发版构建地址:'
dev_l5: '稳定版更新地址:'
dev_l6: 'BUG反馈地址:'
dev_l7: '感谢你的支持.'
gui:
title: '&1词语编辑器'
edit_cancel: '&c取消编辑!'
Expand Down

0 comments on commit ff02784

Please sign in to comment.