toggle & other

Discussion in 'Spigot Plugin Development' started by TheMeowster, Apr 13, 2017.

  1. So I am making a plugin where the player can toggle auto ingot - block. I don't know how to add the toggle, make it automatic and check the whole inventory instead of main hand. This is what I've got so far (it's only checking on the main hand but that is not what I want):

    Code (Text):
    if(cmd.getName().equalsIgnoreCase("block")) {
        if(player.hasPermission("eb.block")) {
            Material m = pi.getItemInMainHand().getType();
            int amount = pi.getItemInMainHand().getAmount();
            int total = amount / 9;
            int remainder = amount % 9;

    if(m == Material.DIAMOND && amount >= 9) {

        pi.addItem(new ItemStack(Material.DIAMOND_BLOCK, total));
        pi.remove(Material.DIAMOND);
        pi.addItem(new ItemStack(Material.DIAMOND, remainder));
        return true;
    }
    if(m == Material.EMERALD && amount >= 9) {
        pi.addItem(new ItemStack(Material.EMERALD_BLOCK, total));
        pi.remove(Material.EMERALD);
        pi.addItem(new ItemStack(Material.EMERALD, remainder));
        return true;
    }
    if(m == Material.COAL && amount >= 9) {
        pi.addItem(new ItemStack(Material.COAL_BLOCK, total));
        pi.remove(Material.COAL);
        pi.addItem(new ItemStack(Material.COAL, remainder));
        return true;
    }
    if(m == Material.IRON_INGOT && amount >= 9) {
        pi.addItem(new ItemStack(Material.IRON_BLOCK, total));
        pi.remove(Material.IRON_INGOT);
        pi.addItem(new ItemStack(Material.IRON_INGOT, remainder));
        return true;
        }
    if(m == Material.GOLD_INGOT && amount >= 9) {
        pi.addItem(new ItemStack(Material.GOLD_BLOCK, total));
        pi.remove(Material.GOLD_INGOT);
        pi.addItem(new ItemStack(Material.GOLD_INGOT, remainder));
        return true;
    }
    Code (Text):
            Player player = (Player) sender;
            PlayerInventory pi = player.getInventory();
     
  2. If you want to add a "each player toggle" you need to make a HashMap where the players name (A string) is used as key and the boolean value is stored as value.

    And to look through the whole inventory you will have to loop through it and check for the material.
     
  3. Never use player names for HashMaps, always use UUID's.
     
  4. Doesn't matter if you remove the player anyway when he leaves. You can also just use the Player object, it's not that hard to do it correctly