diff --git a/data/plugins/bootstrap.rb b/data/plugins/bootstrap.rb index d46f0817..e45732cf 100644 --- a/data/plugins/bootstrap.rb +++ b/data/plugins/bootstrap.rb @@ -14,7 +14,7 @@ require 'java' 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.World' java_import 'org.apollo.game.scheduling.ScheduledTask' @@ -34,14 +34,14 @@ end # A CommandListener which executes a Proc object with two arguments: the player # and the command. -class ProcCommandListener < PrivilegedCommandListener +class ProcCommandListener < CommandListener def initialize(rights, block) super rights @block = block end - def executePrivileged(player, command) - @block.call player, command + def execute(player, command) + @block.call( player, command) end end @@ -54,7 +54,7 @@ class ProcEventHandler < EventHandler end def handle(ctx, player, event) - @block.call ctx, player, event + @block.call(ctx, player, event) end end @@ -66,7 +66,7 @@ class ProcScheduledTask < ScheduledTask end def execute - @block.call self + @block.call(self) end end @@ -84,12 +84,12 @@ def schedule(*args, &block) if args.length == 1 or args.length == 2 delay = args[0] immediate = args.length == 2 ? args[1] : false - World.world.schedule ProcScheduledTask.new(delay, immediate, block) + World.world.schedule(ProcScheduledTask.new(delay, immediate, block)) else raise "invalid combination of arguments" end elsif args.length == 1 - World.world.schedule args[0] + World.world.schedule(args[0]) else raise "invalid combination of arguments" end @@ -122,16 +122,12 @@ end # Defines an action to be taken upon a button press. def on_button(args, proc) - if args.length != 1 - raise "button must have one argument" - end + raise "button must have one argument" unless args.length == 1 id = args[0].to_i on :event, :button do |ctx, player, event| - if event.widget_id == id - proc.call player - end + proc.call player if event.widget_id == id end end @@ -139,29 +135,22 @@ end # The event can either be a symbol with the lower case, underscored class name # or the class itself. def on_event(args, proc) - if args.length != 1 - raise "event must have one argument" - end + raise "event must have one argument" unless args.length == 1 evt = args[0] if evt.is_a? Symbol - class_name = evt.to_s.camelize.concat "Event" - evt = Java::JavaClass.for_name("org.apollo.game.event.impl.".concat class_name) + class_name = evt.to_s.camelize.concat("Event") + evt = Java::JavaClass.for_name("org.apollo.game.event.impl.".concat(class_name)) end - $ctx.add_last_event_handler evt, ProcEventHandler.new(proc) + $ctx.add_last_event_handler(evt, ProcEventHandler.new(proc)) end # Defines an action to be taken upon a command. def on_command(args, proc) - if args.length != 1 and args.length != 2 - raise "command event must have one or two arguments" - end + raise "command event must have one or two arguments" unless (1..2).include? args.length - rights = RIGHTS_STANDARD - if args.length == 2 - rights = args[1] - end + rights = args.length == 2 ? args[1] : RIGHTS_STANDARD - $ctx.add_command_listener args[0].to_s, ProcCommandListener.new(rights, proc) -end + $ctx.add_command_listener(args[0].to_s, ProcCommandListener.new(rights, proc)) +end \ No newline at end of file diff --git a/src/org/apollo/game/command/CommandDispatcher.java b/src/org/apollo/game/command/CommandDispatcher.java index 8831894d..044dfabb 100644 --- a/src/org/apollo/game/command/CommandDispatcher.java +++ b/src/org/apollo/game/command/CommandDispatcher.java @@ -21,7 +21,6 @@ public final class CommandDispatcher { * Creates the command dispatcher and registers a listener for the credits command. */ public CommandDispatcher() { - // not in a plugin so it is harder for people to remove! listeners.put("credits", new CreditsCommandListener()); } @@ -34,7 +33,7 @@ public final class CommandDispatcher { public void dispatch(Player player, Command command) { CommandListener listener = listeners.get(command.getName().toLowerCase()); if (listener != null) { - listener.execute(player, command); + listener.executePrivileged(player, command); } } diff --git a/src/org/apollo/game/command/CommandListener.java b/src/org/apollo/game/command/CommandListener.java index f6d8ee05..c933cc10 100644 --- a/src/org/apollo/game/command/CommandListener.java +++ b/src/org/apollo/game/command/CommandListener.java @@ -1,13 +1,36 @@ package org.apollo.game.command; 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. * * @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. @@ -15,6 +38,18 @@ public interface CommandListener { * @param player The player. * @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); + } + } } \ No newline at end of file diff --git a/src/org/apollo/game/command/CreditsCommandListener.java b/src/org/apollo/game/command/CreditsCommandListener.java index 9ecdb8e4..95a9d643 100644 --- a/src/org/apollo/game/command/CreditsCommandListener.java +++ b/src/org/apollo/game/command/CreditsCommandListener.java @@ -13,7 +13,7 @@ import org.apollo.util.plugin.PluginManager; * * @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 diff --git a/src/org/apollo/game/command/PrivilegedCommandListener.java b/src/org/apollo/game/command/PrivilegedCommandListener.java deleted file mode 100644 index d969c3de..00000000 --- a/src/org/apollo/game/command/PrivilegedCommandListener.java +++ /dev/null @@ -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); - -} \ No newline at end of file diff --git a/src/org/apollo/game/event/handler/chain/EventHandlerChain.java b/src/org/apollo/game/event/handler/chain/EventHandlerChain.java index c1dd430e..655ca5d7 100644 --- a/src/org/apollo/game/event/handler/chain/EventHandlerChain.java +++ b/src/org/apollo/game/event/handler/chain/EventHandlerChain.java @@ -57,7 +57,6 @@ public final class EventHandlerChain { public void breakHandlerChain() { running[0] = false; } - }; for (EventHandler handler : handlers) { @@ -68,4 +67,4 @@ public final class EventHandlerChain { } } -} +} \ No newline at end of file