Auto-Selling Items

Dec 19, 2020
Auto-Selling Items
  • First you want to create a YML file or just use the default config and set each item and the the cost there.
    EX:
    Code (YAML):
    IRON_INGOT:
      cost
    : 10.0
    GOLD_INGOT
    :
      cost
    : 15.0
    DIAMOND
    :
      cost
    : 30.0
    I would also add enabled worlds to the configuration, but that's up to you.

    Then all you have to do is get all of the first level paths using: JavaPlugin#getConfig()#getKeys(false) This will return a set of strings, which will be your list of materials. Then loop through the config and get the price for each individual material and put it in a HashMap like this:
    Code (Java):
    Set<String> materials = JavaPlugin.getConfig.getKeys(false);
    HashMap<String, double> prices = new HashMap<>();
     
    for (String item : materials)
      {
      double cost = materials.get(item);
      prices.put(item, cost);
    }
    I would recommend using BlockBreakEvent as you can get the block drops as such: event#getBlock()#getDrops(e#getItemInHand) This method accounts for fortune drops so you don't have to worry about coding that either.

    Loop through the collection of ItemStacks you just made above and see if the set of keys contains the item's material.

    Then all you have to do is get and set the price!
    Code (Java):
    for(ItemStack item : drops)
    {
    String name = item.getType().toString();
      if(prices.keySet().contains(name))
      {
      double cost = prices.get(name);
      //Do things
      }
    }
    Then prevent the drops using: event#setDropItems(false)
  • Loading...
  • Loading...