Solved Load World by createWorld(): Keeps Resetting

Discussion in 'Plugin Development' started by ShreyP150, Jan 16, 2016.

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

    ShreyP150

    Hi -

    I'm a beginner with the Bukkit API, but I have lots of experience in programming and Java.

    I own a server, and I wanted to write custom plugins. I've been working on a Hub plugin that when enabled loads a previously built Hub into the game. (I'm doing the developing on a local server running on my Mac.) After reading up for an hour, I found that you use a WorldCreator with the name of your world and "load" that using createWorld. Yet every time the server loads up, it recreates the world entirely based on the seed. All Steve-made structures are gone.

    Things I've tried:


    - instead of getServer().createWorld(worldCreatorVar), worldCreatorVar.createWorld();
    - setting the type() and environment() of the WorldCreator
    - letting the WorldCreator create the world, then stopping and replacing the world

    Code:

    Code:
    package com.shreypandya.bukkit;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.WorldCreator;
    import org.bukkit.WorldType;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class BeamHub extends JavaPlugin {
     
        @Override
        public void onEnable() {
         
            getLogger().info("BeamHub enabled!");
         
            WorldCreator generator = new WorldCreator("Hub");
         
            generator.environment(World.Environment.NORMAL);
         
            generator.type(WorldType.NORMAL);
         
            getServer().createWorld(generator);
         
            new PlayerListener(this);
         
        }
     
        @Override
        public void onDisable() {
         
            getLogger().info("BeamHub disabled!");
         
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         
            if (cmd.getName().equalsIgnoreCase("hub") && sender instanceof Player) {
             
                Player player = (Player) sender;
             
                player.teleport(new Location(Bukkit.getWorld("Hub"), 56.57727567123509, 68.0, 59.54991278532413));
             
                return true;
             
            }
         
            return false;
         
        }
    
    }
    

    Yet to no avail. Am I using the wrong code, or is my world save messed up? Thanks in advance!

    EDIT by Timtower: removed unneeded world zip
     
    Last edited by a moderator: Jan 16, 2016
  2. Offline

    Zombie_Striker

    Bukkit already logs your plugin for you. You do not need this line (and as such, do not need onDisabled at all)

    First, it is most likely a bad idea to hardcode these values. Also you can change these lines to "56.5,68,59.5". There's no need to be that specific.

    https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Server.html#createWorld(org.bukkit.WorldCreator)
    Are you sure you're the world is being saved correctly? This link says the way you have it should work.

    [Edit] maybe try unloading the world first before the server shuts down. Maybe that will save the world.
     
  3. Offline

    ShreyP150

    Thanks for the help Zombie_Striker. Unloading before the server shuts down just saves the resetted world.

    Maybe the Hub folder has some corrupted or missing file that's making it reset?

    In which way could the world not be saved correctly? I had attached my save file, but a moderator removed it.

    [EDIT]: On second thought, I'm going to double check the world save to see if it's missing something.
     
    Last edited: Jan 16, 2016
  4. Offline

    Zombie_Striker

    @ShreyP150
    First, please use the "Tahg User" button to tag me (that way I get notified you responded).

    Maybe you should try deleting the world, letting the world auto generate again, and seeing if it saves correctly.
     
  5. Offline

    ShreyP150

    @Zombie_Striker Sorry about that, I'm new to the forums.

    One thing I did test was changing the level name in server.properties to Hub. That regenerated the world as well.

    Can you explain what you meant by delete and regenerate? Delete the save in code while the server's running, or manually? After the server's stopped?
     
  6. Offline

    Zombie_Striker

    @ShreyP150
    Once the server stops, delete the file for the world ("/Hub file")
     
  7. Offline

    ShreyP150

    @Zombie_Striker I just tried that, it just generated a brand new world with a new seed like I thought.

    Anyone else know what could be the problem?

    EDIT: I forgot to mention one thing: after resetting the world, the server doesn't reset the reset. It just uses that save. Maybe I should compare the reset to the original and/or use the new files, but copy in the old region files?
     
    Last edited: Jan 16, 2016
  8. Offline

    ShreyP150

    *Bump* As a followup to the previous edit, copying in the old region files didn't change a thing. I'm running out of ideas. Anyone know what the problem could be?
     
  9. Offline

    teej107

    @ShreyP150 This would annoy me too. To help solve the problem:
    1. No errors in the console right?
    2. May I see your PlayerListener class?
    3. Are there any other plugins you are using?
    4. What server and version are you using?
     
  10. Offline

    ShreyP150

    1. No errors in the console.
    2. I'm not at my computer right now, but I'll get my code to you soon. I don't think it would affect the loading of my world but I'll post it soon.
    3. No, I'm testing on a clean local server with no plugins.
    4. I'm using CraftBukkit 1.8.

    Should I upload my world save file? I can't right now, but if it would help then I can later.

    Thanks for helping!
     
  11. Online

    timtower Administrator Administrator Moderator

    World save wouldn't help us see anything.
     
  12. Offline

    ShreyP150

    @timtower Okay. Here's my PlayerListener class:

    Code:
    package com.shreypandya.bukkit;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.FoodLevelChangeEvent;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class PlayerListener implements Listener {
      
        Collection<? extends Player> onlinePlayers;
        IconMenu gizmos;
      
        public PlayerListener(BeamHub plugin) {
          
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
          
            onlinePlayers = plugin.getServer().getOnlinePlayers();
          
            //this is a call to another class
            gizmos = new IconMenu("Gizmos", 9, new IconMenu.OptionClickEventHandler() {
              
                @Override
                public void onOptionClick(IconMenu.OptionClickEvent event) {
                  
                    //this needs to be finished
                    event.getPlayer().sendMessage("You have chosen " + event.getName());
                    event.setWillClose(true);
                  
                }
            }, plugin)
            .setOption(1, new ItemStack(Material.DIAMOND_BARDING, 1), "Paintball Gun", "Shoot your friends with colorful magic.")
            .setOption(2, new ItemStack(Material.MONSTER_EGG, 1, (short) 98), "Kitty Cannon", "Meow meow BOOM!")
            .setOption(3, new ItemStack(Material.ARROW, 1), "Trails", "Leave a path as you go along.");
          
        }
      
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
          
            Player player = event.getPlayer();
          
          
            //Player join welcome messages
            if (player.hasPlayedBefore() == false) {
              
                for (Player playerI : onlinePlayers) {
                  
                    Location playerLocationI = playerI.getLocation();
                    playerI.getWorld().playSound(playerLocationI, Sound.FIREWORK_BLAST, 1, 0);
                  
                }
              
                Bukkit.broadcastMessage(ChatColor.DARK_AQUA + "Welcome, " + player.getName() + ", to THE BEAM!");
              
            } else {
          
                event.setJoinMessage(ChatColor.AQUA + player.getName() + " has joined The Beam");
          
            }
          
            //Teleport player to spawn
            player.teleport(new Location(Bukkit.getWorld("Hub"), 56.57727567123509, 68.0, 59.54991278532413));
          
            PlayerInventory inventory = player.getInventory();
          
            //Clear inventory
            inventory.clear();
          
            //Give hub items
            ItemStack gamePicker = new ItemStack(Material.COMPASS);
            ItemStack gizmos = new ItemStack(Material.CHEST);
            ItemStack about = new ItemStack(Material.BEACON);
            ItemStack leave = new ItemStack(Material.BED);
          
            setName(gamePicker, "Game Picker", "Right click to pick a game.");
            setName(gizmos, "Gadgets", "Right click to see your gizmos.");
            setName(about, "About BeamMC", "Right click to learn about BeamMC.");
            setName(leave, "Leave the Beam", "Right click to leave the Beam.");
          
            inventory.setItem(0, gamePicker);
            inventory.setItem(4, gizmos);
            inventory.setItem(7, about);
            inventory.setItem(8, leave);
          
            //set health/hunger
            player.setGameMode(GameMode.ADVENTURE);
            player.setHealth(20);
            player.setFoodLevel(20);
            player.setSaturation(20);
            player.setExhaustion(0);
              
        }
      
        @EventHandler
        public void onItemDrop(PlayerDropItemEvent event) {
          
            Player player = event.getPlayer();
          
            if (player.getWorld() == Bukkit.getWorld("Hub")) {
    
                event.setCancelled(true);
              
            }
          
        }
      
        @EventHandler
        public void onRightClick(PlayerInteractEvent event) {
          
            Player player = event.getPlayer();
          
            if (player.getInventory().getItemInHand().getType() == Material.COMPASS) {
              
                if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                  
                    player.sendMessage("Opening games...");
                  
                }
              
            } else if (player.getInventory().getItemInHand().getType() == Material.CHEST) {
              
                if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                  
                    gizmos.open(player);
                  
                }
              
            } else if (player.getInventory().getItemInHand().getType() == Material.BEACON) {
              
                if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                  
                    player.sendMessage("Owner/Programmer: Shrey Pandya");
                    player.sendMessage("Co-owner/Builder: Rishi Raman");
                  
                }
              
            } else if (player.getInventory().getItemInHand().getType() == Material.BED) {
              
                if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                  
                    Random random = new Random();
                  
                    int greeting = random.nextInt(10);
                  
                    switch (greeting) {
                  
                        case 1:
                            player.kickPlayer("Come again, " + player.getName() + "!");
                            break;
                          
                        case 2:
                            player.kickPlayer("See you soon, " + player.getName() + "!");
                            break;
                          
                        case 3:
                            player.kickPlayer("Bon voyage, " + player.getName() + "!");
                            break;
                      
                        case 4:
                            player.kickPlayer("Bye, " + player.getName() + "!");
                            break;
                          
                        case 5:
                            player.kickPlayer("I hate to see you leave, " + player.getName() + " :(");
                            break;
                          
                        case 6:
                            player.kickPlayer("Leaving? Why, " + player.getName() + "?");
                            break;
                          
                        case 7:
                            player.kickPlayer("I see what this is about, " + player.getName() + "! You don't like me?");
                            break;
                          
                        case 8:
                            player.kickPlayer("Please don't go " + player.getName() + "!");
                            break;
                          
                        case 9:
                            player.kickPlayer("Going so soon, " + player.getName() + "?");
                            break;
                          
                        case 10:
                            player.kickPlayer("Eh. Never liked you anyway, " + player.getName() + ".");
                            break;
                    }    
                  
                }
              
            } else {
              
                //break
              
            }
          
        }
      
        @EventHandler
        public void onFoodChange (FoodLevelChangeEvent event) {
          
            event.setCancelled(true);
          
        }
      
        @EventHandler
        public void onPlayerDamage (EntityDamageEvent event) {
          
            event.setCancelled(true);
          
        }
      
        public ItemStack setName (ItemStack item, String name, String lore) {
          
            ArrayList<String> loreList = new ArrayList<String>();
            loreList.add(lore);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(name);
            meta.setLore(loreList);
          
            item.setItemMeta(meta);
            return item;
          
        }
    
    }
    
     
  13. Offline

    teej107

    @ShreyP150 Just to test.
    Create your world and teleport there. Build a simple structure. Run /save-all. Restart the server. Check if the structure is there.
     
  14. Offline

    ShreyP150

    @teej107 Yep, the world doesn't get reset after the initial one, as I said here:

    It's just the first time I load in the world, it starts a reset. Which leads me to believe, as I have from the start, that there is something wrong with my world save file. @timtower says that world save wouldn't help you see anything, but I think that there is some file that doesn't belong or is corrupted. So I ask again @timtower: should the world file be uploaded?
     
  15. Offline

    teej107

    @ShreyP150 Well maybe the world is corrupted but uploading won't help us fix anything. This is just the same one "corrupted" world right?
     
  16. Offline

    ShreyP150

    @teej107 Yes, there's just one save file. Does anyone know a list of the files that should be present in a world or something? Or if there is something wrong with the save file, is there a way to import the world chunk by chunk, similar to the way Multiverse has the /import command?
     
  17. Offline

    teej107

    @ShreyP150 That's not what I meant. What I meant was: Is the world generated (not loaded but actually generated) from your code saving and not re-generating each time?

    This world that isn't saving, is that from somewhere else? eg: Single player world.
     
  18. Offline

    ShreyP150

    @teej107 It's a multiplayer server world, created on a multiplayer server. It's from another server; I'm testing my own plugin on a local server using that world.
     
  19. Offline

    teej107

    And do worlds generated from your local server stay?
     
  20. Offline

    ShreyP150

    Yes, when I delete the Hub world and let the server generate the world, and try to build and /save-all, the world stays. Again, there's a problem with the world save.

    [EDIT]: I have the 3 regular worlds (world, world_nether, and world_end), then I try to load ONLY one world, Hub.
     
    Last edited: Jan 18, 2016
  21. Offline

    ShreyP150

    *BUMP* Anyone know what's going on over here? Anyone who knows what a save file should look like, or how to "copy" a world file chunk by chunk to make the world load properly?
     
  22. Offline

    teej107

    @ShreyP150 Unless you want to look in how MC structures its worlds, I use the world generated from Bukkit since copying over a world is causing problems.
     
  23. Offline

    ShreyP150

    UPDATE: I finally fixed my problem by using a cheat way. I commented out MY world loader code, downloaded Multiverse, used /mvimport and /mvload to load in the Hub, then got rid of Multiverse and uncommented my world loader code. Multiverse did some tidying up and cleaned up the world save, I guess :)
     
  24. Offline

    Zombie_Striker

    @ShreyP150
    If your problem has been solved, mark this thread as solved.
     
  25. Offline

    ShreyP150

    @Zombie_Striker I was confused about how to do that. I looked prior to this and couldn't find a button anywhere. Can you tell me where it is?

    [EDIT:] Got it, never mind :p
     
Thread Status:
Not open for further replies.

Share This Page