Solved Plugin works but there is a ERROR IN THE CONSLE!

Discussion in 'Plugin Development' started by phantomcut3, Jan 8, 2014.

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

    phantomcut3

    This is the plugin and i removed all the little details so nobody copies it.
    the plugin works but when i place a block thats not in the block changing cycle i get a error msg.


    @EventHandler
    public void onInteract(PlayerInteractEvent e) {
    Player p = e.getPlayer();
    if(events.containsKey(p)) {
    if (events.get(p) == 1) {
    if (p.getInventory().getItemInHand() == null) {
    e.setCancelled(true);
    }
    if (p.getInventory().getItemInHand().getType() == Material.) {
    if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
    events.remove(p);

    }
    }
    }
    }
    else {
    if(!events.containsKey(p)) {
    if (p.getInventory().getItemInHand() == null) {
    e.setCancelled(true);
    }
    if (p.getInventory().getItemInHand().getType() == Material.) {
    if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    events.put(p, 1);
    }
    }
    }
    }
    }





    @EventHandler
    public void BlockPlace(BlockPlaceEvent e) {
    Material block = e.getBlock().getType();
    Player p = e.getPlayer();
    if (events.get(p) == 1) {
    for(Material blocked : banList) {
    if(blocked == block) {
    e.getBlock().setType(Material.AIR);
    e.setCancelled(true);;
    }
    e.getBlock().setType(Material.); {
    blockPlaces++;
    if(blockPlaces >= 10){
    blockPlaces = 0;
    events.remove(p);
    }




















    So the error msg is this


    [09:02:39 ERROR]: Could not pass event BlockPlaceEvent to ButterEater v1
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:481) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:466) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactory.callBlockPlaceEvent(CraftEventFactory.java:113) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.ItemBlock.processBlockPlace(ItemBlock.java:88) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.ItemBlock.interactWith(ItemBlock.java:72) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.ItemStack.placeItem(ItemStack.java:78) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.PlayerInteractManager.interact(PlayerInteractManager.java:390) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:628) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.a(SourceFile:60) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.handle(SourceFile:9) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    Caused by: java.lang.NullPointerException
    at me.OMGUFB.ButterEater.Main.BlockPlace(Main.java:86) ~[?:?]
    at sun.reflect.GeneratedMethodAccessor13.invoke(Unknown Source) ~[?:?]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[?:1.6.0_65]
    at java.lang.reflect.Method.invoke(Method.java:597) ~[?:1.6.0_65]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    ... 18 more
    >


    Thanks!
     
  2. Offline

    AmShaegar

    Which line is line 86 of your Main class? There is a Null Pointer Exception.
     
  3. Offline

    phantomcut3

  4. Offline

    AmShaegar

    Well, then it's obvious, that
    Code:
    events == null
     
  5. Offline

    NoLiver92

    phantomcut3 For future note: your coding text is tooo small and hard to read, please use the coding blocks or make it bigger and clear.
     
  6. Offline

    phantomcut3

    @amShaeger
    kk so the hash map "Events" is null so how would i go about fixing that

    AmShaegar

    Ok so I add events == null

    where do i add that in the coding?

    NoLiver92

    Where should i put

    events == null

    and sorry about the code being small if you can't read
    i can re post it

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  7. Offline

    NoLiver92


    Its not for you to put in your code!!!! The (i assume) HashMap called events is null e.g the value you are getting does not exist in the hashmap.

    How much experience do you have in coding java and bukkit?
     
  8. Offline

    phantomcut3

    NoLiver92

    I apologize for i am not as experienced as you, and I still need to learn.
    So if i may ask how to i make the value exist?
     
  9. Offline

    NoLiver92

    phantomcut3
    Ok firstly, before i can answer i need to know what the events hashmap is doing in your code.

    Looking at your code snipit above all i can ever see is a value of 1 which you are checking for. what are you using the hashmap to do in your plugin?
     
  10. Offline

    Ytry

    Making the error 'go away' would be as simple as
    Code:java
    1. if ( events != null )
    2. {
    3. // code goes here
    4. }

    However I am not sure that is what you want because that error is telling you that the value is null. So any code you put in that if statement will never be run unless you make the hash map not null. I can't offer any insight as to how to do this unless you provide more information such as what you are using it for.
     
  11. Offline

    phantomcut3

    NoLiver92
    Ytry


    I am basically using hashMaps, so if this event dose not have the key don't do it. and it gets the key by completing the first event. But i am sure the code will make more sense then me.
    Here is the code:


    Code:
    public class Main extends JavaPlugin implements Listener {
     
        public static Material[] banList = {Material.DRAGON_EGG};
     
        public HashMap<Player, Integer> events = new HashMap<Player, Integer>();
     
        public int blockPlaces = 0;
        public int blockBroke = 0;
     
     
                @Override
        public void onEnable() {
            Bukkit.getServer().getLogger().info("ButterEater" + this.getDescription().getVersion() + " has been enabled!");
            PluginManager pm = Bukkit.getPluginManager();
            pm.registerEvents(this, this);
        }
     
     
     
        @Override
          public void onDisable() {
                        Bukkit.getServer().getLogger().info("ButterEater" + this.getDescription().getVersion() + " has been disabled!");
                }
     
     
     
     
    @EventHandler
    public void onInteract(PlayerInteractEvent e) {
    Player p = e.getPlayer();
    if(events.containsKey(p)) {
    if (events.get(p) == 1) {
    if (p.getInventory().getItemInHand() == null) {
        e.setCancelled(true);
    }
    if (p.getInventory().getItemInHand().getType() == Material.) {
    if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
    events.remove(p);
     
    }
    }
    }                 
    }
    else {
    if(!events.containsKey(p)) {
    if (p.getInventory().getItemInHand() == null) {
            e.setCancelled(true);
    }
    if (p.getInventory().getItemInHand().getType() == Material.) {
    if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    events.put(p, 1);
    }
    }
    }
    }
    }
     
     
     
     
               
    @EventHandler
    public void BlockPlace(BlockPlaceEvent e) {
    Material block = e.getBlock().getType();
    Player p = e.getPlayer();
    if (events.get(p) == 1) {
    for(Material blocked : banList) {
            if(blocked == block) {
                        e.getBlock().setType(Material.AIR);
                        e.setCancelled(true);;
            }
    e.getBlock().setType(Material.); {
    if(blockPlaces >= 10){
    blockPlaces = 0; 
    events.remove(p);
    }
     
    }
    }
    }
    }
    
     
  12. Offline

    NoLiver92

    phantomcut3 Summary: you want to make sure that a player can only complete the events in a certain order?

    for example:
    clicked a certain block -> place 1 block

    but you dont want the player to click twice or place a block twice

    is this correct?
     
  13. Offline

    phantomcut3

    NoLiver92

    Yes they need to be completed in a certain order and when the hash map cycle is completed players are removed from the has map. So they can complete the cycle again. To answer the second part, they should not be able to click on the same thing twice, (lets say they could either click on a apple or a carrot, and clicking the apple would active the events and clicking the carrot would cancel the events, but you couldn't use the apple twice because you we need to use the carrot to cancel the event first). They can infact place a block twice, as you can see in the code (code B), until
    they use place the block x times (in this case 10) then they are removed for the Hashmaps so the second event will do nothing for them unless they activate the first event as you can see in (code A)


    Code A
    Code:
    if(blockPlaces >= 10){
    blockPlaces = 0;
    events.remove(p)
    Code B
    Code:
    @EventHandler
    public void BlockPlace(BlockPlaceEvent e) {
    Material block = e.getBlock().getType();
    Player p = e.getPlayer();
    if (events.get(p) == 1) {
    for(Material blocked : banList) {
            if(blocked == block) {
                        e.getBlock().setType(Material.AIR);
                        e.setCancelled(true);;
            }
    e.getBlock().setType(Material.); {
    if(blockPlaces >= 10){
    blockPlaces = 0;
    events.remove(p);
     
  14. Offline

    NoLiver92

    ok then can i suggest this hashmap config: HashMap<String, Integer>

    the string is the players name and the integer is the event.

    apple would put the value 1 under the players name and carrot would place 2.

    then when getting the value: events.get(playername), and this will give you the event number, then to clear them set their name to null or set a value like 0
     
  15. Offline

    phantomcut3

    NoLiver92

    kk thanks i am going to rewrite it

    @NoLiver92

    So if i wanted to add a key to them i would do

    Code:
    events.put(p.getDisplayName(), 1);


    And then to remove them I would do

    Code:
    events.remove(p.getDisplayName());


    and instead of

    (events.containsKey(p())

    I would do?

    (events.containsKey(p.getDisplayName()))

    ?

    NoLiver92

    For some reasons the tag didn't work in the post above

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  16. Offline

    beastman3226

    Holy Jesus! Do none of you know what you are doing? It is quite obvious what is going wrong, the entry 'p.getName()' does not exist. I would use p.getName() when adding anything to any generic (HashMap, HashSet, ArrayList, etc) because it is final and will never change. To fix your problem check to see if the entry exists or not by using
    Code:java
    1. if (HashMap.contains(p.getName())){//other code logic
    2. }

    To add just use the put method and to return the integer value use the get method. Very truthfully generics are a bit tricky especially to someone who has no idea what they are doing.
    I would suggest reading Head First Java (or any book in the Head First series) it is very short and can be read extremely quickly especially for anyone who is interested in programming.
     
  17. Offline

    NoLiver92

    phantomcut3 no p.getName() always, never do display name as it can be changed by other plugins, the players "name" is the username they login with and will never change. but the principle is correct.

    Also do not use player in the hashmap use string and put in the players name.

    beastman3226 Can you be any more condescending to someone who is trying to learn? yes he made a mistake and should use something else but that comment you made is not needed.
     
  18. Offline

    beastman3226

    NoLiver92
    I could be much more condescending actually. It gets annoying answering the same questions over and over and over again. Especially when a simple google search on "nullpointerexception", "hashmaps", "could not pass event", or even "bukkit plugin making" would have yielded the same answer and he probably would have learned more that way. Also since you were so quick to jump on me for being condescending you didn't notice that I already said that it is very improper to use the player's display name or custom name.
     
  19. Offline

    phantomcut3

    NoLiver92

    What should i do for


    Code:
    events.remove(p);
    events.remove(p.getDisplayName());

    or

    events.put(p.getDisplayName(), 0)

    @NoLiver20

    You said to make (carrot 2) and (apple 1) but it needs to be able to reset so why not make it when you use carrot if your value is 1 make it 0 and when you use apple if your value is 0 set it to 1

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  20. Offline

    NoLiver92

    phantomcut3

    use player.getName NOT player.getDispalyName

    and i was using it as an example, you set it to what you want...
     
  21. Offline

    phantomcut3

    NoLiver92

    Lol ik i am being a real pain XD

    so change all player.getDisplayName to p.getName

    Also should I use events.put(p.getName(), 0); or events.remove(p.getName());
     
  22. Offline

    NoLiver92

    phantomcut3
    yes to first question

    depends, if you remove them you need to check if they are in the hashmap BEFORE you get the value, if you set to 0 then you need to make sure they are in the hashmap when they logged in (playerloginevent)
     
  23. Offline

    phantomcut3

    NoLiver92
    ok so remove sounds easier

    @NoLiver92

    OMG yes it worked thank you
    wasn't working after i replaced it to
    events.remove(p.getName());

    but then i listened to what you said and added

    if(events.containsKey(p.getName())) {

    and it worked!

    Thank YOU SO MUCH!

    You are a miracle worker!

    Is there a way I can repay by liking something or following?

    NoLiver92

    Read the post above!!!!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  24. Offline

    NoLiver92

    phantomcut3 Awesome glad you got it working. I dont mind what you do, im just here to help people and solve my own programming problems
     
Thread Status:
Not open for further replies.

Share This Page