mirror of
https://e.coding.net/circlecloud/Residence.git
synced 2025-11-25 21:56:06 +00:00
move src path and modify config file...
Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
100
src/main/java/com/bekvon/bukkit/residence/utils/ActionBar.java
Normal file
100
src/main/java/com/bekvon/bukkit/residence/utils/ActionBar.java
Normal file
@@ -0,0 +1,100 @@
|
||||
package com.bekvon.bukkit.residence.utils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author hamzaxx
|
||||
*/
|
||||
public class ActionBar {
|
||||
private static String version = "";
|
||||
private static Object packet;
|
||||
private static Method getHandle;
|
||||
private static Method sendPacket;
|
||||
private static Field playerConnection;
|
||||
private static Class<?> nmsChatSerializer;
|
||||
private static Class<?> nmsIChatBaseComponent;
|
||||
private static Class<?> packetType;
|
||||
|
||||
static {
|
||||
try {
|
||||
version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
|
||||
packetType = Class.forName(getPacketPlayOutChat());
|
||||
Class<?> typeCraftPlayer = Class.forName(getCraftPlayerClasspath());
|
||||
Class<?> typeNMSPlayer = Class.forName(getNMSPlayerClasspath());
|
||||
Class<?> typePlayerConnection = Class.forName(getPlayerConnectionClasspath());
|
||||
nmsChatSerializer = Class.forName(getChatSerializerClasspath());
|
||||
nmsIChatBaseComponent = Class.forName(getIChatBaseComponentClasspath());
|
||||
getHandle = typeCraftPlayer.getMethod("getHandle");
|
||||
playerConnection = typeNMSPlayer.getField("playerConnection");
|
||||
sendPacket = typePlayerConnection.getMethod("sendPacket", Class.forName(getPacketClasspath()));
|
||||
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | NoSuchFieldException ex) {
|
||||
Bukkit.getLogger().log(Level.SEVERE, "Error {0}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static void send(Player receivingPacket, String msg) {
|
||||
try {
|
||||
Object serialized = nmsChatSerializer.getMethod("a", String.class).invoke(null, "{\"text\": \"" + ChatColor.translateAlternateColorCodes('&', JSONObject.escape(msg)) + "\"}");
|
||||
if (!version.contains("1_7")) {
|
||||
packet = packetType.getConstructor(nmsIChatBaseComponent, byte.class).newInstance(serialized, (byte) 2);
|
||||
} else {
|
||||
packet = packetType.getConstructor(nmsIChatBaseComponent, int.class).newInstance(serialized, 2);
|
||||
}
|
||||
Object player = getHandle.invoke(receivingPacket);
|
||||
Object connection = playerConnection.get(player);
|
||||
sendPacket.invoke(connection, packet);
|
||||
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException ex) {
|
||||
Bukkit.getLogger().log(Level.SEVERE, "Error {0} " + version, ex);
|
||||
}
|
||||
|
||||
try {
|
||||
Object player = getHandle.invoke(receivingPacket);
|
||||
Object connection = playerConnection.get(player);
|
||||
sendPacket.invoke(connection, packet);
|
||||
} catch (SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
|
||||
Bukkit.getLogger().log(Level.SEVERE, "Error {0}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static String getCraftPlayerClasspath() {
|
||||
return "org.bukkit.craftbukkit." + version + ".entity.CraftPlayer";
|
||||
}
|
||||
|
||||
private static String getPlayerConnectionClasspath() {
|
||||
return "net.minecraft.server." + version + ".PlayerConnection";
|
||||
}
|
||||
|
||||
private static String getNMSPlayerClasspath() {
|
||||
return "net.minecraft.server." + version + ".EntityPlayer";
|
||||
}
|
||||
|
||||
private static String getPacketClasspath() {
|
||||
return "net.minecraft.server." + version + ".Packet";
|
||||
}
|
||||
|
||||
private static String getIChatBaseComponentClasspath() {
|
||||
return "net.minecraft.server." + version + ".IChatBaseComponent";
|
||||
}
|
||||
|
||||
private static String getChatSerializerClasspath() {
|
||||
if(version.equals("v1_8_R1") || version.contains("1_7")){
|
||||
return "net.minecraft.server." + version + ".ChatSerializer";
|
||||
} else {
|
||||
return "net.minecraft.server." + version + ".IChatBaseComponent$ChatSerializer"; // 1_8_R2 moved to IChatBaseComponent
|
||||
}
|
||||
}
|
||||
|
||||
private static String getPacketPlayOutChat() {
|
||||
return "net.minecraft.server." + version + ".PacketPlayOutChat";
|
||||
}
|
||||
}
|
||||
115
src/main/java/com/bekvon/bukkit/residence/utils/DataBackup.java
Normal file
115
src/main/java/com/bekvon/bukkit/residence/utils/DataBackup.java
Normal file
@@ -0,0 +1,115 @@
|
||||
package com.bekvon.bukkit.residence.utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.zip.Deflater;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import org.bukkit.World;
|
||||
|
||||
import com.bekvon.bukkit.residence.Residence;
|
||||
|
||||
public class DataBackup {
|
||||
private File BackupDir = new File(Residence.getDataLocation(), "Backup");
|
||||
|
||||
public static void run() throws IOException {
|
||||
DataBackup backup = new DataBackup();
|
||||
backup.backup();
|
||||
}
|
||||
|
||||
public void backup() throws IOException {
|
||||
try {
|
||||
BackupDir.mkdir();
|
||||
Date date = new Date();
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
|
||||
File fileZip = new File(BackupDir, dateFormat.format(date) + ".zip");
|
||||
|
||||
// Create the Source List, and add directories/etc to the file.
|
||||
List<File> sources = new ArrayList<File>();
|
||||
|
||||
File saveFolder = new File(Residence.getDataLocation(), "Save");
|
||||
File worldFolder = new File(saveFolder, "Worlds");
|
||||
if (!saveFolder.isDirectory()) {
|
||||
return;
|
||||
}
|
||||
File saveFile;
|
||||
for (World world : Residence.getServ().getWorlds()) {
|
||||
saveFile = new File(worldFolder, "res_" + world.getName() + ".yml");
|
||||
if (saveFile.isFile()) {
|
||||
sources.add(saveFile);
|
||||
}
|
||||
}
|
||||
packZip(fileZip, sources);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void packZip(File output, List<File> sources) throws IOException {
|
||||
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(output));
|
||||
zipOut.setLevel(Deflater.DEFAULT_COMPRESSION);
|
||||
|
||||
for (File source : sources) {
|
||||
if (source.isDirectory()) {
|
||||
zipDir(zipOut, "", source);
|
||||
} else {
|
||||
zipFile(zipOut, "", source);
|
||||
}
|
||||
}
|
||||
|
||||
zipOut.flush();
|
||||
zipOut.close();
|
||||
}
|
||||
|
||||
private String buildPath(String path, String file) {
|
||||
if (path == null || path.isEmpty()) {
|
||||
return file;
|
||||
}
|
||||
|
||||
return path + File.separator + file;
|
||||
}
|
||||
|
||||
private void zipDir(ZipOutputStream zos, String path, File dir) throws IOException {
|
||||
if (!dir.canRead()) {
|
||||
return;
|
||||
}
|
||||
|
||||
File[] files = dir.listFiles();
|
||||
path = buildPath(path, dir.getName());
|
||||
|
||||
for (File source : files) {
|
||||
if (source.isDirectory()) {
|
||||
zipDir(zos, path, source);
|
||||
} else {
|
||||
zipFile(zos, path, source);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void zipFile(ZipOutputStream zos, String path, File file) throws IOException {
|
||||
if (!file.canRead()) {
|
||||
return;
|
||||
}
|
||||
|
||||
zos.putNextEntry(new ZipEntry(buildPath(path, file.getName())));
|
||||
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
byte[] buffer = new byte[4092];
|
||||
int byteCount = 0;
|
||||
|
||||
while ((byteCount = fis.read(buffer)) != -1) {
|
||||
zos.write(buffer, 0, byteCount);
|
||||
}
|
||||
|
||||
fis.close();
|
||||
zos.closeEntry();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package com.bekvon.bukkit.residence.utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
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/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 (IOException e) {
|
||||
plugin.getLogger().warning("版本更新检查失败!");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user