Solved PlayerRespawnEvent Help

Discussion in 'Plugin Development' started by Theodossis, Sep 28, 2012.

Thread Status:
Not open for further replies.
  1. I got this code:
    Code:
    @EventHandler
    public void onRespawn(PlayerRespawnEvent e) {
    int len = Bukkit.getServer().getOnlinePlayers().lenght;
    int x = this.getConfig().getInt("Spawns.spawn" + len + ".x");
    int y = this.getConfig().getInt("Spawns.spawn" + len + ".y");
    int z = this.getConfig().getInt("Spawns.spawn" + len + ".z");
    String world = this.getConfig().getString("Spawns.spawn" + len + ".world");
    e.setRespawnLocation(new Location(Bukkit.getServer().getWorld(world), x, y, z));
    }
    But on respawn it teleports me to x: 0 y:64 z:0 while i have setted the spawns....(The "len" works cause i am using it on other event in my plugin)
    Please help!
     
  2. Are you sure you did set the Spawns.spawn(len).x, y and z? because i don't see any real problems with your code.
     
  3. .length is spelled wrong.
     
    kroltan and MCForger like this.
  4. pfffs lol, we all missed that xD
     
    krazytraynz likes this.
  5. That's all my event:
    Code:
    @EventHandler
        public void onRespawn(PlayerRespawnEvent e) {
            Player p = e.getPlayer();
            if (getConfig().getBoolean("Players." + p.getName() + ".VIP") == true) {
                int len = Bukkit.getServer().getOnlinePlayers().length;
                int x = this.getConfig().getInt("Spawns.spawn" + len + ".x");
                int y = this.getConfig().getInt("Spawns.spawn" + len + ".y");
                int z = this.getConfig().getInt("Spawns.spawn" + len + ".z");
                e.setRespawnLocation(new Location(Bukkit.getServer().getWorld("TheoWorld"), x, y, z));
                p.getInventory().addItem(new ItemStack(Material.BOW, 1));
                p.getInventory().addItem(new ItemStack(Material.ARROW, 64));
                p.getInventory().addItem(new ItemStack(Material.ARROW, 64));
                p.getInventory().addItem(new ItemStack(Material.EGG, 1));
                p.getInventory().addItem(new ItemStack(Material.SNOW_BALL, 2));
                p.getInventory().setHelmet(new ItemStack(Material.LEATHER_HELMET));
                p.getInventory().setChestplate(new ItemStack(Material.LEATHER_CHESTPLATE));
                p.getInventory().setLeggings(new ItemStack(Material.LEATHER_LEGGINGS));
                p.getInventory().setBoots(new ItemStack(Material.LEATHER_BOOTS));
            }
            if (getConfig().getBoolean("Players." + p.getName() + ".VIP") == false) {
                int len = Bukkit.getServer().getOnlinePlayers().length;
                int x = this.getConfig().getInt("Spawns.spawn" + len + ".x");
                int y = this.getConfig().getInt("Spawns.spawn" + len + ".y");
                int z = this.getConfig().getInt("Spawns.spawn" + len + ".z");
                e.setRespawnLocation(new Location(Bukkit.getServer().getWorld("TheoWorld"), x, y, z));
                p.getInventory().addItem(new ItemStack(Material.BOW, 1));
                p.getInventory().addItem(new ItemStack(Material.ARROW, 64));
            }
        }
     
  6. So if im not mistaking, you have different x, y, z coords for every different amounth of players?
     
  7. Yes, but it still teleports me to x: 0 y:64 z:0
     
  8. Which are the coordinates of? what amounth of players?
    Have you tried printing the output of x, y and z?
     
  9. This is my error:
    Code:
    2012-09-30 16:41:27 [SEVERE] Could not pass event PlayerRespawnEvent to CoDMod
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:341)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
        at net.minecraft.server.ServerConfigurationManagerAbstract.moveToWorld(ServerConfigurationManagerAbstract.java:363)
        at net.minecraft.server.ServerConfigurationManagerAbstract.moveToWorld(ServerConfigurationManagerAbstract.java:321)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:1076)
        at net.minecraft.server.Packet205ClientCommand.handle(SourceFile:30)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:276)
        at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:111)
        at net.minecraft.server.ServerConnection.b(SourceFile:35)
        at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:578)
        at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:213)
        at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:474)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:406)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.NullPointerException
        at org.bukkit.craftbukkit.CraftServer.getWorld(CraftServer.java:791)
        at me.Theodossis.CoDMod.CoDMod.onRespawn(CoDMod.java:237)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:339)
        ... 16 more
    This is code:
    Code:
    @EventHandler
        public void onRespawn(PlayerRespawnEvent e) {
            Player p = e.getPlayer();
            if (getConfig().getBoolean("Players." + p.getName() + ".VIP") == true) {
                p.getInventory().addItem(new ItemStack(Material.BOW, 1));
                p.getInventory().addItem(new ItemStack(Material.ARROW, 64));
                p.getInventory().addItem(new ItemStack(Material.ARROW, 64));
                p.getInventory().addItem(new ItemStack(Material.EGG, 1));
                p.getInventory().addItem(new ItemStack(Material.SNOW_BALL, 2));
                p.getInventory().setHelmet(new ItemStack(Material.LEATHER_HELMET));
                p.getInventory().setChestplate(new ItemStack(Material.LEATHER_CHESTPLATE));
                p.getInventory().setLeggings(new ItemStack(Material.LEATHER_LEGGINGS));
                p.getInventory().setBoots(new ItemStack(Material.LEATHER_BOOTS));
                int len = Bukkit.getServer().getOnlinePlayers().length;
                int x = this.getConfig().getInt("Spawns.spawn" + len + ".x");
                int y = this.getConfig().getInt("Spawns.spawn" + len + ".y");
                int z = this.getConfig().getInt("Spawns.spawn" + len + ".z");
                String world = this.getConfig().getString("Spawns.spawn" + len + ".world");
                Location loc = new Location(Bukkit.getServer().getWorld(world), x, y, z);
                System.out.println(loc);
            }
            if (getConfig().getBoolean("Players." + p.getName() + ".VIP") == false) {
                p.getInventory().addItem(new ItemStack(Material.BOW, 1));
                p.getInventory().addItem(new ItemStack(Material.ARROW, 64));
                int len = Bukkit.getServer().getOnlinePlayers().length;
                int x = this.getConfig().getInt("Spawns.spawn" + len + ".x");
                int y = this.getConfig().getInt("Spawns.spawn" + len + ".y");
                int z = this.getConfig().getInt("Spawns.spawn" + len + ".z");
                String world = this.getConfig().getString("Spawns.spawn" + len + ".world");
                Location loc = new Location(Bukkit.getServer().getWorld(world), x, y, z);
                System.out.println(loc);
            }
        }
     
  10. If you replace the second IF with else, its much more efficient.
    Otherwise, it looks into IF 1, lets say its true.
    It sets the data to that corresponding if, and continues
    Reads through the next if -> false but it still reads it.
    When you place else, it skips everything under the first match, so if the first if is true, it just stops.
    Makes it faster.

    Anyways, any chance you can post your config?
     
  11. Here is my config:
    Code:
    Spawns:
      spawn1:
        x: 156
        y: 75
        z: 260
        world: TheoWorld
      spawn2:
        x: 149
        y: 72
        z: 261
        world: TheoWorld
      spawn3:
        x: 157
        y: 74
        z: 262
        world: TheoWorld
    
     
  12. Try outputting the value of len to the console and see what you get.
     
  13. Offline

    FTWinston

    Code:
    Caused by: java.lang.NullPointerException
        at org.bukkit.craftbukkit.CraftServer.getWorld(CraftServer.java:791)
        at me.Theodossis.CoDMod.CoDMod.onRespawn(CoDMod.java:237)
    The crash is caused by what you pass to the getWorld function. It's probably null, because for whatever reason, you're trying to get the name of an invalid config. Print out the name of the config you're trying to get, to see what's going wrong with that.
     
  14. Haha i fixed it :p
     
  15. Offline

    Shockwave317

    How because I have been turning my head inside out on how to set-up multiple spawns for 2 groups of people! I could get 1 spawn set then the other brings up a error...
     
Thread Status:
Not open for further replies.

Share This Page