Spoiler for Command:
Code:/**
* @author Elliot Hayes - "Lumiere" <br> [email protected]
* <br> <a href="https://www.rune-server.ee/members/lumiere/">Rune-Server</a>
*/
@CommandConfig(
validCallArray = {}, toggleCommand = false
)
public abstract class Command {
/**
* The {@link Player} entity requesting to execute the {@link Command}
*/
private final Player player;
/**
* The input of the {@link #player}
*/
private final String command;
/**
* Determines whether or not the {@link #command} came from the client command console
*/
private final boolean fromConsole;
/**
* @[Only registered and activated users can see links. Click Here To Register...] {@link #getClassListFromClasspath} with input parameter: "com.helios.net.packet.impl.commands.impl"
*/
public static final List<Class<?>> getCommandClasses() {
return Misc.getClassListFromClasspath( "com.helios.net.packet.impl.commands.impl" );
}
/**
* Constructs the {@link Command} with the following parameters
*
* @[Only registered and activated users can see links. Click Here To Register...] player
* the {@link Player} entity
* @[Only registered and activated users can see links. Click Here To Register...] command
* the input of the {@link player}
*
* @[Only registered and activated users can see links. Click Here To Register...] fromConsole
* whether or not the {@link command} came from the client command console
*/
public Command(Player player, String command, boolean fromConsole) {
this.player = player;
this.command = command;
this.fromConsole = fromConsole;
}
/**
* Gets the {@link #player} requesting to execute the {@link Command}
*
* @[Only registered and activated users can see links. Click Here To Register...]
* the {@link #player}
*/
public final Player getPlayer() {
return player;
}
/**
* Gets the {@link #command} input of the {@link #player}
*
* @[Only registered and activated users can see links. Click Here To Register...]
* the {@link #command}
*/
public final String getCommand() {
return command;
}
/**
* Gets the {@link #command} input of the {@link #player} split by spaces, as an array
*
* @[Only registered and activated users can see links. Click Here To Register...]
* the {@link #command} input split by spaces, as an array
*/
public final String[] getSpaceSplitCommand() {
return command.split(" ");
}
/**
* Gets the {@link #command} input of the {@link #player} split by hyphens, as an array
*
* @[Only registered and activated users can see links. Click Here To Register...]
* the {@link #command} input split by hyphens, as an array
*/
public final String[] getHyphenSplitCommand() {
return command.split("-");
}
/**
* @[Only registered and activated users can see links. Click Here To Register...] @[Only registered and activated users can see links. Click Here To Register...] if the {@link #command} came from the client command console
*
* @[Only registered and activated users can see links. Click Here To Register...] @[Only registered and activated users can see links. Click Here To Register...] otherwise
*/
public final boolean fromConsole() {
return fromConsole;
}
/**
* The default function to send messages to the {@link #player} containing the correct syntax for the {@link #Command}
*
* @[Only registered and activated users can see links. Click Here To Register...] @[Only registered and activated users can see links. Click Here To Register...]
*/
public final boolean invalidCommandSyntax(final String error) {
final CommandConfig annotation = getClass().getAnnotation(CommandConfig.class);
player.getPacketSender().sendMessage( "<img=12> Command input error! " + error );
player.getPacketSender().sendMessage( "<img=12> Valid calls: \"" + String.join( "\", \"", annotation.validCallArray() ) + "\"" );
if ( !Arrays.asList(annotation).isEmpty() )
player.getPacketSender().sendMessage( "<img=12> Example syntax: \"" + String.join( "\", \"", annotation.exampleSyntaxArray() ) + "\"" );
return false;
}
/**
* The default and finalized function to request the {@link Command} be executed
*
* @[Only registered and activated users can see links. Click Here To Register...]
* {@link #execute()} if {@link #isEligable()} and {@link #isValid()} returns true
*/
public final Command request() {
return ( isEligable() && isValid() ) ? execute() : this;
}
/**
* The default function to determine whether or not the {@link #command} input is valid
* <br> Should be overridden by extending classes if more specifications/restrictions are required
*
* @[Only registered and activated users can see links. Click Here To Register...] @[Only registered and activated users can see links. Click Here To Register...] if the length the {@link #command} input split by spaces as an array, is below the {@link #minimumLength()}
*
* @[Only registered and activated users can see links. Click Here To Register...] @[Only registered and activated users can see links. Click Here To Register...] otherwise
*/
public boolean isValid() {
if ( getSpaceSplitCommand().length < minimumLength() )
return invalidCommandSyntax( "Invalid length! Arguments missing." );
final CommandConfig annotation = getClass().getAnnotation(CommandConfig.class);
if ( getSpaceSplitCommand().length > 1 && Arrays.asList(annotation.exampleSyntaxArray()).isEmpty() )
getPlayer().getPacketSender().sendMessage( "<img=12> Note: Command executed functions without arguments." );
return true;
}
/**
* The default function to determine whether or not a {@link #player} is eligable to request the {@link Command} be executed
* <br> Should be overridden by extending classes if more privileges are required
*
* @[Only registered and activated users can see links. Click Here To Register...] @[Only registered and activated users can see links. Click Here To Register...] if the {@link #eligableRank()} array does not contain the {@link PlayerRights} of the {@link #player}
*
* @[Only registered and activated users can see links. Click Here To Register...] @[Only registered and activated users can see links. Click Here To Register...] otherwise
*/
public boolean isEligable() {
if ( Arrays.stream( eligableRank() ).anyMatch( rights -> player.getRights() == rights ) )
return true;
player.getPacketSender().sendMessage( "Sorry, but it appears you are not eligable to use this command.", fromConsole );
return false;
}
/**
* The default array of eligable {@link PlayerRights}
* <br> Should be overridden by extending classes if specific privileges should be required
*
* @[Only registered and activated users can see links. Click Here To Register...]
* an array of all {@link PlayerRights}
*/
public PlayerRights[] eligableRank() {
return PlayerRights.values();
}
/**
* The default minimum length that the {@link #command} input as an array should be
*
* @[Only registered and activated users can see links. Click Here To Register...] the default minimum length: 1
*/
public int minimumLength() {
return 1;
}
/**
* The function to execute upon successful request of a {@link Command}
*/
public abstract Command execute();
}
Spoiler for CommandConfig:
Code:import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author Elliot Hayes - "Lumiere" <br> [email protected]
* <br> <a href="https://www.rune-server.ee/members/lumiere/">Rune-Server Profile</a>
*/
@[Only registered and activated users can see links. Click Here To Register...]({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @[Only registered and activated users can see links. Click Here To Register...]face CommandConfig{
String[] validCallArray();
String[] exampleSyntaxArray() default {};
boolean toggleCommand() default true;
}
Spoiler for CommandPacketListener:
Code:/**
* @author Elliot Hayes - "Lumiere" <br> [email protected]
* <br> <a href="https://www.rune-server.ee/members/lumiere/">Rune-Server</a>
*/
public class CommandPacketListener implements PacketListener {
@Override
public final void handlePacket(Player player, Packet packet) {
final boolean fromConsole = (packet.readByte() == 1);
final String commandInput = packet.readString();
final String commandCall = commandInput.split( " " )[0].toLowerCase();
if( player == null || player.getHitpoints() <= 0 )
return;
if ( commandInput.length() <= 0 || commandInput.contains( "\r" ) || commandInput.contains( "\n" ) )
return;
if( commandInput.startsWith("/") && commandInput.length() >= 1 ) {
final String message = commandInput.substring(1, commandInput.length());
ClanChatManager.sendMessage(player, message);
return;
}
try {
final Optional<Entry<Class<?>, Boolean>> commandEntry = GameBuilder.getCommandEntryFor(commandCall);
if ( !commandEntry.isPresent() || commandEntry.get().getKey() == null || commandEntry.get().getValue() == null )
return;
if ( !commandEntry.get().getValue() ) {
final String className = commandEntry.get().getKey().getSimpleName().replace("Command", "").replaceAll("(.)([ A-Z])", "$1 $2");
player.getPacketSender().sendMessage( "The \"" + className + "\" command is currently disabled.", fromConsole );
return;
}
final Constructor<?> constructor = commandEntry.get().getKey().getConstructor(Player.class, String.class, boolean.class);
commandEntry.get().getKey().getMethod("request").invoke( constructor.newInstance(player, commandInput, fromConsole) );
} catch( Exception exception ) {
exception.printStackTrace();
exception.getCause();
}
return;
}
}
Spoiler for Command Classes:
Will add more.
Spoiler for TestCommand:
Code:/**
* A {@link Command} subclass that prints a message to the {@link Player} upon successful execution for the purpose of testing
*
* @author Elliot Hayes - "Lumiere" <br> [email protected]
* <br> <a href="https://www.rune-server.ee/members/lumiere/">Rune-Server</a>
*/
@CommandConfig(
validCallArray = { "test", "testcmd", "testing", "testcommand" }
)
public final class TestCommand extends Command {
/**
* @see {@link #Command(Player, String, boolean)} for constructor details
*/
public TestCommand(Player player, String command, boolean fromConsole) {
super(player, command, fromConsole);
}
@Override
public final Command execute() {
getPlayer().getPacketSender().sendMessage( "'Test' command successful.", fromConsole() );
getPlayer().getPacketSender().sendMessage( "'Test' command input: " + getCommand(), fromConsole() );
return this;
}
}
Spoiler for ToggleCmdCommand:
Code:/**
* A {@link Command} subclass that changes the stored value of {@link CommandConfig#toggleCommand()}
* for another {@link Command} subclass
*
* @author Elliot Hayes - "Lumiere" <br> [email protected]
* <br> <a href="https://www.rune-server.ee/members/lumiere/">Rune-Server</a>
*/
@CommandConfig(
validCallArray = { "togglecmd", "disablecmd", "enablecmd" },
exampleSyntaxArray = { "togglecmd validCall", "togglecmd item", "togglecmd spawnitem" }
)
public final class ToggleCmdCommand extends Command {
/**
* @see {@link #Command(Player, String, boolean)} for constructor details
*/
public ToggleCmdCommand(Player player, String command, boolean fromConsole) {
super(player, command, fromConsole);
}
@Override
public final Command execute() {
final String commandCall = getSpaceSplitCommand()[0];
final String desiredCommand = getSpaceSplitCommand()[1];
final Optional<Entry<Class<?>, Boolean>> entry = GameBuilder.getCommandEntryFor(desiredCommand);
if ( entry.isPresent() ) {
final boolean isToggled = entry.get().getValue();
final boolean toggleDisable = commandCall.contains("toggle") && isToggled;
final String className = entry.get().getKey().getSimpleName().replaceAll("Command", "").replaceAll("(.)([ A-Z])", "$1 $2");
final String toggleType = (toggleDisable || commandCall.contains( "disable" )) ? "Disabling \"" : "Enabling \"";
if ( (isToggled && commandCall.contains("enable")) || (!isToggled && commandCall.contains("disable")) ) {
final String currentType = (isToggled ? "enabled" : "disabled") + "!";
getPlayer().getPacketSender().sendMessage( "The \"" + className + "\" command is already " + currentType );
return this;
}
getPlayer().getPacketSender().sendMessage( toggleType + className + "\" command..." , fromConsole() );
entry.get().setValue( commandCall.contains("toggle") ? !isToggled : commandCall.contains("enable") );
return this;
}
return this;
}
@Override
public final int minimumLength() {
return 2;
}
}
Spoiler for AnimationCommand:
Code:/**
* A {@link Command} subclass that executes an {@link Animation} for a {@link Player}
*
* @author Elliot Hayes - "Lumiere" <br> [email protected]
* <br> <a href="https://www.rune-server.ee/members/lumiere/">Rune-Server</a>
*/
@CommandConfig(
validCallArray = { "anim", "animation" },
exampleSyntaxArray = { "anim 1", "anim 100" }
)
public final class AnimationCommand extends Command {
/**
* @see {@link #Command(Player, String, boolean)} for constructor details
*/
public AnimationCommand(Player player, String command, boolean fromConsole) {
super(player, command, fromConsole);
}
@Override
public final boolean isValid() {
if ( !StringUtils.isNumeric(getSpaceSplitCommand()[1]) )
return invalidCommandSyntax( "Animation ID(args 1) must be an integer!" );
return super.isValid();
}
@Override
public final Command execute() {
final int animationId = Integer.parseInt(getSpaceSplitCommand()[1]);
final Animation animation = new Animation(animationId);
getPlayer().performAnimation(animation);
return this;
}
@Override
public final PlayerRights[] eligableRank() {
return PlayerRights.getStaffArray();
}
@Override
public final int minimumLength() {
return 2;
}
}
Spoiler for ChatInterfaceCommand:
Code:/**
* A {@link Command} subclass that opens a chat interface for a {@link Player}
*
* @author Elliot Hayes - "Lumiere" <br> [email protected]
* <br> <a href="https://www.rune-server.ee/members/lumiere/">Rune-Server</a>
*/
@CommandConfig(
validCallArray = { "cint", "cinterface", "chatint", "chatinterface" },
exampleSyntaxArray = { "cint 1", "cint 100" }
)
public final class ChatInterfaceCommand extends Command {
/**
* @see {@link #Command(Player, String, boolean)} for constructor details
*/
public ChatInterfaceCommand(Player player, String command, boolean fromConsole) {
super(player, command, fromConsole);
}
public final boolean isValid() {
if ( !StringUtils.isNumeric(getSpaceSplitCommand()[1]) )
return invalidCommandSyntax( "Interface ID(args 1) must be an integer!" );
return super.isValid();
}
@Override
public final Command execute() {
if ( Integer.parseInt( getSpaceSplitCommand()[1] ) == 30000 )
getPlayer().getPacketSender().sendItemOnInterface( 30005, 1129, 1 );
getPlayer().getPacketSender().sendChatboxInterface( Integer.parseInt( getSpaceSplitCommand()[1] ) );
return this;
}
@Override
public final PlayerRights[] eligableRank() {
return PlayerRights.getStaffArray();
}
@Override
public final int minimumLength() {
return 2;
}
}
