Solved PlayerRespawnEvent Help

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

  1. I got this code:
    Code (Text):
    1. @EventHandler
    2. public void onRespawn(PlayerRespawnEvent e) {
    3. int len = Bukkit.getServer().getOnlinePlayers().lenght;
    4. int x = this.getConfig().getInt("Spawns.spawn" + len + ".x");
    5. int y = this.getConfig().getInt("Spawns.spawn" + len + ".y");
    6. int z = this.getConfig().getInt("Spawns.spawn" + len + ".z");
    7. String world = this.getConfig().getString("Spawns.spawn" + len + ".world");
    8. e.setRespawnLocation(new Location(Bukkit.getServer().getWorld(world), x, y, z));
    9. }
    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 (Text):
    1. @EventHandler
    2.     public void onRespawn(PlayerRespawnEvent e) {
    3.         Player p = e.getPlayer();
    4.         if (getConfig().getBoolean("Players." + p.getName() + ".VIP") == true) {
    5.             int len = Bukkit.getServer().getOnlinePlayers().length;
    6.             int x = this.getConfig().getInt("Spawns.spawn" + len + ".x");
    7.             int y = this.getConfig().getInt("Spawns.spawn" + len + ".y");
    8.             int z = this.getConfig().getInt("Spawns.spawn" + len + ".z");
    9.             e.setRespawnLocation(new Location(Bukkit.getServer().getWorld("TheoWorld"), x, y, z));
    10.             p.getInventory().addItem(new ItemStack(Material.BOW, 1));
    11.             p.getInventory().addItem(new ItemStack(Material.ARROW, 64));
    12.             p.getInventory().addItem(new ItemStack(Material.ARROW, 64));
    13.             p.getInventory().addItem(new ItemStack(Material.EGG, 1));
    14.             p.getInventory().addItem(new ItemStack(Material.SNOW_BALL, 2));
    15.             p.getInventory().setHelmet(new ItemStack(Material.LEATHER_HELMET));
    16.             p.getInventory().setChestplate(new ItemStack(Material.LEATHER_CHESTPLATE));
    17.             p.getInventory().setLeggings(new ItemStack(Material.LEATHER_LEGGINGS));
    18.             p.getInventory().setBoots(new ItemStack(Material.LEATHER_BOOTS));
    19.         }
    20.         if (getConfig().getBoolean("Players." + p.getName() + ".VIP") == false) {
    21.             int len = Bukkit.getServer().getOnlinePlayers().length;
    22.             int x = this.getConfig().getInt("Spawns.spawn" + len + ".x");
    23.             int y = this.getConfig().getInt("Spawns.spawn" + len + ".y");
    24.             int z = this.getConfig().getInt("Spawns.spawn" + len + ".z");
    25.             e.setRespawnLocation(new Location(Bukkit.getServer().getWorld("TheoWorld"), x, y, z));
    26.             p.getInventory().addItem(new ItemStack(Material.BOW, 1));
    27.             p.getInventory().addItem(new ItemStack(Material.ARROW, 64));
    28.         }
    29.     }
     
  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 (Text):
    1. 2012-09-30 16:41:27 [SEVERE] Could not pass event PlayerRespawnEvent to CoDMod
    2. org.bukkit.event.EventException
    3.     at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:341)
    4.     at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    5.     at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    6.     at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    7.     at net.minecraft.server.ServerConfigurationManagerAbstract.moveToWorld(ServerConfigurationManagerAbstract.java:363)
    8.     at net.minecraft.server.ServerConfigurationManagerAbstract.moveToWorld(ServerConfigurationManagerAbstract.java:321)
    9.     at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:1076)
    10.     at net.minecraft.server.Packet205ClientCommand.handle(SourceFile:30)
    11.     at net.minecraft.server.NetworkManager.b(NetworkManager.java:276)
    12.     at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:111)
    13.     at net.minecraft.server.ServerConnection.b(SourceFile:35)
    14.     at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
    15.     at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:578)
    16.     at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:213)
    17.     at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:474)
    18.     at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:406)
    19.     at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    20. Caused by: java.lang.NullPointerException
    21.     at org.bukkit.craftbukkit.CraftServer.getWorld(CraftServer.java:791)
    22.     at me.Theodossis.CoDMod.CoDMod.onRespawn(CoDMod.java:237)
    23.     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    24.     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    25.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    26.     at java.lang.reflect.Method.invoke(Unknown Source)
    27.     at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:339)
    28.     ... 16 more
    This is code:
    Code (Text):
    1. @EventHandler
    2.     public void onRespawn(PlayerRespawnEvent e) {
    3.         Player p = e.getPlayer();
    4.         if (getConfig().getBoolean("Players." + p.getName() + ".VIP") == true) {
    5.             p.getInventory().addItem(new ItemStack(Material.BOW, 1));
    6.             p.getInventory().addItem(new ItemStack(Material.ARROW, 64));
    7.             p.getInventory().addItem(new ItemStack(Material.ARROW, 64));
    8.             p.getInventory().addItem(new ItemStack(Material.EGG, 1));
    9.             p.getInventory().addItem(new ItemStack(Material.SNOW_BALL, 2));
    10.             p.getInventory().setHelmet(new ItemStack(Material.LEATHER_HELMET));
    11.             p.getInventory().setChestplate(new ItemStack(Material.LEATHER_CHESTPLATE));
    12.             p.getInventory().setLeggings(new ItemStack(Material.LEATHER_LEGGINGS));
    13.             p.getInventory().setBoots(new ItemStack(Material.LEATHER_BOOTS));
    14.             int len = Bukkit.getServer().getOnlinePlayers().length;
    15.             int x = this.getConfig().getInt("Spawns.spawn" + len + ".x");
    16.             int y = this.getConfig().getInt("Spawns.spawn" + len + ".y");
    17.             int z = this.getConfig().getInt("Spawns.spawn" + len + ".z");
    18.             String world = this.getConfig().getString("Spawns.spawn" + len + ".world");
    19.             Location loc = new Location(Bukkit.getServer().getWorld(world), x, y, z);
    20.             System.out.println(loc);
    21.         }
    22.         if (getConfig().getBoolean("Players." + p.getName() + ".VIP") == false) {
    23.             p.getInventory().addItem(new ItemStack(Material.BOW, 1));
    24.             p.getInventory().addItem(new ItemStack(Material.ARROW, 64));
    25.             int len = Bukkit.getServer().getOnlinePlayers().length;
    26.             int x = this.getConfig().getInt("Spawns.spawn" + len + ".x");
    27.             int y = this.getConfig().getInt("Spawns.spawn" + len + ".y");
    28.             int z = this.getConfig().getInt("Spawns.spawn" + len + ".z");
    29.             String world = this.getConfig().getString("Spawns.spawn" + len + ".world");
    30.             Location loc = new Location(Bukkit.getServer().getWorld(world), x, y, z);
    31.             System.out.println(loc);
    32.         }
    33.     }
     
  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 (Text):
    1. Spawns:
    2.   spawn1:
    3.     x: 156
    4.     y: 75
    5.     z: 260
    6.     world: TheoWorld
    7.   spawn2:
    8.     x: 149
    9.     y: 72
    10.     z: 261
    11.     world: TheoWorld
    12.   spawn3:
    13.     x: 157
    14.     y: 74
    15.     z: 262
    16.     world: TheoWorld
    17.  
     
  12. Try outputting the value of len to the console and see what you get.
     
  13. Offline

    FTWinston

    Code (Text):
    1. Caused by: java.lang.NullPointerException
    2.     at org.bukkit.craftbukkit.CraftServer.getWorld(CraftServer.java:791)
    3.     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...
     

Share This Page