Thread: Command Handler

Page 1 of 2 12 LastLast
Results 1 to 10 of 11
  1. #1 Command Handler 
    Banned
    Join Date
    May 2013
    Posts
    202
    Thanks given
    0
    Thanks received
    40
    Rep Power
    0
    Threw this together in 5 minutes, enjoy. I don't care what you say; it's better than the way Sanity did it.


    Code:
    package com.paladious.rs2.game.content.commands;
    
    import com.paladious.rs2.game.entity.player.Client;
    
    public class CommandHandler {
    	
    	
    	/**
    	 * Enum holding character rights data.
    	 * @author Neurons(http://www.rune-server.ee/members/neurons/)
    	 */
    	enum Rights {
    		 
    		PLAYER(0),
    		MODERATOR(1),
    		ADMINISTRATOR(2),
    		OWNER(3);
    		
    		public int rights;
    		
    		Rights(int rights) {
    			this.rights = rights;
    		}
    		
    		public int getRights(Rights player2) {
    			return rights;
    		}
    	}
    	
    	public static void right***ecute(Client c, String playerCommand) {
    		for(Rights r : Rights.values()) {
    			if(c.playerRights == r.getRights(Rights.PLAYER)) {
    				PlayerCommands.handleRegularCommands(c, playerCommand);
    			}
    			else if(c.playerRights == r.getRights(Rights.MODERATOR)) {
    				ModeratorCommands.handleModeratorCommands(c, playerCommand);
    			}
    			else if(c.playerRights == r.getRights(Rights.ADMINISTRATOR)) {
    				AdminCommands.handleAdminCommands(c, playerCommand);
    			}
    			else if(c.playerRights == r.getRights(Rights.OWNER)) {
    				OwnerCommands.handleOwnerCommands(c, playerCommand);
    			}
    		}
    	}
    
    }
    Code:
    package com.paladious.rs2.game.content.commands;
    
    import com.paladious.network.packet.PacketType;
    import com.paladious.rs2.game.entity.player.Client;
    
    public class CommandExecuter extends CommandHandler implements PacketType {
    	
    	public void processPacket(Client c, int packetType, int packetSize) {
    		String playerCommand = c.getInStream().readString();
    		PlayerCommands.handleRegularCommands(c, playerCommand);
    		ModeratorCommands.handleModeratorCommands(c, playerCommand);
    		AdminCommands.handleAdminCommands(c, playerCommand);
    		OwnerCommands.handleOwnerCommands(c, playerCommand);
    		
        }
    }
    Code:
    package com.paladious.rs2.game.content.commands;
    
    
    import com.paladious.rs2.game.entity.player.Client;
    
    public class PlayerCommands extends CommandExecuter {
    	
    	public static void handleRegularCommands(Client c, String playerCommand) {
    		
    	}
    }
    Code:
    package com.paladious.rs2.game.content.commands;
    
    import com.paladious.rs2.game.entity.player.Client;
    
    public class ModeratorCommands extends CommandExecuter {
    
    	
    	public static void handleModeratorCommands(Client c, String playerCommand) {		
    				
    		      }
    		}
    Code:
    package com.paladious.rs2.game.content.commands;
    
    import com.paladious.rs2.game.entity.player.Client;
    
    public class AdminCommands extends CommandExecuter {
    	
    	public static void handleAdminCommands(Client c, String playerCommand) {		
    		
    		}
    }
    Code:
    package com.paladious.rs2.game.content.commands;
    
    
    import com.paladious.rs2.game.entity.player.Client;
    
    public class OwnerCommands extends CommandExecuter {
    	
    	public static void handleOwnerCommands(Client c, String playerCommand) {		
    		
    	}
    }
    In PacketHandler class replace:
    Code:
    packetId[103] = new Commands();
    With:
    Code:
    packetId[103] = new CommandExecuter();
    Reply With Quote  
     

  2. #2  
    Registered Member

    Join Date
    Jan 2013
    Posts
    553
    Thanks given
    38
    Thanks received
    154
    Rep Power
    127
    Might as well use ordinal for the enum instead of the param. Also:

    Code:
    if(c.playerRights == r.getRights(Rights.PLAYER)) {
    				PlayerCommands.handleRegularCommands(c, playerCommand);
    			}
    			if(c.playerRights == r.getRights(Rights.MODERATOR)) {
    				ModeratorCommands.handleModeratorCommands(c, playerCommand);
    			}
    			if(c.playerRights == r.getRights(Rights.ADMINISTRATOR)) {
    				AdminCommands.handleAdminCommands(c, playerCommand);
    			}
    			if(c.playerRights == r.getRights(Rights.OWNER)) {
    				OwnerCommands.handleOwnerCommands(c, playerCommand);
    			}
    Use else-if.
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  3. #3  
    Banned
    Join Date
    May 2013
    Posts
    202
    Thanks given
    0
    Thanks received
    40
    Rep Power
    0
    Quote Originally Posted by iLiftPlenty View Post
    Might as well use ordinal for the enum instead of the param. Also:

    Code:
    if(c.playerRights == r.getRights(Rights.PLAYER)) {
    				PlayerCommands.handleRegularCommands(c, playerCommand);
    			}
    			if(c.playerRights == r.getRights(Rights.MODERATOR)) {
    				ModeratorCommands.handleModeratorCommands(c, playerCommand);
    			}
    			if(c.playerRights == r.getRights(Rights.ADMINISTRATOR)) {
    				AdminCommands.handleAdminCommands(c, playerCommand);
    			}
    			if(c.playerRights == r.getRights(Rights.OWNER)) {
    				OwnerCommands.handleOwnerCommands(c, playerCommand);
    			}
    Use else-if.
    I knew i missed something. Thanks, any other feedback?
    Reply With Quote  
     

  4. #4  
    Renown Programmer & Respected Member

    Ryley's Avatar
    Join Date
    Aug 2011
    Posts
    597
    Thanks given
    253
    Thanks received
    520
    Discord
    View profile
    Rep Power
    1332
    Quote Originally Posted by iLiftPlenty View Post
    Might as well use ordinal for the enum instead of the param. Also:

    Code:
    if(c.playerRights == r.getRights(Rights.PLAYER)) {
    				PlayerCommands.handleRegularCommands(c, playerCommand);
    			}
    			if(c.playerRights == r.getRights(Rights.MODERATOR)) {
    				ModeratorCommands.handleModeratorCommands(c, playerCommand);
    			}
    			if(c.playerRights == r.getRights(Rights.ADMINISTRATOR)) {
    				AdminCommands.handleAdminCommands(c, playerCommand);
    			}
    			if(c.playerRights == r.getRights(Rights.OWNER)) {
    				OwnerCommands.handleOwnerCommands(c, playerCommand);
    			}
    Use else-if.
    You can't use else if here. The logic will short circuit after completing 1 successful branch.
    Reply With Quote  
     

  5. #5  
    Registered Member

    Join Date
    Jan 2013
    Posts
    553
    Thanks given
    38
    Thanks received
    154
    Rep Power
    127
    Quote Originally Posted by AtomicInt_ View Post
    You can't use else if here. The logic will short circuit after completing 1 successful branch.
    Short circuit? Successful branch? What are you talking about...Please write a comment that makes sense.
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  6. #6  
    Donator

    Jason's Avatar
    Join Date
    Aug 2009
    Posts
    6,108
    Thanks given
    2,402
    Thanks received
    2,825
    Rep Power
    4604
    Quote Originally Posted by AtomicInt_ View Post
    You can't use else if here. The logic will short circuit after completing 1 successful branch.
    Else-ifs don't branch since you can't break them. Also, an else if is fine. There would be no better way to do it other than Ordinal or a Switch.
    Reply With Quote  
     

  7. #7  
    Renown Programmer & Respected Member

    Ryley's Avatar
    Join Date
    Aug 2011
    Posts
    597
    Thanks given
    253
    Thanks received
    520
    Discord
    View profile
    Rep Power
    1332
    Quote Originally Posted by iLiftPlenty View Post
    Short circuit? Successful branch? What are you talking about...Please write a comment that makes sense.
    It makes perfect sense..

    Short circuit = not continue
    Successful branch = completing one block

    Quote Originally Posted by Jason View Post
    Else-ifs don't branch since you can't break them. Also, an else if is fine. There would be no better way to do it other than Ordinal or a Switch.
    In this case they would, say if rights >= 2 if you executed a command that was for 1, 2 & 3 it would only continue to the first branch and break.
    Reply With Quote  
     

  8. #8  
    Registered Member

    Join Date
    Jan 2013
    Posts
    553
    Thanks given
    38
    Thanks received
    154
    Rep Power
    127
    Quote Originally Posted by AtomicInt_ View Post
    It makes perfect sense..

    Short circuit = not continue
    Successful branch = completing one block



    In this case they would, say if rights >= 2 if you executed a command that was for 1, 2 & 3 it would only continue to the first branch and break.
    Choose verbs that make sense according to the scenario next time. I see what you're trying to say at least...But read the damn code...He obviously has

    c.playerRights == r.getRights(Rights...)

    , so yes else-if should be used here...
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  9. #9  
    Renown Programmer & Respected Member

    Ryley's Avatar
    Join Date
    Aug 2011
    Posts
    597
    Thanks given
    253
    Thanks received
    520
    Discord
    View profile
    Rep Power
    1332
    Quote Originally Posted by iLiftPlenty View Post
    Choose verbs that make sense according to the scenario next time. I see what you're trying to say at least...But read the damn code...He obviously has

    c.playerRights == r.getRights(Rights...)

    , so yes else-if should be used here...
    I guess my terminology sucks, eh, whatever, anyway, I did only glimpse at the code, I don't tend to read line-by-line of code posted here.
    Reply With Quote  
     

  10. #10  
    Banned
    Join Date
    May 2013
    Posts
    202
    Thanks given
    0
    Thanks received
    40
    Rep Power
    0
    Thanks for the feedback guys.
    Reply With Quote  
     

Page 1 of 2 12 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. Creating a commands 'handler' - Explained!
    By Roger in forum Tutorials
    Replies: 8
    Last Post: 05-11-2009, 02:15 AM
  2. Walk On Command Handler
    By 1337Coder in forum Tutorials
    Replies: 0
    Last Post: 01-10-2008, 11:49 PM
  3. A simple "Command Handler"
    By samuraiblood2 in forum Tutorials
    Replies: 7
    Last Post: 11-24-2007, 11:15 AM
  4. Command Handler (NOT NOOB FRIENDLY!)
    By P I M P 3 D Tut in forum Tutorials
    Replies: 4
    Last Post: 08-10-2007, 08:39 PM
  5. Command Handler
    By Kbob in forum Tutorials
    Replies: 21
    Last Post: 07-25-2007, 01:56 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
  •