RuneScape 2 Development > Snippets

[PI] ItemDefinitions from JSON + ItemDef editor.

Pages: (1/7) >>>

sk8rdude461:

I take 0 credit in creating the ItemDefinitions. It was made by Galkon. View the original thread

I made some slight modifications to his system (just named some variables a smidge different, and removed 2 ints). That is why I'm including it along with the release of my itemdef editor, as it was made for my system (I'm sure it could load his, with a couple minor edits).

ItemDefinitions.java

--- Code: Java ---package server.model.items; import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException; import com.google.gson.Gson;import com.google.gson.JsonIOException;import com.google.gson.JsonSyntaxException; import server.model.items.WieldType;import server.util.Misc; /** * 20,0073 Item definitions, represents a single item definition which * is held in the array {@link #definitions}. *  * In the making of this I used a lot of the data Dustin provided, but  * added a lot more in. *  * @Author Dustin Greyfield * @Author Maui * Modified by Sk8rdude461 */public class ItemDefinitions {                /**         * The array of definitions for items in the game         */        private static ItemDefinitions[] definitions = new ItemDefinitions[20073];                /**         * Gets the definitions for public access         *          * @return - definitions         */        public static ItemDefinitions[] getDefinitions() {                return definitions;        }                public static ItemDefinitions[] setDefinitions(final ItemDefinitions[] read) {                return definitions = read;        }                /**         * The id of the item         */        private final int id;                /**         * The name of the item         */        private final String name;                /**         * The description of the item         */        private final String description;                /**         * The flag determining if an item is tradeable         */        private final boolean tradeable;                /**         * The flag determining whether an item is destroyable         */        private final boolean destroyable;                /**         * The flag representing whether an item is stackable         */        private final boolean stackable;                /**         * The item's shopPrice         */        private final int shopPrice;                /**         * The price for special stores         */        private final int[] specialPrice;                /**         * The items low alchemy value         */        private final int lowAlchemy;                /**         * The item's high alchemy value         */        private final int highAlchemy;                /**         * The item's weight         */        private final double weight;                /**         * The flag determing if an item is noted         */        private final boolean noted;                /**         * the flag representing whether the item is noteable         */        private final boolean noteable;                /**         * The flag representing if an item is 2 handed         */        private final boolean isTwoHanded;                /**         * The flag representing the equipment type of an item         */        private final WieldType equipmentType;                /**         * The flag representing if an item is a isWeapon         */        private final boolean isWeapon;                /**         * A flag representing if this is a full body item         */        private final boolean fullBody;                /**         * A flag representing if this is a full hat item         */        private final boolean fullHat;                /**         * A flag representing if this is a full mask         */        private final boolean fullMask;                /**         * The bonuses of the item         */        private final double[] bonus;                /**         * The item's levelRequirementss         */        private final int[] levelRequirements;                /**         * The actions this item has         */        private final String[] actions;         /**         * The complete Item Definition         */        public ItemDefinitions(final int id, final String name, final String description,                        final boolean tradeable, final boolean destroyable, final boolean stackable,                        final int shopPrice, final int[] specialPrice, final int lowAlchemy, final int highAlchemy, final double weight,                         final boolean noted, final boolean noteable,                        final boolean isTwoHanded, final WieldType equipmentType, final boolean isWeapon,                         final boolean fullBody, final boolean fullHat, final boolean fullMask, final double[] bonus,                        final int[] levelRequirements, final String[] actions) {                this.id = id;                this.name = name;                this.description = description;                this.tradeable = tradeable;                this.destroyable = destroyable;                this.stackable = stackable;                this.shopPrice = shopPrice;                this.specialPrice = specialPrice;                this.lowAlchemy = lowAlchemy;                this.highAlchemy = highAlchemy;                this.weight = weight;                this.noted = noted;                this.noteable = noteable;                this.isTwoHanded = isTwoHanded;                this.isWeapon = isWeapon;                this.equipmentType = equipmentType;                this.fullBody = fullBody;                this.fullHat = fullHat;                this.fullMask = fullMask;                this.bonus = bonus;                this.levelRequirements = levelRequirements;                this.actions = actions;        }                @Override public String toString() {                return "ITEM[" + id + "," + name + "]";        }         /**         *  @return - id         */        public int getId() {                return id;        }                /**         *  @return - name         */        public String getName() {                return name;        }         /**         *  @return - description         */        public String getDescription() {                return description;        }         /**         *  @return - tradeable         */        public boolean isTradeable() {                return tradeable;        }         /**         *  @return - destroyable         */        public boolean isDestroyable() {                return destroyable;        }         /**         *  @return - stackable         */        public boolean isStackable() {                return stackable;        }         /**         *  @return - shopPrice         */        public int getShopPrice() {                return shopPrice;        }         /**         * @return the specialPrice         */        public int[] getSpecialPrice() {                return specialPrice;        }         /**         *  @return - lowAlchemy         */        public int getLowAlchemy() {                return lowAlchemy;        }         /**         *  @return - highAlchemy         */        public int getHighAlchemy() {                return highAlchemy;        }         /**         *  @return - weight         */        public double getWeight() {                return weight;        }         /**         *  @return - noted         */        public boolean isNoted() {                return noted;        }         /**         * @return - noteable         */        public boolean isNoteable() {                return noteable;        }         /**         *  @return - isTwoHanded         */        public boolean isTwoHanded() {                return isTwoHanded;        }         /**         *  @return - equipmentType         */        public WieldType getEquipmentType() {                return equipmentType;        }         /**         *  @return - isWeapon         */        public boolean isWeapon() {                return isWeapon;        }         /**         * @return the fullBody         */        public boolean isFullBody() {                return fullBody;        }         /**         * @return the fullHat         */        public boolean isFullHat() {                return fullHat;        }         /**         * @return the fullMask         */        public boolean isFullMask() {                return fullMask;        }         /**         *  @return - bonus         */        public double[] getBonus() {                return bonus;        }         /**         *  @return - levelRequirements         */        public int[] getLevelRequirement() {                return levelRequirements;        }         /**         *  @return - actions         */        public String[] getActions() {                return actions;        }                public boolean isWieldable() {                String menu = actions[1].toLowerCase();                return (menu.equals("wear") || menu.equals("wield") || menu.equals("equip"));        }                /**     * Parse the item definitions.     *      * @throws Exception     *         if any errors occur while parsing this file.     */    public static void loadItemDefinitions() throws JsonIOException, JsonSyntaxException, FileNotFoundException {        Gson gson = new Gson();                final long start = System.currentTimeMillis();                System.out.println("Loading item definitions...");                try (final FileReader reader = new FileReader("./Data/cfg/itemdef.json")) {                        ItemDefinitions.setDefinitions(gson.fromJson(reader, ItemDefinitions[].class));                } catch (IOException e) {                        System.out.println("Failed to load item definitions!");                        System.exit(0);                } finally {                        System.out.println("Loaded " + ItemDefinitions.getDefinitions().length + " item definitions in " + (System.currentTimeMillis() - start) + "ms.");                }    }}
Server.java add this in. Put it somewhere in the main method.

