Thread: Client Settings

Page 1 of 3 123 LastLast
Results 1 to 10 of 24
  1. #1 Client Settings 
    Retired. Stop PMing me.

    Galkon's Avatar
    Join Date
    Nov 2007
    Age
    14
    Posts
    7,527
    Thanks given
    1,785
    Thanks received
    2,823
    Discord
    View profile
    Rep Power
    5000
    Today I wrote a new system that allows me to add, update, and remove settings from the client pretty easily. This will allow you to create client settings that will be saved and loaded by the client to remember things such as 'remember me', and really whatever you want it to. It will save every time a setting is changed and it will remove old/unneeded settings still stored on the settings file.

    In your 'startUp()' method you should add 'Settings.load();' at the very top, and make sure you change the file location to the settings file to whatever you need it to be.

    To change or add or remove a setting, simply look at 'public static Object[][] settingValues = {' and it's pretty self explanatory. However let me show you some examples:

    A setting using a string: { "string_example", "default string" }
    A setting using a number: { "number_example", 12 } - 12 being the default value.
    A setting using a boolean: { "boolean_example", true } - true being the default value.

    To use a setting for reference (check if something is enabled for example) you can do:

    For a string: if (((String) Settings.getSetting("string_example")).equals("def ault string")) {
    For a number: if ((Integer) Settings.getSetting("number_example") == 12) {
    For a boolean: if ((Boolean) Settings.getSetting("boolean_example")) {

    Here's the Settings class:
    Code:
    package com.galkon;
    
    import java.io.File;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.util.Hashtable;
    
    
    import com.galkon.constants.Constants;
    
    
    
    
    /**
     * The settings class will manage all settings stored by the client. The client
     * will refer to, change, and remove settings as it needs to. Adding a new client
     * setting is extremely easy and changing them is even easier due to the fact that
     * the Settings class will remove old or nulled settings still stored in the settings
     * file, and it verifies each setting to make sure it's a legitimate setting (to check
     * if you still require the usage of that setting).
     * @author Galkon
     *
     */
    public class Settings {
    
    
        /**
         * The settings stored created and stored by the client.
         */
        public static Hashtable<String, Object> settings = new Hashtable<String, Object>();
    
    
        /**
         * The values of the settings.
         */
        public static Object[][] settingValues = {
                { "display_world_background", false }, { "play_client_sounds", false }, { "quick_login", false }
        };
        
        /**
         * Creates a setting for the specified name and object.
         * @param name
         * @param object
         */
        public static void createSetting(String name, Object object) {
            if (!verify(name)) {
                return;
            }
            settings.put(name, object);
        }
    
    
        /**
         * Returns the object for the specified name.
         * @param name
         * @return
         */
        public static Object getSetting(String name) {
            if (!verify(name)) {
                return null;
            }
            return settings.get(name);
        }
    
    
        /**
         * Changes the specified setting by removing it then re-adding the new values.
         * @param name
         * @param object
         */
        public static void changeSetting(String name, Object object) {
            if (!verify(name)) {
                return;
            }
            settings.remove(name);
            settings.put(name, object);
            save();
        }
    
    
        /**
         * Toggles the specified setting (only works for booleans).
         * @param name
         */
        public static void toggleSetting(String name) {
            if (settings.get(name) instanceof Boolean) {
                changeSetting(name, !(Boolean) settings.get(name));
            }
        }
    
    
        /**
         * Verifies that the specified name is a defined setting.
         * @param name
         * @return
         */
        public static boolean verify(String name) {
            for (int index = 0; index < settingValues.length; index++) {
                String nameValue = (String) settingValues[index][0];
                if (nameValue.equalsIgnoreCase(name)) {
                    return true;
                }
            }
            return false;
        }
    
    
        /**
         * Creates the default settings.
         */
        public static void createDefaults() {
            for (int index = 0; index < settingValues.length; index++) {
                createSetting((String) settingValues[index][0], settingValues[index][1]);
            }
            save();
        }
    
    
        /**
         * Loads and creates the settings from the settings file.
         */
        public static void load() {
            try {
                if (!new File(Constants.getWorkingDirectory() + FILE).exists()) {
                    createDefaults();
                    return;
                }
                RandomAccessFile in = new RandomAccessFile(Constants.getWorkingDirectory() + FILE, "rw");
                int size = in.readShort();
                if (size != settingValues.length) {
                    createDefaults();
                }
                for (int index = 0; index < size; index++) {
                    String name = in.readUTF();
                    Object object = null;
                    int type = in.readByte();
                    if (type == 0) {
                        object = in.readBoolean();
                    } else if (type == 1) {
                        object = in.readInt();
                    } else if (type == 2) {
                        object = in.readUTF();
                    }
                    if (size == settingValues.length) {
                        createSetting(name, object);
                    } else {
                        changeSetting(name, object);
                    }
                }
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    
        /**
         * Saves the settings to the settings file.
         */
        public static void save() {
            try {
                RandomAccessFile out = new RandomAccessFile(Constants.getWorkingDirectory() + FILE, "rw");
                out.writeShort(settings.size());
                for (int index = 0; index < settingValues.length; index++) {
                    String name = (String) settingValues[index][0];
                    Object object = settings.get(name);
                    if (object == null) {
                        object = settingValues[index][1];
                    }
                    out.writeUTF(name);
                    if (object instanceof Boolean) {
                        out.writeByte(0);
                        out.writeBoolean((Boolean) object);
                    } else if (object instanceof Integer) {
                        out.writeByte(1);
                        out.writeInt((Integer) object);
                    } else if (object instanceof String) {
                        out.writeByte(2);
                        out.writeUTF((String) object);
                    }
                }
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    
        /**
         * The settings file name.
         */
        public final static String FILE = "settings";
    
    
    }
    [Only registered and activated users can see links. ]
    Reply With Quote  
     


  2. #2  
    Registered Member Real Brandon's Avatar
    Join Date
    Jan 2010
    Posts
    383
    Thanks given
    28
    Thanks received
    27
    Rep Power
    7
    Very nice galkon

    Reply With Quote  
     

  3. #3  
    Registered Member DUGI's Avatar
    Join Date
    Jan 2013
    Age
    25
    Posts
    191
    Thanks given
    31
    Thanks received
    31
    Rep Power
    41
    Hmm, nice one!

    Definitely going to use, makes it all more user friendly.
    Reply With Quote  
     

  4. #4  
    Extreme Donator Market Banned Market Banned



    Join Date
    Dec 2010
    Age
    22
    Posts
    6,082
    Thanks given
    1,690
    Thanks received
    1,233
    Rep Power
    1704
    Thanks, going to use.
    Reply With Quote  
     

  5. #5  
    Koy
    Koy is offline
    Ex-Staff


    Join Date
    Oct 2010
    Posts
    1,875
    Thanks given
    1,290
    Thanks received
    900
    Rep Power
    5000
    Nice work, will be helpful for login screens.

    [Only registered and activated users can see links. ]


    Reply With Quote  
     

  6. Thankful user:


  7. #6  
    Registered Member

    Join Date
    Jan 2013
    Posts
    553
    Thanks given
    38
    Thanks received
    154
    Rep Power
    127
    So just basically a file writer/reader.

    Thought this would be a better release coming from you, honestly.
    Reply With Quote  
     

  8. Thankful users:


  9. #7  
    Registered Member
    thim slug's Avatar
    Join Date
    Nov 2010
    Age
    25
    Posts
    4,142
    Thanks given
    1,079
    Thanks received
    1,137
    Discord
    View profile
    Rep Power
    5000
    Quote Originally Posted by iLiftPlenty View Post
    So just basically a file writer/reader.

    Thought this would be a better release coming from you, honestly.
    Unfortunately, I have to agree.
    Reply With Quote  
     

  10. #8  
    Retired. Stop PMing me.

    Galkon's Avatar
    Join Date
    Nov 2007
    Age
    14
    Posts
    7,527
    Thanks given
    1,785
    Thanks received
    2,823
    Discord
    View profile
    Rep Power
    5000
    Quote Originally Posted by iLiftPlenty View Post
    So just basically a file writer/reader.

    Thought this would be a better release coming from you, honestly.
    Quote Originally Posted by Leopold Butters Stotch View Post
    Unfortunately, I have to agree.
    Ya basically just that except more versatile, user friendly, easy to use, and universal ????????????? maybe if you read the code you'll clearly see it's a little more than "just a file writer/reader".

    Sry my snippet wasn't good enough for you guys maybe I'll just not post snippets anymore for you guys
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  11. Thankful users:


  12. #9  
    Banned
    Join Date
    Sep 2012
    Posts
    1
    Thanks given
    192
    Thanks received
    317
    Rep Power
    0
    Nice.
    Reply With Quote  
     

  13. #10  
    Retired. Stop PMing me.

    Galkon's Avatar
    Join Date
    Nov 2007
    Age
    14
    Posts
    7,527
    Thanks given
    1,785
    Thanks received
    2,823
    Discord
    View profile
    Rep Power
    5000
    Quote Originally Posted by Herzen View Post
    Nice work, will be helpful for login screens.
    That's what I mainly wrote it for but it'll be of much more use than just that too
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

Page 1 of 3 123 LastLast

Thread Information
Users Browsing this Thread

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


User Tag List

Similar Threads

  1. Replies: 22
    Last Post: 01-03-2014, 02:23 PM
  2. Replies: 0
    Last Post: 09-08-2010, 03:38 PM
  3. Client Settings show-off
    By Shoes in forum Show-off
    Replies: 28
    Last Post: 07-04-2010, 01:52 AM
  4. Option Tab, Graphic Settings, Audio Settings
    By Infexis in forum Show-off
    Replies: 29
    Last Post: 06-14-2010, 05:47 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
  •