Iron ore to Iron ingot using coal in inventory click event.

Discussion in 'Plugin Development' started by SoS_Dylan, Jan 22, 2013.

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

    SoS_Dylan

    I am trying to make a plugin that would make it so if a player clicks on iron with coal it would give him iron ingots and will take away the iron ore and one coal per iron. I would really appreciate it if someone could help me out. :)
    This is what I have and it doesn't seem to work.
    Code:java
    1.  
    2. @EventHandler
    3. public void onInventoryClick(InventoryClickEvent event) {
    4. Player player = ((Player) event).getPlayer();
    5. if (event.getCurrentItem().equals(Material.COAL) && event.getCursor().getType().equals(Material.IRON_ORE)) {
    6. player.sendMessage("Changed to iron ingot!");
    7. }
    8. }
    9.  
     
  2. Offline

    SoS_Dylan

  3. Offline

    H2NCH2COOH

    I think it may use two InventoryClickEvent, first check left click on coal and add a flag to the player, then check if he clicked an ore in the next InventoryClickEvent

    EDIT never mind, I just known the existence of getCrusor()
     
  4. Offline

    Cjreek

    You're doing the opposite of this. You're sending the message if the player clicks on coal with iron ore in his cursor.
     
  5. Offline

    SoS_Dylan

    I ev
    I even spammed click and nothing happened. :'(
     
  6. Offline

    Cjreek

    Mh? I don't know what you're telling me :-D

    Just do this:

    Code:java
    1. @EventHandler
    2. public void onInventoryClick(InventoryClickEvent event) {
    3. Player player = ((Player) event).getPlayer();
    4. if (event.getCurrentItem().equals(Material. IRON_ORE ) && event.getCursor().getType().equals(Material.COAL)) {
    5. player.sendMessage("Changed to iron ingot!");
    6. }
    7. }
     
    ZachBora likes this.
  7. Offline

    SoS_Dylan

    I get this error
    Code:
    2013-01-23 02:31:14 [SEVERE] Could not pass event InventoryClickEvent to Hunger Games v1.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 net.minecraft.server.v1_4_6.PlayerConnection.a(PlayerConnection.java:1289)
    >      at net.minecraft.server.v1_4_6.Packet107SetCreativeSlot.handle(SourceFile:21)
    >      at net.minecraft.server.v1_4_6.NetworkManager.b(NetworkManager.java:290)
    >      at net.minecraft.server.v1_4_6.PlayerConnection.d(PlayerConnection.java:112)
    >      at net.minecraft.server.v1_4_6.ServerConnection.b(SourceFile:39)
    >      at net.minecraft.server.v1_4_6.DedicatedServerConnection.b(SourceFile:30)
    >      at net.minecraft.server.v1_4_6.MinecraftServer.r(MinecraftServer.java:598)
    >      at net.minecraft.server.v1_4_6.DedicatedServer.r(DedicatedServer.java:224)
    >      at net.minecraft.server.v1_4_6.MinecraftServer.q(MinecraftServer.java:494)
    >      at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:427)
    >      at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:849)
    Caused by: java.lang.ClassCastException: org.bukkit.event.inventory.InventoryClickEvent cannot be cast to org.bukkit.entity.Player
    >      at me.SoSDylan.HungerGames.Main.onInventoryClick(Main.java:1473)
    >      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    >      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    >      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    >      at java.lang.reflect.Method.invoke(Method.java:597)
    >      at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    >      ... 14 more
     
  8. Offline

    Cjreek

    Ok this line is nonesense:

    Player player = ((Player) event).getPlayer();

    Change it to

    Player player = event.getPlayer();

    You're casting the event object to Player which doesn't make any sense.
     
  9. Offline

    fireblast709

    Besides that, getPlayer() does not exist. Invoke .getWhoClicked() and cast that to Player (and possibly do a instanceof check). Also, you are comparing an ItemStack with a Material, do .getType() between getCurrentItem() and .equals(Material.IRON_ORE)
     
  10. Offline

    SoS_Dylan

    I don't get any errors but it still doesn't work. :'(

    EDIT: I found out that
    Code:java
    1. event.getCursor().getType().equals(Material.COAL)

    Doesn't work. Any idea on how to get it to work?
     
  11. Offline

    Rprrr

    Because you're only sending a message, and not actually changing it?
     
  12. Offline

    SoS_Dylan

    I know, it won't send the message, I tried it with just the iron, it sent a message, then I tried it with the coal and it didn't send a message. I know how I can change it to iron, i'm just not putting that in for testing.
     
  13. Offline

    Rprrr

    SoS_Dylan
    If you're still using this..
    Code:
    if (event.getCurrentItem().equals(Material.COAL) && event.getCursor().getType().equals(Material.IRON_ORE)) {
    .. you should probably change it to:
    Code:
    if ((event.getCurrentItem().equals(Material.COAL) && event.getCursor().getType().equals(Material.IRON_ORE)) || (event.getCurrentItem().equals(Material.IRON_ORE) && event.getCursor().getType().equals(Material.COAL))) {
    That way it'll be both way. Or is that not what you want it to do?
     
  14. Offline

    chasechocolate

    You need to actually change the item:
    Code:java
    1. event.setCurrentItem(new ItemStack(Material.IRON_INGOT));
     
  15. Offline

    SoS_Dylan

    I know, I will add that script but I can't get the code to make the if statement 'true'.
     
  16. Offline

    skipperguy12

    Try == instead of .equals (worth a try, java sometimes derps out, for me atleast :S)

    Also, try printing to console instead of if statement, ex:
    get rid of if statement, and put these lines:

    if(event.getCurrentItem() != Material.COAL){getLogger().severe("Item is not coal!");}
    if(/*do this with all your checks*/){getLogger().severe("Blah.");}
     
  17. Offline

    SoS_Dylan

    Nope, still doesn't work. :'(
     
  18. Offline

    skipperguy12

    Do the debug messages print, at all?
     
  19. Offline

    SoS_Dylan

    No, it only works on '(event.getCurrentItem().equals(Material. IRON_ORE)' and not 'event.getCursor().getType().equals(Material.COAL)'.
     
  20. Offline

    SoS_Dylan

    No, it only works on '(event.getCurrentItem().equals(Material. IRON_ORE)' and not 'event.getCursor().getType().equals(Material.COAL)'.
     
Thread Status:
Not open for further replies.

Share This Page