Delete world on player death

Discussion in 'Spigot Plugin Development' started by TheItchyTasty, May 25, 2015.

  1. Sorry if this has been answered somewhere else but i could not find anything on it.
    I am trying to make a 'hardcore' map on my server that copies a map with a dynamic name (player name) and deletes it on player death. I have managed to get most of it working using multiverve and variabletriggers but i run into troubles with the on death events in variable triggers and deleting worlds in multiverse.
    I am decent in making mods and have just recently moved into working with plugins so i decided to try and make a plugin for deleting the copied world on player death. Any help would be appreciated! :)
     
  2. Handle PlayerDeathEvent, get the Player from the event, get the world from the player's name, and unload then delete it. You'll need to get the player out of the world first of course.

    Some of this could be done using Multiverse's API I'm sure.
     
    • Informative Informative x 1
  3. Thanks for the quick response! Now i have a place to start looking, will check out the multiverse API and fiddle around some more! WOOT!
     
  4. (You can even do this purely with Bukkit)
     
    • Informative Informative x 1
  5. Using Multiverse will let your main thread stop for 100-600 ms. Load your maps async.
    You should also use the players UUID and not his name ;)

    Deleting the map is no problem:

    @EventHandler
    public void playerDeath(PlayerDeathEvent e){
    World w = e.getPlayer().getWorld();
    if(w.getName().equalsIgnoreCase(e.getPlayer().getUniqueId().toString()){
    e.getPlayer().teleport(Bukkit.getWorlds().get(0).getSpawnLocation());
    Bukkit.unloadWorld(w);
    }
    }

    Hope this helps :)
     
    • Informative Informative x 1
  6. Will mess around with these for sure! Thanks for the help everyone!
     
  7. Ah forgot: You have to delete the world's folder of course!

    new File(plugin.getPluginDir().getParentFile().getParentFile(), p.getUniqueId().toString()).delete();
     
  8. Indeed, good catch. Also, it might not be necessary to get the parent file twice. I'll try to look around for a more direct alternative to getting the world folder and update this post.

    EDIT: Perhaps not then. You could go with the getParentFolder().getParentFolder() method or something like
    Code (Text):
    String[] containingFolders = getDataFolder().getAbsolutePath().split(File.separator);
    String finalFolder = "";
    for (int i = 0; i < containingFolders.length - 2; i++) // This might not be entirely correct if there is a final separator like C:\Users\ vs. C:\Users
    {
        finalFolder += containingFolders[i] + File.separator;
    }
    I'd recommend the former honestly, string manipulation is just ugly for something like this.
     
    #8 ZanderMan9, May 25, 2015
    Last edited: May 25, 2015
  9. I know. Thats why i wrote it is decompiler copy-pasted. It is supposed to work, not to be nice :D
     
  10. Learn how concurrency works first then :p. I can load the world twice, or get in a deadlock if the world already exists and I try to load it again (and that's just the first 20 lines or so)
     
  11. :D it didn't really matter to me. I just copy pasted code out of one of my plugins that hosts multiple minigames on one server and never loads a map twice. You can of course create a patch for this :) For me it works without any issues yet, thats why i never bothered to really look at the code and ask myself whats unsafe :D