Keeping the first slot on the inventory bar open

Discussion in 'Plugin Development' started by Kato233, Aug 22, 2014.

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

    Kato233

    Hey guys,

    I am coding a class called a monk who has a greater increase in damage plus a chance to 'crit' for extra damage when using no weapon. Is there a (simple) way of keeping the first slot of the inventory bar open for his 'fist'?

    Unfortunately, we are unable to create an ItemStack of type AIR, with a renamed value to use in the first slot.

    Hopefully you can amaze me :)!
     
  2. Offline

    fireblast709

    Kato233 empty slots cannot have a name. For it to have a name, there must be an item with the material id > 0. But that will also have the effect that the player will be holding an item, which you clearly didn't want :3.
     
  3. Offline

    PluginMaker

    You will have to handle any event that may cause him to have an item at that slot (cancel or add to another slot).
     
  4. Offline

    Kato233

    Morning folks,

    I was able to quench the thirst of this problem with the following code:

    Code:
    @EventHanlder
    public void onPlayerPickupEvent(PlayerPickupItemEvent event){
            Player player = event.getPlayer();
     
            /**
            * If the player is a monk
            * Create a new item and put it in the first slot of the inventory
            * using the additem method, add the eventItem to the next available slot
            * remove the item from the first slot of the inventory.
            */
            if(player.hasPermission("monk")){
                ItemStack temporaryItemStack = new ItemStack(Material.DROPPER);
                player.getInventory().setItem(0, temporaryItemStack);
                player.getInventory().addItem(event.getItem().getItemStack());
                player.getInventory().remove(temporaryItemStack);
            }
    

    EDIT:

    Hey guys,

    So the code posted above works theoretically, however it seems that the addItem() method take the current amount and adds it. So, to combat this, here is what I did

    Code:
    public void onPlayerPickUpEvent(PlayerPickUpEvent event){
            if(player.hasPermission("monk")){
                ItemStack temporaryItemStack = new ItemStack(Material.DROPPER);
                player.getInventory().setItem(0, temporaryItemStack);
            }
    }
    public void onPlayerMoveEvent(PlayeRMoveEvent event){
            if(player.getInventory().contains(Material.DROPPER)){
                player.getInventory().remove(Material.DROPPER);
            }
    }
     
  5. Offline

    NathanWolf


    That is going to be incredibly resource intensive! Inventory.contains scans through the entire inventory, and PME is called multiple times, per player, per tick in some cases.

    I am facing a similar problem in wanting to implement "bending" skills. One thing I would suggest to bypass this whole issue is make a Resource Pack and distribute with your plugin, or put it on your server. Retexture some 'expendible' item (like a wood hoe....) to be invisible - or even make a "monk powers" icon that wraps around their hand.
     
  6. Offline

    Kato233

    Yeah we thought of doing that, but then for ever player we would have to make sure they had the resource pack. It is a bit resource intense, however a short term solution will work fine for us for now.

    I thought of creating an ArrayList for every slot of the inventory, and keeping [0] as open. If [0].contains(someitem), remove that item. Perhaps I will code that in the future.

    Thanks for your help NathanWolf !
     
  7. Offline

    MnMaxon

    Kato233
    Sorry, I can't use the code thing on my phone, but I used something like this in one of my plugins:

    @EventHanlder
    public void onPlayerPickupEvent(PlayerPickupItemEvent event){
    Player player = event.getPlayer();

    if(player.hasPermission("monk")){
    e.setCancelled(true);
    for (int i= ; i < player.getInventory().size();i++)
    if(player.getInventory.get(i) == null){
    p.getInventory().set(i, e.getItemStack());
    e.getItem().remove();
    }
    }
    }

    I messed up replace i<0 with I =1

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

    AnorZaken

    Um, just remove the item in slot 0 with a 1-tick delayed task?

    Also make the permission name a lot longer than just "monk" - like "yourplugin.monk" - otherwise you risk problems with other plugins.

    Take your first code (without the player move event) and modify it roughly like....
    Code:java
    1. final Player player = event.getPlayer();
    2. if(player.hasPermission("yourplugin.monk")){
    3. ItemStack temporaryItemStack = new ItemStack(Material.DROPPER);
    4. player.getInventory().setItem(0, temporaryItemStack);
    5. player.getServer().getScheduler().runTask( plugin, new Runnable() {
    6. public void run() {
    7. player.getInventory().setItem(0, null);
    8. }
    9. });

    (...might not be 100% accurate because I'm writing it from memory.)

    (oh and just in case you should probably check that the item in slot 0 is still your temporary hopper before you remove it - if it isn't you have a problem though, and so to solve this properly would require a whole lot more code :p)
     
Thread Status:
Not open for further replies.

Share This Page