Set item on slot 1?

Discussion in 'Plugin Development' started by iAmGuus, May 30, 2014.

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

    iAmGuus

    Hey Guys,
    Its me again, Guus.
    I got a question about setting the item on slot when its already full.
    So what i did is this:
    Code:java
    1. @EventHandler
    2. public void feastInteract(PlayerInteractEvent e) {
    3. if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    4. Player p = e.getPlayer();
    5. if (e.getClickedBlock().getState() instanceof Sign) {
    6. Sign s = (Sign) e.getClickedBlock().getState();
    7. if (s.getLine(1).equalsIgnoreCase("Feast")) {
    8. p.teleport(new Location(p.getWorld(), 995.5, 5, 13.5));
    9. p.getInventory().setHelmet(new ItemStack(Material.IRON_HELMET));
    10. p.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
    11. p.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS));
    12. p.getInventory().setBoots(new ItemStack(Material.IRON_BOOTS));
    13. p.getInventory().setItem(0, new ItemStack(Material.IRON_SWORD));
    14. p.sendMessage("" + ChatColor.BOLD + ChatColor.GREEN + "ExperiencePvP>" + ChatColor.GOLD + "You have been teleported to Feast!");
    15. }
    16. }
    17. }

    Anybody knows a solution?
    Thanks
    iAmGuus
     
  2. Offline

    Boomer

    Assuming your question means "if the first item slot is full, how do I find the next one that isn't"...

    get the value of p.getInventory().firstEmpty() (Integer) to determine which slot (if any) is open and available.
    Then set the item for that slot number if one exists, and if not, then deal with the 'player inventory is 100% full, what do I want to do about this specific case now?' situation.
     
  3. Offline

    iAmGuus

    I dont think you understand it. Listen,
    if i do :
    Code:java
    1. p.getInventory().setItem(1, new ItemStack(Material.IRON_SWORD));

    There will just be a iron sword on the second slot. which was a mushroom stew.
    But if i set it to slot 0, nothing will change. already added debug messages. worked.
    :(
     
  4. Offline

    tomudding

    Code:java
    1. p.getPlayer().getInventory().setItem(0, new ItemStack(Material.IRON_SWORD));
    I use this and it is working fine. And maybe update the player his inventory?
     
  5. Offline

    iAmGuus

    I dont know what it is tomudding, but it still doesnt work :(
    =============================================================
    I think the slot is locked,
    because if i drop my stone sword which was before in the inventory then he gets added.
     
  6. Offline

    tomudding

    You could also try this.
    Code:java
    1. ItemStack i = new ItemStack(Material.IRON_SWORD);
    2. Int slot = 0;
    3. p.getPlayer().getInventory().setItem(slot, i);
    4.  
    I don't know if it will work, but you can try it.
     
  7. Offline

    iAmGuus

    nah, didnt work.
    Any other idea?
     
  8. The slots go by 0 to 8
    So number 0 is slot 1. 8 is slot 9 .Hope it helped.
     
  9. Offline

    iAmGuus

    Sean0402 i know that. I think there is a bug in my kit plugin. Let me test it in a minute

    tomudding, what you said about updating their inventory, with p.updateInventory(); well, sofar it didnt work. I saw the swords changing from stone sword to iron and back. but didnt work :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 1, 2016
  10. Offline

    Rocoty

    Please explain EXACTLY point by point what you do, and what you see. I don't mean stuff like "It does not work". I mean "When I do this, that happens. But I would like it to do this instead."
     
  11. Offline

    iAmGuus

    ok, so when i right click my sign, which works fine. then the player gets teleported to certain place on map (feast, works fine). He gets full iron armor (works fine), and a iron sword. and the point where gets the iron sword, isnt working fine.
     
  12. Offline

    Boomer

    put the arming and giving of inventory in a 1-tick delayed process.
    I've seen in one of mine that trying to do it as soon as teleporting can lead to inconsistancies, which disappear by making the inventory fill as a 1 tick delayed task.
     
  13. Offline

    Xyplo

    This is so simple... If you want it in slot one, (slot 2 on your hotbar in game) then it's this.
    Code:java
    1. p.getInventory().addItem(new ItemStack(Material.IRON_SWORD, 1));

    Otherwise if you want it in slot 0 (slot 1 on your hotbar in game) then it's this...
    Code:java
    1. p.getInventory().addItem(new ItemStack(Material.IRON_SWORD, 0));


    EDIT: Yes, I do use this to set an item in a slot even if it isn't empty.
     
  14. Offline

    d3v1n302418

    Xyplo you know you are giving them 0 iron swords... not setting it in slot one. iAmGuus Your code should work. Try updating their inventory player.updateInventory(). Don't mind the depreciation warning.
     
  15. Offline

    Xyplo

    What do you think the '
    Material.IRON_SWORD, 1));' 1 represents... -.-
     
  16. Offline

    iAmGuus

    Xyplo, i thought if you do the mehtod of addItem, that the item will be added to the first open slot in the inventory. And the 1 stands for the amount of Swords.

    d3v1n302418, I already tried updating the inventory. When i do that. I just see it changing from stone sword to iron sword and back :s
     
  17. Offline

    Xyplo

    I just realised that I'm displaying the wrong code... Now I feel dumb... XD
     
  18. Offline

    iAmGuus

    Xyplo, np though, we are here to help. But do you have another solution maybe? Because i really need this thing for my upcoming server :3
     
  19. Offline

    Xyplo

    Why don't you search their inventory for the item, then clear it and then set in the Iron sword..??
    The way i'd search would be to add everything in their inventory in a list..
     
  20. Offline

    iAmGuus

    How could i do that. Im pretty new but know a lot :)
    something like getInventory().contains()?
     
  21. Offline

    Plugers11

    iAmGuus
    Try this

    Code:java
    1. p.getInventory().first(new ItemStack(Material.IRON_SWORD));
     
  22. Offline

    iAmGuus

    Nop, i think there is a bugg in my kits plugin
     
  23. Offline

    funnyman850

    iAmGuus try setting it to air and then on the next line set it to an iron sword
     
  24. Offline

    iAmGuus

    funnyman850, didnt work already tried it a few hours ago
     
  25. Offline

    Konkz

    Let me get this straight, if there is something (We will forget about indexes) in slot one (out of 9) then item won't be given? Weird. I guess a way to bypass it is:

    PHP:
        private void giveItem(Material itemint amountPlayer bukkitPlayerint slot) {
            
    ItemStack i = new ItemStack(itemamount);
            if (
    bukkitPlayer.getInventory().getItem(slot) != null) {
                
    bukkitPlayer.getInventory().setItem(sloti);
            } else {
                
    bukkitPlayer.getInventory().clear(slot);
                
    bukkitPlayer.getInventory().setItem(sloti);
            }
        }
    Wrote it on my phone so it may be incorrect but it seems fine. It simply checks if there is
    an item in slot you provided and if there is it removes it and gives the item.

    To "giveItem" you simply call the method and fill our what Material (EG: Iron_Sword), how many should be given, to what player and which slot.

    EG:
    PHP:
    giveItem(Material.BREAD5bukkitPlayer0);
    That will give 5 bread to the player "bukkitPlayer" in slot 0.
     
  26. Offline

    iAmGuus

    Thank you, so far. But there is an error at the part of the bUkkitPlayer, the player who was given to.
    It doesn't reconigize (sorry if i spelled it wrong) the player on some way.
    Should i make it public?
     
  27. Offline

    LegoPal92

    player.updateInventory();
     
  28. Offline

    iAmGuus

    Mah, dont work, tried many times.
     
  29. Offline

    Konkz

    What error? Eclipse error? Null pointer?
     
  30. Offline

    iAmGuus

    There is no error in the eclipse / ide.

     
Thread Status:
Not open for further replies.

Share This Page