Thread: Different way of handling worlds

Results 1 to 9 of 9
  1. #1 Different way of handling worlds 
    Banned

    Join Date
    Aug 2011
    Posts
    843
    Thanks given
    541
    Thanks received
    220
    Rep Power
    0
    Kinda rushed this so it might not be perfect but could be helpful in most cases for world switching on your server, mainly this is for noobs as it didnt take long to write. Just allows you to use multiple worlds etc

    The main methods. Add anywhere you think it will work, e.g client or source idc where you put it lol

    Code:
        /**
         *  World booleans used as a switch.
         */
        public static boolean regular, member;
    
        /**
         * Int port.
         */
        public static int port;
    
        /**
         * String address.
         */
        public String address;
    
        /**
         * Class variable
         */
        public WorldGrabber world;
    
        /**
         * Changes the world whilst prints a confirmation.
         */
        public void changeWorld() {
            regular =! member;
            if(regular) {
                setWorld(world.REGULAR.grabAddress(), world.REGULAR.grabPort());
                System.out.println("You have selected World "+world.REGULAR.grabWorldId()+".");
                return;
            }
            if(member) {
                setWorld(world.MEMBER.grabAddress(), world.MEMBER.grabPort());
                System.out.println("You have selected World "+world.MEMBER.grabWorldId()+".");
                return;
            }
        }
    
        /**
         * Basically sets the world for you.
         * @param adr
         * @param portId
         */
        public void setWorld(String adr, int portId) {
            address = adr;
            port = portId;
        }
    The main class.

    Code:
    public enum WorldGrabber {
    
        /**
         * Worlds.
         */
        REGULAR(1, "127.0.0.1", 43594),
        MEMBER(2, "127.0.0.1", 5555),
        DONATOR(3, "127.0.0.1", 5555), 
        SUPPORT(4, "127.0.0.1", 5555),
        STAFF(5, "127.0.0.1", 5026);
    
        /**
         * The world and port id.
         */
        private final int portId, worldId;
        
        /**
         * Address.
         */
        private final String adr;
        
        /**
         * Class constructor.
         * @param address
         * @param port
         */
        private WorldGrabber(int worldId, String adr, int port) {
            this.adr = adr;
            this.portId = port;
            this.worldId = worldId;
        }
        
        /**
         * Grabs the world id.
         * @return
         */
        public int grabWorldId() {
            return worldId;
        }
        
        /**
         * Grabs the port.
         * @return
         */
        public int grabPort() {
            return portId;
        }
        
        
        /**
         * Grabs the address.
         * @return
         */
        public String grabAddress() {
            return adr;
        }
    
    }
    Btw it only uses member and regular worlds, just add more switches. Make sure to replace or link all 'port' or 'address' variables to the variables inside the first code block so it works.


    Good luck if using
    Reply With Quote  
     

  2. Thankful user:


  3. #2  
    Donator

    Arithium's Avatar
    Join Date
    May 2010
    Age
    31
    Posts
    4,721
    Thanks given
    199
    Thanks received
    1,256
    Rep Power
    1114
    Wrote something just like this awhile back.

    Code:
    package com.zera.client.constants;
    
    /**
     * The current state of the client build
     * 
     * @author Timothy Howard <Arithium>
     *
     */
    public enum ConnectionStatus {
    
    	/**
    	 * The client is set up for local connections
    	 */
    	LOCAL("127.0.0.1", 43594),
    
    	/**
    	 * The client is set up for live use
    	 */
    	LIVE("127.0.0.1", 43594),
    	
    	/**
    	 * The connection status for the beta
    	 */
    	BETA("127.0.0.1", 43594);
    	/**
    	 * The IP address as a long
    	 */
    	private String address;
    
    	/**
    	 * The port of the address
    	 */
    	private int port;
    
    	/**
    	 * Cached values array.
    	 */
    	private static final ConnectionStatus[] values = values();
    
    	/**
    	 * Constructs a new {@link ConnectionStatus} to know where to connect the
    	 * client too
    	 * 
    	 * @param address
    	 *            The ip address as a long
    	 * @param port
    	 *            The port to connect too
    	 */
    	ConnectionStatus(String address, int port) {
    		this.address = address;
    		this.port = port;
    	}
    
    	/**
    	 * Gets the address to connect too
    	 * 
    	 * @return Gets the address the client will connect too
    	 */
    	public String getAddress() {
    		return address;
    	}
    
    	/**
    	 * Gets the port the client will connect too
    	 * 
    	 * @return The port the client will connect too
    	 */
    	public int getPort() {
    		return port;
    	}
    
    	public ConnectionStatus getNext() {
    		int next = ordinal() + 1;
    		if (next >= values.length)
    			return values[0];
    		return values[next];
    	}
    
    }
    Reply With Quote  
     

  4. Thankful user:


  5. #3  
    Banned

    Join Date
    Aug 2011
    Posts
    843
    Thanks given
    541
    Thanks received
    220
    Rep Power
    0
    Quote Originally Posted by Arithium View Post
    Wrote something just like this awhile back.

    Code:
    package com.zera.client.constants;
    
    /**
     * The current state of the client build
     * 
     * @author Timothy Howard <Arithium>
     *
     */
    public enum ConnectionStatus {
    
        /**
         * The client is set up for local connections
         */
        LOCAL("127.0.0.1", 43594),
    
        /**
         * The client is set up for live use
         */
        LIVE("127.0.0.1", 43594),
        
        /**
         * The connection status for the beta
         */
        BETA("127.0.0.1", 43594);
        /**
         * The IP address as a long
         */
        private String address;
    
        /**
         * The port of the address
         */
        private int port;
    
        /**
         * Cached values array.
         */
        private static final ConnectionStatus[] values = values();
    
        /**
         * Constructs a new {@link ConnectionStatus} to know where to connect the
         * client too
         * 
         * @param address
         *            The ip address as a long
         * @param port
         *            The port to connect too
         */
        ConnectionStatus(String address, int port) {
            this.address = address;
            this.port = port;
        }
    
        /**
         * Gets the address to connect too
         * 
         * @return Gets the address the client will connect too
         */
        public String getAddress() {
            return address;
        }
    
        /**
         * Gets the port the client will connect too
         * 
         * @return The port the client will connect too
         */
        public int getPort() {
            return port;
        }
    
        public ConnectionStatus getNext() {
            int next = ordinal() + 1;
            if (next >= values.length)
                return values[0];
            return values[next];
        }
    
    }
    Oh nice dude, looks good
    Reply With Quote  
     

  6. Thankful user:


  7. #4  
    Extreme Donator

    Join Date
    Apr 2015
    Posts
    369
    Thanks given
    215
    Thanks received
    79
    Rep Power
    74
    haha, cleaner then mine..

    Code:
    /**
    	 * A flag to identify whether we are connecting to the live or not.
    	 */
    	public static final boolean LIVE = false; // true = live server 
    	
    	/**
    	 * A flag to identify if we're testing or not.
    	 */
    	private static final boolean TESTING = false; // ^ true, false = live server true, true = live test server
    	// false, false == 127.0.0.1
    	
    	/**
    	 * A string to identify what IP we are connecting to; we will
    	 * use the {@link ClientConstants#LIVE} flag to create a ternary.
    	 */
    	public static final String IP_ADDRESS = LIVE ? (TESTING ? "********" : "********") : "127.0.0.1";
    Reply With Quote  
     

  8. #5  
    Registered Member
    Project's Avatar
    Join Date
    Dec 2010
    Posts
    2,669
    Thanks given
    1,043
    Thanks received
    820
    Rep Power
    1101
    you could use a ternary operator for this

    Code:
    public void changeWorld() {
            regular =! member;
            if(regular) {
                setWorld(world.REGULAR.grabAddress(), world.REGULAR.grabPort());
                System.out.println("You have selected World "+world.REGULAR.grabWorldId()+".");
                return;
            }
            if(member) {
                setWorld(world.MEMBER.grabAddress(), world.MEMBER.grabPort());
                System.out.println("You have selected World "+world.MEMBER.grabWorldId()+".");
                return;
            }
        }
    Reply With Quote  
     

  9. Thankful user:


  10. #6  
    Extreme Donator

    Join Date
    Apr 2015
    Posts
    369
    Thanks given
    215
    Thanks received
    79
    Rep Power
    74
    Quote Originally Posted by Project View Post
    you could use a ternary operator for this

    Code:
    public void changeWorld() {
            regular =! member;
            if(regular) {
                setWorld(world.REGULAR.grabAddress(), world.REGULAR.grabPort());
                System.out.println("You have selected World "+world.REGULAR.grabWorldId()+".");
                return;
            }
            if(member) {
                setWorld(world.MEMBER.grabAddress(), world.MEMBER.grabPort());
                System.out.println("You have selected World "+world.MEMBER.grabWorldId()+".");
                return;
            }
        }
    Code:
    	public void changeWorld() {
    	setWorld((regular ? world.REGULAR.grabAddress() : world.MEMBER.grabAddress()), (regular ? world.REGULAR.grabPort() : world.MEMBER.grabPort()));
        }
    Reply With Quote  
     

  11. #7  
    Banned

    Join Date
    Aug 2011
    Posts
    843
    Thanks given
    541
    Thanks received
    220
    Rep Power
    0
    Quote Originally Posted by durky View Post
    Code:
        public void changeWorld() {
        setWorld((regular ? world.REGULAR.grabAddress() : world.MEMBER.grabAddress()), (regular ? world.REGULAR.grabPort() : world.MEMBER.grabPort()));
        }
    Yeah, im just in the basic stuff atm, and didnt think of that doing it that way. Thanks
    Reply With Quote  
     

  12. #8  
    Registered Member
    A Wild Gengar's Avatar
    Join Date
    Apr 2016
    Posts
    96
    Thanks given
    63
    Thanks received
    49
    Rep Power
    101
    You don't need 2 booleans; it's either regular (false), or members (true). And just to nitpick , the port number could be a short as it takes less memory. Port 43594 doesn't have any significance, it's just rsps convention.
    Reply With Quote  
     

  13. #9  
    Donator

    Arithium's Avatar
    Join Date
    May 2010
    Age
    31
    Posts
    4,721
    Thanks given
    199
    Thanks received
    1,256
    Rep Power
    1114
    Quote Originally Posted by A Wild Gengar View Post
    You don't need 2 booleans; it's either regular (false), or members (true). And just to nitpick , the port number could be a short as it takes less memory. Port 43594 doesn't have any significance, it's just rsps convention.
    The size difference in memory between a short and integer are minimal. Not to mention you would end up casting back to integer on declaration anyway, unless you were to change all of the methods calling this.
    Reply With Quote  
     


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. Different Way Of Starting Things On Startup
    By Sir Tom in forum Snippets
    Replies: 10
    Last Post: 10-19-2010, 03:20 AM
  2. Different way of loading weapon timers
    By Fire Cape in forum Snippets
    Replies: 1
    Last Post: 09-21-2010, 02:47 AM
  3. Better ways of handling item equiping?
    By Underoath in forum Help
    Replies: 2
    Last Post: 12-24-2009, 10:55 AM
  4. Different way of Highscores
    By Mrthunder23 in forum Help
    Replies: 3
    Last Post: 09-19-2009, 04:13 PM
  5. Different way of ::players? Hmm..
    By Demetri in forum Configuration
    Replies: 9
    Last Post: 02-19-2009, 07:08 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •