Reset World on command

Discussion in 'Plugin Development' started by MiBB3L, May 30, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    MiBB3L

    So I want a command, that resets my world on a command. For example I type the command "reset" all players get kicked and the world called "world" gets deleted and a new world called "world" gets created.
     
  2. Offline

    DJCowGaming

    How far back do you plan on making this be able to be used? You could use Bukkit.unloadWorld and set the boolean parameter to false, where it would not save the world.
     
  3. Offline

    MiBB3L

    Could you give me an exmaple please? I´m not quiet sure what you mean.
     
  4. Offline

    caderapee

    @MiBB3L
    - Teleport all the players in the world into another world.
    - Use Bukkit#unloadWorld for unload the world
    - Remove the world
    - wait some seconds then recreate the world, set the spawn

    A world can take some times to remove, be sure the method unloadWorld return true and the world folder has been removed.

    If tyou want the same world, use the seed for recreate the world
     
  5. Offline

    MiBB3L

    Could you please tell me how to unload, remove and create a world? I have no idea how to do that.
     
  6. Offline

    KarimAKL

    @MiBB3L I've never tried this before but, i'd imagine something like this should work:
    Code:Java
    1. World world = ...; // The world you want to reset
    2. String worldName = world.getName(); // The world name
    3. long worldSeed = world.getSeed(); // The world seed
    4.  
    5. if (!Bukkit.unloadWorld(world, false)) return; // unload the world, return if not successful
    6.  
    7. File worldFolder = new File(plugin.getDataFolder().getParentFile().getParentFile(), worldName); // World folder
    8. worldFolder.delete(); // Delete world folder
    9.  
    10. Bukkit.createWorld(new WorldCreator(worldName).seed(worldSeed)); // Create the world
     
  7. Offline

    MiBB3L

    Thank you for you effort! This is my full Command Class. I get an error on "File worldFolder = new File(plugin.getDataFolder().getParentFile().getParentFile(), worldName);" It can´t find "plugin". I also had to change your "return" after unload world to "return false".
    Code:
    public class ResetCommand implements CommandExecutor{
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player p = (Player) sender;
       
    
                FileConfiguration config = main.getPlugin().getConfig();
                World worldtp = Bukkit.getWorld(config.getString("Spawn.World"));
                double x = config.getDouble("Spawn.X");
                double y = config.getDouble("Spawn.Y");
                double z = config.getDouble("Spawn.Z");
                float yaw = (float) config.getDouble("Spawn.Yaw");
                float pitch = (float) config.getDouble("Spawn.Pitch");
                Location location = new Location(worldtp, x, y, z, yaw, pitch);
                World world = p.getWorld(); // The world you want to reset
                for(Player player : Bukkit.getOnlinePlayers()){
                    player.teleport(location);
                }
               
                String worldName = world.getName(); // The world name
                
                if (!Bukkit.unloadWorld(world, false)) return false; // unload the world, return if not successful
                
                File worldFolder = new File(plugin.getDataFolder().getParentFile().getParentFile(), worldName); // World folder
                worldFolder.delete(); // Delete world folder
                
                Bukkit.createWorld(new WorldCreator(worldName)); // Create the world
           
            return false;
        }
       
       
    }
     
  8. Offline

    KarimAKL

    @MiBB3L The "return" was more of a "return something, or nothing if void" kind of example.

    'plugin' is your main class (the one that extends JavaPlugin) instance, you can pass it through the command class' constructor.
     
  9. Offline

    MiBB3L

    What is wrong?
    This is my error:
    null
    org.bukkit.command.CommandException: Unhandled exception executing command 'reset' in plugin MiBB3L_Funny v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_241]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_241]
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.Error: Unresolved compilation problem:
    plugin cannot be resolved

    at me.MiBB3L.funny.ResetCommand.onCommand(ResetCommand.java:42) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    ... 15 more


    This is my code:

    Code:
    package me.MiBB3L.funny;
    
    import java.io.File;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.WorldCreator;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    
    
    
    public class ResetCommand implements CommandExecutor{
    
        private main plugin;
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player p = (Player) sender;
          
          
    
                FileConfiguration config = main.getPlugin().getConfig();
                World worldtp = Bukkit.getWorld(config.getString("Spawn.World"));
                double x = config.getDouble("Spawn.X");
                double y = config.getDouble("Spawn.Y");
                double z = config.getDouble("Spawn.Z");
                float yaw = (float) config.getDouble("Spawn.Yaw");
                float pitch = (float) config.getDouble("Spawn.Pitch");
                Location location = new Location(worldtp, x, y, z, yaw, pitch);
                World world = p.getWorld(); // The world you want to reset
                for(Player player : Bukkit.getOnlinePlayers()){
                    player.teleport(location);
                }
              
                String worldName = world.getName(); // The world name
               
                if (!Bukkit.unloadWorld(world, false)) return false;  // unload the world, return if not successful
               
                File worldFolder = new File(plugin.getDataFolder().getParentFile().getParentFile(), worldName); // World folder
                worldFolder.delete(); // Delete world folder
               
                Bukkit.createWorld(new WorldCreator(worldName)); // Create the world
          
            return false;
        }
      
      
    }
    
     
  10. Offline

    KarimAKL

Thread Status:
Not open for further replies.

Share This Page