From 44020ecc2575b94ffa727ab2ea1a1a35de74f387 Mon Sep 17 00:00:00 2001 From: Prototik Date: Sun, 14 Jun 2015 19:50:18 +0700 Subject: [PATCH] Support to restart server after update --- .../network/NetHandlerPlayServer.java.patch | 2 +- src/main/java/kcauldron/KCauldron.java | 34 ++++++++++++------- .../kcauldron/KCauldronClassTransformer.java | 12 ++----- src/main/java/kcauldron/KCauldronCommand.java | 15 ++++++-- src/main/java/kcauldron/KCauldronConfig.java | 5 ++- .../kcauldron/updater/KCauldronUpdater.java | 6 ++++ .../java/org/spigotmc/RestartCommand.java | 12 +++++-- 7 files changed, 56 insertions(+), 30 deletions(-) diff --git a/patches/net/minecraft/network/NetHandlerPlayServer.java.patch b/patches/net/minecraft/network/NetHandlerPlayServer.java.patch index 0ab4821..d5b5bdf 100644 --- a/patches/net/minecraft/network/NetHandlerPlayServer.java.patch +++ b/patches/net/minecraft/network/NetHandlerPlayServer.java.patch @@ -1795,7 +1795,7 @@ { packetbuffer = new PacketBuffer(Unpooled.wrappedBuffer(p_147349_1_.func_149558_e())); @@ -1093,16 +2398,18 @@ - { + if (itemstack.getItem() == Items.writable_book && itemstack.getItem() == itemstack1.getItem()) { - itemstack1.setTagInfo("pages", itemstack.getTagCompound().getTagList("pages", 8)); diff --git a/src/main/java/kcauldron/KCauldron.java b/src/main/java/kcauldron/KCauldron.java index 1b0eeea..7ce2921 100644 --- a/src/main/java/kcauldron/KCauldron.java +++ b/src/main/java/kcauldron/KCauldron.java @@ -6,13 +6,16 @@ import java.net.URLDecoder; import java.util.Enumeration; import java.util.Properties; +import org.spigotmc.RestartCommand; + public class KCauldron { private static boolean sManifestParsed = false; - + private static void parseManifest() { - if (sManifestParsed) return; + if (sManifestParsed) + return; sManifestParsed = true; - + try { Enumeration resources = KCauldron.class.getClassLoader() .getResources("META-INF/MANIFEST.MF"); @@ -23,10 +26,11 @@ public class KCauldron { String version = manifest.getProperty("KCauldron-Version"); if (version != null) { String path = url.getPath(); - String jarFilePath = path.substring(path.indexOf(":") + 1, path.indexOf("!")); - jarFilePath = URLDecoder.decode(jarFilePath, "UTF-8"); - sServerLocation = new File(jarFilePath); - + String jarFilePath = path.substring(path.indexOf(":") + 1, + path.indexOf("!")); + jarFilePath = URLDecoder.decode(jarFilePath, "UTF-8"); + sServerLocation = new File(jarFilePath); + sCurrentVersion = version; sBranch = manifest.getProperty("KCauldron-Branch"); sChannel = manifest.getProperty("KCauldron-Channel"); @@ -38,7 +42,7 @@ public class KCauldron { } } - + private static String sCurrentVersion; public static String getCurrentVersion() { @@ -52,22 +56,26 @@ public class KCauldron { parseManifest(); return sServerLocation; } - + private static String sBranch; - + public static String getBranch() { parseManifest(); return sBranch; } - + private static String sChannel; - + public static String getChannel() { parseManifest(); return sChannel; } - + public static File sNewServerLocation; public static String sNewServerVersion; public static boolean sUpdateInProgress; + + public static void restart() { + RestartCommand.restart(true); + } } diff --git a/src/main/java/kcauldron/KCauldronClassTransformer.java b/src/main/java/kcauldron/KCauldronClassTransformer.java index 78e29ce..5a9e7f4 100644 --- a/src/main/java/kcauldron/KCauldronClassTransformer.java +++ b/src/main/java/kcauldron/KCauldronClassTransformer.java @@ -1,5 +1,7 @@ package kcauldron; +import static org.objectweb.asm.Opcodes.*; + import org.apache.logging.log4j.Level; import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassVisitor; @@ -11,19 +13,9 @@ import org.objectweb.asm.tree.InsnList; import org.objectweb.asm.tree.InsnNode; import org.objectweb.asm.tree.IntInsnNode; -import pw.prok.imagine.api.Pair; import pw.prok.imagine.asm.ImagineASM; -import pw.prok.imagine.asm.ImagineMethod; -import pw.prok.imagine.asm.ImagineRemapper; import pw.prok.imagine.asm.Transformer; - -import com.avaje.ebean.annotation.Transactional; - import cpw.mods.fml.common.FMLLog; -import net.minecraft.launchwrapper.IClassTransformer; -import net.minecraft.world.WorldServer; -import net.minecraft.world.chunk.IChunkProvider; -import static org.objectweb.asm.Opcodes.*; @Transformer.RegisterTransformer public class KCauldronClassTransformer implements Transformer { diff --git a/src/main/java/kcauldron/KCauldronCommand.java b/src/main/java/kcauldron/KCauldronCommand.java index c489ba9..94663e5 100644 --- a/src/main/java/kcauldron/KCauldronCommand.java +++ b/src/main/java/kcauldron/KCauldronCommand.java @@ -9,16 +9,20 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandSender; public class KCauldronCommand extends Command { - public static final String NAME = "kcauldron"; + public static final String NAME = "kc"; public static final String CHECK = NAME + ".check"; public static final String UPDATE = NAME + ".update"; + public static final String RESTART = NAME + ".restart"; public KCauldronCommand() { super(NAME); StringBuilder builder = new StringBuilder(); - builder.append(String.format("/%s check\n", NAME)); - builder.append(String.format("/%s update [version]\n", NAME)); + builder.append(String.format("/%s check - Check to update\n", NAME)); + builder.append(String + .format("/%s update [version] - Update to specified or latest version\n", + NAME)); + builder.append(String.format("/%s restart - Restart server\n", NAME)); setUsage(builder.toString()); setPermission("kcauldron"); @@ -50,6 +54,7 @@ public class KCauldronCommand extends Command { return true; if (args.length == 0) { sender.sendMessage(ChatColor.YELLOW + "Please specify action"); + sender.sendMessage(ChatColor.AQUA + usageMessage); return true; } String action = args[0]; @@ -62,6 +67,10 @@ public class KCauldronCommand extends Command { } else if ("update".equals(action)) { KCauldronUpdater.initUpdate(sender, args.length > 1 ? args[1] : null); + } else if ("restart".equals(action)) { + if (!testPermission(sender, RESTART)) + return true; + KCauldron.restart(); } else { sender.sendMessage(ChatColor.RED + "Unknown action"); } diff --git a/src/main/java/kcauldron/KCauldronConfig.java b/src/main/java/kcauldron/KCauldronConfig.java index f491b70..2cdea4e 100644 --- a/src/main/java/kcauldron/KCauldronConfig.java +++ b/src/main/java/kcauldron/KCauldronConfig.java @@ -19,13 +19,15 @@ public class KCauldronConfig extends ConfigBase { "updatechecker.symlinks", "", "(Re)create symlinks after update"); public BoolSetting updatecheckerAutoinstall = new BoolSetting(this, "updatechecker.autoinstall", false, "Install updates without confirming"); + public BoolSetting updatecheckerAutorestart = new BoolSetting(this, + "updatechecker.autorestart", false, "Restart server after updating without confirming (set restart script in spigot.yml)"); public BoolSetting updatecheckerQuite = new BoolSetting(this, "updatechecker.quite", false, "Print less info during update"); public StringSetting updatecheckerInstallAs = new StringSetting(this, "updatechecker.installAs", "", "Install new version with specified name"); public BoolSetting loggingMaterialInjection = new BoolSetting(this, - "loggin.materialInjection", false, "Log material injection event"); + "logging.materialInjection", false, "Log material injection event"); public KCauldronConfig() { super("kcauldron.yml", "kc"); @@ -34,6 +36,7 @@ public class KCauldronConfig extends ConfigBase { register(updatecheckerDeleteOld); register(updatecheckerSymlinks); register(updatecheckerAutoinstall); + register(updatecheckerAutorestart); register(updatecheckerQuite); register(updatecheckerInstallAs); register(loggingMaterialInjection); diff --git a/src/main/java/kcauldron/updater/KCauldronUpdater.java b/src/main/java/kcauldron/updater/KCauldronUpdater.java index d826e21..52ec042 100644 --- a/src/main/java/kcauldron/updater/KCauldronUpdater.java +++ b/src/main/java/kcauldron/updater/KCauldronUpdater.java @@ -152,6 +152,12 @@ public class KCauldronUpdater implements Runnable { } KCauldron.sNewServerLocation = newPath; KCauldron.sNewServerVersion = mVersion; + if (MinecraftServer.kcauldronConfig.updatecheckerAutorestart.getValue()) { + if (mSender != null) { + mSender.sendMessage(ChatColor.DARK_RED + "Initiate server restart"); + } + KCauldron.restart(); + } } catch (Exception e) { if (!quite) { e.printStackTrace(); diff --git a/src/main/java/org/spigotmc/RestartCommand.java b/src/main/java/org/spigotmc/RestartCommand.java index d46f369..9016a52 100644 --- a/src/main/java/org/spigotmc/RestartCommand.java +++ b/src/main/java/org/spigotmc/RestartCommand.java @@ -26,8 +26,12 @@ public class RestartCommand extends Command } return true; } + + public static void restart() { + restart(false); + } - public static void restart() + public static void restart(boolean forbidShutdown) { try { @@ -98,9 +102,13 @@ public class RestartCommand extends Command Runtime.getRuntime().addShutdownHook( shutdownHook ); } else { + if (forbidShutdown) { + System.out.println("Attempt to restart server without restart script, decline request"); + return; + } System.out.println( "Startup script '" + SpigotConfig.restartScript + "' does not exist! Stopping server." ); } - System.exit( 0 ); + cpw.mods.fml.common.FMLCommonHandler.instance().exitJava(0, false); } catch ( Exception ex ) { ex.printStackTrace();