How do I add Multiverse support?

Discussion in 'Plugin Development' started by ocomobock, May 6, 2013.

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

    ocomobock

    Well, I have a working plugin except for that it doesn't work when you're not in the default world. Does anyone know how to add support for multiple worlds? Here's my code:

    Code:java
    1. package tntwand;
    2.  
    3. import java.util.HashMap;
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.GameMode;
    8. import org.bukkit.Location;
    9. import org.bukkit.Material;
    10. import org.bukkit.World;
    11. import org.bukkit.entity.Fireball;
    12. import org.bukkit.entity.Item;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.EventHandler;
    15. import org.bukkit.event.EventPriority;
    16. import org.bukkit.event.Listener;
    17. import org.bukkit.event.block.Action;
    18. import org.bukkit.event.player.PlayerInteractEvent;
    19. import org.bukkit.inventory.ItemStack;
    20. import org.bukkit.plugin.PluginDescriptionFile;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22.  
    23. public class TNTWand extends JavaPlugin implements Listener{
    24.  
    25. public void onEnable()
    26. {
    27. getLogger().info("onEnable has been invoked!");
    28. getServer().getPluginManager().registerEvents(this, this);
    29. }
    30.  
    31. public boolean hasItem(Player p, ItemStack i){
    32. ItemStack[] inv = p.getInventory().getContents();
    33. for(ItemStack item:inv)
    34. {
    35. if(item.getType().equals(i.getType()))
    36. {
    37. return true;
    38. }
    39. }
    40. return false;
    41. }
    42.  
    43. @EventHandler(priority=EventPriority.HIGH)
    44. public void onPlayerUse(PlayerInteractEvent event)
    45. {
    46. Player p = event.getPlayer();
    47. PlayerInteractEvent e = event;
    48. World w = getServer().getWorlds().get(0);
    49. if (event.getClickedBlock().getType() == Material.TNT && p.getItemInHand().getType() == Material.WOOD_HOE && event.getAction() == Action.RIGHT_CLICK_BLOCK && (p.isOp() || p.hasPermission("tntwand.use")))
    50. {
    51. Location loc = new Location(w, e.getClickedBlock().getX(), e.getClickedBlock().getY(), e.getClickedBlock().getZ());
    52. boolean foundTnt = true;
    53. int counter = loc.getBlockY();
    54. if (hasItem(p, new ItemStack(Material.TNT)))
    55. {
    56. while(foundTnt)
    57. {
    58. counter--;
    59. Location newloc = new Location(getServer().getWorlds().get(0), e.getClickedBlock().getX(), counter, e.getClickedBlock().getZ());
    60. if (newloc.getBlock().getTypeId() != 46)
    61. {
    62. foundTnt = false;
    63. w.getBlockAt(newloc).setTypeId(Material.TNT.getId());
    64. if (p.getGameMode() != GameMode.CREATIVE)
    65. {
    66. p.getInventory().removeItem(new ItemStack(Material.TNT, 1));
    67. p.updateInventory();
    68. }
    69. }
    70. }
    71. }
    72. }
    73. }
    74. }


    This plugin basically makes it so when you right click Tnt with a wooden hoe, it takes Tnt from your inventory and puts another block of Tnt at the bottom. So it's like your building downwards sort of.

    Also, this plugin is constantly spamming "java.lang.NullPointerException". The plugin works fine, but it keeps spamming that and it's annoying. Here's a log:

    Code:
    2013-05-06 10:39:56 [SEVERE] Could not pass event PlayerInteractEvent to TNTWand v1.0.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    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 org.bukkit.craftbukkit.v1_5_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:185)
    at org.bukkit.craftbukkit.v1_5_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:155)
    at net.minecraft.server.v1_5_R3.PlayerConnection.a(PlayerConnection.java:1005)
    at net.minecraft.server.v1_5_R3.Packet18ArmAnimation.handle(SourceFile:41)
    at net.minecraft.server.v1_5_R3.NetworkManager.b(NetworkManager.java:292)
    at net.minecraft.server.v1_5_R3.PlayerConnection.d(PlayerConnection.java:110)
    at net.minecraft.server.v1_5_R3.ServerConnection.b(SourceFile:35)
    at net.minecraft.server.v1_5_R3.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:581)
    at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:226)
    at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:477)
    at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:410)
    at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.NullPointerException
    at tntwand.TNTWand.onPlayerUse(TNTWand.java:49)
    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:425)
    ... 16 more
    2013-05-06 10:39:57 [SEVERE] Could not pass event PlayerInteractEvent to TNTWand v1.0.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    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 org.bukkit.craftbukkit.v1_5_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:185)
    at org.bukkit.craftbukkit.v1_5_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:155)
    at net.minecraft.server.v1_5_R3.PlayerConnection.a(PlayerConnection.java:1005)
    at net.minecraft.server.v1_5_R3.Packet18ArmAnimation.handle(SourceFile:41)
    at net.minecraft.server.v1_5_R3.NetworkManager.b(NetworkManager.java:292)
    at net.minecraft.server.v1_5_R3.PlayerConnection.d(PlayerConnection.java:110)
    at net.minecraft.server.v1_5_R3.ServerConnection.b(SourceFile:35)
    at net.minecraft.server.v1_5_R3.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:581)
    at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:226)
    at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:477)
    at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:410)
    at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.NullPointerException
    at tntwand.TNTWand.onPlayerUse(TNTWand.java:49)
    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:425)
    ... 16 more
     
  2. World w = getServer().getWorlds().get(0);
    w needs to be event.getPlayer().getWorld()

    and all getServer().getWorlds().get(0) need to be replaced by w
     
  3. Offline

    ocomobock

    Thank you, that worked :)

    Now I just need to figure out how to make this plugin stop spamming the console.
     
  4. Offline

    Seadragon91

    The clicked block can be null and the item in the hand of the player can be null. Make a null check for them.
     
  5. Offline

    ocomobock

    Thanks, that worked too. I'll mark this as solved

    Actually, I don't think that solved it. This is what I used:

    Code:java
    1. if (event.getClickedBlock().getType() == Material.TNT && event.getClickedBlock() != null && p.getItemInHand().getType() == Material.WOOD_HOE && p.getItemInHand() != null && event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getAction() != null && (p.isOp() || p.hasPermission("tntwand.use")))


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
Thread Status:
Not open for further replies.

Share This Page