From b4176e809712ba855e507b4d4693de7225d37c9d Mon Sep 17 00:00:00 2001 From: atomicint Date: Mon, 23 Mar 2015 09:04:37 -0700 Subject: [PATCH 01/10] Format line endings --- .../apollo/fs/decoder/GameObjectDecoder.java | 6 ++---- .../fs/decoder/ObjectDefinitionDecoder.java | 2 +- src/org/apollo/game/GameService.java | 8 +++----- .../impl/ObjectActionVerificationHandler.java | 1 - .../impl/PlayerSynchronizationMessage.java | 3 +-- src/org/apollo/game/model/Position.java | 3 +-- src/org/apollo/game/model/area/Region.java | 9 +++------ .../model/area/collision/CollisionMatrix.java | 3 +-- .../game/model/area/update/package-info.java | 2 +- src/org/apollo/game/model/entity/Mob.java | 4 ++-- src/org/apollo/game/model/entity/Player.java | 9 +++------ .../model/entity/attr/StringAttribute.java | 2 +- .../game/model/entity/obj/GameObject.java | 3 +-- .../game/model/entity/obj/ObjectGroup.java | 3 +-- .../game/model/entity/obj/package-info.java | 2 +- .../task/PrePlayerSynchronizationTask.java | 18 ++++++------------ src/org/apollo/net/NetworkConstants.java | 6 ++---- .../GroupedRegionUpdateMessageEncoder.java | 3 +-- .../r317/NpcSynchronizationMessageEncoder.java | 4 ++-- .../r317/RemoveObjectMessageEncoder.java | 3 +-- .../GroupedRegionUpdateMessageEncoder.java | 6 ++---- .../r377/NpcSynchronizationMessageEncoder.java | 2 +- .../apollo/net/release/r377/Release377.java | 3 +-- .../update/ComparableChannelRequest.java | 3 +-- src/org/apollo/util/xml/XmlNode.java | 16 ++++++++-------- 25 files changed, 47 insertions(+), 77 deletions(-) diff --git a/src/org/apollo/fs/decoder/GameObjectDecoder.java b/src/org/apollo/fs/decoder/GameObjectDecoder.java index b0cf8148..bb6b9de5 100644 --- a/src/org/apollo/fs/decoder/GameObjectDecoder.java +++ b/src/org/apollo/fs/decoder/GameObjectDecoder.java @@ -116,11 +116,9 @@ public final class GameObjectDecoder { block = true; } - Predicate walls = (value) -> value >= ObjectType.LENGTHWISE_WALL.getValue() - && value <= ObjectType.RECTANGULAR_CORNER.getValue() || value == ObjectType.DIAGONAL_WALL.getValue(); + Predicate walls = (value) -> value >= ObjectType.LENGTHWISE_WALL.getValue() && value <= ObjectType.RECTANGULAR_CORNER.getValue() || value == ObjectType.DIAGONAL_WALL.getValue(); - Predicate roofs = (value) -> value > ObjectType.DIAGONAL_INTERACTABLE.getValue() - && value < ObjectType.FLOOR_DECORATION.getValue(); + Predicate roofs = (value) -> value > ObjectType.DIAGONAL_INTERACTABLE.getValue() && value < ObjectType.FLOOR_DECORATION.getValue(); if (walls.test(type) || roofs.test(type)) { block = true; diff --git a/src/org/apollo/fs/decoder/ObjectDefinitionDecoder.java b/src/org/apollo/fs/decoder/ObjectDefinitionDecoder.java index 946d8236..ac998c30 100644 --- a/src/org/apollo/fs/decoder/ObjectDefinitionDecoder.java +++ b/src/org/apollo/fs/decoder/ObjectDefinitionDecoder.java @@ -128,7 +128,7 @@ public final class ObjectDefinitionDecoder { data.getShort(); data.getShort(); int count = data.get(); - for (int i = 0; i <= count; i++){ + for (int i = 0; i <= count; i++) { data.getShort(); } } else { diff --git a/src/org/apollo/game/GameService.java b/src/org/apollo/game/GameService.java index 49fd689d..388489d4 100644 --- a/src/org/apollo/game/GameService.java +++ b/src/org/apollo/game/GameService.java @@ -50,8 +50,7 @@ public final class GameService extends Service { /** * The scheduled executor service. */ - private final ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory( - "GameService")); + private final ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("GameService")); /** * The {@link ClientSynchronizer}. @@ -157,7 +156,7 @@ public final class GameService extends Service { */ public RegistrationStatus registerPlayer(Player player, GameSession session) { World world = World.getWorld(); - + synchronized (this) { RegistrationStatus status = world.register(player); if (status == RegistrationStatus.OK) { @@ -176,8 +175,7 @@ public final class GameService extends Service { */ @Override public void start() { - scheduledExecutor.scheduleAtFixedRate(new GamePulseHandler(this), GameConstants.PULSE_DELAY, GameConstants.PULSE_DELAY, - TimeUnit.MILLISECONDS); + scheduledExecutor.scheduleAtFixedRate(new GamePulseHandler(this), GameConstants.PULSE_DELAY, GameConstants.PULSE_DELAY, TimeUnit.MILLISECONDS); } /** diff --git a/src/org/apollo/game/message/handler/impl/ObjectActionVerificationHandler.java b/src/org/apollo/game/message/handler/impl/ObjectActionVerificationHandler.java index e8899b45..f2666fb6 100644 --- a/src/org/apollo/game/message/handler/impl/ObjectActionVerificationHandler.java +++ b/src/org/apollo/game/message/handler/impl/ObjectActionVerificationHandler.java @@ -34,7 +34,6 @@ public final class ObjectActionVerificationHandler extends MessageHandler segments) { + public PlayerSynchronizationMessage(Position lastKnownRegion, Position position, boolean regionChanged, SynchronizationSegment segment, int localPlayers, List segments) { this.lastKnownRegion = lastKnownRegion; this.position = position; this.regionChanged = regionChanged; diff --git a/src/org/apollo/game/model/Position.java b/src/org/apollo/game/model/Position.java index 804c0da1..7e57c5af 100644 --- a/src/org/apollo/game/model/Position.java +++ b/src/org/apollo/game/model/Position.java @@ -226,8 +226,7 @@ public final class Position { @Override public String toString() { - return MoreObjects.toStringHelper(this).add("x", getX()).add("y", getY()).add("height", getHeight()) - .add("region", getRegionCoordinates()).toString(); + return MoreObjects.toStringHelper(this).add("x", getX()).add("y", getY()).add("height", getHeight()).add("region", getRegionCoordinates()).toString(); } } \ No newline at end of file diff --git a/src/org/apollo/game/model/area/Region.java b/src/org/apollo/game/model/area/Region.java index c04da299..e5caff4b 100644 --- a/src/org/apollo/game/model/area/Region.java +++ b/src/org/apollo/game/model/area/Region.java @@ -41,8 +41,7 @@ public final class Region { @Override public void execute(Region region, Entity entity, EntityUpdateType update) { EntityType type = entity.getEntityType(); - if (type != EntityType.PLAYER && type != EntityType.NPC - && (type != EntityType.STATIC_OBJECT || update == EntityUpdateType.REMOVE)) { + if (type != EntityType.PLAYER && type != EntityType.NPC && (type != EntityType.STATIC_OBJECT || update == EntityUpdateType.REMOVE)) { region.record(entity, update); } } @@ -190,8 +189,7 @@ public final class Region { Set set = new HashSet<>(Arrays.asList(types)); @SuppressWarnings("unchecked") - Set filtered = (Set) local.stream().filter(entity -> set.contains(entity.getEntityType())) - .collect(Collectors.toSet()); + Set filtered = (Set) local.stream().filter(entity -> set.contains(entity.getEntityType())).collect(Collectors.toSet()); return ImmutableSet.copyOf(filtered); } @@ -291,8 +289,7 @@ public final class Region { * @throws IllegalArgumentException If the specified position is not included in this Region. */ private void checkPosition(Position position) { - Preconditions.checkArgument(coordinates.equals(RegionCoordinates.fromPosition(position)), - "Position is not included in this Region."); + Preconditions.checkArgument(coordinates.equals(RegionCoordinates.fromPosition(position)), "Position is not included in this Region."); } /** diff --git a/src/org/apollo/game/model/area/collision/CollisionMatrix.java b/src/org/apollo/game/model/area/collision/CollisionMatrix.java index 46c25ca1..7c57c153 100644 --- a/src/org/apollo/game/model/area/collision/CollisionMatrix.java +++ b/src/org/apollo/game/model/area/collision/CollisionMatrix.java @@ -184,8 +184,7 @@ public final class CollisionMatrix { @Override public String toString() { - return MoreObjects.toStringHelper(this).add("width", width).add("length", length).add("matrix", Arrays.toString(matrix)) - .toString(); + return MoreObjects.toStringHelper(this).add("width", width).add("length", length).add("matrix", Arrays.toString(matrix)).toString(); } /** diff --git a/src/org/apollo/game/model/area/update/package-info.java b/src/org/apollo/game/model/area/update/package-info.java index 864c3acd..04b2110c 100644 --- a/src/org/apollo/game/model/area/update/package-info.java +++ b/src/org/apollo/game/model/area/update/package-info.java @@ -1,4 +1,4 @@ /** - * Contains snapshot-related classes. + * Contains snapshot-related classes. */ package org.apollo.game.model.area.update; \ No newline at end of file diff --git a/src/org/apollo/game/model/entity/Mob.java b/src/org/apollo/game/model/entity/Mob.java index 7e56f344..6aadedae 100644 --- a/src/org/apollo/game/model/entity/Mob.java +++ b/src/org/apollo/game/model/entity/Mob.java @@ -196,8 +196,8 @@ public abstract class Mob extends Entity { */ public final Direction[] getDirections() { if (firstDirection != Direction.NONE) { - return secondDirection == Direction.NONE ? new Direction[] { firstDirection } : new Direction[] { firstDirection, - secondDirection }; + return secondDirection == Direction.NONE ? new Direction[] { firstDirection } : new Direction[] { + firstDirection, secondDirection }; } return Direction.EMPTY_DIRECTION_ARRAY; diff --git a/src/org/apollo/game/model/entity/Player.java b/src/org/apollo/game/model/entity/Player.java index 7fdfd74c..4fd66a28 100644 --- a/src/org/apollo/game/model/entity/Player.java +++ b/src/org/apollo/game/model/entity/Player.java @@ -942,8 +942,7 @@ public final class Player extends Mob { @Override public String toString() { - return MoreObjects.toStringHelper(this).add("username", getUsername()).add("privilege", privilegeLevel) - .add("client version", getClientVersion()).toString(); + return MoreObjects.toStringHelper(this).add("username", getUsername()).add("privilege", privilegeLevel).add("client version", getClientVersion()).toString(); } /** @@ -962,11 +961,9 @@ public final class Player extends Mob { InventoryListener fullBankListener = new FullInventoryListener(this, FullInventoryListener.FULL_BANK_MESSAGE); InventoryListener appearanceListener = new AppearanceInventoryListener(this); - InventoryListener syncInventoryListener = new SynchronizationInventoryListener(this, - SynchronizationInventoryListener.INVENTORY_ID); + InventoryListener syncInventoryListener = new SynchronizationInventoryListener(this, SynchronizationInventoryListener.INVENTORY_ID); InventoryListener syncBankListener = new SynchronizationInventoryListener(this, BankConstants.BANK_INVENTORY_ID); - InventoryListener syncEquipmentListener = new SynchronizationInventoryListener(this, - SynchronizationInventoryListener.EQUIPMENT_ID); + InventoryListener syncEquipmentListener = new SynchronizationInventoryListener(this, SynchronizationInventoryListener.EQUIPMENT_ID); inventory.addListener(syncInventoryListener); inventory.addListener(fullInventoryListener); diff --git a/src/org/apollo/game/model/entity/attr/StringAttribute.java b/src/org/apollo/game/model/entity/attr/StringAttribute.java index 231dac55..2691bbc3 100644 --- a/src/org/apollo/game/model/entity/attr/StringAttribute.java +++ b/src/org/apollo/game/model/entity/attr/StringAttribute.java @@ -33,7 +33,7 @@ public final class StringAttribute extends Attribute { public byte[] encode() { byte[] bytes = value.getBytes(Charset.forName("UTF-8")); int length = bytes.length; - + bytes = Arrays.copyOf(bytes, length + 1); bytes[length - 1] = 0; return bytes; diff --git a/src/org/apollo/game/model/entity/obj/GameObject.java b/src/org/apollo/game/model/entity/obj/GameObject.java index e04cf80c..c1e12958 100644 --- a/src/org/apollo/game/model/entity/obj/GameObject.java +++ b/src/org/apollo/game/model/entity/obj/GameObject.java @@ -89,8 +89,7 @@ public abstract class GameObject extends Entity { @Override public String toString() { - return MoreObjects.toStringHelper(this).add("id", getId()).add("type", getType()).add("orientation", getOrientation()) - .toString(); + return MoreObjects.toStringHelper(this).add("id", getId()).add("type", getType()).add("orientation", getOrientation()).toString(); } @Override diff --git a/src/org/apollo/game/model/entity/obj/ObjectGroup.java b/src/org/apollo/game/model/entity/obj/ObjectGroup.java index 5955f8ab..9bb50d22 100644 --- a/src/org/apollo/game/model/entity/obj/ObjectGroup.java +++ b/src/org/apollo/game/model/entity/obj/ObjectGroup.java @@ -38,8 +38,7 @@ public enum ObjectGroup { * @throws IllegalArgumentException If there is no ObjectGroup with the specified value. */ public static ObjectGroup valueOf(int value) { - return Arrays.stream(values()).filter(group -> group.value == value).findAny() - .orElseThrow(() -> new IllegalArgumentException("No ObjectGroup with a value of " + value + " exists.")); + return Arrays.stream(values()).filter(group -> group.value == value).findAny().orElseThrow(() -> new IllegalArgumentException("No ObjectGroup with a value of " + value + " exists.")); } /** diff --git a/src/org/apollo/game/model/entity/obj/package-info.java b/src/org/apollo/game/model/entity/obj/package-info.java index 5dd694f7..89834bb4 100644 --- a/src/org/apollo/game/model/entity/obj/package-info.java +++ b/src/org/apollo/game/model/entity/obj/package-info.java @@ -1,4 +1,4 @@ /** - * Contains object-related classes. + * Contains object-related classes. */ package org.apollo.game.model.entity.obj; \ No newline at end of file diff --git a/src/org/apollo/game/sync/task/PrePlayerSynchronizationTask.java b/src/org/apollo/game/sync/task/PrePlayerSynchronizationTask.java index 6589e69b..d20978d6 100644 --- a/src/org/apollo/game/sync/task/PrePlayerSynchronizationTask.java +++ b/src/org/apollo/game/sync/task/PrePlayerSynchronizationTask.java @@ -79,8 +79,7 @@ public final class PrePlayerSynchronizationTask extends SynchronizationTask { * @param updates The {@link Map} containing {@link Region} updates. * @param snapshots The Map containing Region snapshots. */ - public PrePlayerSynchronizationTask(Player player, Map> updates, - Map> snapshots) { + public PrePlayerSynchronizationTask(Player player, Map> updates, Map> snapshots) { this.player = player; this.updates = updates; this.snapshots = snapshots; @@ -210,8 +209,7 @@ public final class PrePlayerSynchronizationTask extends SynchronizationTask { int deltaX = current.getLocalX(last); int deltaY = current.getLocalY(last); - return deltaX <= Position.MAX_DISTANCE || deltaX >= (VIEWPORT_WIDTH - Position.MAX_DISTANCE - 1) - || deltaY <= Position.MAX_DISTANCE || deltaY >= (VIEWPORT_WIDTH - Position.MAX_DISTANCE - 1); + return deltaX <= Position.MAX_DISTANCE || deltaX >= (VIEWPORT_WIDTH - Position.MAX_DISTANCE - 1) || deltaY <= Position.MAX_DISTANCE || deltaY >= (VIEWPORT_WIDTH - Position.MAX_DISTANCE - 1); } /** @@ -224,8 +222,7 @@ public final class PrePlayerSynchronizationTask extends SynchronizationTask { * @param repository The {@link RegionRepository} containing the Regions. * @return The Optional containing the GroupedRegionUpdateMessage. */ - private Optional toUpdateMessage(RegionUpdateMode mode, Position position, - RegionCoordinates coordinates, RegionRepository repository) { + private Optional toUpdateMessage(RegionUpdateMode mode, Position position, RegionCoordinates coordinates, RegionRepository repository) { List messages; /* @@ -241,8 +238,7 @@ public final class PrePlayerSynchronizationTask extends SynchronizationTask { messages = updates.get(coordinates); if (messages == null) { synchronized (updates) { - messages = updates.computeIfAbsent(coordinates, - coords -> repository.get(coords).getUpdates(position.getHeight())); + messages = updates.computeIfAbsent(coordinates, coords -> repository.get(coords).getUpdates(position.getHeight())); } } @@ -251,8 +247,7 @@ public final class PrePlayerSynchronizationTask extends SynchronizationTask { messages = snapshots.get(coordinates); if (messages == null) { synchronized (snapshots) { - messages = snapshots.computeIfAbsent(coordinates, - coords -> repository.get(coords).getSnapshot(position.getHeight())); + messages = snapshots.computeIfAbsent(coordinates, coords -> repository.get(coords).getSnapshot(position.getHeight())); } } @@ -261,8 +256,7 @@ public final class PrePlayerSynchronizationTask extends SynchronizationTask { throw new IllegalArgumentException("Unrecognised RegionUpdateMode " + mode + "."); } - return messages.isEmpty() ? Optional.empty() : Optional - .of(new GroupedRegionUpdateMessage(position, coordinates, messages)); + return messages.isEmpty() ? Optional.empty() : Optional.of(new GroupedRegionUpdateMessage(position, coordinates, messages)); } } \ No newline at end of file diff --git a/src/org/apollo/net/NetworkConstants.java b/src/org/apollo/net/NetworkConstants.java index 56c3e20b..a4bac1c7 100644 --- a/src/org/apollo/net/NetworkConstants.java +++ b/src/org/apollo/net/NetworkConstants.java @@ -72,8 +72,7 @@ public final class NetworkConstants { Preconditions.checkState(rsa != null, "Root node must have a child named 'rsa'."); XmlNode modulus = rsa.getChild("modulus"), exponent = rsa.getChild("private-exponent"); - Preconditions.checkState(modulus != null && exponent != null, - "Rsa node must have two children: 'modulus' and 'private-exponent'."); + Preconditions.checkState(modulus != null && exponent != null, "Rsa node must have two children: 'modulus' and 'private-exponent'."); RSA_MODULUS = new BigInteger(modulus.getValue()); RSA_EXPONENT = new BigInteger(exponent.getValue()); @@ -82,8 +81,7 @@ public final class NetworkConstants { Preconditions.checkState(ports != null, "Root node must have a child named 'ports'."); XmlNode http = ports.getChild("http"), service = ports.getChild("service"), jaggrab = ports.getChild("jaggrab"); - Preconditions.checkState(http != null && service != null && jaggrab != null, - "Ports node must have three children: 'http', 'service', and 'jaggrab'."); + Preconditions.checkState(http != null && service != null && jaggrab != null, "Ports node must have three children: 'http', 'service', and 'jaggrab'."); HTTP_PORT = Integer.parseInt(http.getValue()); SERVICE_PORT = Integer.parseInt(service.getValue()); diff --git a/src/org/apollo/net/release/r317/GroupedRegionUpdateMessageEncoder.java b/src/org/apollo/net/release/r317/GroupedRegionUpdateMessageEncoder.java index a2de6474..289134c8 100644 --- a/src/org/apollo/net/release/r317/GroupedRegionUpdateMessageEncoder.java +++ b/src/org/apollo/net/release/r317/GroupedRegionUpdateMessageEncoder.java @@ -44,8 +44,7 @@ public final class GroupedRegionUpdateMessageEncoder extends MessageEncoder encoder = (MessageEncoder) release.getMessageEncoder(update - .getClass()); + MessageEncoder encoder = (MessageEncoder) release.getMessageEncoder(update.getClass()); GamePacket packet = encoder.encode(update); builder.put(DataType.BYTE, packet.getOpcode()); diff --git a/src/org/apollo/net/release/r317/NpcSynchronizationMessageEncoder.java b/src/org/apollo/net/release/r317/NpcSynchronizationMessageEncoder.java index c4bd440e..a8acc59d 100644 --- a/src/org/apollo/net/release/r317/NpcSynchronizationMessageEncoder.java +++ b/src/org/apollo/net/release/r317/NpcSynchronizationMessageEncoder.java @@ -194,8 +194,8 @@ public final class NpcSynchronizationMessageEncoder extends MessageEncoder encoder = (MessageEncoder) encoders.get(update); - Preconditions.checkState(encoder != null, update.getClass() - + " does not have a registered encoder in GroupedRegionUpdateMessageEncoder."); + Preconditions.checkState(encoder != null, update.getClass() + " does not have a registered encoder in GroupedRegionUpdateMessageEncoder."); GamePacket packet = encoder.encode(update); builder.put(DataType.BYTE, packet.getOpcode()); diff --git a/src/org/apollo/net/release/r377/NpcSynchronizationMessageEncoder.java b/src/org/apollo/net/release/r377/NpcSynchronizationMessageEncoder.java index 9625b8c0..8702363f 100644 --- a/src/org/apollo/net/release/r377/NpcSynchronizationMessageEncoder.java +++ b/src/org/apollo/net/release/r377/NpcSynchronizationMessageEncoder.java @@ -194,7 +194,7 @@ public final class NpcSynchronizationMessageEncoder extends MessageEncoder, MessageEncoder> entry : regionUpdates - .entrySet()) { + for (Map.Entry, MessageEncoder> entry : regionUpdates.entrySet()) { @SuppressWarnings("unchecked") Class clazz = (Class) entry.getKey(); @SuppressWarnings("unchecked") diff --git a/src/org/apollo/update/ComparableChannelRequest.java b/src/org/apollo/update/ComparableChannelRequest.java index 02f90f1d..606d2f9b 100644 --- a/src/org/apollo/update/ComparableChannelRequest.java +++ b/src/org/apollo/update/ComparableChannelRequest.java @@ -9,8 +9,7 @@ import io.netty.channel.Channel; * * @param The type of request. */ -public final class ComparableChannelRequest> extends ChannelRequest implements - Comparable> { +public final class ComparableChannelRequest> extends ChannelRequest implements Comparable> { /** * Creates the ComparableChannelRequest. diff --git a/src/org/apollo/util/xml/XmlNode.java b/src/org/apollo/util/xml/XmlNode.java index 533892ab..fceab153 100644 --- a/src/org/apollo/util/xml/XmlNode.java +++ b/src/org/apollo/util/xml/XmlNode.java @@ -146,14 +146,14 @@ public final class XmlNode implements Iterable { return value; } - /** - * Gets the value of this node, wrapped in an {@link Optional} - * - * @return The value of this node if it exists otherwise {@link Optional#empty()} is returned - */ - public Optional getOptionalValue() { - return Optional.ofNullable(value); - } + /** + * Gets the value of this node, wrapped in an {@link Optional} + * + * @return The value of this node if it exists otherwise {@link Optional#empty()} is returned + */ + public Optional getOptionalValue() { + return Optional.ofNullable(value); + } /** * Checks if this node has a value. From 8981d192439c7a1961522ba4c9562d9aab4e5442 Mon Sep 17 00:00:00 2001 From: atomicint Date: Mon, 23 Mar 2015 09:12:10 -0700 Subject: [PATCH 02/10] Fix private messaging issue #57 https://github.com/apollo-rsps/apollo/issues/57 --- data/plugins/chat/private-messaging/messaging.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/plugins/chat/private-messaging/messaging.rb b/data/plugins/chat/private-messaging/messaging.rb index 17aa7d77..87cbdb65 100644 --- a/data/plugins/chat/private-messaging/messaging.rb +++ b/data/plugins/chat/private-messaging/messaging.rb @@ -6,7 +6,7 @@ java_import 'org.apollo.game.model.entity.setting.PrivacyState' on :message, :private_chat do |ctx, player, message| friend = $world.get_player(message.username) - friend.send(ForwardPrivateChatMessage.new(player.username, player.privilege_level, message.compressed_message)) if interaction_permitted(player, friend) + friend.send(ForwardPrivateChatMessage.new(player.username, player.privilege_level, message.compressed_chat)) if interaction_permitted(player, friend) end # Checks if the sender is permitted to interact with the friend they have added: From 3875fae35a0c8387a988bb7f66f6dd6640aa8b3d Mon Sep 17 00:00:00 2001 From: atomicint Date: Mon, 23 Mar 2015 09:13:40 -0700 Subject: [PATCH 03/10] Tsk tsk, Major copying and pasting is bad. You forget things. Fixes issue #59 https://github.com/apollo-rsps/apollo/issues/59 --- .../apollo/game/message/handler/impl/BankMessageHandler.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/org/apollo/game/message/handler/impl/BankMessageHandler.java b/src/org/apollo/game/message/handler/impl/BankMessageHandler.java index 62e904c7..3930a9f5 100644 --- a/src/org/apollo/game/message/handler/impl/BankMessageHandler.java +++ b/src/org/apollo/game/message/handler/impl/BankMessageHandler.java @@ -8,6 +8,7 @@ import org.apollo.game.model.inter.EnterAmountListener; import org.apollo.game.model.inter.bank.BankConstants; import org.apollo.game.model.inter.bank.BankDepositEnterAmountListener; import org.apollo.game.model.inter.bank.BankUtils; +import org.apollo.game.model.inter.bank.BankWithdrawEnterAmountListener; /** * A {@link MessageHandler} that handles withdrawing and depositing items from/to a player's bank. @@ -80,7 +81,7 @@ public final class BankMessageHandler extends MessageHandler int amount = optionToAmount(message.getOption()); if (amount == -1) { - EnterAmountListener listener = new BankDepositEnterAmountListener(player, message.getSlot(), message.getId()); + EnterAmountListener listener = new BankWithdrawEnterAmountListener(player, message.getSlot(), message.getId()); player.getInterfaceSet().openEnterAmountDialogue(listener); } else if (!BankUtils.withdraw(player, message.getSlot(), message.getId(), amount)) { ctx.breakHandlerChain(); From 5aba2ab23ea5182c11241b44083ce1ebb73c095e Mon Sep 17 00:00:00 2001 From: atomicint Date: Mon, 23 Mar 2015 09:16:26 -0700 Subject: [PATCH 04/10] Add XXX message --- src/org/apollo/game/message/handler/impl/EquipItemHandler.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/org/apollo/game/message/handler/impl/EquipItemHandler.java b/src/org/apollo/game/message/handler/impl/EquipItemHandler.java index a3628e6b..6102dc48 100644 --- a/src/org/apollo/game/message/handler/impl/EquipItemHandler.java +++ b/src/org/apollo/game/message/handler/impl/EquipItemHandler.java @@ -58,6 +58,8 @@ public final class EquipItemHandler extends MessageHandler { Item weapon = equipment.get(EquipmentConstants.WEAPON); Item shield = equipment.get(EquipmentConstants.SHIELD); + // XXX: This is still pretty ugly in some parts, improve. + if (definition.isTwoHanded()) { int slotsRequired = weapon != null && shield != null ? 1 : 0; if (inventory.freeSlots() < slotsRequired) { From e136d7847bd7c2bc7a1bafb5019c4d0888e4b028 Mon Sep 17 00:00:00 2001 From: atomicint Date: Mon, 23 Mar 2015 10:33:31 -0700 Subject: [PATCH 05/10] Removed the hard coded index fix for the mob repository, properly fixed it and cleaned up some of the repository code itself. --- .../game/message/impl/NpcActionMessage.java | 2 +- .../message/impl/PlayerActionMessage.java | 6 +- src/org/apollo/util/MobRepository.java | 140 ++++++++---------- 3 files changed, 68 insertions(+), 80 deletions(-) diff --git a/src/org/apollo/game/message/impl/NpcActionMessage.java b/src/org/apollo/game/message/impl/NpcActionMessage.java index 181eb80b..67173a42 100644 --- a/src/org/apollo/game/message/impl/NpcActionMessage.java +++ b/src/org/apollo/game/message/impl/NpcActionMessage.java @@ -29,7 +29,7 @@ public abstract class NpcActionMessage extends Message { */ public NpcActionMessage(int option, int index) { this.option = option; - this.index = index - 1; + this.index = index; } /** diff --git a/src/org/apollo/game/message/impl/PlayerActionMessage.java b/src/org/apollo/game/message/impl/PlayerActionMessage.java index 5369853e..7f109c5d 100644 --- a/src/org/apollo/game/message/impl/PlayerActionMessage.java +++ b/src/org/apollo/game/message/impl/PlayerActionMessage.java @@ -25,11 +25,11 @@ public abstract class PlayerActionMessage extends Message { * Creates a player action message. * * @param option The option number. - * @param playerIndex The index of the player. + * @param index The index of the player. */ - public PlayerActionMessage(int option, int playerIndex) { + public PlayerActionMessage(int option, int index) { this.option = option; - this.index = playerIndex - 1; + this.index = index; } /** diff --git a/src/org/apollo/util/MobRepository.java b/src/org/apollo/util/MobRepository.java index 876edc0b..4b1e537d 100644 --- a/src/org/apollo/util/MobRepository.java +++ b/src/org/apollo/util/MobRepository.java @@ -1,7 +1,6 @@ package org.apollo.util; import java.util.Iterator; -import java.util.NoSuchElementException; import org.apollo.game.model.entity.Mob; @@ -11,6 +10,7 @@ import com.google.common.base.Preconditions; * A {@link MobRepository} is a repository of {@link Mob}s that are currently active in the game world. * * @author Graham + * @author Ryley * @param The type of Mob. */ public final class MobRepository implements Iterable { @@ -19,57 +19,57 @@ public final class MobRepository implements Iterable { * The {@link Iterator} implementation for the MobRepository. * * @author Graham + * @author Ryley */ private final class MobRepositoryIterator implements Iterator { /** - * The current index of this iterator. + * The repository of {@link Mob}s this {@link Iterator} iterates over. */ - private int index = 0; + private final MobRepository repository; /** - * The previous index of this iterator. + * The current index of this iterator. */ - private int previousIndex = -1; + private int currentIndex; + + /** + * The amount of indexes found. + */ + private int foundIndex; + + /** + * Constructs a new {@link MobRepositoryIterator} with the specified MobRepository. + * + * @param repository The repository of Mob's this Iterator iterates over. + */ + private MobRepositoryIterator(MobRepository repository) { + this.repository = repository; + } @Override public boolean hasNext() { - for (int i = index; i < mobs.length; i++) { - if (mobs[i] != null) { - index = i; + if (foundIndex == repository.size()) { + return false; + } + + while (currentIndex < repository.capacity()) { + if (mobs[currentIndex++] != null) { + foundIndex++; return true; } } return false; } - @SuppressWarnings("unchecked") @Override public T next() { - T mob = null; - for (int i = index; i < mobs.length; i++) { - if (mobs[i] != null) { - mob = (T) mobs[i]; - index = i; - break; - } - } - - if (mob == null) { - throw new NoSuchElementException("Mob does not exist."); - } - - previousIndex = index; - index++; - return mob; + return repository.get(currentIndex); } - @SuppressWarnings("unchecked") @Override public void remove() { - Preconditions.checkState(previousIndex != -1, "Cannot remove as the repository is empty."); - MobRepository.this.remove((T) mobs[previousIndex]); - previousIndex = -1; + repository.remove(currentIndex + 1); } } @@ -79,11 +79,6 @@ public final class MobRepository implements Iterable { */ private final Mob[] mobs; - /** - * The position of the next free index. - */ - private int pointer = 0; - /** * The current size of this repository. */ @@ -95,7 +90,7 @@ public final class MobRepository implements Iterable { * @param capacity The maximum number of Mobs that can be present in the repository. */ public MobRepository(int capacity) { - this.mobs = new Mob[capacity]; + mobs = new Mob[capacity]; } /** @@ -105,41 +100,23 @@ public final class MobRepository implements Iterable { * @return {@code true} if the Mob was added, {@code false} if the size has reached the capacity of this repository. */ public boolean add(T mob) { - if (size == mobs.length) { + if (size == capacity()) { return false; } - int index = -1; - for (int i = pointer; i < mobs.length; i++) { - if (mobs[i] == null) { - index = i; - break; + for (int index = 0; index < capacity(); index++) { + if (mobs[index] != null) { + continue; } + + mobs[index] = mob; + mob.setIndex(index + 1); + size++; + + return true; } - if (index == -1) { - for (int i = 0; i < pointer; i++) { - if (mobs[i] == null) { - index = i; - break; - } - } - } - - if (index == -1) { - return false; // shouldn't happen, but just in case - } - - mobs[index] = mob; - mob.setIndex(index + 1); - - if (index == mobs.length - 1) { - pointer = 0; - } else { - pointer = index; - } - size++; - return true; + return false; } /** @@ -160,13 +137,15 @@ public final class MobRepository implements Iterable { */ @SuppressWarnings("unchecked") public T get(int index) { - Preconditions.checkElementIndex(index, mobs.length, "Mob index is out of bounds."); - return (T) mobs[index]; + if (index < 1 || index >= capacity() + 1) { + throw new IndexOutOfBoundsException("Mob index is out of bounds."); + } + return (T) mobs[index - 1]; } @Override public Iterator iterator() { - return new MobRepositoryIterator(); + return new MobRepositoryIterator(this); } /** @@ -176,18 +155,27 @@ public final class MobRepository implements Iterable { * @return {@code true} if the Mob was removed, {@code false} if not. */ public boolean remove(T mob) { - int index = mob.getIndex() - 1; - if (index < 0 || index >= mobs.length) { + Preconditions.checkNotNull(mob); + return remove(mob.getIndex()); + } + + /** + * Removes a Mob from the repository by the specified index. + * + * @param index The index of the Mob to remove. + * @return {@code true} if the Mob at the specified index was removed otherwise {@code false}. + */ + public boolean remove(int index) { + Mob mob = get(index); + + if (mob.getIndex() != index) { return false; } - if (mobs[index] == mob) { - mobs[index] = null; - mob.setIndex(-1); - size--; - return true; - } - return false; + mobs[index - 1] = null; + mob.setIndex(-1); + size--; + return true; } /** From 9569bf374d95afc6b6be96b8cb7bf9b7fe0ad361 Mon Sep 17 00:00:00 2001 From: atomicint Date: Mon, 23 Mar 2015 16:44:15 -0400 Subject: [PATCH 06/10] Add GameService#shutdown, remove comment on overridden method, rebase some sloppy code. --- src/org/apollo/game/GamePulseHandler.java | 9 ++--- src/org/apollo/game/GameService.java | 42 +++++++++++++++-------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/org/apollo/game/GamePulseHandler.java b/src/org/apollo/game/GamePulseHandler.java index aeb24582..5c5a5144 100644 --- a/src/org/apollo/game/GamePulseHandler.java +++ b/src/org/apollo/game/GamePulseHandler.java @@ -25,7 +25,7 @@ public final class GamePulseHandler implements Runnable { * * @param service The {@link GameService}. */ - GamePulseHandler(GameService service) { + protected GamePulseHandler(GameService service) { this.service = service; } @@ -33,10 +33,11 @@ public final class GamePulseHandler implements Runnable { public void run() { try { service.pulse(); - } catch (Throwable t) { - logger.log(Level.SEVERE, "Exception during pulse.", t); + } catch (Throwable reason) { + logger.log(Level.SEVERE, "Exception occured during pulse!", reason); + } finally { + service.shutdown(false); } - } } \ No newline at end of file diff --git a/src/org/apollo/game/GameService.java b/src/org/apollo/game/GameService.java index 388489d4..69aa7da9 100644 --- a/src/org/apollo/game/GameService.java +++ b/src/org/apollo/game/GameService.java @@ -125,18 +125,11 @@ public final class GameService extends Service { */ public void pulse() { synchronized (this) { - LoginService loginService = getContext().getService(LoginService.class); + finalizeUnregisters(); + World world = World.getWorld(); - - int unregistered = 0; - Player old; - while (unregistered < UNREGISTERS_PER_CYCLE && (old = oldPlayers.poll()) != null) { - loginService.submitSaveRequest(old.getSession(), old); - unregistered++; - } - - for (Player p : world.getPlayerRepository()) { - GameSession session = p.getSession(); + for (Player player : world.getPlayerRepository()) { + GameSession session = player.getSession(); if (session != null) { session.handlePendingMessages(chainGroup); } @@ -147,6 +140,30 @@ public final class GameService extends Service { } } + /** + * Finalizes the unregistration of Player's queued to be unregistered. + */ + private void finalizeUnregisters() { + LoginService loginService = getContext().getService(LoginService.class); + + for (int count = 0; count < UNREGISTERS_PER_CYCLE; count++) { + Player player = oldPlayers.poll(); + if (player == null) { + break; + } + + loginService.submitSaveRequest(player.getSession(), player); + } + } + + /** + * Shuts down this game service. + */ + public void shutdown(boolean natural) { + scheduledExecutor.shutdownNow(); + // TODO: Other events that should happen upon natural or unexpected shutdown. + } + /** * Registers a {@link Player} (may block!). * @@ -170,9 +187,6 @@ public final class GameService extends Service { } } - /** - * Starts the game service. - */ @Override public void start() { scheduledExecutor.scheduleAtFixedRate(new GamePulseHandler(this), GameConstants.PULSE_DELAY, GameConstants.PULSE_DELAY, TimeUnit.MILLISECONDS); From d52a3ae6c8218adaae30c15d1bf1a8ecbd792b33 Mon Sep 17 00:00:00 2001 From: atomicint Date: Mon, 23 Mar 2015 18:44:57 -0400 Subject: [PATCH 07/10] Prevent appearance being updated when non-combat skills are levelled up; only used to update combat level. --- .../game/model/skill/SynchronizationSkillListener.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/org/apollo/game/model/skill/SynchronizationSkillListener.java b/src/org/apollo/game/model/skill/SynchronizationSkillListener.java index 60d0f3b4..149befe9 100644 --- a/src/org/apollo/game/model/skill/SynchronizationSkillListener.java +++ b/src/org/apollo/game/model/skill/SynchronizationSkillListener.java @@ -1,5 +1,7 @@ package org.apollo.game.model.skill; +import java.util.stream.IntStream; + import org.apollo.game.message.impl.UpdateSkillMessage; import org.apollo.game.model.entity.Player; import org.apollo.game.model.entity.Skill; @@ -29,14 +31,14 @@ public final class SynchronizationSkillListener extends SkillAdapter { @Override public void levelledUp(SkillSet set, int id, Skill skill) { - player.getBlockSet().add(SynchronizationBlock.createAppearanceBlock(player)); + if (Skill.isCombatSkill(id)) { + player.getBlockSet().add(SynchronizationBlock.createAppearanceBlock(player)); + } } @Override public void skillsUpdated(SkillSet set) { - for (int id = 0; id < set.size(); id++) { - player.send(new UpdateSkillMessage(id, set.getSkill(id))); - } + IntStream.range(0, set.size()).forEach(id -> skillUpdated(set, id, set.getSkill(id))); } @Override From eb8e4322b752ccd2fb168a9d51a5ba52370fbc94 Mon Sep 17 00:00:00 2001 From: atomicint Date: Mon, 23 Mar 2015 18:47:10 -0400 Subject: [PATCH 08/10] Simplify combat level and total level calculation --- src/org/apollo/game/model/entity/SkillSet.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/org/apollo/game/model/entity/SkillSet.java b/src/org/apollo/game/model/entity/SkillSet.java index 1c0500f8..53523cee 100644 --- a/src/org/apollo/game/model/entity/SkillSet.java +++ b/src/org/apollo/game/model/entity/SkillSet.java @@ -7,6 +7,7 @@ import java.util.List; import org.apollo.game.model.skill.SkillListener; import com.google.common.base.Preconditions; +import com.google.common.primitives.Ints; /** * Represents the set of the player's skills. @@ -58,7 +59,6 @@ public final class SkillSet { */ public static int getLevelForExperience(double experience) { Preconditions.checkArgument(experience >= 0 && experience <= MAXIMUM_EXP, "Experience must be between 0 and " + MAXIMUM_EXP + ", inclusive."); - for (int level = 1; level <= 98; level++) { if (experience < EXPERIENCE_FOR_LEVEL[level + 1]) { return level; // TODO binary search? @@ -140,10 +140,10 @@ public final class SkillSet { int ranged = skills[Skill.RANGED].getMaximumLevel(); int magic = skills[Skill.MAGIC].getMaximumLevel(); - double base = (defence + hitpoints + Math.floor(prayer / 2)) * 0.25; - double melee = (attack + strength) * 0.325; + double base = Ints.max(strength + attack, magic * 2, ranged * 2); + double combat = (base * 1.3 + defence + hitpoints + prayer / 2) / 4; - this.combat = (int) (base + Math.max(melee, Math.max(ranged, magic) * 0.4875)); + this.combat = (int) combat; } /** @@ -209,7 +209,7 @@ public final class SkillSet { * @return The total level. */ public int getTotalLevel() { - return Arrays.stream(skills).map(Skill::getMaximumLevel).reduce(0, (total, level) -> total + level); + return Arrays.stream(skills).mapToInt(Skill::getMaximumLevel).sum(); } /** From cff1184e3326ce4619c25fc9b77d647936fa6c79 Mon Sep 17 00:00:00 2001 From: atomicint Date: Fri, 27 Mar 2015 16:42:08 -0400 Subject: [PATCH 09/10] Format interface method in RegionListener --- src/org/apollo/game/model/area/RegionListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/apollo/game/model/area/RegionListener.java b/src/org/apollo/game/model/area/RegionListener.java index d4c897b3..74feb692 100644 --- a/src/org/apollo/game/model/area/RegionListener.java +++ b/src/org/apollo/game/model/area/RegionListener.java @@ -18,6 +18,6 @@ public interface RegionListener { * @param entity The affected {@link Entity}. * @param type The type of {@link EntityUpdateType}. */ - public abstract void execute(Region region, Entity entity, EntityUpdateType type); + public void execute(Region region, Entity entity, EntityUpdateType type); } \ No newline at end of file From 29f6524d14d755a4a7a73eeeb29d3c5a2cf994ca Mon Sep 17 00:00:00 2001 From: atomicint Date: Fri, 27 Mar 2015 16:42:55 -0400 Subject: [PATCH 10/10] Rebase OnDemandRequestWorker#service(T, Channel, V) --- .../apollo/update/OnDemandRequestWorker.java | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/src/org/apollo/update/OnDemandRequestWorker.java b/src/org/apollo/update/OnDemandRequestWorker.java index 7a28a0a8..dc327d30 100644 --- a/src/org/apollo/update/OnDemandRequestWorker.java +++ b/src/org/apollo/update/OnDemandRequestWorker.java @@ -5,9 +5,7 @@ import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import java.io.IOException; -import java.nio.ByteBuffer; -import org.apollo.fs.FileDescriptor; import org.apollo.fs.IndexedFileSystem; import org.apollo.net.codec.update.OnDemandRequest; import org.apollo.net.codec.update.OnDemandResponse; @@ -20,7 +18,7 @@ import org.apollo.net.codec.update.OnDemandResponse; public final class OnDemandRequestWorker extends RequestWorker { /** - * The maximum length of a chunk, in bytes. + * The maximum length of a chunk, in {@code byte}s. */ private static final int CHUNK_LENGTH = 500; @@ -41,22 +39,12 @@ public final class OnDemandRequestWorker extends RequestWorker 0; chunk++) { - int chunkSize = buffer.remaining(); - if (chunkSize > CHUNK_LENGTH) { - chunkSize = CHUNK_LENGTH; - } - - byte[] tmp = new byte[chunkSize]; - buffer.get(tmp, 0, tmp.length); - ByteBuf chunkData = Unpooled.wrappedBuffer(tmp, 0, chunkSize); - - OnDemandResponse response = new OnDemandResponse(desc, length, chunk, chunkData); + for (int chunk = 0; buffer.readableBytes() > 0; chunk++) { + int chunkSize = Math.min(buffer.readableBytes(), CHUNK_LENGTH); + OnDemandResponse response = new OnDemandResponse(request.getFileDescriptor(), length, chunk, buffer.readBytes(chunkSize)); channel.writeAndFlush(response); } }