Solved WorldEdit api weird error

Discussion in 'Plugin Development' started by supercas240, Sep 6, 2018.

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

    supercas240

    I#m using the wordedit api to spawn in schematics, but i'm getting a weird error that i've never seen before:
    [​IMG]

    basically the error leads back to the point where I call the class, so It's not really telling me what's wrong in the class:
    Code:
    package com.supercas240.RaidEvents;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.plugin.Plugin;
    
    
    import com.sk89q.worldedit.CuboidClipboard;
    import com.sk89q.worldedit.EditSession;
    import com.sk89q.worldedit.MaxChangedBlocksException;
    import com.sk89q.worldedit.Vector;
    import com.sk89q.worldedit.bukkit.BukkitWorld;
    import com.sk89q.worldedit.bukkit.WorldEditPlugin;
    import com.sk89q.worldedit.schematic.MCEditSchematicFormat;
    import com.sk89q.worldedit.world.DataException;
    
    
    
    @SuppressWarnings("deprecation")
    public class CreateEvent {
    
       
        Logger Logger = Bukkit.getLogger();
        public Location loc = Bukkit.getServer().getWorld("world_raidevents").getSpawnLocation();
       
       
       
       
       
       
      public WorldEditPlugin getWorldEdit() {
            Plugin WorldEdit = Bukkit.getServer().getPluginManager().getPlugin("WorldEdit");
            if (WorldEdit instanceof WorldEditPlugin) return (WorldEditPlugin) WorldEdit;
            else return null;
    }
     
        public void schematic(){
       
            File schematic = new File("Worldedit/schematics/test.schematic");
            EditSession session = getWorldEdit().getWorldEdit().getEditSessionFactory().getEditSession(new BukkitWorld(loc.getWorld()), 999999999);
           
             try
                {
                  CuboidClipboard clipboard = MCEditSchematicFormat.getFormat(schematic).load(schematic);
    
                  clipboard.paste(session, new Vector(loc.getX(), loc.getY(), loc.getZ()), false);
                }
                catch (MaxChangedBlocksException|DataException|IOException e)
                {
                  e.printStackTrace();
                }
           
        }
    
    }
    
    so I'm a bit clueless as to what's going on as i'm pretty sure this should work. maybe a problem with the api?
     
  2. Offline

    timtower Administrator Administrator Moderator

    @supercas240 Did you depend on Worldedit in your plugin.yml?
     
  3. Offline

    supercas240

    -_- ffs, yep thx that did it...

    now i'm here
    i'getting a nullpointerexeption on:
    Code:
        public Location loc = Bukkit.getServer().getWorld("world_raidevents").getSpawnLocation();
    that seems odd to me
     
    Last edited: Sep 6, 2018
  4. Offline

    timtower Administrator Administrator Moderator

    @supercas240 That line probably gets called before the onEnable, put the initialization in the onEnable and you will have less issues.
    World is not loaded yet.
     
  5. Offline

    supercas240

    hmm, doesn't seem to do the trick. I probably should've explained this better, My bad. Okay so I have a class that checks if a certain world exists, if not it generates one. this is called in the onEnable. then I have another class that uses some information from that world ( like the spawnlocation ) and some other stuff. this class gets called on a command. so I'm pretty sure that world has loaded by then. but indeed I'm getting an error on startup from that class that only fires on command.

    I tried loading the Location after the worldexistscheck in the onenable but that didn't fix it.
     
  6. Offline

    timtower Administrator Administrator Moderator

    @supercas240 Post the error and the class that is causing it.
     
  7. Offline

    supercas240

    Okay so:
    [​IMG]

    Is being caused by this class:
    Code:
    package com.supercas240.RaidEvents;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.plugin.Plugin;
    
    import com.sk89q.worldedit.CuboidClipboard;
    import com.sk89q.worldedit.EditSession;
    import com.sk89q.worldedit.MaxChangedBlocksException;
    import com.sk89q.worldedit.Vector;
    import com.sk89q.worldedit.bukkit.BukkitWorld;
    import com.sk89q.worldedit.bukkit.WorldEditPlugin;
    import com.sk89q.worldedit.schematic.MCEditSchematicFormat;
    import com.sk89q.worldedit.world.DataException;
    
    @SuppressWarnings("deprecation")
    public class CreateEvent {
    
        Logger Logger = Bukkit.getLogger();
        public Location loc = Bukkit.getServer().getWorld("world_raidevents").getSpawnLocation();
    
        public WorldEditPlugin getWorldEdit() {
            Plugin WorldEdit = Bukkit.getServer().getPluginManager().getPlugin("WorldEdit");
            if (WorldEdit instanceof WorldEditPlugin)
                return (WorldEditPlugin) WorldEdit;
            else
                return null;
        }
    
        public void schematic() {
    
            File schematic = new File("Worldedit/schematics/test.schematic");
            EditSession session = getWorldEdit().getWorldEdit().getEditSessionFactory()
                    .getEditSession(new BukkitWorld(loc.getWorld()), 999999999);
    
            try {
                CuboidClipboard clipboard = MCEditSchematicFormat.getFormat(schematic).load(schematic);
    
                clipboard.paste(session, new Vector(loc.getX(), loc.getY(), loc.getZ()), false);
            } catch (MaxChangedBlocksException | DataException | IOException e) {
                e.printStackTrace();
            }
    
        }
    
    }
    
    world_raidevents is being create in the onenalbe here:
    Code:
    package com.supercas240.RaidEvents;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.World;
    import org.bukkit.WorldCreator;
    import org.bukkit.WorldType;
    
    public class worldGeneration {
    
        Logger Logger = Bukkit.getLogger();
        public String RaidEvents = "world_raidevents";
        public World world = Bukkit.getServer().getWorld(RaidEvents);
        WorldCreator creator = WorldCreator.name(RaidEvents).type(WorldType.FLAT);
    
        public void generate() {
            if (world == null) {
    
                Bukkit.getServer().createWorld(creator);
                world.setSpawnLocation(0, 1, 0);
    
                Logger.info("RaidEvents world has been created.");
    
            } else {
    
                Logger.info("RaidEvents world already exists.");
            }
    
        }
    
    }
    
     
  8. Offline

    timtower Administrator Administrator Moderator

    @supercas240 And your main class?
    Avoid using Bukkit outside the methods, you don't know the call order.
     
  9. Offline

    supercas240

    Main class:
    Code:
    package com.supercas240.RaidEvents;
    
    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import java.util.logging.Logger;
    
    public class Main extends JavaPlugin implements Listener {
        Logger Logger = Bukkit.getLogger();
        private Commands commands = new Commands();
    
        public void onEnable() {
    
            getConfig().options().copyDefaults(true);
            saveConfig();
    
            getCommand(commands.cmd1).setExecutor(commands);
    
            worldGeneration wg = new worldGeneration();
            wg.generate();
    
            Logger.info("RaidEvents enabled succesfully.");
        }
    
        public void onDisable() {
            Logger.info("RaidEvents disabled succesfully.");
        }
    
    }
    
    commands class:
    Code:
    package com.supercas240.RaidEvents;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    
    import net.minecraft.server.v1_8_R1.CommandExecute;
    
    public class Commands extends CommandExecute implements Listener, CommandExecutor {
        public String cmd1 = "raidevents";
        public String cmd2 = "random";
        public World world = Bukkit.getServer().getWorld("world_RaidEvents");
    
        CreateEvent create = new CreateEvent();
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender.hasPermission("raidevents.use")) {
                if (cmd.getName().equalsIgnoreCase(cmd1)) {
                    if (args.length > 0) {
                        if (args[0].equalsIgnoreCase(cmd2)) { // starts a raid event with a randomly picked design
                            // stuff
    
                            create.schematic();
    
                            Location loc = Bukkit.getServer().getWorld("world_raidevents").getSpawnLocation();
                            Player player = (Player) sender;
                            player.teleport(loc);
    
                            return true;
                        } else
                            return false;
                    } else {
                        // code for when command is issued without arguments
                        return true;
                    }
                }
            }
    
            if (sender.hasPermission("raidevents.play")) {
                if (cmd.getName().equalsIgnoreCase("raidevents")) {
                    if (args.length > 0) {
                        if (args[0].equalsIgnoreCase("join")) { // join a raidevent with your faction
                            // stuff
    
                            return true;
                        } else if (args[1].equalsIgnoreCase("leave")) { // leave a raidevent
                            // stuff
    
                            return true;
                        } else
                            return false;
                    } else {
                        // code for when command is issued without arguments
                        return true;
                    }
                }
            }
            return false;
        }
    }
     
  10. Offline

    timtower Administrator Administrator Moderator

    @supercas240 Code order it is.
    Initialize commands in the onEnable
     
  11. Offline

    supercas240

    thought I was?

    Code:
            getCommand(commands.cmd1).setExecutor(commands);
     
  12. Offline

    timtower Administrator Administrator Moderator

  13. Offline

    supercas240

    right gotcha, that didn't resolve the error though:

    [​IMG]
     
  14. Offline

    timtower Administrator Administrator Moderator

  15. Offline

    supercas240

    same as before

    onenable 19 referse to my worldgeneration class,
    worldgeneration 22 is:
    Code:
    world.setSpawnLocation(0, 1, 0);
    of that newly generated world
     
  16. Offline

    timtower Administrator Administrator Moderator

    @supercas240 Full class, not single lines please.
    Because I have no idea when world gets initialized.
     
  17. Offline

    supercas240

    all the classes are in the posts above...

    worldgeneration:
    Code:
    package com.supercas240.RaidEvents;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.World;
    import org.bukkit.WorldCreator;
    import org.bukkit.WorldType;
    
    public class worldGeneration {
    
        Logger Logger = Bukkit.getLogger();
        public String RaidEvents = "world_raidevents";
        public World world = Bukkit.getServer().getWorld(RaidEvents);
        WorldCreator creator = WorldCreator.name(RaidEvents).type(WorldType.FLAT);
    
        public void generate() {
            if (world == null) {
    
                Bukkit.getServer().createWorld(creator);
                world.setSpawnLocation(0, 1, 0);
    
                Logger.info("RaidEvents world has been created.");
    
            } else {
    
                Logger.info("RaidEvents world already exists.");
            }
    
        }
    
    }
     
  18. Offline

    timtower Administrator Administrator Moderator

    @supercas240 world is still null after you generate a world.
    Value doesn't magically change.
    Would assume that createWorld returns a World, use it.
     
  19. Offline

    supercas240

    well yes see, i'm checking if "world_raidevents" exists, if not, I create a world_raidevents

    but you're right, calling this world again inside the statement worked, thx
     
Thread Status:
Not open for further replies.

Share This Page