Solved Standards when making lots of custom items

Discussion in 'Spigot Plugin Development' started by tanko_, Aug 16, 2021.

  1. Hi, I am trying to focus on making sure quality of my plugin is high, what is the best way to go about making lots of custom items. example I will use is making lots of custom crop items.

    Method 1:

    Create a file called Crops and have lots of private getters that each have a ItemStack


    Method 2:

    Create a seperate class for each Item that has its own ItemStack property

    What is the best way to go about it, recently been doing method 2 but am not sure if I am creating lots of files unecessarily?
     
  2. Definitely the second option.

    If you haven't already, I would make an abstract class "CustomCrop" with all the abstract methods that each custom crop would implement. Then each class would extend "CustomCrop" and implement those methods.
     
  3. Great, I am glad what ive been doing is right. yes I may make a parent class but the only individual trait I have for crops at least is their base price so it would not be much of a useful file.
     
    #3 tanko_, Aug 16, 2021
    Last edited: Aug 16, 2021
  4. Heavily depends on what you actually want to do with the instances. I.e. what data do they store, what methods do they contain, how different are they, how important is strict typing?

    There are also more possible answers, like for example constants and enums.
     
  5. I disagree, definitely make a parent class. This approach is always preferred as it is easier to maintain and scale your plugin.
     
  6. Hi, here is what I have got for my parent and example of a child class, any improvements before I go make the rest like this?

    Base Crop Class:
    Code (Java):

    protected ItemStack item;
    protected Integer price;
    protected MerchantTrading mt;
    protected Integer weight;
    public BaseCrop(String type, MerchantTrading mt){
        this.mt = mt;
        price = mt.getConfig().getInt("merchants.crops." + type + ".price");
        weight = mt.getConfig().getInt("merchants.crops." + type + ".weight");
    }
     
    Beetroot Class:
    Code (Java):

    public Beetroot(String type,MerchantTrading mt){
        super(type, mt);
        this.item = createItem();
    }

    private ItemStack createItem() {
        ItemStack beetroot = new ItemStack(Material.BEETROOT);
        ItemMeta beetrootMeta = beetroot.getItemMeta();
        beetrootMeta.setDisplayName(ChatColor.WHITE + "Beetroot");
        NamespacedKey price = new NamespacedKey(mt, "price");
        beetrootMeta.getPersistentDataContainer().set(price, PersistentDataType.INTEGER,this.price);
        NamespacedKey weight = new NamespacedKey(mt, "weight");
        beetrootMeta.getPersistentDataContainer().set(weight, PersistentDataType.INTEGER,this.weight);
        ArrayList<String> beetrootLore = new ArrayList<>();
        beetrootLore.add(ChatColor.GRAY + "Price: " + ChatColor.GOLD + price);
        beetrootLore.add("");
        beetrootLore.add(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "Common Crop");
        beetrootMeta.setLore(beetrootLore);
        beetroot.setItemMeta(beetrootMeta);
        return beetroot;
    }

    public ItemStack getItem() {
        return item;
    }
     
     
  7. createItem and getItem could both be declared abstract methods in BaseCrop
     
  8. Another approach would be to make createItem abstract while implementing getItem in the super class. Avoids some unnecessary code duplication.
    Code (Java):
    protected ItemStack item;
    // ...

    public BaseCrop(String type, MerchantTrading mt){
        // ...
        this.item = createItem();
    }

    protected abstract ItemStack createItem();

    public ItemStack getItem() {
        return this.item;
    }
     
    • Like Like x 1
  9. You're right, said that wrong
     
  10. So all the specialized class does is implementing createItem? In that case you might as well create an enum where you pass the item in the constructor.
     
  11. Yes I could it was only because the name, material etc isnt the same, though I could pass those as parameters the super very easily
     
  12. Yes this group of items in particular only is an item, dont really have any special functions, I'll do some research on enums too, havent really used them much before, Cheers :)