Jump to content
Sign in to follow this  
axter

Basic Command Handler

Recommended Posts

The handler:

[CODE]package com.rs.game.player.content;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.List;

import com.rs.Settings;
import com.rs.net.decoders.handlers.Command;
import com.rs.utils.Logger;

/**
*
* @author Taylor Moon
*
*/
public final class CommandHandler {

/**
* A list of commands to be activated. Processing speed takes about 10 ms at
* most. If the commands are empty the process will terminate
*/
private static List<Command> commands = Collections
.synchronizedList(new ArrayList<Command>());

/**
* Processes all the commands in the list. Once the commands are processed,
* the commands are removed from the list.
*
* @throws ConcurrentModificationException
* when 1 thread iterates over the collection and one processes
* the commands
*/
public static void processCommands() {
try {
if (commands.isEmpty() || commands == null)
return;
for (Command toBeProcessed : commands) {
toBeProcessed.activate();
logCommand(toBeProcessed);
remove(toBeProcessed);
}
} catch (ConcurrentModificationException e) {
// One thread iterates while one processes. (Throws this)
if (Settings.DEBUG)
Logger.log("Successfully processed command");
}
}

/**
* Adds a new command to the list of commands and is forced to wait the
* processing speed to be activated.
*
* @param command
* the command being added to the list
*/
public static void writeCommand(Command command) {
commands.add(command);
}

/**
* Removes a command from the list. If the command doesn't exist, then
* nothing will happen
*
* @param command
* the command being removed from the list
*/
public static void remove(Command command) {
if (command == null)
return;
commands.remove(command);
}

/**
* Logs a command to a .txt file in data/logs
*
* @param command
*/
public static void logCommand(Command command) {
try {
@SuppressWarnings("resource")
BufferedWriter writer = new BufferedWriter(new FileWriter(
"data/logs/Commands/" + command.player.getDisplayName()
+ ".txt"));
writer.append("Log at ("
+ Calendar.getInstance().getTime()
+ ") for "
+ (command.rights == 2 ? "Elixrr staff" : command.player
.isSupporter() ? "Support team member"
: "moderator") + ": "
+ command.player.getDisplayName());
writer.newLine();
writer.write("Command: " + command.args[0] + "");
writer.newLine();
writer.write(command.target == null ? "This command was not used on anyone."
: "Used on player: " + command.target.getDisplayName());
writer.newLine();
for (String s : command.args)
writer.write("Arguments: "
+ s.replaceAll(",", " : ") + " --- ");
writer.newLine();
writer.newLine();// so it will classify
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}[/CODE]

Represents a command:

[CODE]package com.rs.net.decoders.handlers;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.TimerTask;

import com.rs.Settings;
import com.rs.cache.loaders.AnimationDefinitions;
import com.rs.cache.loaders.ItemDefinitions;
import com.rs.cache.loaders.ObjectDefinitions;
import com.rs.cores.CoresManager;
import com.rs.game.Animation;
import com.rs.game.ForceMovement;
import com.rs.game.Graphics;
import com.rs.game.Hit;
import com.rs.game.Region;
import com.rs.game.World;
import com.rs.game.WorldObject;
import com.rs.game.WorldTile;
import com.rs.game.Hit.HitLook;
import com.rs.game.item.Item;
import com.rs.game.minigames.FightPits;
import com.rs.game.minigames.clanwars.ClanWars;
import com.rs.game.npc.NPC;
import com.rs.game.npc.others.Bork;
import com.rs.game.player.Player;
import com.rs.game.player.Skills;
import com.rs.game.player.content.FadingScreen;
import com.rs.game.player.content.Highscores;
import com.rs.game.player.content.TicketSystem;
import com.rs.game.player.content.Notes.Note;
import com.rs.game.player.controlers.FightKiln;
import com.rs.game.tasks.WorldTask;
import com.rs.game.tasks.WorldTasksManager;
import com.rs.io.OutputStream;
import com.rs.utils.Encrypt;
import com.rs.utils.IPBanL;
import com.rs.utils.Logger;
import com.rs.utils.PkRank;
import com.rs.utils.SerializableFilesManager;
import com.rs.utils.ShopsHandler;
import com.rs.utils.Utils;

/**
* Represent's a server or client command. When the command is instanced, the
* command will be added to a list of other commands then will be processed
* after the last message was processed within about 50 - 100 ms.
*
* @author Taylor Moon
*
*/
public final class Command {

/**
* Represents the rank ID player moderators are given
*/
public static final int MOD_RANK = 1;

/**
* Represents the rank ID that Elixrr staff are given.
*/
public static final int ADMIN_RANK = 2;

/** The command to execute */
public String command;

/** The attributes corresponding to this command */
public String[] args;

/** The rights for this player */
public int rights;

/** If the command is comming from the blue drop down console */
public boolean fromConsole;

/** If the command is coming from the client */
public boolean fromClient;

/** The player executing this command */
public Player player;

/** If the command has a target */
public Player target;

/**
* Represents a single command
*
* @param command
* - command being executed
* @param player
* - the player executing the command
* @param fromConsole
* - if it's from the client console
* @param clientPacket
* - if it's from the client
*/
public Command(String command, Player player, boolean fromConsole,
boolean clientPacket) {
if (command == null
|| command.replaceAll(" ", "").replaceAll("_", "").equals("")
|| command.length() < 1)
return;
this.command = command.toLowerCase().replaceAll("_", " ");
this.args = command.toLowerCase().split(" ");
this.player = player;
this.fromConsole = fromConsole;
this.fromClient = clientPacket;
this.rights = player.getRights();
}

/**
* Activates a new command and seperates a command depending on the player
* that's executing this command's rights.
*/
public void activate() {
switch (rights) {
case ADMIN_RANK:
processAdminCommand();
return;
case MOD_RANK:
processModCommand();
return;
default:
if (player.isSupporter()) {
processSupportCommand();
} else {
player.getPackets().sendPanelBoxMessage(
"Unknown developer command: \"" + command + "\"");
// what rs says :P
}
}
}

/**
* Processes a command if the player executing it is a player moderator
*/
private void processModCommand() {
String name = "";
switch (args[0]) {
case "mute":
name = String.valueOf(args[1]);
target = World.getPlayerByDisplayName(name);
target.addMute_hours(Integer.valueOf(args[2]));
return;
case "kick":
name = String.valueOf(args[1]);
target = World.getPlayerByDisplayName(name);
target.getSession().getChannel().disconnect();
return;
case "sz":

}
}

/**
* Processes a command if the player executing it is a Elixrr staff
*/
private void processAdminCommand() {
// Add commands here
}

/**
* Processes a command if the player executing it is a Support member
*/
private void processSupportCommand() {
// Add commands here
}
}[/CODE]

When the packet is received:

[CODE]if (message.startsWith("::") || message.startsWith(";;")) {
CommandHandler.writeCommand(new Command(message.replace(
"::", "").replace(";;", ""), player, false, false));[/CODE]

(For console)

[CODE]} else if (packetId == COMMANDS_PACKET) {
if (!player.isRunning())
return;
boolean clientCommand = stream.readUnsignedByte() == 1;
boolean unknown = stream.readUnsignedByte() == 1;
String command = stream.readString();
CommandHandler.writeCommand(new Command(command, player, true, clientCommand));[/CODE]

In your wold processor:

[CODE]CommandHandler.processCommands();[/CODE]

Have fun

Share this post


Link to post
Share on other sites
Axter, I like ya and all bro....
But legit, don't remake things that have already been done just to try and look like a better coder.

To look like a better coder, code things that no one has seen before...
This is just.... Not needed, in fact, it's actually worse than the old one...

Share this post


Link to post
Share on other sites
[code]
/**
* A list of commands to be activated. Processing speed takes about 10 ms at
* most. If the commands are empty the process will terminate
*/
private static List<Command> commands = Collections
.synchronizedList(new ArrayList<Command>());
[/code]
[code]commands == null)[/code]
stopped reading after that line lol :c

edit: I don't quite understand this. Why are you constructing a new command class when a command is written? You have a list for just one class that you initiate multiple times? Edited by Stacx

Share this post


Link to post
Share on other sites
[quote name='Stacx'][code]
/**
* A list of commands to be activated. Processing speed takes about 10 ms at
* most. If the commands are empty the process will terminate
*/
private static List<Command> commands = Collections
.synchronizedList(new ArrayList<Command>());
[/code]
[code]commands == null)[/code]
stopped reading after that line lol :c

edit: I don't quite understand this. Why are you constructing a new command class when a command is written? You have a list for just one class that you initiate multiple times?[/QUOTE]

Good point. The list and everything was kinda ment for larger amount of staff.

[quote name='hellman2741']Axter, I like ya and all bro....
But legit, don't remake things that have already been done just to try and look like a better coder.

To look like a better coder, code things that no one has seen before...
This is just.... Not needed, in fact, it's actually worse than the old one...[/QUOTE]

Thanks for being honest :P

Share this post


Link to post
Share on other sites
You "hard coded" as dragonkk would say. No disrespect intended but could've been much more simpler.

Share this post


Link to post
Share on other sites
[quote name='Sonicforce41']You "hard coded" as dragonkk would say. No disrespect intended but could've been much more simpler.[/QUOTE]

If I was you I wouldn't talk about simplicity haha.

Share this post


Link to post
Share on other sites
This is just awful, there are far better ways to do this. Just store the commands into a hashmap and read from the map if it matches any of the strings. And make the commands implement an interface.

Share this post


Link to post
Share on other sites
Ever heard of a hashmap?

opps, the above post summarized what I was going to say.

Share this post


Link to post
Share on other sites
Sign in to follow this  

×