Solved Adding limited amount of items into a chest!

Discussion in 'Plugin Development' started by jcbjoe, Dec 28, 2014.

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

    jcbjoe

    Hello,

    I have been trying to figure out how to do something in my plugin for a few days now and just cannot seem to figure it out! I own a server and am going through a reset process and decided to make a vault plugin for people to transfer a few items. However so people don't transfer all there diamond blocks and diamonds etc, which would completely destroy the point of a reset, i decided to add restrictions on to the vault. However i am having huge problems on counting the current items that's inside the chest and then adding the ones in the cursor, the thing causing the problem is i am trying to add restrictions for 2 things so i say you can only put 1 stack of diamonds in the vault, that means you can put diamond blocks in to however the combined amount of blocks and diamonds in the chest must add up to 64 and this is the part i am doing wrong.

    Currently I have two events, the inventory click and the inventory drag and i use a separate method i will list below on getting the items.

    Code:
        public int getAmountOf(ItemStack itemstack, Inventory inv){
           
            int amount = 0;
            for(ItemStack invItem : inv){
                if(invItem != null){
                    if(itemstack.getType() == Material.DIAMOND){
                        if(invItem.getType() == Material.DIAMOND_BLOCK){
                            amount = amount+(invItem.getAmount()*9);
                        }else{
                        }
                    }
                }
            }
            return amount+itemstack.getAmount();
        }
    Code:
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event){
            if(event.getInventory().getTitle() == "Vault"){
               
                if(event.isShiftClick()){
                    event.setCancelled(true);
                    return;
                }
                if ((!event.getInventory().getType().equals(InventoryType.PLAYER)) && (!event.getCursor().getType().equals(Material.AIR)))
                {
                  Player player = (Player)event.getWhoClicked();
                  boolean stop_placement = false;
                  if (event.getRawSlot() < event.getInventory().getSize())
                  {
                    Material mat_in_question = event.getCursor().getType();
                     if (mat_in_question == Material.DIAMOND) {
                         if(this.getAmountOf(event.getCursor(), event.getInventory()) > 64){
                             stop_placement = true;
                         }
                     }
                  }
                  if ((stop_placement))
                  {     
                    event.setCancelled(true);
                    player.sendMessage("You are only allowed 64 diamonds!");
                  }
                  Bukkit.broadcastMessage("Amount: "+getAmountOf(event.getCursor(), event.getInventory()));
                }
             }
    }
    NOTE: the drag and click are pretty much the same thats why i only added one.

    Thanks in advance,
    Joe
     
  2. Offline

    WarmakerT

    Can't you just prohibit the transfer of diamond blocks?
    Also, I'm not really sure what your question is.
     
  3. Offline

    jcbjoe

    The question is not puting over 64 diamonds in a chest, that includes diamond blocks. so for example have 55 diamonds in a chest and 1 diamond block, this is so players would not need to convert items to ingots/ores
     
  4. Offline

    1Rogue Retired Staff

    Why don't you just disallow diamond blocks in the first place since diamonds can be converted between gems and their respective blocks freely? Then you just need to make sure there's not more than one stack of diamonds.
     
  5. Offline

    jcbjoe

    I could do that, but I posted on how to do it with the diamond block aswell so if you have any suggestions for that go for it
     
  6. Offline

    WarmakerT

    Code:
        public int getAmountOf(ItemStack itemstack, Inventory inv){
            int amount = itemstack.getAmount();
            for(ItemStack is : inv) {
                if(is == null) continue;
                if(is.getType() == Material.DIAMOND) {
                    amount += is.getAmount();
                } else if(is.getType() == Material.DIAMOND_BLOCK) {
                    amount += is.getAmount()*9;
                }
            }
    
            return amount;
        }
    I still don't quite understand what you need/what's not working.
    This is a cleaner version of your first method.
     
  7. Offline

    jcbjoe

    what im trying to do is if the player has diamond blocks in there vault they will count towards there total amount of diamonds
     
  8. Offline

    1Rogue Retired Staff

    So then calculate the total amount of both items in the chest, then multiply the value of the diamond blocks by 9. Add the two together, and if the value is greater than or equal to 64, don't allow them to place more of either. If it's greater than 55, don't let them place any diamond blocks.
     
  9. Offline

    WarmakerT

    The method I wrote should do that, except for the cancelling which is pretty straight-forward.
     
  10. Offline

    jcbjoe

    the problem is the itemstack in the getamouunt method is the cursor, the items in hand and you have to count them however if you count them in the for statement you end up getting, well however many times it runs and then if you add it at the end like i do it doesnt work either

    with your method the diamond block check must be under the diamond place

    Ill have a look tomorrow and see if i get anywhere and then give you a update

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
  11. Offline

    WarmakerT

    It doesn't. An ItemStack is a slot of an inventory. An ItemStack can only be either a diamond block or a diacmond. In Minecraft, Diamond blocks aren't sub-sets of diamonds.
    It's not this:
    Diamond:
    - Diamond
    - Diamond Block
    - Diamond Axe
    - Diamond Hoe
    -Etc

    Each item is its own, Minecraft doesn't have certain materials that something is made of when it comes to code.

    This should do what you're asking.
    Code:
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            if (event.getInventory().getName() == "Vault") {
    
                if (event.isShiftClick()) {
                    event.setCancelled(true);
                    return;
                }
    
    
                if (event.getCursor().getType() == Material.DIAMOND || event.getCursor().getType() == Material.DIAMOND_BLOCK) {
                    Player player = (Player) event.getWhoClicked();
                    if(getAmountOf(event.getCursor(), event.getInventory()) > 64) {
                        event.setCancelled(true);
                        Bukkit.broadcastMessage("§cYou can only have 64 diamonds in your vault.");
                    }
                    Bukkit.broadcastMessage("§aAmount of diamonds: §f" + getAmountOf(event.getCursor(), event.getInventory()));
                }
            }
        }
    
        public int getAmountOf(ItemStack itemstack, Inventory inv){
            int amount = itemstack.getAmount();
            for(ItemStack is : inv) {
                if(is == null) continue;
                if(is.getType() == Material.DIAMOND) {
                    amount += is.getAmount();
                } else if(is.getType() == Material.DIAMOND_BLOCK) {
                    amount += is.getAmount()*9;
                }
            }
    
            return amount;
        }
     
  12. Offline

    jcbjoe


    Yes your right, however when adding a diamond it needs to check for diamond blocks to. Each diamond Nick needs to be inverted to 9 diamonds and added to the amount if the cursor(itemstack) is a diamond so you get a collective amount
     
  13. Offline

    WarmakerT

    @jcbjoe And it's doing that:
    Code:
                if(is.getType() == Material.DIAMOND) {
                    amount += is.getAmount();
                } else if(is.getType() == Material.DIAMOND_BLOCK) {
                    amount += is.getAmount()*9;
                }
     
  14. Offline

    jcbjoe


    Alright ill give it a quick go and see what happens

    Thank you so much for helping me getting it working! Your code worked fine sorry, my error. I made it so you can right click items which is one at a time instead of the full stack, so if you right clicked it would send true to getamount and then set amount = 1 but if it was false it would set to cursor!

    Thanks again appreciate it!
    Joe

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
  15. Offline

    stoneminer02

    Bukkit.broadcastMessage("Amount: "+getAmountOf(event.getCursor(), event.getInventory()));
    ... why?
     
  16. Offline

    jcbjoe

    It was a debug message to show me how many of the item were being counted, using this i could see the problems of the getAmount method
     
Thread Status:
Not open for further replies.

Share This Page