Solved Durability bar

Discussion in 'Plugin Development' started by kangkyuchang, Jul 28, 2020.

  1. Offline

    kangkyuchang

    Hello, First Thank you for you that help me. I want to remove the durability bar (green bar) display. However, the lack of durability does not mean that the item is not broken. I just want to get rid of the durability bar.
     
  2. Offline

    Strahan

    I doubt that's possible, at least, not while retaining wear. If it is, it would likely be a client modification not a server plugin.

    One possible way would be when an item is damaged, keep track of that item's "health" in config and set the item itself to be non breakable. That would suppress the damage bar (since the item is unbreakable) and you'd track the health every time the item is used. Then when the tracked health indicates it should break, you just remove the item and play the breaking sound.
     
  3. Offline

    kangkyuchang

    Ah, Thank you
     
  4. Offline

    Strahan

    I found it an intriguing idea, so I implemented it as a plugin:


    Here is the code:
    Show Spoiler
    Code:
    public class Test16 extends JavaPlugin implements Listener {
      @Override
      public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
      }
    
      @EventHandler
      public void onPlayerItemDamage(PlayerItemDamageEvent e) {
        int itemClass = getConfig().getInt("itemclasses." + e.getItem().getType().name(), -1);
        if (itemClass == -1) return;
    
        ItemStack currentItem = e.getItem();
        String itemTag = getItemTag(e.getItem());
        if (itemTag == "") {
          itemTag = e.getPlayer().getUniqueId().toString() + System.currentTimeMillis();
          currentItem = tagItem(itemTag, currentItem);
        }
    
        int currentDamage = getConfig().getInt("damage." + itemTag);
        currentDamage++;
    
        currentItem = updateItem(currentDamage, currentItem);
    
        if (currentDamage >= e.getItem().getType().getMaxDurability()) {
          currentItem.setAmount(currentItem.getAmount()-1);
          e.getPlayer().getWorld().playSound(e.getPlayer().getLocation(), (itemClass == 40)?Sound.ITEM_SHIELD_BREAK:Sound.ENTITY_ITEM_BREAK, 1f, 0.7f);
          getConfig().set("damage." + itemTag, null);
        } else {
          getConfig().set("damage." + itemTag, currentDamage);
        }
        saveConfig();
    
        if (itemClass == 0) {
          e.getPlayer().getInventory().setItemInMainHand(currentItem);
        } else {
          e.getPlayer().getInventory().setItem(itemClass, currentItem);
        }
      }
    
      private ItemStack updateItem(int currentDamage, ItemStack i) {
        String healthString = getConfig().getString("config.healthdisplay", "&aLife Remaining: &2%dmgpct% &7(%dmgleft%)");
        String healthFingerprint = getConfig().getString("config.healthfp", "Life Remaining: ");
        Damageable d = (Damageable)i.getItemMeta();
        i.setItemMeta((ItemMeta)d);
    
        ItemMeta im = i.getItemMeta();
        int healthLine = -1, damagePercentage = (((i.getType().getMaxDurability() - currentDamage) * 100) / i.getType().getMaxDurability());
        List<String> lore = (im.getLore() == null)?new ArrayList<>():im.getLore();
        for (int x=0; x<lore.size(); x++) {
          String loreLine = ChatColor.stripColor(lore.get(x));
          if (loreLine.length() < healthFingerprint.length()) continue;
    
          if (loreLine.substring(0, healthFingerprint.length()).equalsIgnoreCase(healthFingerprint)) {
            healthLine = x;
            break;
          }
        }
    
        if (healthLine == -1) {
          if (lore.size() > 0) lore.add(" ");
          lore.add(" ");
          healthLine = lore.size()-1;
        }
    
        healthString = healthString.replace("%dmgpct%", String.valueOf(damagePercentage) + "%");
        healthString = healthString.replace("%dmg%", String.valueOf(currentDamage));
        healthString = healthString.replace("%maxdmg%", String.valueOf(i.getType().getMaxDurability()));
        healthString = healthString.replace("%dmgleft%", String.valueOf(i.getType().getMaxDurability() - currentDamage));
        lore.set(healthLine, ChatColor.translateAlternateColorCodes('&', healthString));
        im.setLore(lore);
        i.setItemMeta(im);
        return i;
      }
    
      private ItemStack tagItem(String tagValue, ItemStack i) {
        net.minecraft.server.v1_16_R1.ItemStack nms = CraftItemStack.asNMSCopy(i);
        if (!nms.hasTag()) nms.setTag(new NBTTagCompound());
      
        NBTTagCompound tag = nms.getTag();
        tag.setString("durabilityTag", tagValue);
        return CraftItemStack.asCraftMirror(nms);
      }
    
      private String getItemTag(ItemStack i) {
        net.minecraft.server.v1_16_R1.ItemStack nms = CraftItemStack.asNMSCopy(i);
        if (!nms.hasTag()) return "";
    
        NBTTagCompound tag = nms.getTag();
        return tag.getString("durabilityTag");
      }
    }


    Config for the plugin:
    Show Spoiler
    Code:
    itemclasses:
      IRON_HELMET: 39
      GOLDEN_HELMET: 39
      DIAMOND_HELMET: 39
      NETHERITE_HELMET: 39
      CHAINMAIL_HELMET: 39
      IRON_CHESTPLATE: 38
      GOLDEN_CHESTPLATE: 38
      DIAMOND_CHESTPLATE: 38
      NETHERITE_CHESTPLATE: 38
      CHAINMAIL_CHESTPLATE: 38
      IRON_LEGGINGS: 37
      GOLDEN_LEGGINGS: 37
      DIAMOND_LEGGINGS: 37
      NETHERITE_LEGGINGS: 37
      CHAINMAIL_LEGGINGS: 37
      IRON_BOOTS: 36
      GOLDEN_BOOTS: 36
      DIAMOND_BOOTS: 36
      NETHERITE_BOOTS: 36
      CHAINMAIL_BOOTS: 36
      SHIELD: 40
      WOODEN_PICKAXE: 0
      WOODEN_SHOVEL: 0
      WOODEN_AXE: 0
      WOODEN_SWORD: 0
      WOODEN_HOE: 0
      STONE_PICKAXE: 0
      STONE_SHOVEL: 0
      STONE_AXE: 0
      STONE_SWORD: 0
      STONE_HOE: 0
      IRON_PICKAXE: 0
      IRON_SHOVEL: 0
      IRON_AXE: 0
      IRON_SWORD: 0
      IRON_HOE: 0
      GOLDEN_PICKAXE: 0
      GOLDEN_SHOVEL: 0
      GOLDEN_AXE: 0
      GOLDEN_SWORD: 0
      GOLDEN_HOE: 0
      NETHERITE_PICKAXE: 0
      NETHERITE_SHOVEL: 0
      NETHERITE_AXE: 0
      NETHERITE_SWORD: 0
      NETHERITE_HOE: 0
      FLINT_AND_STEEL: 0

    I just edited the code as it bugged me to be hard coding the item class in the code. I prefer using config as it's flexible, so ignore the part in the vid where I talk about the setting of item slots from a method in the code. Now I just read it from config.
     
    Last edited: Jul 29, 2020
  5. Offline

    kangkyuchang

    Thank you ! It was very helpful.
     
    Strahan likes this.

Share This Page