diff --git a/data/plugins/bootstrap.rb b/data/plugins/bootstrap.rb index 134e006b..da4a123c 100644 --- a/data/plugins/bootstrap.rb +++ b/data/plugins/bootstrap.rb @@ -81,7 +81,7 @@ class ProcMessageHandler < MessageHandler # Creates the ProcMessageListener. def initialize(block, option) - super() + super($world) @block = block @option = option end @@ -200,15 +200,5 @@ def on_command(args, proc) raise 'Command message must have one or two arguments.' unless (1..2).include?(args.length) rights = args.length == 2 ? args[1] : RIGHTS_STANDARD - PluginContext::add_command_listener(args[0].to_s, ProcCommandListener.new(rights, proc)) -end - -# Defines an action to be taken upon login. -def on_login(proc) - PluginContext::add_login_listener(ProcLoginListener.new(proc)) -end - -# Defines an action to be taken upon logout. -def on_logout(proc) - PluginContext::add_logout_listener(ProcLogoutListener.new(proc)) + $world.command_dispatcher.register(args[0].to_s, ProcCommandListener.new(rights, proc)) end \ No newline at end of file diff --git a/data/plugins/cmd/teleport/teleport.rb b/data/plugins/cmd/teleport/teleport.rb index cc82a25d..f49ca2ff 100644 --- a/data/plugins/cmd/teleport/teleport.rb +++ b/data/plugins/cmd/teleport/teleport.rb @@ -4,7 +4,9 @@ java_import 'org.apollo.game.model.Position' # Sends the player's position. on :command, :pos, RIGHTS_MOD do |player, command| - player.send_message("You are at: #{player.position}.") + position = player.position + player.send_message("You are at: #{position}.") + player.send_message("Local coordinates: (#{position.get_local_x}, #{position.get_local_y}).") end # Teleports the player to the specified position. diff --git a/data/plugins/entity/spawning/npc-spawn.rb b/data/plugins/entity/spawning/npc-spawn.rb index 65aeeec3..cb874dec 100644 --- a/data/plugins/entity/spawning/npc-spawn.rb +++ b/data/plugins/entity/spawning/npc-spawn.rb @@ -47,7 +47,7 @@ def get_npc(hash) z = hash.delete(:z) position = Position.new(hash.delete(:x), hash.delete(:y), z == nil ? 0 : z) - return Npc.new(id, position) + return Npc.new($world, id, position) end # Applies a decoded hash (one aquired using parse_hash) to the specified npc. diff --git a/data/plugins/location/tutorial-island/guide.rb b/data/plugins/location/tutorial-island/guide.rb index b3239f7d..34b70789 100644 --- a/data/plugins/location/tutorial-island/guide.rb +++ b/data/plugins/location/tutorial-island/guide.rb @@ -18,8 +18,8 @@ CHARACTER_DESIGN = 3559 # Sends the appropriate data to the client when the player logs in to the game. on :login do |event, player| if player.in_tutorial_island - TutorialInstructions::show_instruction(player) - INITIAL_TABS.each_with_index { |tab, index| player.send(SwitchTabInterfaceMessage.new(index, tab)) } + # TutorialInstructions::show_instruction(player) + # INITIAL_TABS.each_with_index { |tab, index| player.send(SwitchTabInterfaceMessage.new(index, tab)) } if (player.tutorial_island_progress == :not_started) show_hint_icon(player) diff --git a/src/org/apollo/Server.java b/src/org/apollo/Server.java index 4812f475..eb3703c9 100644 --- a/src/org/apollo/Server.java +++ b/src/org/apollo/Server.java @@ -52,7 +52,6 @@ public final class Server { SocketAddress http = new InetSocketAddress(NetworkConstants.HTTP_PORT); SocketAddress jaggrab = new InetSocketAddress(NetworkConstants.JAGGRAB_PORT); - server.start(); server.bind(service, http, jaggrab); logger.fine("Starting apollo took " + (System.currentTimeMillis() - start) + " ms."); } catch (Throwable t) { @@ -60,11 +59,6 @@ public final class Server { } } - /** - * The server's context. - */ - private ServerContext context; - /** * The {@link ServerBootstrap} for the HTTP listener. */ @@ -85,11 +79,6 @@ public final class Server { */ private final EventLoopGroup loopGroup = new NioEventLoopGroup(); - /** - * The service manager. - */ - private final ServiceManager serviceManager = new ServiceManager(); - /** * Creates the Apollo server. * @@ -109,13 +98,13 @@ public final class Server { public void bind(SocketAddress serviceAddress, SocketAddress httpAddress, SocketAddress jagGrabAddress) { try { logger.fine("Binding service listener to address: " + serviceAddress + "..."); - serviceBootstrap.bind(serviceAddress).sync(); + serviceBootstrap.bind(serviceAddress).syncUninterruptibly(); logger.fine("Binding HTTP listener to address: " + httpAddress + "..."); - httpBootstrap.bind(httpAddress).sync(); + httpBootstrap.bind(httpAddress).syncUninterruptibly(); logger.fine("Binding JAGGRAB listener to address: " + jagGrabAddress + "..."); - jagGrabBootstrap.bind(jagGrabAddress).sync(); + jagGrabBootstrap.bind(jagGrabAddress).syncUninterruptibly(); } catch (Exception e) { logger.log(Level.SEVERE, "Binding to a port failed: ensure apollo isn't already running.", e); System.exit(1); @@ -128,11 +117,9 @@ public final class Server { * Initialises the server. * * @param releaseClassName The class name of the current active {@link Release}. - * @throws ClassNotFoundException If the release class could not be found. - * @throws IllegalAccessException If the release class could not be accessed. - * @throws InstantiationException If the release class could not be instantiated. + * @throws Exception If an error occurs. */ - public void init(String releaseClassName) throws ClassNotFoundException, InstantiationException, IllegalAccessException { + public void init(String releaseClassName) throws Exception { Class clazz = Class.forName(releaseClassName); Release release = (Release) clazz.newInstance(); @@ -142,7 +129,9 @@ public final class Server { httpBootstrap.group(loopGroup); jagGrabBootstrap.group(loopGroup); - context = new ServerContext(release, serviceManager); + World world = new World(); + ServiceManager serviceManager = new ServiceManager(world); + ServerContext context = new ServerContext(release, serviceManager); ApolloHandler handler = new ApolloHandler(context); ChannelInitializer serviceInitializer = new ServiceChannelInitializer(handler); @@ -156,20 +145,13 @@ public final class Server { ChannelInitializer jagGrabInitializer = new JagGrabChannelInitializer(handler); jagGrabBootstrap.channel(NioServerSocketChannel.class); jagGrabBootstrap.childHandler(jagGrabInitializer); - } - /** - * Starts the server. - * - * @throws Exception If an error occurs. - */ - public void start() throws Exception { - PluginManager manager = new PluginManager(new PluginContext(context)); + PluginManager manager = new PluginManager(world, new PluginContext(context)); serviceManager.startAll(); - int releaseNo = context.getRelease().getReleaseNumber(); + int releaseNo = release.getReleaseNumber(); IndexedFileSystem fs = new IndexedFileSystem(Paths.get("data/fs", Integer.toString(releaseNo)), true); - World.getWorld().init(releaseNo, fs, manager); + world.init(releaseNo, fs, manager); } } \ No newline at end of file diff --git a/src/org/apollo/Service.java b/src/org/apollo/Service.java index 16637b5f..8b19ee74 100644 --- a/src/org/apollo/Service.java +++ b/src/org/apollo/Service.java @@ -1,17 +1,33 @@ package org.apollo; +import org.apollo.game.model.World; + /** - * Represents a service that the server provides. + * Represents a service that the server provides for a {@link World}. * * @author Graham */ public abstract class Service { + /** + * The World this Service is for. + */ + protected final World world; + /** * The server context. */ private ServerContext context; + /** + * Creates the Service. + * + * @param world The {@link World} the Service is for. + */ + public Service(World world) { + this.world = world; + } + /** * Gets the {@link ServerContext}. * diff --git a/src/org/apollo/ServiceManager.java b/src/org/apollo/ServiceManager.java index bb107134..736a84cc 100644 --- a/src/org/apollo/ServiceManager.java +++ b/src/org/apollo/ServiceManager.java @@ -7,6 +7,7 @@ import java.util.HashMap; import java.util.Map; import java.util.logging.Logger; +import org.apollo.game.model.World; import org.apollo.util.xml.XmlNode; import org.apollo.util.xml.XmlParser; import org.xml.sax.SAXException; @@ -31,12 +32,13 @@ public final class ServiceManager { /** * Creates and initializes the {@link ServiceManager}. * + * @param world The {@link World} to create the {@link Service}s for. * @throws IOException If there is an error reading from the xml file. * @throws SAXException If there is an error parsing the xml file. * @throws ReflectiveOperationException If there is an error accessing or creating services using reflection. */ - public ServiceManager() throws IOException, SAXException, ReflectiveOperationException { - init(); + public ServiceManager(World world) throws IOException, SAXException, ReflectiveOperationException { + init(world); } /** @@ -53,14 +55,13 @@ public final class ServiceManager { /** * Initializes this service manager. * + * @param world The {@link World} to create the {@link Service}s for. * @throws SAXException If the service XML file could not be parsed. * @throws IOException If the file could not be accessed. - * @throws IllegalAccessException If the service could not be accessed. - * @throws InstantiationException If the service could not be instantiated. - * @throws ClassNotFoundException If the service could not be found. + * @throws ReflectiveOperationException If the Service could not be created. */ @SuppressWarnings("unchecked") - private void init() throws SAXException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException { + private void init(World world) throws SAXException, IOException, ReflectiveOperationException { logger.fine("Registering services..."); XmlParser parser = new XmlParser(); @@ -84,7 +85,7 @@ public final class ServiceManager { } Class service = (Class) Class.forName(child.getValue()); - register((Class) service, service.newInstance()); + register((Class) service, service.getConstructor(World.class).newInstance(world)); } } diff --git a/src/org/apollo/fs/decoder/GameObjectDecoder.java b/src/org/apollo/fs/decoder/GameObjectDecoder.java index b0cf8148..8206abcf 100644 --- a/src/org/apollo/fs/decoder/GameObjectDecoder.java +++ b/src/org/apollo/fs/decoder/GameObjectDecoder.java @@ -11,6 +11,7 @@ import java.util.function.Predicate; import org.apollo.fs.IndexedFileSystem; import org.apollo.fs.decoder.MapFileDecoder.MapDefinition; import org.apollo.game.model.Position; +import org.apollo.game.model.World; import org.apollo.game.model.area.Region; import org.apollo.game.model.area.RegionRepository; import org.apollo.game.model.area.collision.CollisionMatrix; @@ -70,10 +71,11 @@ public final class GameObjectDecoder { /** * Decodes the GameObjects from their MapDefinitions. * + * @param world The {@link World} containing the StaticGameObjects. * @return The decoded objects. * @throws IOException If there is an error decoding the {@link MapDefinition}s. */ - public GameObject[] decode() throws IOException { + public GameObject[] decode(World world) throws IOException { Map definitions = MapFileDecoder.decode(fs); for (Entry entry : definitions.entrySet()) { @@ -85,7 +87,7 @@ public final class GameObjectDecoder { ByteBuffer objects = fs.getFile(4, definition.getObjectFile()); ByteBuffer decompressed = ByteBuffer.wrap(CompressionUtil.degzip(objects)); - decodeObjects(decompressed, x, y); + decodeObjects(world, decompressed, x, y); ByteBuffer terrain = fs.getFile(4, definition.getTerrainFile()); decompressed = ByteBuffer.wrap(CompressionUtil.degzip(terrain)); @@ -189,11 +191,12 @@ public final class GameObjectDecoder { /** * Decodes object data stored in the specified {@link ByteBuffer}. * + * @param world The {@link World} containing the StaticGameObjects. * @param buffer The ByteBuffer. * @param x The x coordinate of the top left tile of the map file. * @param y The y coordinate of the top left tile of the map file. */ - private void decodeObjects(ByteBuffer buffer, int x, int y) { + private void decodeObjects(World world, ByteBuffer buffer, int x, int y) { int id = -1; int idOffset = BufferUtil.readSmart(buffer); @@ -215,7 +218,7 @@ public final class GameObjectDecoder { int orientation = attributes & 0x3; Position position = new Position(x + localX, y + localY, height); - GameObject object = new StaticGameObject(id, position, type, orientation); + GameObject object = new StaticGameObject(world, id, position, type, orientation); objects.add(object); block(object, position); diff --git a/src/org/apollo/game/GameService.java b/src/org/apollo/game/GameService.java index 49fd689d..e8604422 100644 --- a/src/org/apollo/game/GameService.java +++ b/src/org/apollo/game/GameService.java @@ -59,11 +59,13 @@ public final class GameService extends Service { private ClientSynchronizer synchronizer; /** - * Creates the game service. + * Creates the GameService. * + * @param world The {@link World} the GameService is for. * @throws Exception If an error occurs during initialization. */ - public GameService() throws Exception { + public GameService(World world) throws Exception { + super(world); init(); } @@ -74,7 +76,7 @@ public final class GameService extends Service { */ public void finalizePlayerUnregistration(Player player) { synchronized (this) { - World.getWorld().unregister(player); + world.unregister(player); } } @@ -93,14 +95,12 @@ public final class GameService extends Service { * @throws IOException If there is an error with the file (e.g. does not exist, cannot be read, does not contain * valid nodes). * @throws SAXException If there is an error parsing the file. - * @throws ClassNotFoundException If a message handler could not be found. - * @throws InstantiationException If a message handler could not be instantiated. - * @throws IllegalAccessException If a message handler could not be accessed. + * @throws ReflectiveOperationException If a MessageHandler could not be created. */ - private void init() throws IOException, SAXException, ClassNotFoundException, InstantiationException, IllegalAccessException { + private void init() throws IOException, SAXException, ReflectiveOperationException { try (InputStream is = new FileInputStream("data/messages.xml")) { MessageHandlerChainParser chainGroupParser = new MessageHandlerChainParser(is); - chainGroup = chainGroupParser.parse(); + chainGroup = chainGroupParser.parse(world); } try (InputStream is = new FileInputStream("data/synchronizer.xml")) { @@ -127,7 +127,6 @@ public final class GameService extends Service { public void pulse() { synchronized (this) { LoginService loginService = getContext().getService(LoginService.class); - World world = World.getWorld(); int unregistered = 0; Player old; @@ -144,7 +143,7 @@ public final class GameService extends Service { } world.pulse(); - synchronizer.synchronize(); + synchronizer.synchronize(world.getPlayerRepository(), world.getNpcRepository()); } } @@ -156,8 +155,6 @@ public final class GameService extends Service { * @return A {@link RegistrationStatus}. */ public RegistrationStatus registerPlayer(Player player, GameSession session) { - World world = World.getWorld(); - synchronized (this) { RegistrationStatus status = world.register(player); if (status == RegistrationStatus.OK) { diff --git a/src/org/apollo/game/command/CommandDispatcher.java b/src/org/apollo/game/command/CommandDispatcher.java index be774010..2dbeb852 100644 --- a/src/org/apollo/game/command/CommandDispatcher.java +++ b/src/org/apollo/game/command/CommandDispatcher.java @@ -2,6 +2,7 @@ package org.apollo.game.command; import java.util.HashMap; import java.util.Map; +import java.util.Set; import org.apollo.game.model.entity.Player; @@ -18,10 +19,12 @@ public final class CommandDispatcher { private final Map listeners = new HashMap<>(); /** - * Creates the command dispatcher and registers a listener for the credits command. + * Initialises this CommandDispatcher. + * + * @param authors The {@link Set} of plugin authors. */ - public CommandDispatcher() { - listeners.put("credits", new CreditsCommandListener()); + public void init(Set authors) { + listeners.put("credits", new CreditsCommandListener(authors)); } /** diff --git a/src/org/apollo/game/command/CreditsCommandListener.java b/src/org/apollo/game/command/CreditsCommandListener.java index 6e1faec4..0983b36f 100644 --- a/src/org/apollo/game/command/CreditsCommandListener.java +++ b/src/org/apollo/game/command/CreditsCommandListener.java @@ -4,9 +4,9 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; -import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; -import org.apollo.util.plugin.PluginManager; + +import com.google.common.collect.ImmutableSet; /** * Implements a {@code ::credits} command that lists the authors of all plugins used in the server. @@ -14,6 +14,20 @@ import org.apollo.util.plugin.PluginManager; * @author Graham */ public final class CreditsCommandListener extends CommandListener { + + /** + * The Set of authors. + */ + private final Set authors; + + /** + * Creates the CreditsCommandListener. + * + * @param authors The {@link Set} of authors. + */ + public CreditsCommandListener(Set authors) { + this.authors = ImmutableSet.copyOf(authors); + } /* * If you are considering removing this command, please bear in mind that Apollo took several people thousands of @@ -26,9 +40,6 @@ public final class CreditsCommandListener extends CommandListener { @Override public void execute(Player player, Command command) { - PluginManager mgr = World.getWorld().getPluginManager(); - final Set authors = mgr.getAuthors(); - List text = new ArrayList<>(12 + authors.size()); text.add("@dre@Apollo"); text.add("@dre@Introduction"); diff --git a/src/org/apollo/game/message/handler/MessageHandler.java b/src/org/apollo/game/message/handler/MessageHandler.java index ba0e0b34..3e425835 100644 --- a/src/org/apollo/game/message/handler/MessageHandler.java +++ b/src/org/apollo/game/message/handler/MessageHandler.java @@ -1,6 +1,7 @@ package org.apollo.game.message.handler; import org.apollo.game.message.Message; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; /** @@ -11,6 +12,20 @@ import org.apollo.game.model.entity.Player; */ public abstract class MessageHandler { + /** + * The World the Message occurred in. + */ + protected final World world; + + /** + * Creates the MessageHandler. + * + * @param world The {@link World} the {@link Message} occurred in. + */ + public MessageHandler(World world) { + this.world = world; + } + /** * Handles a message. * diff --git a/src/org/apollo/game/message/handler/impl/BankButtonMessageHandler.java b/src/org/apollo/game/message/handler/impl/BankButtonMessageHandler.java index 67f07fac..23223019 100644 --- a/src/org/apollo/game/message/handler/impl/BankButtonMessageHandler.java +++ b/src/org/apollo/game/message/handler/impl/BankButtonMessageHandler.java @@ -3,6 +3,7 @@ package org.apollo.game.message.handler.impl; import org.apollo.game.message.handler.MessageHandler; import org.apollo.game.message.handler.MessageHandlerContext; import org.apollo.game.message.impl.ButtonMessage; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; /** @@ -12,6 +13,15 @@ import org.apollo.game.model.entity.Player; */ public final class BankButtonMessageHandler extends MessageHandler { + /** + * Creates the BankButtonMessageHandler. + * + * @param world The {@link World} the {@link ButtonMessage} occurred in. + */ + public BankButtonMessageHandler(World world) { + super(world); + } + /** * The withdraw as item button id. */ diff --git a/src/org/apollo/game/message/handler/impl/BankMessageHandler.java b/src/org/apollo/game/message/handler/impl/BankMessageHandler.java index 62e904c7..fe8ca0ee 100644 --- a/src/org/apollo/game/message/handler/impl/BankMessageHandler.java +++ b/src/org/apollo/game/message/handler/impl/BankMessageHandler.java @@ -3,6 +3,7 @@ package org.apollo.game.message.handler.impl; import org.apollo.game.message.handler.MessageHandler; import org.apollo.game.message.handler.MessageHandlerContext; import org.apollo.game.message.impl.ItemActionMessage; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; import org.apollo.game.model.inter.EnterAmountListener; import org.apollo.game.model.inter.bank.BankConstants; @@ -41,21 +42,12 @@ public final class BankMessageHandler extends MessageHandler } /** - * Handles a deposit action. - * - * @param ctx The message handler context. - * @param player The player. - * @param message The message. + * Creates the BankMessageHandler. + * + * @param world The {@link World} the {@link ItemActionMessage} occurred in. */ - private static void deposit(MessageHandlerContext ctx, Player player, ItemActionMessage message) { - int amount = optionToAmount(message.getOption()); - - if (amount == -1) { - EnterAmountListener listener = new BankDepositEnterAmountListener(player, message.getSlot(), message.getId()); - player.getInterfaceSet().openEnterAmountDialogue(listener); - } else if (!BankUtils.deposit(player, message.getSlot(), message.getId(), amount)) { - ctx.breakHandlerChain(); - } + public BankMessageHandler(World world) { + super(world); } @Override @@ -69,6 +61,24 @@ public final class BankMessageHandler extends MessageHandler } } + /** + * Handles a deposit action. + * + * @param ctx The message handler context. + * @param player The player. + * @param message The message. + */ + private void deposit(MessageHandlerContext ctx, Player player, ItemActionMessage message) { + int amount = optionToAmount(message.getOption()); + + if (amount == -1) { + EnterAmountListener listener = new BankDepositEnterAmountListener(player, message.getSlot(), message.getId()); + player.getInterfaceSet().openEnterAmountDialogue(listener); + } else if (!BankUtils.deposit(player, message.getSlot(), message.getId(), amount)) { + ctx.breakHandlerChain(); + } + } + /** * Handles a withdraw action. * @@ -76,7 +86,7 @@ public final class BankMessageHandler extends MessageHandler * @param player The player. * @param message The message. */ - private static void withdraw(MessageHandlerContext ctx, Player player, ItemActionMessage message) { + private void withdraw(MessageHandlerContext ctx, Player player, ItemActionMessage message) { int amount = optionToAmount(message.getOption()); if (amount == -1) { diff --git a/src/org/apollo/game/message/handler/impl/ChatMessageHandler.java b/src/org/apollo/game/message/handler/impl/ChatMessageHandler.java index a1ec4202..05b5deeb 100644 --- a/src/org/apollo/game/message/handler/impl/ChatMessageHandler.java +++ b/src/org/apollo/game/message/handler/impl/ChatMessageHandler.java @@ -3,6 +3,7 @@ package org.apollo.game.message.handler.impl; import org.apollo.game.message.handler.MessageHandler; import org.apollo.game.message.handler.MessageHandlerContext; import org.apollo.game.message.impl.ChatMessage; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; import org.apollo.game.sync.block.SynchronizationBlock; @@ -13,6 +14,15 @@ import org.apollo.game.sync.block.SynchronizationBlock; */ public final class ChatMessageHandler extends MessageHandler { + /** + * Creates the ChatMessageHandler. + * + * @param world The {@link World} the {@link ChatMessage} occurred in. + */ + public ChatMessageHandler(World world) { + super(world); + } + @Override public void handle(MessageHandlerContext ctx, Player player, ChatMessage message) { player.getBlockSet().add(SynchronizationBlock.createChatBlock(player, message)); diff --git a/src/org/apollo/game/message/handler/impl/ChatVerificationHandler.java b/src/org/apollo/game/message/handler/impl/ChatVerificationHandler.java index 36bf1efd..a52d6f8e 100644 --- a/src/org/apollo/game/message/handler/impl/ChatVerificationHandler.java +++ b/src/org/apollo/game/message/handler/impl/ChatVerificationHandler.java @@ -3,6 +3,7 @@ package org.apollo.game.message.handler.impl; import org.apollo.game.message.handler.MessageHandler; import org.apollo.game.message.handler.MessageHandlerContext; import org.apollo.game.message.impl.ChatMessage; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; /** @@ -12,6 +13,15 @@ import org.apollo.game.model.entity.Player; */ public final class ChatVerificationHandler extends MessageHandler { + /** + * Creates the ChatVerificationHandler. + * + * @param world The {@link World} the {@link ChatMessage} occurred in. + */ + public ChatVerificationHandler(World world) { + super(world); + } + @Override public void handle(MessageHandlerContext ctx, Player player, ChatMessage message) { int color = message.getTextColor(); diff --git a/src/org/apollo/game/message/handler/impl/ClosedInterfaceMessageHandler.java b/src/org/apollo/game/message/handler/impl/ClosedInterfaceMessageHandler.java index d755bf05..3831c654 100644 --- a/src/org/apollo/game/message/handler/impl/ClosedInterfaceMessageHandler.java +++ b/src/org/apollo/game/message/handler/impl/ClosedInterfaceMessageHandler.java @@ -3,6 +3,7 @@ package org.apollo.game.message.handler.impl; import org.apollo.game.message.handler.MessageHandler; import org.apollo.game.message.handler.MessageHandlerContext; import org.apollo.game.message.impl.ClosedInterfaceMessage; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; /** @@ -12,6 +13,15 @@ import org.apollo.game.model.entity.Player; */ public final class ClosedInterfaceMessageHandler extends MessageHandler { + /** + * Creates the ClosedInterfaceMessageHandler. + * + * @param world The {@link World} the {@link ClosedInterfaceMessage} occurred in. + */ + public ClosedInterfaceMessageHandler(World world) { + super(world); + } + @Override public void handle(MessageHandlerContext ctx, Player player, ClosedInterfaceMessage message) { player.getInterfaceSet().interfaceClosed(); diff --git a/src/org/apollo/game/message/handler/impl/CommandMessageHandler.java b/src/org/apollo/game/message/handler/impl/CommandMessageHandler.java index f5042acd..d8b79c7b 100644 --- a/src/org/apollo/game/message/handler/impl/CommandMessageHandler.java +++ b/src/org/apollo/game/message/handler/impl/CommandMessageHandler.java @@ -1,6 +1,7 @@ package org.apollo.game.message.handler.impl; import org.apollo.game.command.Command; +import org.apollo.game.message.Message; import org.apollo.game.message.handler.MessageHandler; import org.apollo.game.message.handler.MessageHandlerContext; import org.apollo.game.message.impl.CommandMessage; @@ -14,6 +15,15 @@ import org.apollo.game.model.entity.Player; */ public final class CommandMessageHandler extends MessageHandler { + /** + * Creates the CommandMessageHandler. + * + * @param world The {@link World} the {@link Message} occurred in. + */ + public CommandMessageHandler(World world) { + super(world); + } + @Override public void handle(MessageHandlerContext ctx, Player player, CommandMessage message) { String[] components = message.getCommand().split(" "); @@ -23,7 +33,7 @@ public final class CommandMessageHandler extends MessageHandler System.arraycopy(components, 1, arguments, 0, arguments.length); Command command = new Command(name, arguments); - World.getWorld().getCommandDispatcher().dispatch(player, command); + world.getCommandDispatcher().dispatch(player, command); } } \ No newline at end of file diff --git a/src/org/apollo/game/message/handler/impl/DialogueButtonHandler.java b/src/org/apollo/game/message/handler/impl/DialogueButtonHandler.java index 9d4971e3..5bb7e825 100644 --- a/src/org/apollo/game/message/handler/impl/DialogueButtonHandler.java +++ b/src/org/apollo/game/message/handler/impl/DialogueButtonHandler.java @@ -3,6 +3,7 @@ package org.apollo.game.message.handler.impl; import org.apollo.game.message.handler.MessageHandler; import org.apollo.game.message.handler.MessageHandlerContext; import org.apollo.game.message.impl.ButtonMessage; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; import org.apollo.game.model.inter.InterfaceType; @@ -14,12 +15,21 @@ import org.apollo.game.model.inter.InterfaceType; */ public final class DialogueButtonHandler extends MessageHandler { + /** + * Creates the DialogueButtonHandler. + * + * @param world The {@link World} the {@link ButtonMessage} occurred in. + */ + public DialogueButtonHandler(World world) { + super(world); + } + @Override public void handle(MessageHandlerContext ctx, Player player, ButtonMessage message) { if (player.getInterfaceSet().contains(InterfaceType.DIALOGUE)) { - boolean breakChain = player.getInterfaceSet().buttonClicked(message.getWidgetId()); + boolean terminate = player.getInterfaceSet().buttonClicked(message.getWidgetId()); - if (breakChain) { + if (terminate) { ctx.breakHandlerChain(); } } diff --git a/src/org/apollo/game/message/handler/impl/DialogueContinueMessageHandler.java b/src/org/apollo/game/message/handler/impl/DialogueContinueMessageHandler.java index 3b5a7b1a..df3c8fce 100644 --- a/src/org/apollo/game/message/handler/impl/DialogueContinueMessageHandler.java +++ b/src/org/apollo/game/message/handler/impl/DialogueContinueMessageHandler.java @@ -3,6 +3,7 @@ package org.apollo.game.message.handler.impl; import org.apollo.game.message.handler.MessageHandler; import org.apollo.game.message.handler.MessageHandlerContext; import org.apollo.game.message.impl.DialogueContinueMessage; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; import org.apollo.game.model.inter.InterfaceType; @@ -13,6 +14,15 @@ import org.apollo.game.model.inter.InterfaceType; */ public final class DialogueContinueMessageHandler extends MessageHandler { + /** + * Creates the DialogueContinueMessageHandler. + * + * @param world The {@link World} the {@link DialogueContinueMessage} occurred in. + */ + public DialogueContinueMessageHandler(World world) { + super(world); + } + @Override public void handle(MessageHandlerContext ctx, Player player, DialogueContinueMessage message) { if (player.getInterfaceSet().contains(InterfaceType.DIALOGUE)) { diff --git a/src/org/apollo/game/message/handler/impl/EnteredAmountMessageHandler.java b/src/org/apollo/game/message/handler/impl/EnteredAmountMessageHandler.java index aad2923d..07a4a66f 100644 --- a/src/org/apollo/game/message/handler/impl/EnteredAmountMessageHandler.java +++ b/src/org/apollo/game/message/handler/impl/EnteredAmountMessageHandler.java @@ -3,6 +3,7 @@ package org.apollo.game.message.handler.impl; import org.apollo.game.message.handler.MessageHandler; import org.apollo.game.message.handler.MessageHandlerContext; import org.apollo.game.message.impl.EnteredAmountMessage; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; /** @@ -12,6 +13,15 @@ import org.apollo.game.model.entity.Player; */ public final class EnteredAmountMessageHandler extends MessageHandler { + /** + * Creates the EnteredAmountMessageHandler. + * + * @param world The {@link World} the {@link EnteredAmountMessage} occurred in. + */ + public EnteredAmountMessageHandler(World world) { + super(world); + } + @Override public void handle(MessageHandlerContext ctx, Player player, EnteredAmountMessage message) { player.getInterfaceSet().enteredAmount(message.getAmount()); diff --git a/src/org/apollo/game/message/handler/impl/EquipItemHandler.java b/src/org/apollo/game/message/handler/impl/EquipItemHandler.java index a3628e6b..494f5a47 100644 --- a/src/org/apollo/game/message/handler/impl/EquipItemHandler.java +++ b/src/org/apollo/game/message/handler/impl/EquipItemHandler.java @@ -4,6 +4,7 @@ import org.apollo.game.message.handler.MessageHandler; import org.apollo.game.message.handler.MessageHandlerContext; import org.apollo.game.message.impl.ItemOptionMessage; import org.apollo.game.model.Item; +import org.apollo.game.model.World; import org.apollo.game.model.def.EquipmentDefinition; import org.apollo.game.model.entity.EquipmentConstants; import org.apollo.game.model.entity.Player; @@ -20,10 +21,24 @@ import org.apollo.util.LanguageUtil; * @author Ryley */ public final class EquipItemHandler extends MessageHandler { + + /** + * The option used when equipping an item. + */ + private static final int EQUIP_OPTION = 2; + + /** + * Creates the EquipItemHandler. + * + * @param world The {@link World} the {@link ItemOptionMessage} occurred in. + */ + public EquipItemHandler(World world) { + super(world); + } @Override public void handle(MessageHandlerContext ctx, Player player, ItemOptionMessage message) { - if (message.getOption() != 2 || message.getInterfaceId() != SynchronizationInventoryListener.INVENTORY_ID) { + if (message.getOption() != EQUIP_OPTION || message.getInterfaceId() != SynchronizationInventoryListener.INVENTORY_ID) { return; } @@ -82,7 +97,8 @@ public final class EquipItemHandler extends MessageHandler { return; } - if (definition.getSlot() == EquipmentConstants.SHIELD && weapon != null && EquipmentDefinition.lookup(weapon.getId()).isTwoHanded()) { + if (definition.getSlot() == EquipmentConstants.SHIELD && weapon != null + && EquipmentDefinition.lookup(weapon.getId()).isTwoHanded()) { equipment.set(EquipmentConstants.SHIELD, inventory.reset(inventorySlot)); inventory.add(equipment.reset(EquipmentConstants.WEAPON)); return; diff --git a/src/org/apollo/game/message/handler/impl/ItemOnItemVerificationHandler.java b/src/org/apollo/game/message/handler/impl/ItemOnItemVerificationHandler.java index e40a3350..0b5deb99 100644 --- a/src/org/apollo/game/message/handler/impl/ItemOnItemVerificationHandler.java +++ b/src/org/apollo/game/message/handler/impl/ItemOnItemVerificationHandler.java @@ -4,6 +4,7 @@ import org.apollo.game.message.handler.MessageHandler; import org.apollo.game.message.handler.MessageHandlerContext; import org.apollo.game.message.impl.ItemOnItemMessage; import org.apollo.game.model.Item; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; import org.apollo.game.model.inter.bank.BankConstants; import org.apollo.game.model.inv.Inventory; @@ -16,6 +17,15 @@ import org.apollo.game.model.inv.SynchronizationInventoryListener; */ public final class ItemOnItemVerificationHandler extends MessageHandler { + /** + * Creates the ItemOnItemVerificationHandler. + * + * @param world The {@link World} the {@link ItemOnItemMessage} occurred in. + */ + public ItemOnItemVerificationHandler(World world) { + super(world); + } + @Override public void handle(MessageHandlerContext ctx, Player player, ItemOnItemMessage message) { Inventory inventory; diff --git a/src/org/apollo/game/message/handler/impl/ItemOnObjectVerificationHandler.java b/src/org/apollo/game/message/handler/impl/ItemOnObjectVerificationHandler.java index 925461a4..6e17d237 100644 --- a/src/org/apollo/game/message/handler/impl/ItemOnObjectVerificationHandler.java +++ b/src/org/apollo/game/message/handler/impl/ItemOnObjectVerificationHandler.java @@ -4,6 +4,7 @@ import org.apollo.game.message.handler.MessageHandler; import org.apollo.game.message.handler.MessageHandlerContext; import org.apollo.game.message.impl.ItemOnObjectMessage; import org.apollo.game.model.Item; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; import org.apollo.game.model.inter.bank.BankConstants; import org.apollo.game.model.inv.Inventory; @@ -16,9 +17,19 @@ import org.apollo.game.model.inv.SynchronizationInventoryListener; */ public final class ItemOnObjectVerificationHandler extends MessageHandler { + /** + * Creates the ItemOnObjectVerificationHandler. + * + * @param world The {@link World} the {@link ItemOnObjectMessage} occurred in. + */ + public ItemOnObjectVerificationHandler(World world) { + super(world); + } + @Override public void handle(MessageHandlerContext ctx, Player player, ItemOnObjectMessage message) { - if (message.getInterfaceId() != SynchronizationInventoryListener.INVENTORY_ID && message.getInterfaceId() != BankConstants.SIDEBAR_INVENTORY_ID) { + if (message.getInterfaceId() != SynchronizationInventoryListener.INVENTORY_ID + && message.getInterfaceId() != BankConstants.SIDEBAR_INVENTORY_ID) { ctx.breakHandlerChain(); return; } diff --git a/src/org/apollo/game/message/handler/impl/ItemVerificationHandler.java b/src/org/apollo/game/message/handler/impl/ItemVerificationHandler.java index 4429bdfd..a9754433 100644 --- a/src/org/apollo/game/message/handler/impl/ItemVerificationHandler.java +++ b/src/org/apollo/game/message/handler/impl/ItemVerificationHandler.java @@ -7,6 +7,7 @@ import org.apollo.game.message.handler.MessageHandler; import org.apollo.game.message.handler.MessageHandlerContext; import org.apollo.game.message.impl.InventoryItemMessage; import org.apollo.game.model.Item; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; import org.apollo.game.model.inter.bank.BankConstants; import org.apollo.game.model.inv.Inventory; @@ -20,6 +21,15 @@ import org.apollo.game.model.inv.SynchronizationInventoryListener; */ public final class ItemVerificationHandler extends MessageHandler { + /** + * Creates the ItemVerificationHandler. + * + * @param world The {@link World} the {@link InventoryItemMessage} occurred in. + */ + public ItemVerificationHandler(World world) { + super(world); + } + /** * A supplier for an {@link Inventory}. * diff --git a/src/org/apollo/game/message/handler/impl/NpcActionVerificationHandler.java b/src/org/apollo/game/message/handler/impl/NpcActionVerificationHandler.java index 3b5287fa..f1e22e01 100644 --- a/src/org/apollo/game/message/handler/impl/NpcActionVerificationHandler.java +++ b/src/org/apollo/game/message/handler/impl/NpcActionVerificationHandler.java @@ -18,13 +18,18 @@ import org.apollo.util.MobRepository; public final class NpcActionVerificationHandler extends MessageHandler { /** - * The world's npc repository. + * Creates the NpcActionVerificationHandler. + * + * @param world The {@link World} the {@link NpcActionMessage} occurred in. */ - private final MobRepository repository = World.getWorld().getNpcRepository(); + public NpcActionVerificationHandler(World world) { + super(world); + } @Override public void handle(MessageHandlerContext ctx, Player player, NpcActionMessage message) { int index = message.getIndex(); + MobRepository repository = world.getNpcRepository(); if (index < 0 || index >= repository.capacity()) { ctx.breakHandlerChain(); diff --git a/src/org/apollo/game/message/handler/impl/ObjectActionVerificationHandler.java b/src/org/apollo/game/message/handler/impl/ObjectActionVerificationHandler.java index e8899b45..6e8a516e 100644 --- a/src/org/apollo/game/message/handler/impl/ObjectActionVerificationHandler.java +++ b/src/org/apollo/game/message/handler/impl/ObjectActionVerificationHandler.java @@ -9,7 +9,6 @@ import org.apollo.game.message.impl.ObjectActionMessage; import org.apollo.game.model.Position; import org.apollo.game.model.World; import org.apollo.game.model.area.Region; -import org.apollo.game.model.area.RegionRepository; import org.apollo.game.model.def.ObjectDefinition; import org.apollo.game.model.entity.Entity.EntityType; import org.apollo.game.model.entity.Player; @@ -23,9 +22,24 @@ import org.apollo.game.model.entity.obj.GameObject; public final class ObjectActionVerificationHandler extends MessageHandler { /** - * The world's RegionRepository. + * Indicates whether or not the {@link List} of {@link GameObject}s contains the object with the specified id. + * + * @param id The id of the object. + * @param objects The list of objects. + * @return {@code true} if the list does contain the object with the specified id, otherwise {@code false}. */ - private final RegionRepository repository = World.getWorld().getRegionRepository(); + private static boolean containsObject(int id, Set objects) { + return objects.stream().anyMatch(object -> object.getId() == id); + } + + /** + * Creates the ObjectActionVerificationHandler. + * + * @param world The {@link World} the {@link ObjectActionMessage} occurred in. + */ + public ObjectActionVerificationHandler(World world) { + super(world); + } @Override public void handle(MessageHandlerContext ctx, Player player, ObjectActionMessage message) { @@ -34,10 +48,9 @@ public final class ObjectActionVerificationHandler extends MessageHandler objects = region.getEntities(position, EntityType.STATIC_OBJECT, EntityType.DYNAMIC_OBJECT); if (!player.getPosition().isWithinDistance(position, 15) || !containsObject(id, objects)) { @@ -52,15 +65,4 @@ public final class ObjectActionVerificationHandler extends MessageHandler objects) { - return objects.stream().anyMatch(object -> object.getId() == id); - } - } \ No newline at end of file diff --git a/src/org/apollo/game/message/handler/impl/PlayerActionVerificationHandler.java b/src/org/apollo/game/message/handler/impl/PlayerActionVerificationHandler.java index cc05c18d..3c43fe70 100644 --- a/src/org/apollo/game/message/handler/impl/PlayerActionVerificationHandler.java +++ b/src/org/apollo/game/message/handler/impl/PlayerActionVerificationHandler.java @@ -15,13 +15,18 @@ import org.apollo.util.MobRepository; public final class PlayerActionVerificationHandler extends MessageHandler { /** - * The world's player repository. + * Creates the PlayerActionVerificationHandler. + * + * @param world The {@link World} the {@link PlayerActionMessage} occurred in. */ - private final MobRepository repository = World.getWorld().getPlayerRepository(); + public PlayerActionVerificationHandler(World world) { + super(world); + } @Override public void handle(MessageHandlerContext ctx, Player player, PlayerActionMessage message) { int index = message.getIndex(); + MobRepository repository = world.getPlayerRepository(); if (index < 0 || index >= repository.capacity()) { ctx.breakHandlerChain(); diff --git a/src/org/apollo/game/message/handler/impl/PlayerDesignMessageHandler.java b/src/org/apollo/game/message/handler/impl/PlayerDesignMessageHandler.java index 7e7ad3af..d0842ca7 100644 --- a/src/org/apollo/game/message/handler/impl/PlayerDesignMessageHandler.java +++ b/src/org/apollo/game/message/handler/impl/PlayerDesignMessageHandler.java @@ -4,6 +4,7 @@ import org.apollo.game.message.handler.MessageHandler; import org.apollo.game.message.handler.MessageHandlerContext; import org.apollo.game.message.impl.CloseInterfaceMessage; import org.apollo.game.message.impl.PlayerDesignMessage; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; /** @@ -13,6 +14,15 @@ import org.apollo.game.model.entity.Player; */ public final class PlayerDesignMessageHandler extends MessageHandler { + /** + * Creates the PlayerDesignMessageHandler. + * + * @param world The {@link World} the {@link PlayerDesignMessage} occurred in. + */ + public PlayerDesignMessageHandler(World world) { + super(world); + } + @Override public void handle(MessageHandlerContext ctx, Player player, PlayerDesignMessage message) { player.setAppearance(message.getAppearance()); diff --git a/src/org/apollo/game/message/handler/impl/PlayerDesignVerificationHandler.java b/src/org/apollo/game/message/handler/impl/PlayerDesignVerificationHandler.java index d09a5669..a2b379ec 100644 --- a/src/org/apollo/game/message/handler/impl/PlayerDesignVerificationHandler.java +++ b/src/org/apollo/game/message/handler/impl/PlayerDesignVerificationHandler.java @@ -4,6 +4,7 @@ import org.apollo.game.message.handler.MessageHandler; import org.apollo.game.message.handler.MessageHandlerContext; import org.apollo.game.message.impl.PlayerDesignMessage; import org.apollo.game.model.Appearance; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; import org.apollo.game.model.entity.setting.Gender; @@ -14,6 +15,15 @@ import org.apollo.game.model.entity.setting.Gender; */ public final class PlayerDesignVerificationHandler extends MessageHandler { + /** + * Creates the PlayerDesignVerificationHandler. + * + * @param world The {@link World} the {@link PlayerDesignMessage} occurred in. + */ + public PlayerDesignVerificationHandler(World world) { + super(world); + } + @Override public void handle(MessageHandlerContext ctx, Player player, PlayerDesignMessage message) { if (!valid(message.getAppearance())) { @@ -27,7 +37,7 @@ public final class PlayerDesignVerificationHandler extends MessageHandler { + /** + * Creates the RemoveEquippedItemHandler. + * + * @param world The {@link World} the {@link ItemActionMessage} occurred in. + */ + public RemoveEquippedItemHandler(World world) { + super(world); + } + @Override public void handle(MessageHandlerContext ctx, Player player, ItemActionMessage message) { if (message.getOption() == 1 && message.getInterfaceId() == SynchronizationInventoryListener.EQUIPMENT_ID) { diff --git a/src/org/apollo/game/message/handler/impl/SwitchItemMessageHandler.java b/src/org/apollo/game/message/handler/impl/SwitchItemMessageHandler.java index 96ab49c8..3f0579dd 100644 --- a/src/org/apollo/game/message/handler/impl/SwitchItemMessageHandler.java +++ b/src/org/apollo/game/message/handler/impl/SwitchItemMessageHandler.java @@ -3,6 +3,7 @@ package org.apollo.game.message.handler.impl; import org.apollo.game.message.handler.MessageHandler; import org.apollo.game.message.handler.MessageHandlerContext; import org.apollo.game.message.impl.SwitchItemMessage; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; import org.apollo.game.model.inter.bank.BankConstants; import org.apollo.game.model.inv.Inventory; @@ -16,6 +17,15 @@ import org.apollo.game.model.inv.SynchronizationInventoryListener; */ public final class SwitchItemMessageHandler extends MessageHandler { + /** + * Creates the SwitchItemMessageHandler. + * + * @param world The {@link World} the {@link SwitchItemMessage} occurred in. + */ + public SwitchItemMessageHandler(World world) { + super(world); + } + @Override public void handle(MessageHandlerContext ctx, Player player, SwitchItemMessage message) { Inventory inventory; @@ -37,9 +47,10 @@ public final class SwitchItemMessageHandler extends MessageHandler= 0 && message.getNewSlot() >= 0 && message.getOldSlot() < inventory.capacity() && message.getNewSlot() < inventory.capacity()) { + int old = message.getOldSlot(), next = message.getNewSlot(); + if (old >= 0 && next >= 0 && old < inventory.capacity() && next < inventory.capacity()) { // events must be fired for it to work if a sidebar inventory overlay is used - inventory.swap(insertPermitted && message.isInserting(), message.getOldSlot(), message.getNewSlot()); + inventory.swap(insertPermitted && message.isInserting(), old, next); } } diff --git a/src/org/apollo/game/message/handler/impl/WalkMessageHandler.java b/src/org/apollo/game/message/handler/impl/WalkMessageHandler.java index f7a4ecea..bb1deac6 100644 --- a/src/org/apollo/game/message/handler/impl/WalkMessageHandler.java +++ b/src/org/apollo/game/message/handler/impl/WalkMessageHandler.java @@ -4,6 +4,7 @@ import org.apollo.game.message.handler.MessageHandler; import org.apollo.game.message.handler.MessageHandlerContext; import org.apollo.game.message.impl.WalkMessage; import org.apollo.game.model.Position; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; import org.apollo.game.model.entity.WalkingQueue; @@ -14,6 +15,15 @@ import org.apollo.game.model.entity.WalkingQueue; */ public final class WalkMessageHandler extends MessageHandler { + /** + * Creates the WalkMessageHandler. + * + * @param world The {@link World} the {@link WalkMessage} occurred in. + */ + public WalkMessageHandler(World world) { + super(world); + } + @Override public void handle(MessageHandlerContext ctx, Player player, WalkMessage message) { WalkingQueue queue = player.getWalkingQueue(); diff --git a/src/org/apollo/game/message/impl/HintIconMessage.java b/src/org/apollo/game/message/impl/HintIconMessage.java index 6a9757ac..b99c958e 100644 --- a/src/org/apollo/game/message/impl/HintIconMessage.java +++ b/src/org/apollo/game/message/impl/HintIconMessage.java @@ -2,6 +2,7 @@ package org.apollo.game.message.impl; import java.util.NoSuchElementException; import java.util.Optional; +import java.util.OptionalInt; import org.apollo.game.message.Message; import org.apollo.game.model.Position; @@ -62,7 +63,7 @@ public final class HintIconMessage extends Message { * @return The HintIconMessage. */ public static HintIconMessage forNpc(int index) { - return new HintIconMessage(Type.NPC, Optional.of(index), Optional.empty()); + return new HintIconMessage(Type.NPC, OptionalInt.of(index), Optional.empty()); } /** @@ -72,7 +73,7 @@ public final class HintIconMessage extends Message { * @return The HintIconMessage. */ public static HintIconMessage forPlayer(int index) { - return new HintIconMessage(Type.PLAYER, Optional.of(index), Optional.empty()); + return new HintIconMessage(Type.PLAYER, OptionalInt.of(index), Optional.empty()); } /** @@ -96,7 +97,7 @@ public final class HintIconMessage extends Message { /** * The index of the Mob, if applicable. */ - private final Optional index; + private final OptionalInt index; /** * The Position of the tile, if applicable. @@ -115,7 +116,7 @@ public final class HintIconMessage extends Message { * @param index The index of the Mob, if applicable. * @param position The Position of the tile, if applicable. */ - private HintIconMessage(Type type, Optional index, Optional position) { + private HintIconMessage(Type type, OptionalInt index, Optional position) { this.type = type; this.index = index; this.position = position; @@ -128,7 +129,7 @@ public final class HintIconMessage extends Message { * @throws NoSuchElementException If no index is available for this HintIcon. */ public int getIndex() { - return index.get(); + return index.getAsInt(); } /** diff --git a/src/org/apollo/game/model/World.java b/src/org/apollo/game/model/World.java index 2284286a..e74d0fbc 100644 --- a/src/org/apollo/game/model/World.java +++ b/src/org/apollo/game/model/World.java @@ -23,9 +23,9 @@ import org.apollo.game.model.def.NpcDefinition; import org.apollo.game.model.def.ObjectDefinition; import org.apollo.game.model.entity.Entity; import org.apollo.game.model.entity.Entity.EntityType; -import org.apollo.game.model.entity.obj.GameObject; import org.apollo.game.model.entity.Npc; import org.apollo.game.model.entity.Player; +import org.apollo.game.model.entity.obj.GameObject; import org.apollo.game.model.event.Event; import org.apollo.game.model.event.EventListener; import org.apollo.game.model.event.EventListenerChainSet; @@ -77,24 +77,10 @@ public final class World { */ private static final Logger logger = Logger.getLogger(World.class.getName()); - /** - * The world. - */ - private static final World world = new World(); - - /** - * Gets the world. - * - * @return The world. - */ - public static World getWorld() { - return world; - } - /** * The command dispatcher. */ - private final CommandDispatcher commandDispatcher = new CommandDispatcher(); + private CommandDispatcher commandDispatcher = new CommandDispatcher(); /** * The EventListenerChainSet for this World. @@ -144,7 +130,7 @@ public final class World { /** * Creates the world. */ - private World() { + public World() { } @@ -247,14 +233,15 @@ public final class World { logger.fine("Loaded " + objectDefs.length + " object definitions."); GameObjectDecoder staticDecoder = new GameObjectDecoder(fs, regions); - GameObject[] objects = staticDecoder.decode(); + GameObject[] objects = staticDecoder.decode(this); placeEntities(objects); logger.fine("Loaded " + objects.length + " static objects."); - npcMovement = new NpcMovementTask(); // Must be exactly here because of ordering issues. + npcMovement = new NpcMovementTask(regions); // Must be exactly here because of ordering issues. scheduler.schedule(npcMovement); manager.start(); + commandDispatcher.init(manager.getAuthors()); pluginManager = manager; } diff --git a/src/org/apollo/game/model/entity/Entity.java b/src/org/apollo/game/model/entity/Entity.java index eca2bbc8..af0f6f78 100644 --- a/src/org/apollo/game/model/entity/Entity.java +++ b/src/org/apollo/game/model/entity/Entity.java @@ -1,6 +1,7 @@ package org.apollo.game.model.entity; import org.apollo.game.model.Position; +import org.apollo.game.model.World; import org.apollo.game.model.area.EntityUpdateType; import org.apollo.game.model.area.Region; import org.apollo.game.model.area.update.UpdateOperation; @@ -17,21 +18,16 @@ public abstract class Entity { */ public enum EntityType { - /** - * An Item that is positioned on the ground. - */ - GROUND_ITEM, - - /** - * A GameObject that is loaded statically (i.e. from the game resources) at start-up. - */ - STATIC_OBJECT, - /** * A GameObject that is loaded dynamically, usually for specific Players. */ DYNAMIC_OBJECT, + /** + * An Item that is positioned on the ground. + */ + GROUND_ITEM, + /** * An Npc. */ @@ -45,7 +41,12 @@ public abstract class Entity { /** * A projectile (e.g. an arrow). */ - PROJECTILE; + PROJECTILE, + + /** + * A GameObject that is loaded statically (i.e. from the game resources) at start-up. + */ + STATIC_OBJECT; /** * Returns whether or not this EntityType is for a Mob. @@ -59,16 +60,23 @@ public abstract class Entity { } /** - * The position of this entity. + * The Position of this Entity. */ protected Position position; /** - * Creates a new entity with the specified position. - * - * @param position The position. + * The World containing this Entity. */ - public Entity(Position position) { + protected final World world; + + /** + * Creates the Entity. + * + * @param world The {@link World} containing the Entity. + * @param position The {@link Position} of the Entity. + */ + public Entity(World world, Position position) { + this.world = world; this.position = position; } @@ -91,6 +99,15 @@ public abstract class Entity { return position; } + /** + * Gets the {@link World} this Entity is in. + * + * @return The World. + */ + public World getWorld() { + return world; + } + @Override public abstract int hashCode(); diff --git a/src/org/apollo/game/model/entity/GroundItem.java b/src/org/apollo/game/model/entity/GroundItem.java index 1dc322d4..0a0ce3de 100644 --- a/src/org/apollo/game/model/entity/GroundItem.java +++ b/src/org/apollo/game/model/entity/GroundItem.java @@ -2,8 +2,9 @@ package org.apollo.game.model.entity; import org.apollo.game.model.Item; import org.apollo.game.model.Position; -import org.apollo.game.model.area.Region; +import org.apollo.game.model.World; import org.apollo.game.model.area.EntityUpdateType; +import org.apollo.game.model.area.Region; import org.apollo.game.model.area.update.ItemUpdateOperation; import org.apollo.game.model.area.update.UpdateOperation; @@ -17,24 +18,26 @@ public final class GroundItem extends Entity { /** * Creates a new GroundItem. * + * @param world The {@link World} containing the GroundItem. * @param position The {@link Position} of the Item. * @param item The Item displayed on the ground. * @return The GroundItem. */ - public static GroundItem create(Position position, Item item) { - return new GroundItem(position, item, -1); + public static GroundItem create(World world, Position position, Item item) { + return new GroundItem(world, position, item, -1); } /** * Creates a new dropped GroundItem. * + * @param world The {@link World} containing the GroundItem. * @param position The {@link Position} of the Item. * @param item The Item displayed on the ground. * @param owner The the {@link Player} who dropped this GroundItem. * @return The GroundItem. */ - public static GroundItem dropped(Position position, Item item, Player owner) { - return new GroundItem(position, item, owner.getIndex()); + public static GroundItem dropped(World world, Position position, Item item, Player owner) { + return new GroundItem(world, position, item, owner.getIndex()); } /** @@ -49,13 +52,14 @@ public final class GroundItem extends Entity { /** * Creates the GroundItem. - * - * @param position The {@link Position} of the Item. + * + * @param world The {@link World} containing the GroundItem. + * @param position The {@link Position} of the GroundItem. * @param item The Item displayed on the ground. * @param index The index of the {@link Player} who dropped this GroundItem. */ - private GroundItem(Position position, Item item, int index) { - super(position); + private GroundItem(World world, Position position, Item item, int index) { + super(world, position); this.item = item; this.index = index; } diff --git a/src/org/apollo/game/model/entity/Mob.java b/src/org/apollo/game/model/entity/Mob.java index 7e56f344..528ff1aa 100644 --- a/src/org/apollo/game/model/entity/Mob.java +++ b/src/org/apollo/game/model/entity/Mob.java @@ -116,22 +116,24 @@ public abstract class Mob extends Entity { private transient boolean teleporting = false; /** - * Creates the mob with the specified initial {@link Position}. + * Creates the Mob. * - * @param position The position. + * @param world The {@link World} containing the Mob + * @param position The {@link Position} of the Mob. */ - public Mob(Position position) { - this(position, null); + public Mob(World world, Position position) { + this(world, position, null); } /** - * Creates the mob. + * Creates the Mob. * - * @param position The initial position. - * @param definition The {@link NpcDefinition}. + * @param world The {@link World} containing the Mob + * @param position The {@link Position} of the Mob. + * @param definition The {@link NpcDefinition} of the Mob. */ - public Mob(Position position, NpcDefinition definition) { - super(position); + public Mob(World world, Position position, NpcDefinition definition) { + super(world, position); this.definition = Optional.ofNullable(definition); init(); @@ -435,9 +437,9 @@ public abstract class Mob extends Entity { * @param position The Position. */ public final void setPosition(Position position) { - if (World.getWorld().submit(new MobPositionUpdateEvent(this, position))) { + if (world.submit(new MobPositionUpdateEvent(this, position))) { Position old = this.position; - RegionRepository repository = World.getWorld().getRegionRepository(); + RegionRepository repository = world.getRegionRepository(); Region current = repository.fromPosition(old), next = repository.fromPosition(position); current.removeEntity(this); @@ -482,7 +484,7 @@ public abstract class Mob extends Entity { } this.action = action; - return World.getWorld().schedule(action); + return world.schedule(action); } /** @@ -541,7 +543,7 @@ public abstract class Mob extends Entity { * Initialises this mob. */ private void init() { - World.getWorld().schedule(new SkillNormalizationTask(this)); + world.schedule(new SkillNormalizationTask(this)); } } \ No newline at end of file diff --git a/src/org/apollo/game/model/entity/Npc.java b/src/org/apollo/game/model/entity/Npc.java index 5c3fdc88..e84249d0 100644 --- a/src/org/apollo/game/model/entity/Npc.java +++ b/src/org/apollo/game/model/entity/Npc.java @@ -3,6 +3,7 @@ package org.apollo.game.model.entity; import java.util.Optional; import org.apollo.game.model.Position; +import org.apollo.game.model.World; import org.apollo.game.model.def.NpcDefinition; import org.apollo.game.sync.block.SynchronizationBlock; @@ -22,24 +23,26 @@ public final class Npc extends Mob { private Optional boundaries; /** - * Creates a new Npc with the specified id and {@link Position}. + * Creates the Npc. * + * @param world The {@link World} containing the Npc. * @param id The id. * @param position The position. */ - public Npc(int id, Position position) { - this(position, NpcDefinition.lookup(id), null); + public Npc(World world, int id, Position position) { + this(world, position, NpcDefinition.lookup(id), null); } /** - * Creates a new Npc with the specified {@link NpcDefinition} and {@link Position}. + * Creates the Npc. * + * @param world The {@link World} containing the Npc. * @param position The Position. * @param definition The NpcDefinition. * @param boundaries The boundary Positions. */ - public Npc(Position position, NpcDefinition definition, Position[] boundaries) { - super(position, definition); + public Npc(World world, Position position, NpcDefinition definition, Position[] boundaries) { + super(world, position, definition); this.boundaries = Optional.ofNullable(boundaries); } diff --git a/src/org/apollo/game/model/entity/Player.java b/src/org/apollo/game/model/entity/Player.java index 7fdfd74c..89582f71 100644 --- a/src/org/apollo/game/model/entity/Player.java +++ b/src/org/apollo/game/model/entity/Player.java @@ -195,13 +195,14 @@ public final class Player extends Mob { private transient int worldId = 1; /** - * Creates the {@link Player}. + * Creates the Player. * + * @param world The {@link World} containing the Player. * @param credentials The player's credentials. * @param position The initial position. */ - public Player(PlayerCredentials credentials, Position position) { - super(position); + public Player(World world, PlayerCredentials credentials, Position position) { + super(world, position); this.credentials = credentials; init(); @@ -597,7 +598,7 @@ public final class Player extends Mob { * Logs the player out, if possible. */ public void logout() { - if (World.getWorld().submit(new LogoutEvent(this))) { + if (world.submit(new LogoutEvent(this))) { send(new LogoutMessage()); } } @@ -698,7 +699,7 @@ public final class Player extends Mob { bank.forceRefresh(); skillSet.forceRefresh(); - World.getWorld().submit(new LoginEvent(this)); + world.submit(new LoginEvent(this)); } /** @@ -747,7 +748,6 @@ public final class Player extends Mob { send(new IgnoreListMessage(ignores)); } - World world = World.getWorld(); for (String username : friends) { int worldId = world.isPlayerOnline(username) ? world.getPlayer(username).worldId : 0; send(new SendFriendMessage(username, worldId)); diff --git a/src/org/apollo/game/model/entity/obj/DynamicGameObject.java b/src/org/apollo/game/model/entity/obj/DynamicGameObject.java index 004792bd..bf99cb55 100644 --- a/src/org/apollo/game/model/entity/obj/DynamicGameObject.java +++ b/src/org/apollo/game/model/entity/obj/DynamicGameObject.java @@ -5,6 +5,7 @@ import java.util.HashSet; import java.util.Set; import org.apollo.game.model.Position; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; /** @@ -31,7 +32,7 @@ public final class DynamicGameObject extends GameObject { @Override public boolean equals(Object obj) { if (obj instanceof WeakPlayerReference) { - WeakPlayerReference other = (WeakPlayerReference) obj; + WeakPlayerReference other = (WeakPlayerReference) obj; // TODO need to have a reference queue Player player = get(); return player != null && player.equals(other.get()); @@ -51,27 +52,29 @@ public final class DynamicGameObject extends GameObject { /** * Creates a DynamicGameObject that is visible only to {@link Player}s specified later. * + * @param world The {@link World} containing the DynamicGameObject. * @param id The id of the DynamicGameObject * @param position The {@link Position} of the DynamicGameObject. * @param type The type of the DynamicGameObject. * @param orientation The orientation of the DynamicGameObject. * @return The DynamicGameObject. */ - public static DynamicGameObject createLocal(int id, Position position, int type, int orientation) { - return new DynamicGameObject(id, position, type, orientation, false); + public static DynamicGameObject createLocal(World world, int id, Position position, int type, int orientation) { + return new DynamicGameObject(world, id, position, type, orientation, false); } /** * Creates a DynamicGameObject that is always visible. * + * @param world The {@link World} containing the DynamicGameObject. * @param id The id of the DynamicGameObject * @param position The {@link Position} of the DynamicGameObject. * @param type The type of the DynamicGameObject. * @param orientation The orientation of the DynamicGameObject. * @return The DynamicGameObject. */ - public static DynamicGameObject createPublic(int id, Position position, int type, int orientation) { - return new DynamicGameObject(id, position, type, orientation, true); + public static DynamicGameObject createPublic(World world, int id, Position position, int type, int orientation) { + return new DynamicGameObject(world, id, position, type, orientation, true); } /** @@ -87,14 +90,15 @@ public final class DynamicGameObject extends GameObject { /** * Creates the DynamicGameObject. * + * @param world The {@link World} containing the DynamicGameObject. * @param id The id of the DynamicGameObject * @param position The {@link Position} of the DynamicGameObject. * @param type The type of the DynamicGameObject. * @param orientation The orientation of the DynamicGameObject. * @param alwaysVisible The flag indicates whether or not this DynamicGameObject is visible to every player. */ - private DynamicGameObject(int id, Position position, int type, int orientation, boolean alwaysVisible) { - super(id, position, type, orientation); + private DynamicGameObject(World world, int id, Position position, int type, int orientation, boolean alwaysVisible) { + super(world, id, position, type, orientation); this.alwaysVisible = alwaysVisible; } @@ -126,7 +130,7 @@ public final class DynamicGameObject extends GameObject { } @Override - public boolean viewableBy(Player player) { + public boolean viewableBy(Player player, World world) { return alwaysVisible || players.contains(new WeakPlayerReference(player)); } diff --git a/src/org/apollo/game/model/entity/obj/GameObject.java b/src/org/apollo/game/model/entity/obj/GameObject.java index e04cf80c..092911b8 100644 --- a/src/org/apollo/game/model/entity/obj/GameObject.java +++ b/src/org/apollo/game/model/entity/obj/GameObject.java @@ -1,6 +1,7 @@ package org.apollo.game.model.entity.obj; import org.apollo.game.model.Position; +import org.apollo.game.model.World; import org.apollo.game.model.area.EntityUpdateType; import org.apollo.game.model.area.Region; import org.apollo.game.model.area.update.ObjectUpdateOperation; @@ -26,13 +27,14 @@ public abstract class GameObject extends Entity { /** * Creates the GameObject. * + * @param world The {@link World} containing the GameObject. * @param id The id of the GameObject * @param position The {@link Position} of the GameObject. * @param type The type of the GameObject. * @param orientation The orientation of the GameObject. */ - public GameObject(int id, Position position, int type, int orientation) { - super(position); + public GameObject(World world, int id, Position position, int type, int orientation) { + super(world, position); this.packed = id << 8 | (type & 0x3F) << 2 | orientation & 0x3; } @@ -102,8 +104,9 @@ public abstract class GameObject extends Entity { * Returns whether or not this GameObject can be seen by the specified {@link Player}. * * @param player The Player. + * @param world The {@link World} containing the GameObject. * @return {@code true} if the Player can see this GameObject, {@code false} if not. */ - public abstract boolean viewableBy(Player player); + public abstract boolean viewableBy(Player player, World world); } \ No newline at end of file diff --git a/src/org/apollo/game/model/entity/obj/StaticGameObject.java b/src/org/apollo/game/model/entity/obj/StaticGameObject.java index b59c20f7..16810286 100644 --- a/src/org/apollo/game/model/entity/obj/StaticGameObject.java +++ b/src/org/apollo/game/model/entity/obj/StaticGameObject.java @@ -16,13 +16,14 @@ public final class StaticGameObject extends GameObject { /** * Creates the StaticGameObject. * + * @param world The {@link World} containing the StaticGameObject. * @param id The id of the StaticGameObject * @param position The {@link Position} of the StaticGameObject. * @param type The type code of the StaticGameObject. * @param orientation The orientation of the StaticGameObject. */ - public StaticGameObject(int id, Position position, int type, int orientation) { - super(id, position, type, orientation); + public StaticGameObject(World world, int id, Position position, int type, int orientation) { + super(world, id, position, type, orientation); } @Override @@ -31,8 +32,8 @@ public final class StaticGameObject extends GameObject { } @Override - public boolean viewableBy(Player player) { - RegionRepository repository = World.getWorld().getRegionRepository(); + public boolean viewableBy(Player player, World world) { + RegionRepository repository = world.getRegionRepository(); RegionCoordinates coordinates = position.getRegionCoordinates(); return repository.get(coordinates).contains(this); diff --git a/src/org/apollo/game/model/entity/path/AStarPathfindingAlgorithm.java b/src/org/apollo/game/model/entity/path/AStarPathfindingAlgorithm.java index 6c10ed58..a8348454 100644 --- a/src/org/apollo/game/model/entity/path/AStarPathfindingAlgorithm.java +++ b/src/org/apollo/game/model/entity/path/AStarPathfindingAlgorithm.java @@ -10,6 +10,7 @@ import java.util.Queue; import java.util.Set; import org.apollo.game.model.Position; +import org.apollo.game.model.area.RegionRepository; /** * A {@link PathfindingAlgorithm} that utilises the A* algorithm to find a solution. @@ -32,10 +33,12 @@ public final class AStarPathfindingAlgorithm extends PathfindingAlgorithm { /** * Creates the A* pathfinding algorithm with the specified heuristic. - * + * + * @param repository The {@link RegionRepository}. * @param heuristic The heuristic. */ - public AStarPathfindingAlgorithm(Heuristic heuristic) { + public AStarPathfindingAlgorithm(RegionRepository repository, Heuristic heuristic) { + super(repository); this.heuristic = heuristic; } diff --git a/src/org/apollo/game/model/entity/path/PathfindingAlgorithm.java b/src/org/apollo/game/model/entity/path/PathfindingAlgorithm.java index 513c7ff0..f60ccee2 100644 --- a/src/org/apollo/game/model/entity/path/PathfindingAlgorithm.java +++ b/src/org/apollo/game/model/entity/path/PathfindingAlgorithm.java @@ -5,7 +5,6 @@ import java.util.Optional; import org.apollo.game.model.Direction; import org.apollo.game.model.Position; -import org.apollo.game.model.World; import org.apollo.game.model.area.Region; import org.apollo.game.model.area.RegionRepository; import org.apollo.game.model.entity.Entity.EntityType; @@ -20,9 +19,18 @@ import com.google.common.base.Preconditions; abstract class PathfindingAlgorithm { /** - * The repository of Regions. + * The RegionRepository. */ - private static final RegionRepository REPOSITORY = World.getWorld().getRegionRepository(); + private final RegionRepository repository; + + /** + * Creates the PathfindingAlgorithm. + * + * @param repository The {@link RegionRepository}. + */ + public PathfindingAlgorithm(RegionRepository repository) { + this.repository = repository; + } /** * Finds a valid path from the origin {@link Position} to the target one. @@ -77,7 +85,7 @@ abstract class PathfindingAlgorithm { } Position next = new Position(x, y, height); - Region region = REPOSITORY.get(next.getRegionCoordinates()); + Region region = repository.get(next.getRegionCoordinates()); if (region.traversable(next, EntityType.NPC, direction) && (positions.length == 0 || inside(next, positions))) { return true; } diff --git a/src/org/apollo/game/model/entity/path/SimplePathfindingAlgorithm.java b/src/org/apollo/game/model/entity/path/SimplePathfindingAlgorithm.java index 8789eb38..1307a725 100644 --- a/src/org/apollo/game/model/entity/path/SimplePathfindingAlgorithm.java +++ b/src/org/apollo/game/model/entity/path/SimplePathfindingAlgorithm.java @@ -6,6 +6,7 @@ import java.util.Optional; import org.apollo.game.model.Direction; import org.apollo.game.model.Position; +import org.apollo.game.model.area.RegionRepository; /** * A very simple pathfinding algorithm that simply walks in the direction of the target until it either reaches it or is @@ -15,6 +16,15 @@ import org.apollo.game.model.Position; */ public final class SimplePathfindingAlgorithm extends PathfindingAlgorithm { + /** + * Creates the SimplePathfindingAlgorithm. + * + * @param repository The {@link RegionRepository}. + */ + public SimplePathfindingAlgorithm(RegionRepository repository) { + super(repository); + } + /** * The Optional containing the boundary Positions. */ diff --git a/src/org/apollo/game/model/inter/InterfaceSet.java b/src/org/apollo/game/model/inter/InterfaceSet.java index 613d7d38..420f7c1e 100644 --- a/src/org/apollo/game/model/inter/InterfaceSet.java +++ b/src/org/apollo/game/model/inter/InterfaceSet.java @@ -12,7 +12,6 @@ import org.apollo.game.message.impl.OpenInterfaceMessage; import org.apollo.game.message.impl.OpenInterfaceSidebarMessage; import org.apollo.game.message.impl.OpenOverlayMessage; import org.apollo.game.message.impl.OpenSidebarMessage; -import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; import org.apollo.game.model.event.impl.CloseInterfacesEvent; import org.apollo.game.model.inter.dialogue.DialogueListener; @@ -87,7 +86,7 @@ public final class InterfaceSet { */ public void close() { CloseInterfacesEvent event = new CloseInterfacesEvent(player); - if (World.getWorld().submit(event)) { + if (player.getWorld().submit(event)) { closeAndNotify(); player.send(new CloseInterfaceMessage()); } diff --git a/src/org/apollo/game/scheduling/impl/NpcMovementTask.java b/src/org/apollo/game/scheduling/impl/NpcMovementTask.java index 9a87a94a..5072c2f7 100644 --- a/src/org/apollo/game/scheduling/impl/NpcMovementTask.java +++ b/src/org/apollo/game/scheduling/impl/NpcMovementTask.java @@ -7,6 +7,7 @@ import java.util.Queue; import java.util.Random; import org.apollo.game.model.Position; +import org.apollo.game.model.area.RegionRepository; import org.apollo.game.model.entity.Npc; import org.apollo.game.model.entity.WalkingQueue; import org.apollo.game.model.entity.path.SimplePathfindingAlgorithm; @@ -39,7 +40,7 @@ public final class NpcMovementTask extends ScheduledTask { /** * The PathfindingAlgorithm used by this Task. */ - private final SimplePathfindingAlgorithm algorithm = new SimplePathfindingAlgorithm(); + private final SimplePathfindingAlgorithm algorithm; /** * The Queue of Npcs. @@ -48,9 +49,12 @@ public final class NpcMovementTask extends ScheduledTask { /** * Creates the NpcMovementTask. + * + * @param repository The {@link RegionRepository}. */ - public NpcMovementTask() { + public NpcMovementTask(RegionRepository repository) { super(DELAY, false); + algorithm = new SimplePathfindingAlgorithm(repository); } /** diff --git a/src/org/apollo/game/sync/ClientSynchronizer.java b/src/org/apollo/game/sync/ClientSynchronizer.java index eb96fc98..21237688 100644 --- a/src/org/apollo/game/sync/ClientSynchronizer.java +++ b/src/org/apollo/game/sync/ClientSynchronizer.java @@ -1,5 +1,9 @@ package org.apollo.game.sync; +import org.apollo.game.model.entity.Npc; +import org.apollo.game.model.entity.Player; +import org.apollo.util.MobRepository; + /** * The {@link ClientSynchronizer} manages the update sequence which keeps clients synchronized with the in-game world. * There are two implementations distributed with Apollo: {@link SequentialClientSynchronizer} which is optimized for a @@ -16,7 +20,10 @@ public abstract class ClientSynchronizer { /** * Synchronizes the state of the clients with the state of the server. + * + * @param players The {@link MobRepository} containing the {@link Player}s. + * @param npcs The {@link MobRepository} containing the {@link Npc}s. */ - public abstract void synchronize(); + public abstract void synchronize(MobRepository players, MobRepository npcs); } \ No newline at end of file diff --git a/src/org/apollo/game/sync/ParallelClientSynchronizer.java b/src/org/apollo/game/sync/ParallelClientSynchronizer.java index f2620e5a..49ddc088 100644 --- a/src/org/apollo/game/sync/ParallelClientSynchronizer.java +++ b/src/org/apollo/game/sync/ParallelClientSynchronizer.java @@ -10,7 +10,6 @@ import java.util.concurrent.ThreadFactory; import org.apollo.game.GameService; import org.apollo.game.message.impl.RegionUpdateMessage; -import org.apollo.game.model.World; import org.apollo.game.model.area.RegionCoordinates; import org.apollo.game.model.entity.Npc; import org.apollo.game.model.entity.Player; @@ -58,9 +57,7 @@ public final class ParallelClientSynchronizer extends ClientSynchronizer { } @Override - public void synchronize() { - MobRepository players = World.getWorld().getPlayerRepository(); - MobRepository npcs = World.getWorld().getNpcRepository(); + public void synchronize(MobRepository players, MobRepository npcs) { int playerCount = players.size(); int npcCount = npcs.size(); diff --git a/src/org/apollo/game/sync/SequentialClientSynchronizer.java b/src/org/apollo/game/sync/SequentialClientSynchronizer.java index f6671ed2..eef83b46 100644 --- a/src/org/apollo/game/sync/SequentialClientSynchronizer.java +++ b/src/org/apollo/game/sync/SequentialClientSynchronizer.java @@ -6,7 +6,6 @@ import java.util.Map; import org.apollo.game.GameService; import org.apollo.game.message.impl.RegionUpdateMessage; -import org.apollo.game.model.World; import org.apollo.game.model.area.RegionCoordinates; import org.apollo.game.model.entity.Npc; import org.apollo.game.model.entity.Player; @@ -31,10 +30,7 @@ import org.apollo.util.MobRepository; public final class SequentialClientSynchronizer extends ClientSynchronizer { @Override - public void synchronize() { - MobRepository players = World.getWorld().getPlayerRepository(); - MobRepository npcs = World.getWorld().getNpcRepository(); - + public void synchronize(MobRepository players, MobRepository npcs) { Map> updates = new HashMap<>(); Map> snapshots = new HashMap<>(); diff --git a/src/org/apollo/game/sync/task/NpcSynchronizationTask.java b/src/org/apollo/game/sync/task/NpcSynchronizationTask.java index b8d63221..e8739cc0 100644 --- a/src/org/apollo/game/sync/task/NpcSynchronizationTask.java +++ b/src/org/apollo/game/sync/task/NpcSynchronizationTask.java @@ -6,7 +6,6 @@ import java.util.List; import org.apollo.game.message.impl.NpcSynchronizationMessage; import org.apollo.game.model.Position; -import org.apollo.game.model.World; import org.apollo.game.model.entity.Npc; import org.apollo.game.model.entity.Player; import org.apollo.game.sync.seg.AddNpcSegment; @@ -50,7 +49,8 @@ public final class NpcSynchronizationTask extends SynchronizationTask { for (Iterator it = localNpcs.iterator(); it.hasNext();) { Npc npc = it.next(); - if (!npc.isActive() || npc.isTeleporting() || npc.getPosition().getLongestDelta(playerPosition) > player.getViewingDistance()) { + if (!npc.isActive() || npc.isTeleporting() + || npc.getPosition().getLongestDelta(playerPosition) > player.getViewingDistance()) { it.remove(); segments.add(new RemoveMobSegment()); } else { @@ -60,7 +60,7 @@ public final class NpcSynchronizationTask extends SynchronizationTask { int added = 0; - for (Npc npc : World.getWorld().getNpcRepository()) { + for (Npc npc : player.getWorld().getNpcRepository()) { if (localNpcs.size() >= 255) { player.flagExcessiveNpcs(); break; @@ -69,7 +69,8 @@ public final class NpcSynchronizationTask extends SynchronizationTask { } Position npcPosition = npc.getPosition(); - if (npcPosition.isWithinDistance(playerPosition, player.getViewingDistance()) && !localNpcs.contains(npc) && npcPosition.getHeight() == playerPosition.getHeight()) { + if (npcPosition.isWithinDistance(playerPosition, player.getViewingDistance()) && !localNpcs.contains(npc) + && npcPosition.getHeight() == playerPosition.getHeight()) { localNpcs.add(npc); added++; npc.turnTo(npc.getFacingPosition()); diff --git a/src/org/apollo/game/sync/task/PlayerSynchronizationTask.java b/src/org/apollo/game/sync/task/PlayerSynchronizationTask.java index 12382927..aa888bec 100644 --- a/src/org/apollo/game/sync/task/PlayerSynchronizationTask.java +++ b/src/org/apollo/game/sync/task/PlayerSynchronizationTask.java @@ -6,7 +6,6 @@ import java.util.List; import org.apollo.game.message.impl.PlayerSynchronizationMessage; import org.apollo.game.model.Position; -import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; import org.apollo.game.sync.block.AppearanceBlock; import org.apollo.game.sync.block.ChatBlock; @@ -80,7 +79,7 @@ public final class PlayerSynchronizationTask extends SynchronizationTask { int added = 0; - MobRepository repository = World.getWorld().getPlayerRepository(); + MobRepository repository = player.getWorld().getPlayerRepository(); for (Iterator it = repository.iterator(); it.hasNext();) { Player other = it.next(); if (localPlayers.size() >= 255) { diff --git a/src/org/apollo/game/sync/task/PrePlayerSynchronizationTask.java b/src/org/apollo/game/sync/task/PrePlayerSynchronizationTask.java index 6589e69b..1ca693a9 100644 --- a/src/org/apollo/game/sync/task/PrePlayerSynchronizationTask.java +++ b/src/org/apollo/game/sync/task/PrePlayerSynchronizationTask.java @@ -12,7 +12,6 @@ import org.apollo.game.message.impl.GroupedRegionUpdateMessage; import org.apollo.game.message.impl.RegionChangeMessage; import org.apollo.game.message.impl.RegionUpdateMessage; import org.apollo.game.model.Position; -import org.apollo.game.model.World; import org.apollo.game.model.area.Region; import org.apollo.game.model.area.RegionCoordinates; import org.apollo.game.model.area.RegionRepository; @@ -169,7 +168,7 @@ public final class PrePlayerSynchronizationTask extends SynchronizationTask { RegionCoordinates base = position.getRegionCoordinates(); int baseX = base.getX(), baseY = base.getY(); - RegionRepository repository = World.getWorld().getRegionRepository(); + RegionRepository repository = player.getWorld().getRegionRepository(); List messages = new ArrayList<>(); for (int x = baseX - REGION_COUNT; x <= baseX + REGION_COUNT; x++) { diff --git a/src/org/apollo/io/MessageHandlerChainParser.java b/src/org/apollo/io/MessageHandlerChainParser.java index 27208725..df14262c 100644 --- a/src/org/apollo/io/MessageHandlerChainParser.java +++ b/src/org/apollo/io/MessageHandlerChainParser.java @@ -11,6 +11,7 @@ import org.apollo.game.message.Message; import org.apollo.game.message.handler.MessageHandler; import org.apollo.game.message.handler.MessageHandlerChain; import org.apollo.game.message.handler.MessageHandlerChainGroup; +import org.apollo.game.model.World; import org.apollo.util.xml.XmlNode; import org.apollo.util.xml.XmlParser; import org.xml.sax.SAXException; @@ -45,15 +46,13 @@ public final class MessageHandlerChainParser { /** * Parses the XML and produces a group of {@link MessageHandlerChain}s. * + * @param world The {@link World} this MessageHandlerChainGroup is for. + * @return A {@link MessageHandlerChainGroup}. * @throws IOException If an I/O error occurs. * @throws SAXException If a SAX error occurs. - * @throws ClassNotFoundException If a class was not found. - * @throws IllegalAccessException If a class was accessed illegally. - * @throws InstantiationException If a class could not be instantiated. - * @return A {@link MessageHandlerChainGroup}. + * @throws ReflectiveOperationException If a reflection error occurs. */ - @SuppressWarnings("unchecked") - public MessageHandlerChainGroup parse() throws IOException, SAXException, ClassNotFoundException, InstantiationException, IllegalAccessException { + public MessageHandlerChainGroup parse(World world) throws IOException, SAXException, ReflectiveOperationException { XmlNode messages = parser.parse(is); if (!messages.getName().equals("messages")) { throw new IOException("Root node name is not 'messages'."); @@ -80,6 +79,7 @@ public final class MessageHandlerChainParser { throw new IOException("Type node must have a value."); } + @SuppressWarnings("unchecked") Class messageClass = (Class) Class.forName(messageClassName); List> handlers = new ArrayList<>(); @@ -93,13 +93,15 @@ public final class MessageHandlerChainParser { throw new IOException("Handler node must have a value."); } - Class> handlerClass = (Class>) Class.forName(handlerClassName); - MessageHandler handler = handlerClass.newInstance(); + @SuppressWarnings("unchecked") + Class> handlerClass = (Class>) Class + .forName(handlerClassName); + MessageHandler handler = handlerClass.getConstructor(World.class).newInstance(world); handlers.add(handler); } MessageHandler[] handlersArray = handlers.toArray(new MessageHandler[handlers.size()]); - @SuppressWarnings("rawtypes") + @SuppressWarnings({ "rawtypes", "unchecked" }) MessageHandlerChain chain = new MessageHandlerChain(handlersArray); chains.put(messageClass, chain); diff --git a/src/org/apollo/io/player/BinaryPlayerSerializer.java b/src/org/apollo/io/player/BinaryPlayerSerializer.java index 91a01126..a73cf368 100644 --- a/src/org/apollo/io/player/BinaryPlayerSerializer.java +++ b/src/org/apollo/io/player/BinaryPlayerSerializer.java @@ -19,6 +19,7 @@ import java.util.Set; import org.apollo.game.model.Appearance; import org.apollo.game.model.Item; import org.apollo.game.model.Position; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; import org.apollo.game.model.entity.Skill; import org.apollo.game.model.entity.SkillSet; @@ -48,7 +49,16 @@ import com.lambdaworks.crypto.SCryptUtil; * @author Graham * @author Major */ -public final class BinaryPlayerSerializer implements PlayerSerializer { +public final class BinaryPlayerSerializer extends PlayerSerializer { + + /** + * Creates the BinaryPlayerSerializer. + * + * @param world The {@link World} to place the {@link Player}s in. + */ + public BinaryPlayerSerializer(World world) { + super(world); + } /** * The Path to the saved games directory. @@ -69,7 +79,7 @@ public final class BinaryPlayerSerializer implements PlayerSerializer { public PlayerLoaderResponse loadPlayer(PlayerCredentials credentials) throws IOException { Path path = getFile(credentials.getUsername()); if (!Files.exists(path)) { - Player player = new Player(credentials, TUTORIAL_ISLAND_SPAWN); + Player player = new Player(world, credentials, TUTORIAL_ISLAND_SPAWN); credentials.setPassword(SCryptUtil.scrypt(credentials.getPassword(), 16384, 8, 1)); return new PlayerLoaderResponse(LoginConstants.STATUS_OK, player); @@ -108,7 +118,7 @@ public final class BinaryPlayerSerializer implements PlayerSerializer { colors[slot] = in.readUnsignedByte(); } - Player player = new Player(credentials, new Position(x, y, height)); + Player player = new Player(world, credentials, new Position(x, y, height)); player.setPrivilegeLevel(privilege); player.setMembers(members); player.setChatPrivacy(chatPrivacy); diff --git a/src/org/apollo/io/player/DummyPlayerSerializer.java b/src/org/apollo/io/player/DummyPlayerSerializer.java index 5466f385..07ec539e 100644 --- a/src/org/apollo/io/player/DummyPlayerSerializer.java +++ b/src/org/apollo/io/player/DummyPlayerSerializer.java @@ -1,5 +1,6 @@ package org.apollo.io.player; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; import org.apollo.game.model.entity.setting.MembershipStatus; import org.apollo.game.model.entity.setting.PrivilegeLevel; @@ -12,13 +13,22 @@ import org.apollo.security.PlayerCredentials; * @author Graham * @author Major */ -public final class DummyPlayerSerializer implements PlayerSerializer { +public final class DummyPlayerSerializer extends PlayerSerializer { + + /** + * Creates the DummyPlayerSerializer. + * + * @param world The {@link World} to place the {@link Player}s in. + */ + public DummyPlayerSerializer(World world) { + super(world); + } @Override public PlayerLoaderResponse loadPlayer(PlayerCredentials credentials) { int status = LoginConstants.STATUS_OK; - Player player = new Player(credentials, TUTORIAL_ISLAND_SPAWN); + Player player = new Player(world, credentials, TUTORIAL_ISLAND_SPAWN); player.setPrivilegeLevel(PrivilegeLevel.ADMINISTRATOR); player.setMembers(MembershipStatus.PAID); diff --git a/src/org/apollo/io/player/JdbcPlayerSerializer.java b/src/org/apollo/io/player/JdbcPlayerSerializer.java index c8e48cf0..a08e149f 100644 --- a/src/org/apollo/io/player/JdbcPlayerSerializer.java +++ b/src/org/apollo/io/player/JdbcPlayerSerializer.java @@ -1,5 +1,6 @@ package org.apollo.io.player; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; import org.apollo.security.PlayerCredentials; @@ -8,7 +9,16 @@ import org.apollo.security.PlayerCredentials; * * @author Major */ -public final class JdbcPlayerSerializer implements PlayerSerializer { +public final class JdbcPlayerSerializer extends PlayerSerializer { + + /** + * Creates the JdbcPlayerSerializer. + * + * @param world The {@link World} to place the {@link Player}s in. + */ + public JdbcPlayerSerializer(World world) { + super(world); + } @Override public void savePlayer(Player player) throws Exception { diff --git a/src/org/apollo/io/player/PlayerSerializer.java b/src/org/apollo/io/player/PlayerSerializer.java index 4d246a74..11ce058b 100644 --- a/src/org/apollo/io/player/PlayerSerializer.java +++ b/src/org/apollo/io/player/PlayerSerializer.java @@ -1,6 +1,7 @@ package org.apollo.io.player; import org.apollo.game.model.Position; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; import org.apollo.security.PlayerCredentials; @@ -11,12 +12,26 @@ import org.apollo.security.PlayerCredentials; * @author Graham * @author Major */ -public interface PlayerSerializer { +public abstract class PlayerSerializer { /** * The spawn point for Players, on Tutorial Island. */ - Position TUTORIAL_ISLAND_SPAWN = new Position(3093, 3104); + protected static final Position TUTORIAL_ISLAND_SPAWN = new Position(3093, 3104); + + /** + * The World this PlayerSerializer is for. + */ + protected final World world; + + /** + * Creates the PlayerSerializer. + * + * @param world The {@link World} this PlayerSerializer is for. + */ + public PlayerSerializer(World world) { + this.world = world; + } /** * Loads a {@link Player}. @@ -25,7 +40,7 @@ public interface PlayerSerializer { * @return The {@link PlayerLoaderResponse}. * @throws Exception If an error occurs. */ - public PlayerLoaderResponse loadPlayer(PlayerCredentials credentials) throws Exception; + public abstract PlayerLoaderResponse loadPlayer(PlayerCredentials credentials) throws Exception; /** * Saves a {@link Player}. @@ -33,6 +48,6 @@ public interface PlayerSerializer { * @param player The Player to save. * @throws Exception If an error occurs. */ - public void savePlayer(Player player) throws Exception; + public abstract void savePlayer(Player player) throws Exception; } \ No newline at end of file diff --git a/src/org/apollo/login/LoginService.java b/src/org/apollo/login/LoginService.java index d5664526..d0984dc1 100644 --- a/src/org/apollo/login/LoginService.java +++ b/src/org/apollo/login/LoginService.java @@ -7,6 +7,7 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.apollo.Service; +import org.apollo.game.model.World; import org.apollo.game.model.entity.Player; import org.apollo.io.player.PlayerLoaderResponse; import org.apollo.io.player.PlayerSerializer; @@ -41,9 +42,11 @@ public final class LoginService extends Service { /** * Creates the login service. * + * @param world The {@link World} to log Players in to. * @throws Exception If an error occurs. */ - public LoginService() throws Exception { + public LoginService(World world) throws Exception { + super(world); init(); } @@ -52,11 +55,9 @@ public final class LoginService extends Service { * * @throws SAXException If there is an error parsing the XML file. * @throws IOException If there is an error accessing the file. - * @throws ClassNotFoundException If the player loader/saver implementation could not be found. - * @throws IllegalAccessException If the player loader/saver implementation could not be accessed. - * @throws InstantiationException If the player loader/saver implementation could not be instantiated. + * @throws ReflectiveOperationException If the {@link PlayerSerializer} implementation could not be created. */ - private void init() throws SAXException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException { + private void init() throws SAXException, IOException, ReflectiveOperationException { XmlParser parser = new XmlParser(); XmlNode rootNode; @@ -74,7 +75,7 @@ public final class LoginService extends Service { } Class clazz = Class.forName(serializer.getValue()); - this.serializer = (PlayerSerializer) clazz.newInstance(); + this.serializer = (PlayerSerializer) clazz.getConstructor(World.class).newInstance(world); } /** diff --git a/src/org/apollo/net/codec/update/UpdateEncoder.java b/src/org/apollo/net/codec/update/UpdateEncoder.java index 638844fc..c46d8bca 100644 --- a/src/org/apollo/net/codec/update/UpdateEncoder.java +++ b/src/org/apollo/net/codec/update/UpdateEncoder.java @@ -22,7 +22,7 @@ public final class UpdateEncoder extends MessageToMessageEncoder { +public final class MobAnimationResetMessageEncoder extends MessageEncoder { @Override public GamePacket encode(MobAnimationResetMessage message) { diff --git a/src/org/apollo/net/session/LoginSession.java b/src/org/apollo/net/session/LoginSession.java index 61584ecc..59c4d463 100644 --- a/src/org/apollo/net/session/LoginSession.java +++ b/src/org/apollo/net/session/LoginSession.java @@ -115,11 +115,11 @@ public final class LoginSession extends Session { IsaacRandomPair randomPair = request.getRandomPair(); Release release = serverContext.getRelease(); - channel.pipeline().addFirst("eventEncoder", new GameMessageEncoder(release)); - channel.pipeline().addBefore("eventEncoder", "gameEncoder", new GamePacketEncoder(randomPair.getEncodingRandom())); + channel.pipeline().addFirst("messageEncoder", new GameMessageEncoder(release)); + channel.pipeline().addBefore("messageEncoder", "gameEncoder", new GamePacketEncoder(randomPair.getEncodingRandom())); channel.pipeline().addBefore("handler", "gameDecoder", new GamePacketDecoder(randomPair.getDecodingRandom(), serverContext.getRelease())); - channel.pipeline().addAfter("gameDecoder", "eventDecoder", new GameMessageDecoder(release)); + channel.pipeline().addAfter("gameDecoder", "messageDecoder", new GameMessageDecoder(release)); channel.pipeline().remove("loginDecoder"); channel.pipeline().remove("loginEncoder"); diff --git a/src/org/apollo/update/OnDemandRequestWorker.java b/src/org/apollo/update/OnDemandRequestWorker.java index 7a28a0a8..bb42b66c 100644 --- a/src/org/apollo/update/OnDemandRequestWorker.java +++ b/src/org/apollo/update/OnDemandRequestWorker.java @@ -47,14 +47,11 @@ public final class OnDemandRequestWorker extends RequestWorker 0; chunk++) { - int chunkSize = buffer.remaining(); - if (chunkSize > CHUNK_LENGTH) { - chunkSize = CHUNK_LENGTH; - } + int chunkSize = Math.min(buffer.remaining(), CHUNK_LENGTH); - byte[] tmp = new byte[chunkSize]; - buffer.get(tmp, 0, tmp.length); - ByteBuf chunkData = Unpooled.wrappedBuffer(tmp, 0, chunkSize); + byte[] data = new byte[chunkSize]; + buffer.get(data, 0, data.length); + ByteBuf chunkData = Unpooled.wrappedBuffer(data, 0, chunkSize); OnDemandResponse response = new OnDemandResponse(desc, length, chunk, chunkData); channel.writeAndFlush(response); diff --git a/src/org/apollo/update/UpdateService.java b/src/org/apollo/update/UpdateService.java index aa9862be..4957db11 100644 --- a/src/org/apollo/update/UpdateService.java +++ b/src/org/apollo/update/UpdateService.java @@ -12,6 +12,7 @@ import java.util.logging.Logger; import org.apollo.Service; import org.apollo.fs.IndexedFileSystem; +import org.apollo.game.model.World; /** * A class which services file requests. @@ -51,9 +52,12 @@ public final class UpdateService extends Service { private final List> workers = new ArrayList<>(); /** - * Creates the update service. + * Creates the UpdateService. + * + * @param world The {@link World} the update service is for. */ - public UpdateService() { + public UpdateService(World world) { + super(world); int totalThreads = REQUEST_TYPES * THREADS_PER_REQUEST_TYPE; service = Executors.newFixedThreadPool(totalThreads); } diff --git a/src/org/apollo/util/plugin/PluginContext.java b/src/org/apollo/util/plugin/PluginContext.java index 46f62063..8fa6fc05 100644 --- a/src/org/apollo/util/plugin/PluginContext.java +++ b/src/org/apollo/util/plugin/PluginContext.java @@ -2,15 +2,10 @@ package org.apollo.util.plugin; import org.apollo.ServerContext; import org.apollo.game.GameService; -import org.apollo.game.command.CommandListener; import org.apollo.game.message.Message; import org.apollo.game.message.handler.MessageHandler; import org.apollo.game.message.handler.MessageHandlerChain; import org.apollo.game.message.handler.MessageHandlerChainGroup; -import org.apollo.game.model.World; -import org.apollo.game.model.event.EventListener; -import org.apollo.game.model.event.impl.LoginEvent; -import org.apollo.game.model.event.impl.LogoutEvent; /** * The {@link PluginContext} contains methods a plugin can use to interface with the server, for example, by adding @@ -22,42 +17,14 @@ import org.apollo.game.model.event.impl.LogoutEvent; public final class PluginContext { /** - * Adds a {@link CommandListener}. - * - * @param name The name of the listener. - * @param listener The listener. - */ - public static void addCommandListener(String name, CommandListener listener) { - World.getWorld().getCommandDispatcher().register(name, listener); - } - - /** - * Adds an {@link EventListener} for a {@link LoginEvent}. - * - * @param listener The listener. - */ - public static void addLoginListener(EventListener listener) { - World.getWorld().listenFor(LoginEvent.class, listener); - } - - /** - * Adds an {@link EventListener} for a {@link LogoutEvent}. - * - * @param listener The listener. - */ - public static void addLogoutListener(EventListener listener) { - World.getWorld().listenFor(LogoutEvent.class, listener); - } - - /** - * The server context. + * The ServerContext. */ private final ServerContext context; /** - * Creates the plugin context. + * Creates the PluginContext. * - * @param context The server context. + * @param context The {@link ServerContext}. */ public PluginContext(ServerContext context) { this.context = context; diff --git a/src/org/apollo/util/plugin/PluginManager.java b/src/org/apollo/util/plugin/PluginManager.java index f11cb906..5175a5fb 100644 --- a/src/org/apollo/util/plugin/PluginManager.java +++ b/src/org/apollo/util/plugin/PluginManager.java @@ -15,6 +15,7 @@ import java.util.TreeSet; import java.util.function.Function; import java.util.stream.Collectors; +import org.apollo.game.model.World; import org.apollo.io.PluginMetaDataParser; import org.xml.sax.SAXException; @@ -36,12 +37,19 @@ public final class PluginManager { private final PluginContext context; /** - * Creates the plugin manager. - * - * @param context The plugin context. + * The World this PluginManager is for. */ - public PluginManager(PluginContext context) { + private final World world; + + /** + * Creates the PluginManager. + * + * @param world The {@link World} the PluginManager is for. + * @param context The PluginContext. + */ + public PluginManager(World world, PluginContext context) { this.context = context; + this.world = world; initAuthors(); } @@ -138,7 +146,7 @@ public final class PluginManager { Map plugins = createMap(findPlugins()); Set started = new HashSet<>(); - PluginEnvironment env = new RubyPluginEnvironment(); // TODO isolate plugins if possible in the future! + PluginEnvironment env = new RubyPluginEnvironment(world); // TODO isolate plugins if possible in the future! env.setContext(context); for (PluginMetaData plugin : plugins.values()) { @@ -156,7 +164,8 @@ public final class PluginManager { * @throws DependencyException If a dependency error occurs. * @throws IOException If an I/O error occurs. */ - private void start(PluginEnvironment env, PluginMetaData plugin, Map plugins, Set started) throws DependencyException, IOException { + private void start(PluginEnvironment env, PluginMetaData plugin, Map plugins, + Set started) throws DependencyException, IOException { // TODO check for cyclic dependencies! this way just won't cut it, we need an exception if (started.contains(plugin)) { return; diff --git a/src/org/apollo/util/plugin/RubyPluginEnvironment.java b/src/org/apollo/util/plugin/RubyPluginEnvironment.java index ad4e894b..59133ee6 100644 --- a/src/org/apollo/util/plugin/RubyPluginEnvironment.java +++ b/src/org/apollo/util/plugin/RubyPluginEnvironment.java @@ -20,12 +20,19 @@ public final class RubyPluginEnvironment implements PluginEnvironment { */ private final ScriptingContainer container = new ScriptingContainer(); + /** + * The World this RubyPluginEnvironment is for. + */ + private final World world; + /** * Creates and bootstraps the Ruby plugin environment. * + * @param world The {@link World} this RubyPluginEnvironment is for. * @throws IOException If an I/O error occurs during bootstrapping. */ - public RubyPluginEnvironment() throws IOException { + public RubyPluginEnvironment(World world) throws IOException { + this.world = world; parseBootstrapper(); } @@ -54,7 +61,7 @@ public final class RubyPluginEnvironment implements PluginEnvironment { @Override public void setContext(PluginContext context) { container.put("$ctx", context); - container.put("$world", World.getWorld()); + container.put("$world", world); } } \ No newline at end of file