Solved Moving Items around

Discussion in 'Plugin Development' started by TheManiacGamers, Dec 12, 2016.

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

    TheManiacGamers

    Hello,

    What I am trying to do is move the Item in the Off Hand to the Main hand, and vice versa.

    This is what I have thought of, but, I couldn't get it working. I tried searching around, but I couldn't find anything.

    Code:
    p.getInventory().getItemInOffHand().getItemMeta();
                                ItemStack offHand = new ItemStack(p.getInventory().getItemInOffHand().getType(), p.getInventory().getItemInOffHand().getAmount());
                                ItemMeta offHandMeta = offHand.getItemMeta();
                                offHandMeta.getLore());
                                offHandMeta.getEnchants();
                                offHandMeta.getDisplayName();
                                offHand.setItemMeta(offHandMeta);
                                ItemStack mainHand = new ItemStack(p.getInventory().getItemInOffHand().getType(), p.getInventory().getItemInOffHand().getAmount());
                                ItemMeta mainHandMeta = mainHand.getItemMeta();
                                mainHandMeta.getLore();
                                mainHandMeta.getEnchants();
                                mainHand.setItemMeta(mainHandMeta);
                                ItemStack air = new ItemStack(Material.AIR, 1);
                                p.updateInventory();
                                p.getInventory().setItemInHand(air);
                                p.getInventory().setItemInMainHand(air);
                                p.getInventory().setItemInOffHand(air);
                                p.getInventory().setItemInHand(offHand);
                                p.getInventory().setItemInOffHand(mainHand);
                                p.updateInventory();

    I am trying to also copy over the ItemMeta, enchant, lore, etc, but that's what I am having trouble on.
    There's an error in console which points to line 95, which is " offHandMeta.getLore());"

    because, well, that ain't good. Haha.

    Anyways,
    Here's the error:

    Code:
    [10:51:40 WARN]: [OPPrison] Task #34 for OPPrison v1.0.0.0 generated an exception
    java.lang.NullPointerException
            at co.plasmanetwork.listeners.items.TrollPickaxeListener$2.run(TrollPickaxeListener.java:95) ~[?:?]
            at org.bukkit.craftbukkit.v1_11_R1.scheduler.CraftTask.run(CraftTask.java:71) ~[spigot111.jar:git-Spigot-f950f8e-b64d852]
            at org.bukkit.craftbukkit.v1_11_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:353) [spigot111.jar:git-Spigot-f950f8e-b64d852]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:730) [spigot111.jar:git-Spigot-f950f8e-b64d852]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot111.jar:git-Spigot-f950f8e-b64d852]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:675) [spigot111.jar:git-Spigot-f950f8e-b64d852]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:574) [spigot111.jar:git-Spigot-f950f8e-b64d852]
            at java.lang.Thread.run(Thread.java:745) [?:1.7.0_111]

    Thanks. :)
     
    Last edited: Dec 12, 2016
  2. Offline

    timtower Administrator Administrator Moderator

    @TheManiacGamers Why not set main to temp variable, hand to main, temp to hand.
    Just by moving ItemStacks and not every part on its own.
     
  3. Offline

    mythbusterma

    @TheManiacGamers

    Well first off, it looks like there are mismatched parentheses here. But you never actually check if the item has metadata, which it possible that it does not. ItemStack has a "hasMetaData" method or something like that. Use that to check to see if it has metadata before trying to use it.

    Next time, check the millions of forum threads on this, or read the documentation.
     
  4. Offline

    TheManiacGamers

    Hate to be a noob, but I'm not sure how to do that,
    Could you please help?
    @timtower

    That would been a good idea, can't believe I forgot that. :p
     
    Last edited: Dec 17, 2016
  5. Offline

    timtower Administrator Administrator Moderator

    This is a basic method.
    Just moving a couple variables around.
    I won't spoonfeed.
     
  6. Offline

    TheManiacGamers

    I wasn't asking for spoonfeed code.
    I was just asking for a description on how to,
    but okay, ill spend hours tryna figure it out. All good :p
    @timtower
     
  7. Offline

    timtower Administrator Administrator Moderator

  8. Offline

    TheManiacGamers

    @timtower
    *facepalms* I see what you're saying.

    I need sleep x,D

    Thanks. Will post how I go.

    // wow, *is ashamed*

    Code:
                                ItemStack temp = p.getInventory().getItemInOffHand();
                                ItemStack mainHand = p.getInventory().getItemInMainHand();
                                p.getInventory().setItemInOffHand(mainHand);
                                p.getInventory().setItemInMainHand(temp);
                                p.updateInventory();
     
    Last edited: Dec 12, 2016
Thread Status:
Not open for further replies.

Share This Page