How to retrieve item values from a string list

Discussion in 'Spigot Plugin Development' started by DeathNote_15, Jun 12, 2015.

  1. Hey guys,

    So I'm wondering exactly what is in the title. If its unclear, this is what I mean:

    I have a string list(s) in my config, for example
    Code (Text):
    allowed-items:
      - 2
      - 3:1
      - 1623:1513631
      - you get the point, right?
    Now, I want in my code to check if an item being crafted (or brewed, picked up, thrown, etc. Code shouldn't be very different for each case, so I only need an example of one) is equal to an item in that String List. How would I go on about to do that? Heres some code I'm using, for example:

    Code (Text):
        @EventHandler
        public void craftItem(PrepareItemCraftEvent e) {
            Material itemType = e.getRecipe().getResult().getType();
            Byte itemData = e.getRecipe().getResult().getData().getData();
            if(itemType == /* Heres where I want it to check if its equal to an item in a string list */) {
                e.getInventory().setResult(new ItemStack(Material.AIR));
                for(HumanEntity he:e.getViewers()) {
                    if(he instanceof Player) {
                        ((Player)he).sendMessage(ChatColor.RED+"You cannot craft this!");
                    }
                }
            }
        }
    Suggestions?
     
  2. Code (Text):
        public boolean doesitequal(Material m) {
            for(String item : getConfig().getStringList("items")) {
                if(Material.getMaterial(item) == m) {
                    return true;
                }
            }
            return false;
        }
     
    • Winner Winner x 1
  3. I could hear the butt-hurt ppl already screaming for spoon feeding :D
     
  4. Naa I wanna implement my string list checker right in that of statement. Thanks for the reply tho
     
  5. From the List you get from the config, you could use the String.split() method to split the string into two parts, the Material id and the byte data. You can perform a simple check to see whether there does happen to be a data component. Afterwards, in your if statement, you can check if A) itemType == Material from the material id && B) itemData = byte from byte data.
     
    • Useful Useful x 1
  6. I have a better way :D

    Code (Text):
    public boolean DoesItEqual(Material m){
    if(getConfig().getStringList("items").contains(m.getId())){
    return true;
    }
    return false;
    }
     
  7. Of course, let's use deprecated item ids, that's the best way to do it.