|
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
The main class.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; }
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.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; } }
Good luck if using
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]; } }
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";
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; } }
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.
« Previous Thread | Next Thread » |
Thread Information |
Users Browsing this ThreadThere are currently 1 users browsing this thread. (0 members and 1 guests) |