Help with small plugin-Another error!

Discussion in 'Plugin Development' started by Zoroark173, Nov 30, 2012.

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

    Zoroark173

    First error is fixed thanks guys!
    Show Spoiler
    Hello everybody it is I the great Zoroark173, and I have finally moved from requesting plugins to making them! I know a very small amount of Java, and I'm trying my hand at making plugins. Using the tutorials at the bukkit wiki, and some different plugins source codes to try and make my own plugin. What this plugin will do is when you right-click with coal, then it turns into a diamond. I am most likely not going to make this a whole plugin, but I decided to do something small as my first plugin. But I have come across an across when trying to use the plugin! Here s the code! (I assume that the error is something to do with giving you the diamond) If anybody could tell me how to fix, that would be greatly appreciated!
    Code:
    package temporarily.hidden.for.security.purposes;
     
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
     
    public final class testplugin extends JavaPlugin implements Listener{
     
        public void onDisable() {
         
        }
        public void onEnable() {
                Bukkit.getPluginManager().registerEvents(this, this);
        } 
     
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
                Player player = event.getPlayer();
                if(event.getAction() == Action.RIGHT_CLICK_AIR ||
                                event.getAction() == Action.RIGHT_CLICK_BLOCK &&
                                player.getItemInHand().getType() == Material.COAL) {
                                              new ItemStack(Material.COAL, 1) ;
                                      player.getInventory().removeItem(new ItemStack[] {
                                              new ItemStack(Material.DIAMOND, 1) });
                                      player.getInventory().addItem( new ItemStack[1]);
                                        }else{
                                                event.setCancelled(true);
                                        }
                                }
             
        }
     
     
    


    Bahh, there is a second error! This time though, it's not actually an error ._. ... Whats wrong, is that with the new, longer code that I made, when you right click with a stone or dirt block in the air, it gives you diamonds, while when you right click using them on the ground, they give you their correct items, gold and bread respectively. I looked through my code and could not figure out what on earth was wrong, I'm thinking that it may have something to do with the fact that it checks if your clickng in the sky/on the ground first, but I really have no clue.

    CODE:
    Code:
    package com.gmail.zoroarkchidori;
     
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
     
    public final class basicalchemy extends JavaPlugin implements Listener{
     
        public void onDisable() {
     
        }
        public void onEnable() {
            Bukkit.getPluginManager().registerEvents(this, this);
        }   
     
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            if(event.getAction() == Action.RIGHT_CLICK_AIR ||
                    event.getAction() == Action.RIGHT_CLICK_BLOCK &&
                    player.getItemInHand().getType() == Material.COAL) {
                ItemStack coal = new ItemStack(Material.COAL, 64);
                ItemStack diamond = new ItemStack(Material.DIAMOND, 1);
                player.getInventory().removeItem(coal);
                player.getInventory().addItem(diamond);
                player.updateInventory();
            }
            else if
     
            (event.getAction() == Action.RIGHT_CLICK_AIR ||
            event.getAction() == Action.RIGHT_CLICK_BLOCK &&
            player.getItemInHand().getType() == Material.STONE) {
                ItemStack stone = new ItemStack(Material.STONE, 64);
                ItemStack gold = new ItemStack(Material.GOLD_INGOT, 1);
                player.getInventory().removeItem(stone);
                player.getInventory().addItem(gold);
                player.updateInventory();
            }
            else if
            (event.getAction() == Action.RIGHT_CLICK_AIR ||
            event.getAction() == Action.RIGHT_CLICK_BLOCK &&
            player.getItemInHand().getType() == Material.DIRT) {
                ItemStack dirt = new ItemStack(Material.DIRT, 64);
                ItemStack bread = new ItemStack(Material.BREAD, 1);
                player.getInventory().removeItem(dirt);
                player.getInventory().addItem(bread);
                player.updateInventory();
            }
        }
    }
     
     
     
    
     
  2. Offline

    Bryguy

    It would help to know what the error was...
     
  3. Offline

    Zoroark173

    Here, this is what my server logs say:
    Show Spoiler

    [SEVERE] Could not pass event PlayerInteractEvent to testplugin v0.1
    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 org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:177)
    at net.minecraft.server.ItemInWorldManager.interact(ItemInWorldManager.java:376)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:662)
    at net.minecraft.server.Packet15Place.handle(SourceFile:58)
    at net.minecraft.server.NetworkManager.b(NetworkManager.java:290)
    at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:113)
    at net.minecraft.server.ServerConnection.b(SourceFile:39)
    at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.MinecraftServer.r(MinecraftServer.java:595)
    at net.minecraft.server.DedicatedServer.r(DedicatedServer.java:222)
    at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:493)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:426)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:856)
    Caused by: java.lang.NullPointerException
    at org.bukkit.craftbukkit.inventory.CraftItemStack.<init>(CraftItemStack.java:27)
    at org.bukkit.craftbukkit.inventory.CraftInventory.firstPartial(CraftInventory.java:232)
    at org.bukkit.craftbukkit.inventory.CraftInventory.addItem(CraftInventory.java:258)
    at com.gmail.zoroarkchidori.testplugin.onPlayerInteract(testplugin.java:31)
    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
     
  4. Offline

    RealDope

    Little confused by your code, just do this:

    Code:JAVA
    1.  
    2. ItemStack coal = new ItemStack(Material.COAL, 1);
    3. ItemStack diamond = new ItemStack(Material.DIAMOND, 1);
    4. player.getInventory().removeItem(coal);
    5. player.getInventory().addItem(diamond);
    6. }
    7.  


    Should work, just something I wrote off the top of my head though.
     
  5. Offline

    The_Coder

    First when posting a question about errors please post the error in the console. Secound try ItemStack <nametheitemstack> = new ItemStack(<material>, <amount>); Here:

    @EventHandler
    public void coalfordiamond(PlayerInteractEvent event) {
    Player player = event.getPlayer();
    If(event.getAction() == Action.LEFT_CLICK) {
    ItemStack coal = new ItemStack(Material.COAL, 1);
    if(player.getInventory().getIteminhand().equals(coal)) {
    player.getIventory.remove(coal);
    ItemStack diamond = new ItemStack(Material.DIAMOND, 1);
    player.getIventory.addItem(diamond);
    player.updateinventory;
    }
    }
    }
     
  6. Offline

    Zoroark173

  7. Offline

    Bryguy

    I won't lie, looking at your code again I'm noticing numerous errors in what you're trying to do. For example, ItemStack[] is an array, or collection, of individual ItemStacks. It's empty by default. You don't want an empty collection of ItemStacks, you want one single, usable ItemStack.
    Secondly, you've created a new Coal itemstack, but not with assigned to a variable. Why you'd even create a new one is beyond me, you're trying to remove the itemstack of coal that the player is holding (That you've checked for in your conditional) from his hand, not some new itemstack that isn't even in the player's inventory.

    You have parts of your code right, but many wrong or in the wrong places. The new diamond itemstack should be added by itself into the part where you're adding items to the player inventory. The item the player is holding should be what's removed, not some new itemstack. You don't even need the creation of the coal itemstack you've added.

    Also, don't cancel the event. What you're doing there is saying, "If this player has coal in his hand, give him diamonds. If not, don't let him do anything that required that right click. No placing blocks, not opening doors, nothin'." Is that what you really want?

    Also, what tutorials have you been looking at? I'd really like to check the validity of what they've been teaching...
     
  8. Offline

    Zoroark173

  9. Offline

    Cammy_the_block

    I tried that before it doesn't work that well if you don't have a good understanding of java.

    TIP: I just past lines like
    Code:
    public void setMetadata(Player player, String key, Object value, Plugin plugin
     
  10. Offline

    Bryguy

    Honestly the reason why that person used ItemStack[]{ new ItemStack(Material...., #); } is beyond me. Seems rather ineffective way of doing things, creating an array and putting a new item in it for the purpose of removing an item. I wouldn't begin to know their logic behind it without asking them.

    And another thing, don't just stick with Bukkit tutorials. If you want to do well, venture out of Bukkit a bit and look into actual Java tutorials, starting from the basics. Then everything makes much more sense and it becomes easier to go further. Trust me.

    For additional fun, also try out Minecraft Client modding sometime.
     
  11. Offline

    Cammy_the_block

    This is also true. I tried to make a plugin without knowing any java at all. It was really hard. Then I learned some basic java. Now it is easier.
     
  12. Offline

    Bryguy

    I'd suggest either search on YouTube for either Basic Java Tutorials or Beginning Java, or doing the same in Google (Or your usual search engine), that can be one way of finding resources for learning. Also, it's possible your local library might have books, or might be able to get books, about learning Java. Books are possibly some of the best resources.
     
  13. Offline

    Cammy_the_block

  14. Offline

    Zoroark173

    I do know some basics of Java :D The most advanced thing that I have made though outside of bukkit was an n-console number guessing game! ( also know Lua and TI-BASIC, but that's irrelevant)
     
  15. Offline

    Cammy_the_block

    IK Just-Basic. JB = Easy fun coding
     
  16. Offline

    Zoroark173

    TI-BASIC is probably one of the easiest and most fun languages out there! And the best part is- Its the programming language for calculators.

    I now have a second error, but must go to bed. I will check back in the morning though maybe a good-nights sleep will help me think clearly!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  17. Offline

    feildmaster

    use brackets to group things together, what your logic was saying before was:

    if (right click air) do this
    OR if (right click block && coal) also do this
     
    Bryguy likes this.
  18. Offline

    Zoroark173

    Ohhhh, so kind of like how in math 1+2x3 is different than (1+2)x3... I get it now...
    EDIT: How do I specify which block they clicked?
     
Thread Status:
Not open for further replies.

Share This Page