Get item id of enchanted items

Discussion in 'Plugin Development' started by chaniloko, Sep 4, 2021.

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

    chaniloko

    • Hi guys, i'm, developing a economy plugin, just for learn, and in this plugin i'm trying to make a sign shop, but i got stuck in items id. I can take id of items with this:
    • Code:
      int itemid = player.getInventory().getItemInHand().getTypeId();
       short itemDur = player.getInventory().getItemInHand().getDurability();
    • But with this two lines I can't get id of enchanted items, for example.
    • I'll have to create an meta for each item?
    • Someone can tell-me what I have to do to get "custom id's" ?
     
  2. Offline

    Strahan

    You shouldn't be using numeric IDs in the first place, they're deprecated. That aside, you can't get it because there is no such thing as enchanted item IDs. Both a regular and an enchanted diamond pickaxe will be 278 for example. You need to look at the item's meta to figure out the enchants.

    Personally, I'd ditch physical shop and do a shop command that uses GUIs. Much easier to provide tons of information to the shoppers. Or if you want to have a physical interface, maybe do a hybrid. Like have a sign that that player needs to click that opens an inventory to handle sales of all variants of the specified item.
     
    Kars likes this.
  3. Offline

    chaniloko

    Yeah, it's an good idea, but I want to know how to get an item with enchants in sign, just to learn.
     
  4. Offline

    Strahan

    Well, you'd have to make an interface in the sign then that tells you the specification for the item being sold. Something like:

    [buy]
    1
    diamond_sword
    sharpness:3

    Maybe develop a system of shorthand so if you want a diamond sword with sharpness 2, unbreaking 3 and fire aspect you could have like

    [buy]
    1
    diamond_sword
    s2 u3 f

    You're limited to 15 characters per line so perhaps also parse the item for sale line for enchant codes in case someone wants to sell a super OP item with tons of enchants.

    EDIT: I was curious about how I'd go about implementing this myself, so I fired up my testbed plugin to add buy sign functionality. Not sure if it's the most efficient method, but it works good for me with both material names and the old "magic numbers" style. I gave myself a pretty OP sword with a sign like:

    [buy]
    1
    276 b5 fa2 k2 l
    s5 sm5 u3

    Yes, some of those are redundant and some don't even belong on a sword, lol but I just wanted to cram a bunch in to test. I didn't add any validation code, it will let you do whatever you want. I figured that kind of code is simple enough for anyone to implement themselves. This is, after all, just proof of concept not click and drop production ready.

    Code:
    Map<String, Enchantment> enchTranslation = new HashMap<>();
    
    @Override
    public void onEnable() {
      getServer().getPluginManager().registerEvents(this, this);
    
      enchTranslation.put("A",Enchantment.WATER_WORKER);
      enchTranslation.put("B",Enchantment.DAMAGE_ARTHROPODS);
      enchTranslation.put("BP",Enchantment.PROTECTION_EXPLOSIONS);
      enchTranslation.put("D",Enchantment.DEPTH_STRIDER);
      enchTranslation.put("E",Enchantment.DIG_SPEED);
      enchTranslation.put("FF",Enchantment.PROTECTION_FALL);
      enchTranslation.put("FA",Enchantment.FIRE_ASPECT);
      enchTranslation.put("FP",Enchantment.PROTECTION_FIRE);
      enchTranslation.put("FL",Enchantment.ARROW_FIRE);
      enchTranslation.put("F",Enchantment.LOOT_BONUS_BLOCKS);
      enchTranslation.put("I",Enchantment.ARROW_INFINITE);
      enchTranslation.put("K",Enchantment.KNOCKBACK);
      enchTranslation.put("L",Enchantment.LOOT_BONUS_MOBS);
      enchTranslation.put("LS",Enchantment.LUCK);
      enchTranslation.put("LU",Enchantment.LURE);
      enchTranslation.put("PO",Enchantment.ARROW_DAMAGE);
      enchTranslation.put("PP",Enchantment.PROTECTION_PROJECTILE);
      enchTranslation.put("P",Enchantment.PROTECTION_ENVIRONMENTAL);
      enchTranslation.put("PU",Enchantment.ARROW_KNOCKBACK);
      enchTranslation.put("R",Enchantment.OXYGEN);
      enchTranslation.put("S",Enchantment.DAMAGE_ALL);
      enchTranslation.put("ST",Enchantment.SILK_TOUCH);
      enchTranslation.put("SM",Enchantment.DAMAGE_UNDEAD);
      enchTranslation.put("T",Enchantment.THORNS);
      enchTranslation.put("U",Enchantment.DURABILITY);
    
      // Following are not valid in 1.8
      //enchTranslation.put("CH",Enchantment.CHANNELING);
      //enchTranslation.put("FW",Enchantment.FROST_WALKER);
      //enchTranslation.put("IM",Enchantment.IMPALING);
      //enchTranslation.put("LO",Enchantment.LOYALTY);
      //enchTranslation.put("M",Enchantment.MENDING);
      //enchTranslation.put("MS",Enchantment.MULTISHOT);
      //enchTranslation.put("PI",Enchantment.PIERCING);
      //enchTranslation.put("Q",Enchantment.QUICK_CHARGE);
      //enchTranslation.put("RT",Enchantment.RIPTIDE);
      //enchTranslation.put("SS",Enchantment.SOUL_SPEED);
      //enchTranslation.put("SE",Enchantment.SWEEPING_EDGE);
    }
    
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
      if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return;
      if (e.getClickedBlock() == null) return;
      if (!(e.getClickedBlock().getState() instanceof Sign)) return;
    
      Sign s = (Sign)e.getClickedBlock().getState();
      String l1 = s.getLine(0).toLowerCase();
      if (!l1.contains("[buy]")) return;
    
      String loc = e.getClickedBlock().getLocation().toString();
      try {
        int qty = Integer.valueOf(s.getLine(1));
        String[] tmp = s.getLine(2).toLowerCase().split(" ");
        if (tmp.length == 0) {
          getLogger().warning("** Malformed sign at " + loc);
          return;     
        }
     
        ItemStack item = null;
        if (tmp[0].contains(":")) {
          String[] deprecatedItemCode = tmp[0].split(":");
          int itemCode = Integer.valueOf(deprecatedItemCode[0]);
          short itemData = Short.valueOf(deprecatedItemCode[1]);
          item = new ItemStack(itemCode, qty, itemData);
        } else {
          Material mat = Material.matchMaterial(tmp[0]);
          if (mat != null) item = new ItemStack(mat, qty);
        }
    
        if (item == null) {
          getLogger().warning("** Malformed sign at " + loc);
          return;     
        }
    
        ItemMeta im = item.getItemMeta();
        if (!addEnchant(im, StringUtils.join(tmp, " ", 1, tmp.length)) || !addEnchant(im, s.getLine(3))) {
          getLogger().warning("** Malformed sign at " + loc);
          return;     
        }
     
        item.setItemMeta(im);
        e.getPlayer().getInventory().addItem(item);
      } catch (NumberFormatException ex) {
        getLogger().warning("** Malformed sign at " + loc);
        return;     
      }
    }
    
    private boolean addEnchant(ItemMeta im, String data) {
      if (data.trim() == "") return true;
      for (String enchcode : data.split(" ")) {
        try {
          String enchID = enchcode.replaceAll("[^A-Za-z]+", "").toUpperCase();
          String enchLV = enchcode.replaceAll("[^0-9]+", "");
          if (!enchTranslation.containsKey(enchID)) return false;
       
          int lv = enchLV == "" ? 1 : Integer.valueOf(enchLV);
          im.addEnchant(enchTranslation.get(enchID), lv, true);
        } catch (NumberFormatException ex) {
          return false;
        }
      }
      return true;
    }
     
    Last edited: Sep 6, 2021
Thread Status:
Not open for further replies.

Share This Page