Setting item limit

Discussion in 'Plugin Development' started by drillzy, May 10, 2017.

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

    drillzy

    Hello guys i need some help with a plugin that im making. i want to add an item limit with a specific meta so you can get more than 20 of it. such as this:

    Code:
    @EventHandler
    
    public void onInteract(PlayerInteractEvent event){
    
        ItemStack held =  (event.getPlayer().getItemInHand());
    
        Player p = event.getPlayer();
    
     
    
    
      if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
    
    
    
        if(held.getType() == Material.INK_SACK && (int) held.getDurability() == 5){
    
     
    
        ItemStack item = p.getItemInHand();
    
        if(item.hasItemMeta()){
    
        ItemMeta meta = item.getItemMeta();
    
        if(meta.getDisplayName().equalsIgnoreCase(ChatColor.translateAlternateColorCodes('&', "&5&lDark Eco"))){
    
     
    
        }
    
     
    
          if(held.getAmount() >= getConfig().getInt("Cost")){
    
       
    
          final int cooldownTime = getConfig().getInt("CooldownTime");
    
            if(cooldowns.containsKey(p.getName())) {
    
                long secondsLeft = ((cooldowns.get(p.getName())/1000)+cooldownTime) - (System.currentTimeMillis()/1000);
    
                if(secondsLeft>0) {
    
             
    
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&5&lRage &8ยป &cYou have to wait for &6" + secondsLeft + " &cmore second(s) Until you can activate your Rage again!" ));
    
                    return;
    
                }
    
            }
    
         
    
            cooldowns.put(p.getName(), System.currentTimeMillis());
    
            p.removePotionEffect(PotionEffectType.SPEED);
    
            p.removePotionEffect(PotionEffectType.INCREASE_DAMAGE);
    
            p.removePotionEffect(PotionEffectType.DAMAGE_RESISTANCE);
    
            event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 2400, 0));
    
            event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 2400, 2));
    
            event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 2400, 1));
    
            event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("RageActivated")));
    
            held.setAmount(held.getAmount() - getConfig().getInt("Cost")); // Sets the ammount by getting the total ammount - 20
    
                event.getPlayer().getInventory().setItemInHand(held); //Sets the new ammount to the item held
    
                event.getPlayer().updateInventory(); //updates inventory with the new ammount
    
       
    
      } else {
    
            event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("NotEnoughEco")));
    
      }
    
        }
    
      }
    
     
    
    }
    
    }
    
    How would i be able to set the limit for the purple dye of a meta name such as "&5&lDark Eco"?
     
  2. Offline

    martian3333

    @drillzy
    Check the length of the string and if it's too long use a substring limited to the max characters you're looking for.
     
  3. Offline

    drillzy

    @martian3333
    i havent really gotten into substrings so you do mind showing me how it works or how to do it?
     
  4. Offline

    martian3333

  5. Offline

    drillzy

    @martian3333
    Oh wait you are talking completely about something else. thats limiting a string. I want to limit an item from the inventory such as a dye. and when they are on the limit max they get a msg. Sorry if i was misunderstanding :(
     
  6. Offline

    martian3333

    Sorry about that. I thought you were trying to limit the length of the string shown as displayname. After re-reading the post I can see that I shouldn't have really thought that.

    @drillzy
    So what are you trying to limit? The number they can have in their inventory? The number they can have in a stack?
     
  7. Offline

    drillzy

    @martian3333
    Its alright, we all make mistakes. Not everyone is perfect :)

    @martian3333
    What i am trying to limit is a specific itemstack. the purple dye, MATERIAL.INK_SACK with the durability 5 = purple dye
     
    Last edited by a moderator: May 12, 2017
  8. Offline

    martian3333

    @drillzy
    You're going to have to catch inventoryclick events, inventorydrag events, and pickup events; check the changed items for the event; and cancel the event/split the item into the correct size stacks/allow a certain amount and then drop the rest.... whichever suits what you'd like to accomplish.
     
Thread Status:
Not open for further replies.

Share This Page