Thread: [PI] - New way of fetching players - Clean up your commands! - [PI]

Page 1 of 3 123 LastLast
Results 1 to 10 of 26
  1. #1 [PI] - New way of fetching players - Clean up your commands! - [PI] 
    Donator

    Robgob69's Avatar
    Join Date
    Oct 2010
    Age
    30
    Posts
    749
    Thanks given
    71
    Thanks received
    139
    Discord
    View profile
    Rep Power
    117
    Alright, so a lot of project insanity sources have really messy commands that look like this..
    Code:
    		if (playerCommand.startsWith("xteletome")) {
    			String name = playerCommand.substring(10);
    			for (int i = 0; i < Config.MAX_PLAYERS; i++) {
    				if (Server.playerHandler.players[i] != null) {
    					if (Server.playerHandler.players[i].playerName.equalsIgnoreCase(name)) {
    						Client other = (Client) Server.playerHandler.players[i];
    						other.getPA().movePlayer(c.getX(), c.getY(), c.heightLevel);
    					}
    				}
    			}			
    		}
    
    		if (playerCommand.startsWith("alltome")) {
    			String name = playerCommand.substring(10);
    			for (int i = 0; i < Config.MAX_PLAYERS; i++) {
    				if (Server.playerHandler.players[i] != null) {
    					Client other = (Client) Server.playerHandler.players[i];
    					other.getPA().movePlayer(c.getX(), c.getY(), c.heightLevel);
    				}
    			}			
    		}
    These small snippets i made will clean that up and will make making player based commands easier :]
    add this into your Client.java somewhere:
    Code:
    
    	// By Robgob
    	public Client getClient(String name) {
    		name = name.toLowerCase();
    		for(int i = 0; i < Config.MAX_PLAYERS; i++) {
    			if(validClient(i)) {
    				Client client = getClient(i);
    				if(client.playerName.toLowerCase().equalsIgnoreCase(name)) {
    					return client;
    				}
    			}
    		}
    		return null;
    	}
    	public Client getClient(int id) {
    		return (Client) Server.playerHandler.players[id];
    	}
    	public boolean validClient(int id) {
    		if (id < 0 || id > Config.MAX_PLAYERS) {
    			return false;
    		}
    		return validClient(getClient(id));
    	}
    	public boolean validClient(String name) {
    		return validClient(getClient(name));
    	}
    	public boolean validClient(Client client) {
    		return (client != null && !client.disconnected);
    	}
    Now your commands look nice, short, and neat :]]

    Code:
    		if (playerCommand.startsWith("xteletome")) {
    			String name = playerCommand.substring(10);
    			if (c.validClient(name)) {
    				c.getClient(name).getPA().movePlayer(c.getX(), c.getY(), c.heightLevel);
    			}		
    		}
    
    		if (playerCommand.startsWith("alltome")) {
    			String name = playerCommand.substring(10);
    			for (int i = 0; i < Config.MAX_PLAYERS; i++) {
    				if(c.validClient(i))
    					c.getClient(i).getPA().movePlayer(c.getX(), c.getY(), c.heightLevel);
    			}			
    		}
    
    		//I'll throw in my heal command :]]
    		if (playerCommand.startsWith("heal")) { // ::heal || ::heal name
    			if (playerCommand.indexOf(" ") > -1) {
    				String name = playerCommand.substring(5);
    				if (c.validClient(name)) {
    					Client p = c.getClient(name);
    					for (int i = 0; i < 20; i++) {
    						p.playerLevel[i] = p.getLevelForXP(p.playerXP[i]);
    						p.getPA().refreshSkill(i);
    					}
    					p.sendMessage("You have been healed by " + c.playerName + ".");
    				} else {
    					c.sendMessage("Player must be offline.");
    				}
    			} else {
    				for (int i = 0; i < 20; i++) {
    					c.playerLevel[i] = c.getLevelForXP(c.playerXP[i]);
    					c.getPA().refreshSkill(i);
    				}
    				c.sendMessage("You have been healed.");
    			}
    		}
    Not that big of a difference, but still..
    Reply With Quote  
     

  2. Thankful user:


  3. #2  
    Banned

    Join Date
    Aug 2010
    Age
    26
    Posts
    315
    Thanks given
    46
    Thanks received
    21
    Rep Power
    0
    Why "fix" something that already works?
    Reply With Quote  
     

  4. #3  
    Donator

    Robgob69's Avatar
    Join Date
    Oct 2010
    Age
    30
    Posts
    749
    Thanks given
    71
    Thanks received
    139
    Discord
    View profile
    Rep Power
    117
    because its faster to make commands, and it doesn't make your eyes bleed.
    tho that may just be me..
    Reply With Quote  
     

  5. Thankful user:


  6. #4  
    Registered Member JeagerJacques's Avatar
    Join Date
    Jun 2007
    Posts
    142
    Thanks given
    0
    Thanks received
    0
    Rep Power
    55
    very nice, i suppose. shortens up the command.java

    :trollface:
    Reply With Quote  
     

  7. #5  
    Donator

    Robgob69's Avatar
    Join Date
    Oct 2010
    Age
    30
    Posts
    749
    Thanks given
    71
    Thanks received
    139
    Discord
    View profile
    Rep Power
    117
    yes :]
    Reply With Quote  
     

  8. #6  
    Client Developer

    Pb600's Avatar
    Join Date
    Dec 2008
    Posts
    2,184
    Thanks given
    306
    Thanks received
    489
    Rep Power
    925
    Quote Originally Posted by Zaboomafoo View Post
    Why "fix" something that already works?
    That's not a fix, is a better and cleaner way to that
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  9. #7  
    Donator

    Robgob69's Avatar
    Join Date
    Oct 2010
    Age
    30
    Posts
    749
    Thanks given
    71
    Thanks received
    139
    Discord
    View profile
    Rep Power
    117
    Does anyone find this helpful, or should i remove?
    Reply With Quote  
     

  10. #8  
    Banned
    Join Date
    Nov 2009
    Posts
    65
    Thanks given
    0
    Thanks received
    0
    Rep Power
    0
    Quote Originally Posted by Robgob69 View Post
    Alright, so a lot of project insanity sources have really messy commands that look like this..
    Code:
    		if (playerCommand.startsWith("xteletome")) {
    			String name = playerCommand.substring(10);
    			for (int i = 0; i < Config.MAX_PLAYERS; i++) {
    				if (Server.playerHandler.players[i] != null) {
    					if (Server.playerHandler.players[i].playerName.equalsIgnoreCase(name)) {
    						Client other = (Client) Server.playerHandler.players[i];
    						other.getPA().movePlayer(c.getX(), c.getY(), c.heightLevel);
    					}
    				}
    			}			
    		}
    
    		if (playerCommand.startsWith("alltome")) {
    			String name = playerCommand.substring(10);
    			for (int i = 0; i < Config.MAX_PLAYERS; i++) {
    				if (Server.playerHandler.players[i] != null) {
    					Client other = (Client) Server.playerHandler.players[i];
    					other.getPA().movePlayer(c.getX(), c.getY(), c.heightLevel);
    				}
    			}			
    		}
    These small snippets i made will clean that up and will make making player based commands easier :]
    add this into your Client.java somewhere:
    Code:
    
    	// By Robgob
    	public Client getClient(String name) {
    		name = name.toLowerCase();
    		for(int i = 0; i < Config.MAX_PLAYERS; i++) {
    			if(validClient(i)) {
    				Client client = getClient(i);
    				if(client.playerName.toLowerCase().equalsIgnoreCase(name)) {
    					return client;
    				}
    			}
    		}
    		return null;
    	}
    	public Client getClient(int id) {
    		return (Client) Server.playerHandler.players[id];
    	}
    	public boolean validClient(int id) {
    		if (id < 0 || id > Config.MAX_PLAYERS) {
    			return false;
    		}
    		return validClient(getClient(id));
    	}
    	public boolean validClient(String name) {
    		return validClient(getClient(name));
    	}
    	public boolean validClient(Client client) {
    		return (client != null && !client.disconnected);
    	}
    Now your commands look nice, short, and neat :]]

    Code:
    		if (playerCommand.startsWith("xteletome")) {
    			String name = playerCommand.substring(10);
    			if (c.validClient(name)) {
    				c.getClient(name).getPA().movePlayer(c.getX(), c.getY(), c.heightLevel);
    			}		
    		}
    
    		if (playerCommand.startsWith("alltome")) {
    			String name = playerCommand.substring(10);
    			for (int i = 0; i < Config.MAX_PLAYERS; i++) {
    				if(c.validClient(i))
    					c.getClient(i).getPA().movePlayer(c.getX(), c.getY(), c.heightLevel);
    			}			
    		}
    
    		//I'll throw in my heal command :]]
    		if (playerCommand.startsWith("heal")) { // ::heal || ::heal name
    			if (playerCommand.indexOf(" ") > -1) {
    				String name = playerCommand.substring(5);
    				if (c.validClient(name)) {
    					Client p = c.getClient(name);
    					for (int i = 0; i < 20; i++) {
    						p.playerLevel[i] = p.getLevelForXP(p.playerXP[i]);
    						p.getPA().refreshSkill(i);
    					}
    					p.sendMessage("You have been healed by " + c.playerName + ".");
    				} else {
    					c.sendMessage("Player must be offline.");
    				}
    			} else {
    				for (int i = 0; i < 20; i++) {
    					c.playerLevel[i] = c.getLevelForXP(c.playerXP[i]);
    					c.getPA().refreshSkill(i);
    				}
    				c.sendMessage("You have been healed.");
    			}
    		}
    Not that big of a difference, but still..
    Yea you should delete it.
    Reply With Quote  
     

  11. #9  
    Donator

    Robgob69's Avatar
    Join Date
    Oct 2010
    Age
    30
    Posts
    749
    Thanks given
    71
    Thanks received
    139
    Discord
    View profile
    Rep Power
    117
    why quote that? lmao but ok
    Reply With Quote  
     

  12. #10  
    Banned

    Join Date
    Nov 2010
    Posts
    687
    Thanks given
    16
    Thanks received
    47
    Rep Power
    0
    now you cant delete it :trollface:
    Reply With Quote  
     

  13. Thankful user:


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. Pali [508] Commands Not Working For Players
    By Makememyserver in forum Help
    Replies: 7
    Last Post: 07-26-2010, 07:41 PM
  2. [B]508 Pali Clean Commands.java[/B]
    By grim in forum Tutorials
    Replies: 2
    Last Post: 07-10-2010, 12:13 PM
  3. Fetching files over a network
    By inFamous in forum Application Development
    Replies: 5
    Last Post: 10-06-2009, 06:54 PM
  4. 2 fun commands i use to mess with players lol..
    By Runebay™ in forum Configuration
    Replies: 9
    Last Post: 05-15-2009, 05:54 AM
  5. Error Fetching Image
    By Clouds Mine in forum Help
    Replies: 5
    Last Post: 01-22-2009, 02:20 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
  •