Thread: [ANY] Better Command System

Page 1 of 2 12 LastLast
Results 1 to 10 of 11
  1. #1 [ANY] Better Command System 
    Registered Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks given
    11
    Thanks received
    11
    Rep Power
    16
    EDIT: Not done, just don't like people looking at my shitwork

    Command.java
    Code:
    public interface Command 
    {
    	public void execute(Client player, String[] args);
    }
    CommandListener.java
    Code:
    public class CommandListener implements PacketType 
    {
    	public enum Rank
    	{
    		MEMBER,
    		MODERATOR,
    		ADMIN,
    		OWNER,
    		CODER,
    		DONATOR;
    		
    		public String getCleanName()
    		{
    			return Misc.capitalize(name().toLowerCase());
    		}
    	}
    	
    	public enum Commands
    	{
    		SETRANK("Sets any characters rank.", Rank.CODER,
    		new Command()
    		{
    
    			@Override
    			public void execute(Client player, String[] args) 
    			{
    				if(args.length != 3)
    				{
    					return;
    				}
    				
    				Client PlayerToRankUp = PlayerHandler.get(args[1].replace('_', ' '));
    				
    				PlayerToRankUp.playerRights = Integer.parseInt(args[2]);
    				PlayerToRankUp.disconnected = true;
    				player.sendMessage("You have set "+Misc.capitalize(PlayerToRankUp.playerName)+" to player rights " +PlayerToRankUp.playerRights+".");
    			}
    		}),
    		HELP("Requests help from any online staff members.", Rank.MEMBER, 
    		new Command()
    		{
    			@Override
    			public void execute(Client player, String[] args) 
    			{
    				player.sendMessage("You have requested help from "+ MassMessage.MessageAllStaff("" + Misc.capitalize(player.playerName) + " is requesting assistance!") +" staff member(s).");
    				player.sendMessage("Please wait for a staff member to reply.");
    			}
    		}),
    		
    		PNPC("Changes you into an npc, specified by the id entered.", Rank.ADMIN, 
    		new Command()
    		{
    			@Override
    			public void execute(Client player, String[]args)
    			{
    				int npc = Integer.parseInt(args[1]);
    				if (npc < 9999) 
    				{
    					player.npcId2 = npc;
    					player.isNpc = true;
    					player.updateRequired = true;
    					player.appearanceUpdateRequired = true;
    				}
    			}
    		}),
    		
    		UNPC("Reverts you to your normal player.", Rank.ADMIN, 
    		new Command()
    		{
    
    			@Override
    			public void execute(Client player, String[] args) 
    			{
    				player.isNpc = false;
    		    	player.updateRequired = true;
    		    	player.appearanceUpdateRequired = true;
    			}
    		}),
    		
    		COMMANDS("Gives you a list of all the commands Ingame.", Rank.MEMBER, 
    		new Command()
    		{
    
    			@Override
    			public void execute(Client player, String[] args) 
    			{
    				if(player.checkBusy())
    				{
    					return;
    				}
    				player.getPA().closeAllWindows();
    				
    				int index = 8144;
    				
    				for(int i = index; i < 8195; i++)
    				{
    					player.getPA().sendFrame126("", i);
    				}
    				
    				player.getPA().sendFrame126("@[email protected]" + Config.SERVER_NAME + " Commands", index+=2);
    				
    				for(Rank r : Rank.values())
    				{
    					if(hasRights(player, r))
    					index += (printCommands(player, r, index) - index);
    				}
    				
    				player.getPA().showInterface(8134);
    			}
    			
    		});
    		
    		private String description;
    		private Rank rank;
    		private Command c;
    		
    		Commands(String description, Rank rank, Command c)
    		{
    			this.description = description;
    			this.rank = rank;
    			this.c = c;
    		}
    		
    		public String getCommandName()
    		{
    			return name().toLowerCase();
    		}
    		
    		public String getDescription()
    		{
    			return description;
    		}
    		
    		public Rank getRank()
    		{
    			return rank;
    		}
    		
    		public Command getCommand()
    		{
    			return c;
    		}
    	}
    	
    	public static int printCommands(Client player, Rank r, int index)
    	{
    		index++;
    		player.getPA().sendFrame126("@[email protected]" + r.getCleanName() + " Commands: ", index++);
    		
    		for(Commands c : Commands.values())
    		{
    			if(c.getRank().equals(r))
    			{
    				player.getPA().sendFrame126("@[email protected]" + c.getCommandName() + " - " + c.getDescription(), index++);
    			}
    		}
    		return index;
    	}
    	
    	public static boolean hasRights(Client player, Rank r)
    	{
    		Byte byte_Rank = (byte)r.ordinal();
    		Rank playerRank = Rank.values()[player.playerRights];		
    		
    		if(playerRank.equals(r))
    		{
    			return true;
    		}
    		
    		if(playerRank.ordinal() < Rank.DONATOR.ordinal())
    		{
    			if(playerRank.ordinal() > byte_Rank)
    			{
    				return true;
    			}
    		}
    		else
    		{
    			if(r.equals(Rank.MEMBER))
    			{
    				return true;
    			}
    			
    			if(byte_Rank >= Rank.DONATOR.ordinal())
    			{
    				if(playerRank.ordinal() > byte_Rank || 
    				  (playerRank.ordinal() > Rank.MEMBER.ordinal() &&
    				   playerRank.ordinal() < Rank.DONATOR.ordinal()))
    				{
    					return true;
    				}
    			}
    		}
    		return false;
    	}
    	
    	@Override
    	public void processPacket(Client player, int packetType, int packetSize) 
    	{
    		String playerCommand = player.getInStream().readString();
    		
    		if (Config.SERVER_DEBUG) 
    		{
    			Misc.println(player.playerName + " playerCommand: " + playerCommand);
    		}
    		
    		if(ClanChatHandler.chat(player, playerCommand))
    		{
    			return;
    		}
    		
    		String args[] = playerCommand.split(" ");
    		
    		for(Commands c : Commands.values())
    		{
    			if(args[0].equalsIgnoreCase(c.getCommandName()))
    			{
    				if(hasRights(player, c.getRank()))
    				{
    					c.getCommand().execute(player, args);
    				}
    			}
    		}
    	}
    }
    Talked to lare and he helped me think of a better way~
    so like idunno 50% to him
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  2. #2  
    Registered Member
    Join Date
    Jan 2015
    Posts
    337
    Thanks given
    17
    Thanks received
    4
    Rep Power
    11
    It's alright, I have a command system people that people like, Its the
    Code:
    case "command":
    commands code
    break;
    My command system doesn't have to deal with nearly the amount of code you put in like
    Code:
     static final String STRING_COMMANDS[] =
        {
        	"yell",
        	"commands",
            "help"
        };
    But I like what you did here, Did you do this?
    Reply With Quote  
     

  3. #3  
    Registered Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks given
    11
    Thanks received
    11
    Rep Power
    16
    Quote Originally Posted by zeke View Post
    It's alright, I have a command system people that people like, Its the
    Code:
    case "command":
    commands code
    break;
    My command system doesn't have to deal with nearly the amount of code you put in like
    Code:
     static final String STRING_COMMANDS[] =
        {
        	"yell",
        	"commands",
            "help"
        };
    But I like what you did here, Did you do this?
    "(No one was giving me constructive criticism so i decided to post it so people can give me nonconstructive criticism)"

    Yeah o-o why would i put up someone elses work?

    and this way, the show commands command is dynamic, it always, shows every command up there~
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  4. #4  
    Registered Member
    Zivik's Avatar
    Join Date
    Oct 2007
    Age
    25
    Posts
    4,432
    Thanks given
    892
    Thanks received
    1,525
    Rep Power
    3228
    Lol no offence but this is awful
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  5. Thankful users:


  6. #5  
    Java Programmer
    _Jon's Avatar
    Join Date
    Jan 2015
    Age
    27
    Posts
    206
    Thanks given
    36
    Thanks received
    63
    Rep Power
    47
    A simple listener would be a much better approach.
    Reply With Quote  
     

  7. #6  
    Registered Member
    Optimum's Avatar
    Join Date
    Apr 2012
    Posts
    3,517
    Thanks given
    830
    Thanks received
    1,580
    Discord
    View profile
    Rep Power
    5000
    The command system i wrote:
    Code:
    package server.net.packets.command;
    
    import java.util.HashMap;
    
    import server.model.entity.players.Client;
    import server.net.packets.PacketType;
    import server.net.packets.command.impl.*;
    import server.net.packets.command.impl.Object;
    import server.model.entity.players.*;
    
    /**
     * A new command engine
     * @author Zack/Optimum
     *
     */
    public class CommandPacketEncoder implements PacketType
    {
    
    	/**
    	 * Hashmap will contain all of the commands. and the
    	 * activation for them
    	 */
    	private HashMap<String, Command> map = new HashMap<>();
    	
    	/**
    	 * Constructor for the command handler which
    	 * adds all the commands to the @link map
    	 */
    	public CommandPacketEncoder()
    	{
    		map.put("tele", new Teleport());
    		map.put("item", new SpawnItem());
    		map.put("master", new Master());
    		map.put("setlevel", new SetLevel());
    		map.put("interface", new Interface());
    		map.put("object", new Object());
    		map.put("gfx", new Gfx());
    		map.put("npc", new SpawnNpc());
    		map.put("anim", new Animation());
    		map.put("itemsearch", new ItemSearch());
    	}
    	
    	/**
    	 * Process the command packet.
    	 */
    	@Override
    	public void processPacket(Client player, int packetType, int packetSize) 
    	{
    		String command = player.getInStream().readString();
    		for(String commandText : map.keySet())
    		{
    			String[] args = command.split(" ");
    			
    			if(commandText.equals(args[0]))
    			{
    				Command cmd = map.get(commandText);
    				for(PlayerRight p : cmd.allowedPlayers())
    				{
    					if(player.playerRights == p.getPlayerRight())
    					{
    						cmd.execute(player, command);
    						return;
    					}
    				}
    			}
    		}
    	}
    
    }//end of class
    Code:
    import server.model.entity.players.Client;
    
    /**
    * Handles the commands
    * @author Zack/Optimum
    */
    public abstract class Command
    {
    
    	/**
    	* All the players that can use the command
    	*/
    	public abstract PlayerRight[] allowedPlayers();
    	
    	/**
    	* Executes the command
    	* @param player - The player
    	* @param command - the command
    	*/
    	public abstract void execute(Client player, String command);
    }
    Code:
    package server.net.packets.command.impl;
    
    import server.model.entity.players.Client;
    import server.model.entity.players.PlayerRight;
    import server.net.packets.command.Command;
    
    public class SpawnItem extends Command
    {
    
    	@Override
    	public PlayerRight[] allowedPlayers() 
    	{
    		return new PlayerRight[] 
    		{
    				PlayerRight.ADMIN,
    				PlayerRight.OWNER
    		};
    	}
    
    	@Override
    	public void execute(Client player, String command) 
    	{
    		try 
    		{
    			String args[] = command.split(" ");
    			int itemId = Integer.parseInt(args[1]);
    			if(itemId > 21000)
    			{
    				player.sendMessage("Can't spawn this id : " + itemId);
    				return;
    			}
    			int itemAmount = Integer.parseInt(args[2]);
    			
    			player.getItems().addItem(itemId, itemAmount);
    			player.sendMessage("You have just spawned " + itemAmount + "x " 
    					+ player.getItems().getItemName(itemId) + ".");
    		}
    		catch (Exception e)
    		{
    			player.sendMessage("Syntax error!: ::item itemId amount");
    		}
    	}
    
    }
    Code:
    package server.net.packets.command.impl;
    
    import server.Server;
    import server.model.entity.players.Client;
    import server.model.entity.players.PlayerRight;
    import server.net.packets.command.Command;
    
    public class ItemSearch extends Command 
    {
    
    	@Override
    	public PlayerRight[] allowedPlayers() 
    	{
    		return PlayerRight.allRights();
    	}
    
    	@Override
    	public void execute(Client player, String command) 
    	{
    		String a[] = command.split(" ");
    		String name = "";
    		int results = 0;
    		for (int i = 1; i < a.length; i++)
    		{
    			name = name + a[i] + " ";
    		}
    
    		name = name.substring(0, name.length() - 1);
    		player.sendMessage("Searching: " + name);
    		for (int j = 0; j < Server.itemHandler.ItemList.length; j++) 
    		{
    			if (Server.itemHandler.ItemList[j] != null)
    				if (Server.itemHandler.ItemList[j].itemName
    						.replace("_", " ").toLowerCase()
    						.contains(name.toLowerCase())) {
    					player.sendMessage("@[email protected]" + Server.itemHandler.ItemList[j].itemName
    									.replace("_", " ") + " - "
    							+ Server.itemHandler.ItemList[j].itemId);
    					results++;
    				}
    		}
    		player.sendMessage(results + " results found...");
    	}
    
    }

    Quote Originally Posted by DownGrade View Post
    Don't let these no life creeps get to you, its always the same on here. They'd rather spend hours upon hours in the rune-server spam section then getting laid! ha ha!Its honestly pathetic i haven't seen so many lowlifes in my life its actually insane i wish that this section would just vanish its probably the only way to get these people out of the community...
    PLEASE BE AWARE OF IMPOSTERS MY DISCORD ID: 362240000760348683
    Reply With Quote  
     

  8. #7  
    Donator

    Join Date
    Oct 2014
    Posts
    591
    Thanks given
    29
    Thanks received
    76
    Rep Power
    26
    This is awful! I highly suggest you to learn how enums work.

    oh and #values() is a expensive call, avoid it.
    Programming service - Fast, Cheap & Quality
    [Only registered and activated users can see links. ]

    Project that I am currently working on:
    [Only registered and activated users can see links. ]

    Reply With Quote  
     

  9. #8  
    Registered Member

    Join Date
    Nov 2014
    Posts
    247
    Thanks given
    38
    Thanks received
    144
    Rep Power
    201
    Quote Originally Posted by Excision Pk View Post
    This is awful! I highly suggest you to learn how enums work.

    oh and #values() is a expensive call, avoid it.
    Not really that expensive, but it wouldn't be a bad idea to avoid having to clone the backing array by retrieving one copy.

    Code:
    public static final Command[] COMMANDS = Command.values()
    but you should ditch what you're doing all together. There is nothing wrong with using enums for this, what you would want to do is something like:

    Code:
    private enum Command {
    PLAYERS {
    
    public void execute(Player player) { player.sendMessage("There are $size players online"); }
    
    };
    
    public void execute(Player player) { throw new AbstractMethodError("Not going to occur"); }
    public boolean hasRights(Rank rank) { return true; }
    
    
    
    }
    
    public void handleCommand(final Player player, final String s) {
    final String[] default_arguments = s.split(" ");
    final Command command = Command.valueOf(default_arguments[0]);
    
    if(command.hasRights(player.getRank())
    command.execute(player);
    }
    but it would get really clogged, so I would reccommend making an abstract class Command and working off that.
    Reply With Quote  
     

  10. Thankful users:


  11. #9  
    touched like seafood
    Tyluur's Avatar
    Join Date
    Jun 2010
    Age
    23
    Posts
    4,837
    Thanks given
    1,676
    Thanks received
    1,566
    Discord
    View profile
    Rep Power
    1390
    This shows you dont really understand how enums work. Not much better than having a packed class full of commands either.
    [Only registered and activated users can see links. ] | [Only registered and activated users can see links. ] | [Only registered and activated users can see links. ] (official dog of rune-server)
    Quote Originally Posted by blakeman8192 View Post
    Keep trying. Quitting is the only true failure.
    Reply With Quote  
     

  12. #10  
    Registered Member
    Join Date
    Dec 2011
    Posts
    790
    Thanks given
    201
    Thanks received
    175
    Rep Power
    173
    What's wrong with the normal way, a single class full of else-if statements?
    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. [Hyperion]Better command system
    By Sakon in forum Snippets
    Replies: 20
    Last Post: 09-23-2014, 11:09 AM
  2. [matrix] [718] Better Command System [718] [matrix]
    By Dr. Client in forum Tutorials
    Replies: 18
    Last Post: 01-16-2014, 08:37 AM
  3. Replies: 39
    Last Post: 10-24-2013, 04:53 AM
  4. Better Commands System.
    By I.Use.Java in forum Snippets
    Replies: 8
    Last Post: 04-23-2013, 06:32 PM
  5. Rune-Evo || Better Yell Command / System
    By Captain Ahoy in forum Snippets
    Replies: 14
    Last Post: 04-23-2013, 10:46 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
  •