can this be improved?

Discussion in 'Plugin Development' started by guitargun, Mar 31, 2015.

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

    guitargun

    a while ago I asked is someone knew code for prerequisite checking, after a few days I found a way to do it but it had a lot of hardcoded stuff in it. I am improving this code and came with this:
    Code:
        // mapping the tier2 to tier3
            for (Critters tl : Critters.values()) {
                Critters t = tl;
                while (t.hasPrereq()) {
    
                    if (t.hasPrereq() && !t.getPrereq().hasPrereq() && t != tl) {
                        if (tier2.get(t) == null) {
                            tier2.put(t, new ArrayList<Critters>(Arrays.asList(tl)));
                        } else {
                            tier2.get(t).add(tl);
                        }
                        if (playerhasininv.contains(tl.getName())) {
                            playerhas++;
                        }
    
                    }
                    t = t.getPrereq();
    
                }
            }
    
            //counting how much the max is for one subtier
            HashMap<Critters, Integer> tier2c = new HashMap<Critters, Integer>();
            for (Critters tl : Critters.values()) {
                if (tier2.get(tl) != null) {
                    for (Critters inlist : tier2.get(tl)) {
                        if (playerhasininv.contains(inlist.getName())) {
                            if (tier2c.get(tl) == null) {
                                tier2c.put(tl, 1);
                            } else {
                                tier2c.put(tl, (tier2c.get(tl) + 1));
                            }
                        }
                    }
                }
            }
            //totalmax is for the whole tier3 including all branches
            int totalmax = 0;
            for (Critters keys : tier2.keySet()) {
                totalmax += tier2.get(keys).size();
            }
            System.out.println(tier2);
            System.out.println(tier2c);
            for (Critters tl : Critters.values()) {
                Critters t = tl;
                //basic standard parameters
                ItemStack in = new ItemStack(tl.getType());
                ItemMeta m = in.getItemMeta();
                ArrayList<String> lore = tl.getModifiers();
               
                if(tl.getPassive() != null){
                    lore.add(ChatColor.GREEN + tl.getPassive());
                }
                //basic check if the player doesn't have the item and doesn't have the maximum
                if (totalmax != playerhas && !playerhasininv.contains(tl.getName())) {
                    //check if it is tier 2
                    if (tier2.get(tl) != null) {
                            //basic check if the player hasn't the maximum of that branch
                        if (tier2c.get(tl) != null && tier2.get(tl).size() == tier2c.get(tl)) {
                            m.setLore(tl.getModifiers());
                            continue;
                        }
                        //adding the cost
                        int cost = 0;
                        t = tl;
                        while (t.hasPrereq()) {
                            if (!playerhasininv.contains(t.getName())) {
                                cost += t.getCost();
                            }
                            t = t.getPrereq();
                        }
                        lore.add(ChatColor.GOLD + Integer.toString(cost));
                    }else{
                        int cost = t.getCost();
                        t = tl;
                        while(t.hasPrereq()){
                            t = t.getPrereq();
                            if(!playerhasininv.contains(t.getName())){
                                cost += t.getCost();
                            }
                        }
                        lore.add(ChatColor.GOLD + Integer.toString(cost));
                       
                    }
                }
                m.setLore(lore);
                m.setDisplayName(tl.getName());
                in.setItemMeta(m);
                inv.addItem(in);
            }

    this is the class I tested it for:
    Code:
    package testsite;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    
    import org.bukkit.Material;
    
    public enum Critters {
        /**
         * To add another item
         * name
         * Material
         * the passive (null for no passive)
         * new ArrayList<String>(Arrays.asList('modifyer')
         * cost
         * prereq (null for no prereq) from bottem to top (see .jpeg) also in arraylist
         */
       
            //tier 1
        SMALLSWORD("Small Sword", Material.STONE_SWORD, null, new ArrayList<String>(Arrays.asList("Damage +2", "Crit 5%")), 850, null),
            //tier 2
        CUTLAS("Cutlas", Material.IRON_SWORD, null, new ArrayList<String>(Arrays.asList("Damage +4", "Crit 10%")), 1700, Critters.SMALLSWORD),
        EXPLOSIVE_BOW("Explosive Bow", Material.BOW, null, new ArrayList<String>(Arrays.asList("Damage +4", "Crit 5%")), 2550, Critters.SMALLSWORD),
            //tier 3
        LONGSWORD("Long Sword", Material.DIAMOND_SWORD, "Passive damage +4 with every protect item. Max 3 stacks", new ArrayList<String>(Arrays.asList("Damage +6", "Crit 15%")), 2300, Critters.CUTLAS),
        RAGEAXE("Rage Axe", Material.DIAMOND_AXE, "Passive missed crit +5%. Max 4 stack (reset oncrit)", new ArrayList<String>(Arrays.asList("Damage +5", "Crit 20%")), 2125, Critters.CUTLAS),
        GOLD_BOW("Golden Bow", Material.BOW, "Passive lifesteal +4% for every 2 kills. Max 3 stacks", new ArrayList<String>(Arrays.asList("Damage +6", "Crit 15%")), 2200,Critters.EXPLOSIVE_BOW);
       
       
        final private String name;
        final private Material type;
        final private String passive;
        final private ArrayList<String> modifiers;
        final private int cost;
        final private Critters prereq;
       
        Critters(String name, Material type, String passive, ArrayList<String> modifiers, int cost, Critters prereq){
            this.name = name;
            this.type = type;
            this.passive = passive;
            this.modifiers = modifiers;
            this.cost = cost;
            this.prereq = prereq;
        }
    
        public static Critters returncritter(String name){
                for(Critters critter : values()){
                    if(critter.getName().equals(name)){
                        return critter;
                    }
                }
                return null;
        }
       
       
        public String getName() {
            return name;
        }
    
        public Material getType() {
            return type;
        }
    
        public String getPassive() {
            return passive;
        }
    
        public ArrayList<String> getModifiers() {
            return modifiers;
        }
    
        public int getCost() {
            return cost;
        }
    
        public Critters getPrereq() {
            return prereq;
        }
       
        public static ArrayList<String> returnmax(){
            ArrayList<String> count = new ArrayList<String>();
            for(Critters piece : Critters.values()){
                if(piece.getPrereq() != null){
                    if(piece.getPrereq().getPrereq() != null){
                        count.add(piece.getName());
                    }
                }
            }
            return count;
        }
       
        public boolean hasPrereq(){
            if(prereq != null){
                return true;
            }else{
                return false;
            }
        }
    }
    
    is there a way to improve this?
     
  2. Offline

    1Rogue

    ...Why does your "Critters" enum contain constants for items?
     
  3. Offline

    guitargun

    The constants are representations of items the player can buy. I did this because for a simpler version instead of a huge list of classes/itemstacks each item. I did a huge list of itemstacks first but that didn't really work out to prevent the hard-coded stuff. The constants are used for the shop only and I thought this was a better way for storing and using it

    If you are refering to the number after the string arraylist that is the price of that item. If for the prerequisite it was the only way I thought of achieving this.
     
  4. Offline

    1Rogue

    ...Why is it called "Critters"?
     
  5. Offline

    guitargun

    @1Rogue it's called critters because these items are defined on giving a critical hit chance if bought (this is for a arena plugin) same as I have other enums that define their subject of items (armors for protect items, aura for aura items, etc.) I use it to read it back easy and if I need to add another item I know where it belongs without searching for its place
     
Thread Status:
Not open for further replies.

Share This Page