--- Code: Java ---                        ItemDefinitions.loadItemDefinitions(); 
Here's some extra methods for you, to convert your system from the ItemList to the ItemDef's. (idk if I got em all)
ItemAssistant.java replace each method with the ones I've provided.
getBonus

--- Code: Java ---        public void getBonus() {                for (int i = 0; i < c.playerEquipment.length; i++) {                        if (c.playerEquipment[i] > -1) {                                for (int k = 0; k < c.playerBonus.length; k++) {                                        c.playerBonus[k] += ItemDefinitions.getDefinitions()[c.playerEquipment[i]].getBonus()[k];                                }                        }                }        }is2handed

--- Code: Java ---        public boolean is2handed(String itemName, int itemId) {// I was too lazy to remove the String parameter.                return ItemDefinitions.getDefinitions()[itemId].isTwoHanded();        }In wearItem, remove anything related to changing/getting the targetSlot.
Then, where targetSlot is declared within the method, replace it with this:

--- Code: Java ---                        int targetSlot = ItemDefinitions.getDefinitions()[wearID].getEquipmentType().getSlot();
Note: I forgot to do level reqs. Sorry.
getItemName

--- Code: Java ---         public static String getItemName(int ItemID) {                if(ItemID < 0)                        return "Unarmed";                return ItemDefinitions.getDefinitions()[ItemID].getName();        }getItemId (I know I could have done this better)

--- Code: Java ---        public int getItemId(String itemName) {                String item = itemName.trim();                item = item.replaceAll("_", " ");                for (int i = 0; i < Config.ITEM_LIMIT; i++) {                        String toCheck = ItemDefinitions.getDefinitions()[i].getName();                        toCheck = toCheck.trim();                        toCheck = toCheck.replaceAll("_", " ");                        if(toCheck.equalsIgnoreCase(item))                                return ItemDefinitions.getDefinitions()[i].getId();                }                return -1;        }getUnnotedItem (again, did this shitty, this was based off how it was handled previous)

--- Code: Java ---        public int getItemId(String itemName) {                String item = itemName.trim();                item = item.replaceAll("_", " ");                for (int i = 0; i < Config.ITEM_LIMIT; i++) {                        String toCheck = ItemDefinitions.getDefinitions()[i].getName();                        toCheck = toCheck.trim();                        toCheck = toCheck.replaceAll("_", " ");                        if(toCheck.equalsIgnoreCase(item))                                return ItemDefinitions.getDefinitions()[i].getId();                }                return -1;        }Oh and in ShopAssistant, you can do this for getting the value:

--- Code: Java ---        public int getItemShopValue(int itemId) {                if (itemId < 0)                        return 0;                return ItemDefinitions.getDefinitions()[itemId].getShopPrice();        }
Lastly, here is my itemdef editor along with my json file (goes in Data/cfg/ on a pi).
Click here to download[/url]
Have fun.


Here's a preview of the item editor. Idgaf how bad it looks.

Edit:
This is to replace the method getRequirements(String itemName, int itemId).


--- Code: Java ---        public boolean canWearItem(int item) {                ItemDefinitions itemDef = ItemDefinitions.getDefinitions()[item];                if(!itemDef.isWieldable()) {                        c.sendMessage("You're not allowed to wear that item.");                        return false;                }                for(int i = 0; i < c.playerLevel.length; i++) {                        if(c.playerLevel[i] < itemDef.getLevelRequirement()[i]) {                                c.sendMessage("You need a " + c.getLevels().getNameForSkillID(i) +                                                " level of " + itemDef.getLevelRequirement()[i] +                                                " to wear a " + getItemName(item) + ".");                                return false;                        }                }                return true;        }In wearItem, replace where the getRequirements method was called with


--- Code: Java ---                        if(!canWearItem(wearID))                                return false;

Dan0194:

this method seems pretty useless



--- Code: Java(TM) 2 Platform Standard Edition 5.0 ---            public boolean is2handed(String itemName, int itemId) {// I was too lazy to remove the String parameter.                    return ItemDefinitions.getDefinitions()[itemId].isTwoHanded();            }
why not just do.


--- Code: Java(TM) 2 Platform Standard Edition 5.0 ---ItemDefinitions.getDefinitions()[itemId].isTwoHanded();
Just a suggestion start using foreach statements they look cleaner


--- Code: Java(TM) 2 Platform Standard Edition 5.0 ---            public int getItemId(String itemName) {                    String item = itemName.trim();                    item = item.replaceAll("_", " ");                    for (ItemDefinitions definition : definitions) {                            name = definition.getName().replaceAll("_", " ").trim();                            if(name.equalsIgnoreCase(item))                                     return definition.getId();                    }                    return -1;            }

sk8rdude461:


--- Quote from: Dan0194 on July 17, 2014, 10:50:28 AM ---this method seems pretty useless



--- Code: Java(TM) 2 Platform Standard Edition 5.0 ---            public boolean is2handed(String itemName, int itemId) {// I was too lazy to remove the String parameter.                    return ItemDefinitions.getDefinitions()[itemId].isTwoHanded();            }
why not just do.


--- Code: Java(TM) 2 Platform Standard Edition 5.0 ---ItemDefinitions.getDefinitions()[itemId].isTwoHanded();
--- End quote ---
I was just directly replacing the methods instead of where ever it was declared. However, I'm sure I'd eventually remove that method.


--- Quote from: Dan0194 on July 17, 2014, 10:50:28 AM ---Just a suggestion start using foreach statements they look cleaner


--- Code: Java(TM) 2 Platform Standard Edition 5.0 ---            public int getItemId(String itemName) {                    String item = itemName.trim();                    item = item.replaceAll("_", " ");                    for (ItemDefinitions definition : definitions) {                            name = definition.getName().replaceAll("_", " ").trim();                            if(name.equalsIgnoreCase(item))                                     return definition.getId();                    }                    return -1;            }
--- End quote ---
I use them every now and then. I tend to forget to use them over the standard for statement. And there are times when I don't want to use it (rare, but happens).

Dan0194:


--- Quote from: sk8rdude461 on July 17, 2014, 11:01:21 AM ---
--- Quote from: Dan0194 on July 17, 2014, 10:50:28 AM ---this method seems pretty useless



--- Code: Java(TM) 2 Platform Standard Edition 5.0 ---            public boolean is2handed(String itemName, int itemId) {// I was too lazy to remove the String parameter.                    return ItemDefinitions.getDefinitions()[itemId].isTwoHanded();            }
why not just do.


--- Code: Java(TM) 2 Platform Standard Edition 5.0 ---ItemDefinitions.getDefinitions()[itemId].isTwoHanded();
--- End quote ---
I was just directly replacing the methods instead of where ever it was declared. However, I'm sure I'd eventually remove that method.


--- Quote from: Dan0194 on July 17, 2014, 10:50:28 AM ---Just a suggestion start using foreach statements they look cleaner


--- Code: Java(TM) 2 Platform Standard Edition 5.0 ---            public int getItemId(String itemName) {                    String item = itemName.trim();                    item = item.replaceAll("_", " ");                    for (ItemDefinitions definition : definitions) {                            name = definition.getName().replaceAll("_", " ").trim();                            if(name.equalsIgnoreCase(item))                                     return definition.getId();                    }                    return -1;            }
--- End quote ---
I use them every now and then. I tend to forget to use them over the standard for statement. And there are times when I don't want to use it (rare, but happens).

--- End quote ---

yah happens with me sometimes when i actually need to use the variable in the loop.

Pure_:

// I was too lazy to remove the String parameter.

Pages: (1/7) >>>

Go to full version