Thread: [Elvarg] Wardrobe: Drop-in configurable dialogue based robe set spawning object.

Results 1 to 6 of 6
  1. #1 [Elvarg] Wardrobe: Drop-in configurable dialogue based robe set spawning object. 
    Donator

    Join Date
    Feb 2017
    Posts
    99
    Thanks given
    0
    Thanks received
    10
    Rep Power
    36
    My code basically allows you to turn any object with a click option into a vending machine for robes. By default all canifis and gnome robes (basic colored robes with no stats or significant value) are enabled and free. To add or remove the robes or change their price simply edit the enum. The dialogues are built off the enum values and will update automatically. This was designed to be added to Elvarg servers with minimal effort just drag and drop the file into the correct package (or edit the package) and add these two lines of code to your object handler. Don't forget to change the objectId from -1. For more details and redundant details open the spoiler and read the comments I left in the file.


    Code:
    if(Wardrobe.handleObjectClick(player, object))
    return;
    Spoiler for Wardrobe.java:

    Code:
    package com.elvarg.game.model;
    
    import com.elvarg.game.entity.impl.object.GameObject;
    import com.elvarg.game.entity.impl.player.Player;
    import com.elvarg.game.model.dialogue.*;
    import com.elvarg.util.Misc;
    
    import java.util.ArrayList;
    import java.util.Optional;
    
    public class Wardrobe {
    
        /**
         *
         ** What is this?:
         *
         * When I made my server I had an idea to create an interface for a wardrobe which you allow players to change the colors of their robes for a small fee.
         * Well this is a far less fancy and uses dialogues to allow players to choose which robes they want to spawn.
         *
         **
         *
         * Features:
         * - Item sets can be turned on or off and automatically added or removed from the dialogue menu by means of boolean in the enum.
         * - Configurable buy price for each item set.
         * - Easy to add more item sets.
         * - Easy to install.
         *
         **
         *
         * Installing:
         * - This was developed for elvarg if you have another source and want to adapt it figure it out.
         * - Drop this file in Wardrobe.java at com.elvarg.game.model
         * - Stick the following code in your object handler packet:
         *
         *          if(Wardrobe.handleObjectClick(player, object))
         * 					return;
         *
         * - The wardrobe is currently set to the edgeville bank booth ids. You may wish to change this.
         *
         **
         *
         * Why is my code so hideously cringy?:
         * - I'm a hobbyist. Not a pro.
         * - It was designed for light weight plug and play.
         * - I habitually overcomplicate my life.
         * - I wanted it fast and dirty.
         * - Rewrite it yourself if you care to.
         *
         **
         */
    
        /** Custom Settings **/
        //For sets without gloves hats or boots (-1 in the enum) add these
        //Default: Gloves: (False, Leather Gloves), Boots: (False, Leather Boots), Hat: (False, Unspecified)
        private final boolean enAddGloves = false;
        private final int addGloveId = 1059;
        private final boolean enAddBoots = false;
        private final int addBootId = 1061;
        private final boolean enAddHat = false;
        private final int addHatId = -1;
    
        //Wardrobe Settings
        private static final int wardrobeId = 6943;
        private static final boolean checkPosition = false;
        private static final Position[] wardrobePositions = new Position[] {
            //If you want only certain wardrobes in certain locations to function place their positions here and turn checkPosition to true.
        };
    
        //Financial Settings
        //The point at which the player is shown a confirmation dialogue before being allowed to purchase.
        private static final int purchaseWarningThreshold = 25_000;
    
        //Message Settings
        private final boolean sendBlankSetMsg = true;
        private final String blankSetMsg = "More sets coming soon.";
    
        //If enabled will give wizard boots with the wizard sets. Warning: Could break economy. May wanna look into that first.
        private final boolean giveWizardBoots = false;
    
        /**
         * The boolean value determines if the set shows up in the dialogue.
         * The integers are hat, top, bottom, gloves, boots respectively.
         * The last value is the price. 0 = free.
         */
        private enum WardrobeData {
    
            GNOME_PINK(656, 646, 636, -1, 626, true, 0),
            GNOME_GREEN(658, 648, 638, -1, 628, true, 0),
            GNOME_BLUE(660, 650, 640, -1, 630, true, 0),
            GNOME_CREAM(662, 652, 642, -1, 632, true, 0),
            GNOME_TURQUOISE(664, 654, 644, -1, 634, true, 0),
    
            CANIFIS_GREY(2900, 2896, 2898, 2902, 2894, true, 0),
            CANIFIS_RED(2910, 2906, 2908, 2912, 2904, true, 0),
            CANIFIS_YELLOW(2920, 2916, 2918, 2922, 2914, true, 0),
            CANIFIS_TEAL(2930, 2926, 2928, 2932, 2924, true, 0),
            CANIFIS_PURPLE(2940, 2936, 2938, 2942, 2934, true, 0),
    
            BLUE_WIZARD(579, 577, 1011, -1, 2579, false, 0),
            BLUE_WIZARD_T(7396, 7392, 7388, -1, 2579, false, 0),
            BLUE_WIZARD_G(7394, 7390, 7386, -1, 2579, false, 0),
    
            BLACK_WIZARD_T(12455, 12451, 12447, -1, -1, false, 0),
            BLACK_WIZARD_G(12453, 12449, 12445, -1, -1, false, 0),
    
            MONK(-1, 544, 542, -1, -1, false, 0),
            MONK_G(-1, 20199, 20202, -1, -1, false, 0),
    
            PRIEST(-1, 428, 426, -1, -1, false, 0),
            SHADE(-1, 546, 548, -1, -1, false, 0),
            DRUID(-1, 540, 538, -1, -1, false, 0),
    
            ARMADYL(-1, 12253, 12255, -1, -1, false, 0),
            GUTHIX(-1, 10462, 10466, -1, -1, false, 0),
            SARADOMIN(-1, 10458, 10464, -1, -1, false, 0),
            ZAMORAK(-1, 10786, 10468, -1, -1, false, 0);
    
            private int hatId;
            private int topId;
            private int bottomsId;
            private int glovesId;
            private int bootsId;
            private boolean isActive;
            private int price;
    
            WardrobeData(int hatId, int topId, int bottomsId, int glovesId, int bootsId, boolean isActive, int price) {
                this.hatId = hatId;
                this.topId = topId;
                this.bottomsId = bottomsId;
                this.glovesId = glovesId;
                this.bootsId = bootsId;
                this.isActive = isActive;
                this.price = price;
            }
    
        }
    
        /**
         * End Custom Settings everything beyond this point is necessary for script no touchy!
         * **/
        private static final Dialogue[] dialogues = new Dialogue[getActiveSets() / 4 + 1];
        private static int optionPage = 1;
    
        static {
            for (int i = 0; i < dialogues.length; i++) {
                dialogues[i] = createDialogue(i * 4);
            }
        }
    
        private static int getActiveSets() {
            int active = 0;
            for (WardrobeData value : WardrobeData.values()) {
                if(value.isActive)
                    active++;
            }
            return active;
        }
    
        public static boolean handleObjectClick(Player player, Optional<GameObject> object) {
            GameObject obj = object.get();
            if(obj.getId() == wardrobeId) {
                if(checkPosition) {
                    for (Position wardrobePosition : wardrobePositions) {
                        if(wardrobePosition.equals(obj.getPosition())) {
                            new Wardrobe().openMenu(player);
                            return true;
                        }
                    }
                    return false;
                }
                new Wardrobe().openMenu(player);
                return true;
            }
            return false;
        }
    
        private void openMenu(Player player) {
            setOptionHandler(player);
            openDialogueByLastOption(player, 0);
        }
    
        private void setOptionHandler(Player player) {
            optionPage = 1;
            player.setDialogueOptions(new DialogueOptions() {
                @Override
                public void handleOption(Player player, int option) {
                    String playerSelection = player.getDialogue().dialogue()[option - 1];
                    if(playerSelection.equalsIgnoreCase("Next")) {
                        openDialogueByLastOption(player, (option - 1) * optionPage++);
                    } else {
                        if(!playerSelection.equalsIgnoreCase("---")) {
                            for (WardrobeData value : WardrobeData.values()) {
                                if(value.toString().equalsIgnoreCase(playerSelection.toUpperCase().replace(" (" + Misc.insertCommasToNumber(String.valueOf(value.price)) + ")", "").replace(" ", "_"))) {
                                    if(value.price > purchaseWarningThreshold) {
                                        confirmAdd(player, value);
                                    } else {
                                        addItems(player, value);
                                        player.getPacketSender().sendInterfaceRemoval();
                                    }
                                }
                            }
                        } else if(sendBlankSetMsg) {
                            player.getPacketSender().sendMessage(blankSetMsg);
                            player.getPacketSender().sendInterfaceRemoval();
                        }
                    }
                }
            });
        }
    
        private void confirmAdd(Player player, WardrobeData value) {
            player.setDialogueOptions(new DialogueOptions() {
                @Override
                public void handleOption(Player player, int option) {
                    switch (option) {
                        case 1:
                            if(player.getInventory().contains(new Item(995, value.price))) {
                                player.getPacketSender().sendInterfaceRemoval();
                                player.getInventory().delete(995, value.price);
                                addItems(player, value);
                            } else {
                                player.getPacketSender().sendMessage("You don't have enough gold in your inventory to purchase the robes.");
                                player.getPacketSender().sendInterfaceRemoval();
                            }
                            break;
                        case 2:
                            player.getPacketSender().sendInterfaceRemoval();
                            break;
                    }
                }
            });
            DialogueManager.start(player, new Dialogue() {
                @Override
                public DialogueType type() {
                    return DialogueType.OPTION;
                }
    
                @Override
                public DialogueExpression animation() {
                    return DialogueExpression.DEFAULT;
                }
    
                @Override
                public String[] dialogue() {
                    return new String[] {
                            "Confirm Purchase ("+Misc.insertCommasToNumber(String.valueOf(value.price))+")", "Nevermind",
                    };
                }
            });
        }
    
        private static Dialogue createDialogue(int lastOptionClicked) {
            return new Dialogue() {
                @Override
                public DialogueType type() {
                    return DialogueType.OPTION;
                }
    
                @Override
                public DialogueExpression animation() {
                    return DialogueExpression.DEFAULT;
                }
    
                @Override
                public String[] dialogue() {
                    return refineDialogueOptionsForPage(lastOptionClicked);
                }
            };
        }
    
        private void openDialogueByLastOption(Player player, int lastOptionClicked) {
            int toOpen = lastOptionClicked / 4;
            if(dialogues.length > toOpen)
                DialogueManager.start(player, dialogues[toOpen]);
        }
    
        /** Uses the last option clicked to determine what should be on the next page of options. **/
        private static String[] refineDialogueOptionsForPage(int lastOptionClicked) {
            ArrayList<String> options = buildAllDialogueOptionsFromEnum();
            ArrayList<String> smallOptions = new ArrayList<>();
    
            for (int i = 0; i < 4; i++) {
                smallOptions.add(i, "---");
                int index = (lastOptionClicked == 0 ? i : lastOptionClicked + i);
                if(options.size() > index) {
                    smallOptions.set(i, options.get(index));
                }
            }
    
            return new String[] {
                    smallOptions.get(0), smallOptions.get(1), smallOptions.get(2), smallOptions.get(3), "Next"
            };
        }
    
        private void addItems(Player player, WardrobeData data) {
            int reqSlots = getRequiredSlots(data);
            boolean playerHasSpace = player.getInventory().getFreeSlots() > reqSlots;
            if(playerHasSpace) {
                if(setHasHat(data)) {
                    int hatId = data.hatId == -1 ? addHatId : data.hatId;
                    player.getInventory().add(hatId, 1);
                }
                if(setHasTop(data))
                    player.getInventory().add(data.topId, 1);
                if(setHasBottoms(data))
                    player.getInventory().add(data.bottomsId, 1);
                if(setHasGloves(data)) {
                    int glovesId = data.glovesId == -1 ? addGloveId : data.glovesId;
                    player.getInventory().add(glovesId, 1);
                }
                if(setHasBoots(data)) {
                    int bootsId = data.bootsId == -1 ? addBootId : data.bootsId;
                    if(data.bootsId == 2579 && giveWizardBoots) {
                        player.getInventory().add(bootsId, 1);
                    } else if(data.bootsId != 2579) {
                        player.getInventory().add(bootsId, 1);
                    }
                }
            } else {
                player.getPacketSender().sendMessage("You don't have enough space for this item set. Required spaces: " + reqSlots + ".");
            }
        }
    
        private boolean setHasBoots(WardrobeData data) {
            return data.bootsId > -1 || enAddBoots;
        }
    
        private boolean setHasGloves(WardrobeData data) {
            return data.glovesId > -1 || enAddGloves;
        }
    
        private boolean setHasBottoms(WardrobeData data) {
            return data.bottomsId > -1;
        }
    
        private boolean setHasTop(WardrobeData data) {
            return data.topId > -1;
        }
    
        private boolean setHasHat(WardrobeData data) {
            return data.hatId > -1 || enAddHat;
        }
    
        /** Calculates the amount of space required for the set. **/
        private int getRequiredSlots(WardrobeData data) {
            int slots = 0;
            if(setHasHat(data))
                slots++;
            if(setHasTop(data))
                slots++;
            if(setHasBottoms(data))
                slots++;
            if(setHasGloves(data))
                slots++;
            if(setHasBoots(data))
                slots++;
            return slots;
        }
    
        private static ArrayList<String> buildAllDialogueOptionsFromEnum() {
            ArrayList<String> strings = new ArrayList<>();
            for (WardrobeData value : WardrobeData.values())
                if(value.isActive)
                    strings.add(value.toString().toLowerCase().replace("_", " ") + " (" + Misc.insertCommasToNumber(String.valueOf(value.price)) + ")");
            return strings;
        }
    
    }


    Quote Originally Posted by Ocelot View Post
    Good god that's a lot of comments. Why the hell didn't you just remove that big comment at the top and post It directly on the thread??
    Mainly because I kinda just wanted a summary for the thread don't want to make people read to much unless they are interested. Also... Lol...

    Quote Originally Posted by harryl View Post
    Should work on your documentation and OOP skills, none the less i like the idea
    This was designed to be standalone class beyond the explanations I threw in for kicks documentation shouldn't be necessary.
    As far as OOP skills please elaborate. Although I have a feeling it's mainly due to it being designed as a standalone class.
    Also if anyone wants to clean it up and re-post or make improvements I don't care.
    Last edited by XScapeRsps; 02-13-2019 at 01:57 AM.
    Reply With Quote  
     

  2. #2  
    Registered Member

    Join Date
    May 2012
    Posts
    120
    Thanks given
    141
    Thanks received
    51
    Rep Power
    92
    Good god that's a lot of comments. Why the hell didn't you just remove that big comment at the top and post It directly on the thread??
    Reply With Quote  
     

  3. Thankful users:


  4. #3  
    Registered Member
    Join Date
    Nov 2014
    Posts
    88
    Thanks given
    10
    Thanks received
    33
    Rep Power
    26
    Should work on your documentation and OOP skills, none the less i like the idea
    Click me!
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  5. #4  
    nbness2#5894

    nbness2's Avatar
    Join Date
    Aug 2011
    Posts
    616
    Thanks given
    234
    Thanks received
    109
    Rep Power
    256
    i wanted it fast and dirty
    ( ͡ ͜ʖ ͡)


    And you don't have to be a pro to write good code, if anything being a programmer as a hobby should contribute to good code!
    Until I complete school, I guess I'm just a "hobbyist" programmer but I keep trying to learn whats readable and what works best.

    That said it definitely looks much better than my earlier work from my first couple years. Notepad programming is a hell of a drug.

    Keep working on it!
    KT/JAVA - NBX 637 - [Only registered and activated users can see links. ]!
    KT - Drop tables made Easy and Readable - [Only registered and activated users can see links. ]!
    KT - Item Containers made Safe, Easy and Powerful - [Only registered and activated users can see links. ]
    KT - Utils to make your life easier - [Only registered and activated users can see links. ]
    KT - Hopping Islands: From Java to Kotlin - [Only registered and activated users can see links. ] - [Only registered and activated users can see links. ] - [Only registered and activated users can see links. ]
    Reply With Quote  
     

  6. #5  
    Registered Member Vito the Don's Avatar
    Join Date
    Feb 2019
    Age
    21
    Posts
    75
    Thanks given
    6
    Thanks received
    4
    Rep Power
    66
    Interesting thanks for the release
    The Dons
    Reply With Quote  
     

  7. #6  
    Registered Member
    Join Date
    Apr 2016
    Posts
    25
    Thanks given
    6
    Thanks received
    13
    Rep Power
    69
    Capitalize your constants.

    Code:
    public static final String MY_CONSTANT_NAME = "should look like this";
    Reply With Quote  
     

  8. Thankful user:



Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Adding Npc drops in Delta (better explained)
    By white full in forum Snippets
    Replies: 17
    Last Post: 04-14-2009, 10:30 PM
  2. [508] Major Bug in Pali's base (Fix)
    By Gravediggah in forum Configuration
    Replies: 12
    Last Post: 02-25-2009, 08:36 AM
  3. Replies: 0
    Last Post: 01-04-2009, 02:26 PM
  4. Replies: 0
    Last Post: 01-04-2009, 01:22 PM
  5. Making stackable items drop in stacks from npcs
    By daniel89 in forum Tutorials
    Replies: 6
    Last Post: 10-07-2008, 07:55 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •