mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-05 08:40:08 +00:00
Command changes (nothing plugin breaking).
This commit is contained in:
+18
-29
@@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
require 'java'
|
require 'java'
|
||||||
java_import 'org.apollo.game.event.handler.EventHandler'
|
java_import 'org.apollo.game.event.handler.EventHandler'
|
||||||
java_import 'org.apollo.game.command.PrivilegedCommandListener'
|
java_import 'org.apollo.game.command.CommandListener'
|
||||||
java_import 'org.apollo.game.model.Player'
|
java_import 'org.apollo.game.model.Player'
|
||||||
java_import 'org.apollo.game.model.World'
|
java_import 'org.apollo.game.model.World'
|
||||||
java_import 'org.apollo.game.scheduling.ScheduledTask'
|
java_import 'org.apollo.game.scheduling.ScheduledTask'
|
||||||
@@ -34,14 +34,14 @@ end
|
|||||||
|
|
||||||
# A CommandListener which executes a Proc object with two arguments: the player
|
# A CommandListener which executes a Proc object with two arguments: the player
|
||||||
# and the command.
|
# and the command.
|
||||||
class ProcCommandListener < PrivilegedCommandListener
|
class ProcCommandListener < CommandListener
|
||||||
def initialize(rights, block)
|
def initialize(rights, block)
|
||||||
super rights
|
super rights
|
||||||
@block = block
|
@block = block
|
||||||
end
|
end
|
||||||
|
|
||||||
def executePrivileged(player, command)
|
def execute(player, command)
|
||||||
@block.call player, command
|
@block.call( player, command)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -54,7 +54,7 @@ class ProcEventHandler < EventHandler
|
|||||||
end
|
end
|
||||||
|
|
||||||
def handle(ctx, player, event)
|
def handle(ctx, player, event)
|
||||||
@block.call ctx, player, event
|
@block.call(ctx, player, event)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -66,7 +66,7 @@ class ProcScheduledTask < ScheduledTask
|
|||||||
end
|
end
|
||||||
|
|
||||||
def execute
|
def execute
|
||||||
@block.call self
|
@block.call(self)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -84,12 +84,12 @@ def schedule(*args, &block)
|
|||||||
if args.length == 1 or args.length == 2
|
if args.length == 1 or args.length == 2
|
||||||
delay = args[0]
|
delay = args[0]
|
||||||
immediate = args.length == 2 ? args[1] : false
|
immediate = args.length == 2 ? args[1] : false
|
||||||
World.world.schedule ProcScheduledTask.new(delay, immediate, block)
|
World.world.schedule(ProcScheduledTask.new(delay, immediate, block))
|
||||||
else
|
else
|
||||||
raise "invalid combination of arguments"
|
raise "invalid combination of arguments"
|
||||||
end
|
end
|
||||||
elsif args.length == 1
|
elsif args.length == 1
|
||||||
World.world.schedule args[0]
|
World.world.schedule(args[0])
|
||||||
else
|
else
|
||||||
raise "invalid combination of arguments"
|
raise "invalid combination of arguments"
|
||||||
end
|
end
|
||||||
@@ -122,16 +122,12 @@ end
|
|||||||
|
|
||||||
# Defines an action to be taken upon a button press.
|
# Defines an action to be taken upon a button press.
|
||||||
def on_button(args, proc)
|
def on_button(args, proc)
|
||||||
if args.length != 1
|
raise "button must have one argument" unless args.length == 1
|
||||||
raise "button must have one argument"
|
|
||||||
end
|
|
||||||
|
|
||||||
id = args[0].to_i
|
id = args[0].to_i
|
||||||
|
|
||||||
on :event, :button do |ctx, player, event|
|
on :event, :button do |ctx, player, event|
|
||||||
if event.widget_id == id
|
proc.call player if event.widget_id == id
|
||||||
proc.call player
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -139,29 +135,22 @@ end
|
|||||||
# The event can either be a symbol with the lower case, underscored class name
|
# The event can either be a symbol with the lower case, underscored class name
|
||||||
# or the class itself.
|
# or the class itself.
|
||||||
def on_event(args, proc)
|
def on_event(args, proc)
|
||||||
if args.length != 1
|
raise "event must have one argument" unless args.length == 1
|
||||||
raise "event must have one argument"
|
|
||||||
end
|
|
||||||
|
|
||||||
evt = args[0]
|
evt = args[0]
|
||||||
if evt.is_a? Symbol
|
if evt.is_a? Symbol
|
||||||
class_name = evt.to_s.camelize.concat "Event"
|
class_name = evt.to_s.camelize.concat("Event")
|
||||||
evt = Java::JavaClass.for_name("org.apollo.game.event.impl.".concat class_name)
|
evt = Java::JavaClass.for_name("org.apollo.game.event.impl.".concat(class_name))
|
||||||
end
|
end
|
||||||
|
|
||||||
$ctx.add_last_event_handler evt, ProcEventHandler.new(proc)
|
$ctx.add_last_event_handler(evt, ProcEventHandler.new(proc))
|
||||||
end
|
end
|
||||||
|
|
||||||
# Defines an action to be taken upon a command.
|
# Defines an action to be taken upon a command.
|
||||||
def on_command(args, proc)
|
def on_command(args, proc)
|
||||||
if args.length != 1 and args.length != 2
|
raise "command event must have one or two arguments" unless (1..2).include? args.length
|
||||||
raise "command event must have one or two arguments"
|
|
||||||
end
|
|
||||||
|
|
||||||
rights = RIGHTS_STANDARD
|
rights = args.length == 2 ? args[1] : RIGHTS_STANDARD
|
||||||
if args.length == 2
|
|
||||||
rights = args[1]
|
|
||||||
end
|
|
||||||
|
|
||||||
$ctx.add_command_listener args[0].to_s, ProcCommandListener.new(rights, proc)
|
$ctx.add_command_listener(args[0].to_s, ProcCommandListener.new(rights, proc))
|
||||||
end
|
end
|
||||||
@@ -21,7 +21,6 @@ public final class CommandDispatcher {
|
|||||||
* Creates the command dispatcher and registers a listener for the credits command.
|
* Creates the command dispatcher and registers a listener for the credits command.
|
||||||
*/
|
*/
|
||||||
public CommandDispatcher() {
|
public CommandDispatcher() {
|
||||||
// not in a plugin so it is harder for people to remove!
|
|
||||||
listeners.put("credits", new CreditsCommandListener());
|
listeners.put("credits", new CreditsCommandListener());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,7 +33,7 @@ public final class CommandDispatcher {
|
|||||||
public void dispatch(Player player, Command command) {
|
public void dispatch(Player player, Command command) {
|
||||||
CommandListener listener = listeners.get(command.getName().toLowerCase());
|
CommandListener listener = listeners.get(command.getName().toLowerCase());
|
||||||
if (listener != null) {
|
if (listener != null) {
|
||||||
listener.execute(player, command);
|
listener.executePrivileged(player, command);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,36 @@
|
|||||||
package org.apollo.game.command;
|
package org.apollo.game.command;
|
||||||
|
|
||||||
import org.apollo.game.model.Player;
|
import org.apollo.game.model.Player;
|
||||||
|
import org.apollo.game.model.Player.PrivilegeLevel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An interface which should be implemented by classes to listen to {@link Command}s.
|
* An interface which should be implemented by classes to listen to {@link Command}s.
|
||||||
*
|
*
|
||||||
* @author Graham
|
* @author Graham
|
||||||
|
* @author Major
|
||||||
*/
|
*/
|
||||||
public interface CommandListener {
|
public abstract class CommandListener {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The minimum privilege level.
|
||||||
|
*/
|
||||||
|
private final PrivilegeLevel level;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new command listener with a {@link PrivilegeLevel#STANDARD} requirement.
|
||||||
|
*/
|
||||||
|
public CommandListener() {
|
||||||
|
this(PrivilegeLevel.STANDARD);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new command listener.
|
||||||
|
*
|
||||||
|
* @param level The required {@link PrivilegeLevel}.
|
||||||
|
*/
|
||||||
|
public CommandListener(PrivilegeLevel level) {
|
||||||
|
this.level = level;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the action for this command.
|
* Executes the action for this command.
|
||||||
@@ -15,6 +38,18 @@ public interface CommandListener {
|
|||||||
* @param player The player.
|
* @param player The player.
|
||||||
* @param command The command.
|
* @param command The command.
|
||||||
*/
|
*/
|
||||||
public void execute(Player player, Command command);
|
public abstract void execute(Player player, Command command);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes a privileged command.
|
||||||
|
*
|
||||||
|
* @param player The player.
|
||||||
|
* @param command The command.
|
||||||
|
*/
|
||||||
|
public final void executePrivileged(Player player, Command command) {
|
||||||
|
if (player.getPrivilegeLevel().toInteger() >= level.toInteger()) {
|
||||||
|
execute(player, command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -13,7 +13,7 @@ import org.apollo.util.plugin.PluginManager;
|
|||||||
*
|
*
|
||||||
* @author Graham
|
* @author Graham
|
||||||
*/
|
*/
|
||||||
public final class CreditsCommandListener implements CommandListener {
|
public final class CreditsCommandListener extends CommandListener {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If you are considering removing this command, please bear in mind that Apollo took several people thousands of
|
* If you are considering removing this command, please bear in mind that Apollo took several people thousands of
|
||||||
|
|||||||
@@ -1,42 +0,0 @@
|
|||||||
package org.apollo.game.command;
|
|
||||||
|
|
||||||
import org.apollo.game.model.Player;
|
|
||||||
import org.apollo.game.model.Player.PrivilegeLevel;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A {@link CommandListener} which checks the {@link PrivilegeLevel} of the {@link Player} executing the command.
|
|
||||||
*
|
|
||||||
* @author Graham
|
|
||||||
*/
|
|
||||||
public abstract class PrivilegedCommandListener implements CommandListener {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The minimum privilege level.
|
|
||||||
*/
|
|
||||||
private final PrivilegeLevel level;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates the privileged command listener with the specified minimum level.
|
|
||||||
*
|
|
||||||
* @param level The minimum privilege level.
|
|
||||||
*/
|
|
||||||
public PrivilegedCommandListener(PrivilegeLevel level) {
|
|
||||||
this.level = level;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void execute(Player player, Command command) {
|
|
||||||
if (player.getPrivilegeLevel().toInteger() >= level.toInteger()) {
|
|
||||||
executePrivileged(player, command);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Executes a privileged command.
|
|
||||||
*
|
|
||||||
* @param player The player.
|
|
||||||
* @param command The command.
|
|
||||||
*/
|
|
||||||
public abstract void executePrivileged(Player player, Command command);
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -57,7 +57,6 @@ public final class EventHandlerChain<E extends Event> {
|
|||||||
public void breakHandlerChain() {
|
public void breakHandlerChain() {
|
||||||
running[0] = false;
|
running[0] = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
for (EventHandler<E> handler : handlers) {
|
for (EventHandler<E> handler : handlers) {
|
||||||
@@ -68,4 +67,4 @@ public final class EventHandlerChain<E extends Event> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user