Solved Kicking all players and Restarting server

Discussion in 'Plugin Development' started by ThatGuyWhoDied13, Nov 25, 2013.

Thread Status:
Not open for further replies.
  1. Hey I was wondering how would I go about restarting my server on command but kicking all players before restarting with the message
    Code:java
    1. ("" + ChatColor.DARK_PURPLE + ChatColor.BOLD + "Kevy" + ChatColor.GREEN + ChatColor.BOLD + "&" + ChatColor.GOLD + ChatColor.BOLD + "Azza's" + ChatColor.YELLOW + " Server Restarting Now!")

    here's my code so far:
    Code:java
    1. if (commandLabel.equalsIgnoreCase("restartserver")) {
    2. if (!player.isOp()) {
    3. player.sendMessage("You don't have permission to do that!");
    4. }else{
    5. if (player.isOp()) {
    6. //kick all players
    7.  
    8. //restart server
    9. }
    10. }
    11. }
     
  2. Offline

    TeeePeee

    Kicking everyone is easy:
    Code:java
    1. for(Player player : Bukkit.getOnlinePlayers()) {
    2. player.kickPlayer(ChatColor.RED+"Derp'd");
    3. }

    Restarting the server is a bit more challenging. It can't actually be done without modifying a launcher bat file or using some crazy reflection. Check out this thread on restarting.
     
  3. TeeePeee Thanks. I was more wondering on how to do the restarting than the kicking but I thought i'd throw it in there to give an explanation on why I was doing it
     
  4. Offline

    HGPDev

    Code:java
    1. if (commandLabel.equalsIgnoreCase("restartserver")) {
    2. if (!player.isOp()) {
    3. // Kick all players
    4.  
    5. // Restart
    6. }else{
    7. player.sendMessage("You don't have permission to do that!");
    8.  
    9. }
    10. }
    11. }


    This is good.
     
  5. Offline

    jimbo8

    That is not possible without changing your batch file/whatever you are using.

    The best thing you can do is to stop the server with a simple line, i don't remember right now, though.
     
  6. Offline

    HGPDev

    Code:java
    1. if (commandLabel.equalsIgnoreCase("restartserver")) {
    2. if (!player.isOp()) {
    3. for(Player player : Bukkit.getOnlinePlayers()) {
    4. player.kickPlayer(ChatColor.RED + "Kicked!");
    5. }
    6.  
    7. // Restart
    8. }else{
    9. player.sendMessage("You don't have permission to do that!");
    10.  
    11. }
    12. }
    13. }


    For the restart you need a restart loop in your .bat file

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  7. HGPDev and how does one do that
     
  8. Offline

    HGPDev

    Script for your .bat:
    Code:
    :begin
     
    "c:\Program Files\Java\Jre7\bin\java.exe" -Xms1024M -Xmx2048M -jar craftbukkit.jar
     
    goto begin
     
  9. Offline

    HGPDev

    KevyPorter No problem. Just tahg me if you need help
     
  10. HGPDev Wait do I put this in my main .bat
     
  11. Offline

    HGPDev

    KevyPorter You start your server with a .bat file and you need to paste the code i gave you in the .bat file.
     
  12. HGPDev below the stuff I already have?

    e.g.

    java -Xmx1024M -jar craftbukkit.jar -o true
    PAUSE
    :begin
    "c:\Program Files\Java\Jre7\bin\java.exe" -Xms1024M -Xmx2048M -jar craftbukkit.jar
    goto begin
     
  13. Offline

    HGPDev

    KevyPorter No just replace the hole code. So remove your PAUSE and your java -Xmx1024M -jar craftbukkit.jar -o true
     
  14. HGPDev It infinitely loops now
     
  15. Offline

    HGPDev

    KevyPorter True, just get the code form your code in the jar file and the jar file have to run the code and the normal server just runs on the code that you already had. You just have to make a new .bat file and get the .bat file in you .jar file and make it so it runs the .bat file you just created. you leave the old .bat as it is. and you run your server with your old .bat and the code gets the new one.

    Not clear? Read: THIS
     
  16. Offline

    1Rogue


    Because you just made an infinite loop. It'd be a lot cleaner to add a shutdown hook for running a timed script to be honest, and have the restart script have a configurable location.

    Code:java
    1. String f = /*string of file location*/;
    2. Runtime.getRuntime().addShutdownHook(new Thread() {
    3. public void run() {
    4. Runtime.getRuntime().exec(f);
    5. }
    6. }


    From there, your script should have a 5-10 second delay on starting up. You could even use this with the startup script.

    You should only hook this if that restart command is used, otherwise you'll end up with an infinite loop again.
     
    HGPDev likes this.
  17. Offline

    HGPDev

    1Rogue Thanks for helping me explain.. Its a bit hard to explain but you did good!
     
  18. 1Rogue Thanks, I'll try that.

    HGPDev I figured out another way:
    @ ECHO OFF (Without the space)
    SET BINDIR=%~dp0
    CD /D "%BINDIR%"
    :start
    "%ProgramFiles%\Java\jre6\bin\java.exe" -Xmx1024M -Xms1024M -jar craftbukkit.jar
    goto start
     
  19. Offline

    1Rogue


    The whole point is this will loop whether you are restarting or just stopping in general, don't do that.
     
  20. Offline

    HGPDev

  21. Offline

    1Rogue

    There shouldn't be an infinite loop regardless.
     
  22. Offline

    HGPDev

  23. 1Rogue But I want my server to be 24/7 so if it crashes it will restart and I will be able to restart it on demand.

    Should I just reload instead
     
  24. Offline

    1Rogue


    If your server is crashing then obviously you have other issues to look into, it shouldn't be just randomly crashing...
     
  25. 1Rogue Not only if it crashes, It's only crashed once and that was when I was BETA testing a plugin
     
  26. Offline

    HGPDev

    KevyPorter If your testing a plugin, most of the time you look in console what the error is? or something like that.
     
  27. HGPDev I do but if I'm testing my hub plugin and I made a brutal mistake and the server crashes I want it to restart

    And I don't understand this (I do understand it but not all of it)
    Code:java
    1. String f = /*string of file location*/;
    2. Runtime.getRuntime().addShutdownHook(new Thread() {
    3. public void run() {
    4. Runtime.getRuntime().exec(f);
    5. }
    6. }
     
  28. Offline

    HGPDev

    KevyPorter I Think @1Roguecan explain that better, I have a guess but i dont know for sure

    @KevyPorter I Think 1Rogue can explain that better, I have a guess but i dont know for sure

    PS: I cant edit my posts, doesn anyone have that too?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  29. Offline

    moose517

    wouldn't it be easier to just use remotetoolkit? same thing basically?
     
Thread Status:
Not open for further replies.

Share This Page