diff --git a/src/org/apollo/game/message/impl/FifthNpcActionMessage.java b/src/org/apollo/game/message/impl/FifthNpcActionMessage.java index 6dddd036..02347bcb 100644 --- a/src/org/apollo/game/message/impl/FifthNpcActionMessage.java +++ b/src/org/apollo/game/message/impl/FifthNpcActionMessage.java @@ -4,13 +4,14 @@ package org.apollo.game.message.impl; * The fifth {@link NpcActionMessage}. * * @author Major + * @author Stuart */ public final class FifthNpcActionMessage extends NpcActionMessage { /** - * Creates a new fifth npc action message. - * - * @param index The index of the npc. + * Creates the FifthNpcActionMessage. + * + * @param index The index of the Npc. */ public FifthNpcActionMessage(int index) { super(5, index); diff --git a/src/org/apollo/game/message/impl/FourthNpcActionMessage.java b/src/org/apollo/game/message/impl/FourthNpcActionMessage.java index 2ecd1ae1..d0babf44 100644 --- a/src/org/apollo/game/message/impl/FourthNpcActionMessage.java +++ b/src/org/apollo/game/message/impl/FourthNpcActionMessage.java @@ -4,13 +4,14 @@ package org.apollo.game.message.impl; * The fourth {@link NpcActionMessage}. * * @author Major + * @author Stuart */ public final class FourthNpcActionMessage extends NpcActionMessage { /** - * Creates a new fourth npc action message. + * Creates the FourthNpcActionMessage. * - * @param index The index of the npc. + * @param index The index of the Npc. */ public FourthNpcActionMessage(int index) { super(4, index); diff --git a/src/org/apollo/game/message/impl/MagicOnMobMessage.java b/src/org/apollo/game/message/impl/MagicOnMobMessage.java new file mode 100644 index 00000000..939e8086 --- /dev/null +++ b/src/org/apollo/game/message/impl/MagicOnMobMessage.java @@ -0,0 +1,68 @@ +package org.apollo.game.message.impl; + +import org.apollo.game.message.Message; +import org.apollo.game.model.entity.Entity; + +/** + * A {@link Message} sent by the client when a Player uses a magic spell on a Mob. + * + * @author Stuart + */ +public abstract class MagicOnMobMessage extends Message { + + /** + * The type of the Mob. + */ + private final Entity.EntityType type; + + /** + * The index of the Mob. + */ + private final int index; + + /** + * The spell if used. + */ + private final int spellId; + + /** + * Creates the MagicOnMobMessage. + * + * @param type The Mob type. + * @param index The Mob index. + * @param spellId The spell id. + */ + public MagicOnMobMessage(Entity.EntityType type, int index, int spellId) { + this.type = type; + this.index = index; + this.spellId = spellId; + } + + /** + * Gets the type of the Mob the spell is being used on. + * + * @return The Mob type. + */ + public Entity.EntityType getType() { + return type; + } + + /** + * Gets the index of the Mob the spell is being used on. + * + * @return The Mob index. + */ + public int getIndex() { + return index; + } + + /** + * Gets the spell id that is being used. + * + * @return The spell id. + */ + public int getSpellId() { + return spellId; + } + +} \ No newline at end of file diff --git a/src/org/apollo/game/message/impl/MagicOnNpcMessage.java b/src/org/apollo/game/message/impl/MagicOnNpcMessage.java new file mode 100644 index 00000000..52e562b2 --- /dev/null +++ b/src/org/apollo/game/message/impl/MagicOnNpcMessage.java @@ -0,0 +1,22 @@ +package org.apollo.game.message.impl; + +import org.apollo.game.model.entity.Entity; + +/** + * The magic on npc {@link MagicOnNpcMessage} + * + * @author Stuart + */ +public final class MagicOnNpcMessage extends MagicOnMobMessage { + + /** + * Creates the MagicOnMobMessage. + * + * @param index The Npc index. + * @param spellId The spell id used. + */ + public MagicOnNpcMessage(int index, int spellId) { + super(Entity.EntityType.NPC, index, spellId); + } + +} diff --git a/src/org/apollo/game/message/impl/MagicOnPlayerMessage.java b/src/org/apollo/game/message/impl/MagicOnPlayerMessage.java new file mode 100644 index 00000000..5addfba7 --- /dev/null +++ b/src/org/apollo/game/message/impl/MagicOnPlayerMessage.java @@ -0,0 +1,22 @@ +package org.apollo.game.message.impl; + +import org.apollo.game.model.entity.Entity; + +/** + * The Player {@link MagicOnMobMessage}. + * + * @author Stuart + */ +public final class MagicOnPlayerMessage extends MagicOnMobMessage { + + /** + * Creates the MagicOnPlayerMessage. + * + * @param index The index of the player. + * @param spellId The spell id used. + */ + public MagicOnPlayerMessage(int index, int spellId) { + super(Entity.EntityType.PLAYER, index, spellId); + } + +} \ No newline at end of file diff --git a/src/org/apollo/game/message/impl/MouseClickedMessage.java b/src/org/apollo/game/message/impl/MouseClickedMessage.java index d7eab5d7..706d4cb2 100644 --- a/src/org/apollo/game/message/impl/MouseClickedMessage.java +++ b/src/org/apollo/game/message/impl/MouseClickedMessage.java @@ -4,79 +4,82 @@ import org.apollo.game.message.Message; /** * A {@link Message} sent by the client to indicate when the mouse button/s have been clicked. This can be used in - * combination with {@link org.apollo.game.message.impl.FocusUpdateMessage} to work out if the player is clicking - * whilst the client is closed + * combination with {@link FocusUpdateMessage} to work out if the player is clicking whilst the client is closed. * * @author Stuart + * @author Major */ public final class MouseClickedMessage extends Message { - /** - * Time in milliseconds since the last click - */ - private final long lastClickedDelay; - /** - * Right mouse button flag - */ - private final boolean rightMouseButton; - /** - * The y position of the cursor - */ - private final int x; - /** - * The x position of the cursor - */ - private final int y; + /** + * The time, in milliseconds, since the last click. + */ + private final long clickDelay; - /** - * Creates a new mouse clicked message - * - * @param lastClickedDelay The delay since the last click - * @param rightMouseButton Whether or not the right mouse button was used - * @param x The x cursor position when clicked - * @param y The y cursor position when clicked - */ - public MouseClickedMessage(long lastClickedDelay, boolean rightMouseButton, int x, int y) { - this.lastClickedDelay = lastClickedDelay; - this.rightMouseButton = rightMouseButton; - this.x = x; - this.y = y; - } + /** + * Indicates whether or not the mouse button pressed was the right mouse button. + */ + private final boolean right; - /** - * Gets the time in milliseconds since the last click - * - * @return The time in milliseconds since the last click - */ - public long getLastClickedDelay() { - return lastClickedDelay; - } + /** + * The y position of the cursor. + */ + private final int x; - /** - * Gets whether the right mouse button was used or not - * - * @return If the mouse right button was used to click - */ - public boolean usingRightMouseButton() { - return rightMouseButton; - } + /** + * The x position of the cursor. + */ + private final int y; - /** - * Gets the x position of the cursor - * - * @return The x position of the cursor when clicked - */ - public int getX() { - return x; - } + /** + * Creates the MouseClickedMessage. + * + * @param clickDelay The delay, in milliseconds, since the last click. + * @param right Whether or not the mouse button pressed was the right mouse button. + * @param x The x cursor position when clicked. + * @param y The y cursor position when clicked. + */ + public MouseClickedMessage(long clickDelay, boolean right, int x, int y) { + this.clickDelay = clickDelay; + this.right = right; + this.x = x; + this.y = y; + } - /** - * Gets the y position of the cursor - * - * @return The y position of the cursor when clicked - */ - public int getY() { - return y; - } + /** + * Gets the delay, in milliseconds, since the last click. + * + * @return The time delay. + */ + public long getClickDelay() { + return clickDelay; + } + + /** + * Gets the x position of the cursor. + * + * @return The x position of the cursor when clicked. + */ + public int getX() { + return x; + } + + /** + * Gets the y position of the cursor. + * + * @return The y position of the cursor when clicked. + */ + public int getY() { + return y; + } + + /** + * Returns whether or not the right mouse button was used. + * + * @return {@code true} if the right mouse button was used to click, {@code false} if not. + */ + public boolean wasRightMouseButton() { + return right; + } } diff --git a/src/org/apollo/game/model/inv/Inventory.java b/src/org/apollo/game/model/inv/Inventory.java index 9a455fa2..5e9fd664 100644 --- a/src/org/apollo/game/model/inv/Inventory.java +++ b/src/org/apollo/game/model/inv/Inventory.java @@ -3,6 +3,7 @@ package org.apollo.game.model.inv; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Optional; import org.apollo.game.model.Item; import org.apollo.game.model.def.ItemDefinition; @@ -116,84 +117,90 @@ public final class Inventory { * @return The amount that remains. */ public int add(int id, int amount) { - Item item = add(new Item(id, amount)); - return (item != null) ? item.getAmount() : 0; + Optional item = add(new Item(id, amount)); + return item.isPresent() ? item.get().getAmount() : 0; } /** - * Adds an item to this inventory. This will attempt to add as much of the item that is possible. If the item - * remains, it will be returned (in the case of stackable items, any quantity that remains in the stack is - * returned). If nothing remains, the method will return {@code null}. If something remains, the listener will also - * be notified which could be used, for example, to send a message to the player. - * + * Attempts to add as much of the specified {@code item} to this inventory as possible. If any of the item remains, + * an {@link Item item with the remainder} will be returned (in the case of stack-able items, any quantity that + * remains in the stack is returned). If nothing remains, the method will return {@link Optional#empty an empty + * Optional}. + * + *

+ * If anything remains at all, the listener will be notified which could be used for notifying a player that their + * inventory is full, for example. + * * @param item The item to add to this inventory. - * @return The item that remains if there is not enough room in the inventory. If nothing remains, {@code null}. + * @return The item that may remain, if nothing remains, {@link Optional#empty an empty Optional} is returned. */ - public Item add(Item item) { + public Optional add(Item item) { int id = item.getId(); boolean stackable = isStackable(item.getDefinition()); if (stackable) { - for (int slot = 0; slot < capacity; slot++) { + int slot = slotOf(id); + + if (slot != -1) { Item other = items[slot]; - if (other != null && other.getId() == id) { - long total = item.getAmount() + other.getAmount(); - int amount, remaining; + long total = (long) item.getAmount() + other.getAmount(); + int amount, remaining; - if (total > Integer.MAX_VALUE) { - amount = (int) (total - Integer.MAX_VALUE); - remaining = (int) (total - amount); - notifyCapacityExceeded(); - } else { - amount = (int) total; - remaining = 0; - } - - set(slot, new Item(id, amount)); - return remaining > 0 ? new Item(id, remaining) : null; + if (total > Integer.MAX_VALUE) { + amount = (int) (total - Integer.MAX_VALUE); + remaining = (int) (total - amount); + notifyCapacityExceeded(); + } else { + amount = (int) total; + remaining = 0; } + + set(slot, new Item(id, amount)); + return remaining > 0 ? Optional.of(new Item(id, remaining)) : Optional.empty(); } - for (int slot = 0; slot < capacity; slot++) { - Item other = items[slot]; - if (other == null) { + for (slot = 0; slot < capacity; slot++) { + if (items[slot] == null) { set(slot, item); - return null; + return Optional.empty(); } } notifyCapacityExceeded(); - return item; + return Optional.of(item); } int remaining = item.getAmount(); + if (remaining == 0) { + return Optional.empty(); + } + stopFiringEvents(); try { Item single = new Item(item.getId(), 1); + for (int slot = 0; slot < capacity; slot++) { if (items[slot] == null) { - remaining--; set(slot, single); // share the instances - if (remaining <= 0) { - break; + if (--remaining <= 0) { + return Optional.empty(); } } } } finally { startFiringEvents(); + + if (remaining != item.getAmount()) { + notifyItemsUpdated(); + } } - if (remaining != item.getAmount()) { - notifyItemsUpdated(); - } - if (remaining > 0) { - notifyCapacityExceeded(); - } + notifyCapacityExceeded(); - return new Item(item.getId(), remaining); + return Optional.of(new Item(item.getId(), remaining)); } /** @@ -442,38 +449,48 @@ public final class Inventory { * @return The amount that was removed. */ public int remove(int id, int amount) { - ItemDefinition definition = ItemDefinition.lookup(id); - boolean stackable = isStackable(definition); + ItemDefinition def = ItemDefinition.lookup(id); + boolean stackable = isStackable(def); if (stackable) { - for (int slot = 0; slot < capacity; slot++) { + int slot = slotOf(id); + + if (slot != -1) { Item item = items[slot]; - if (item != null && item.getId() == id) { - if (amount >= item.getAmount()) { - set(slot, null); - return item.getAmount(); - } - - set(slot, new Item(item.getId(), item.getAmount() - amount)); - return amount; + if (amount >= item.getAmount()) { + set(slot, null); + return item.getAmount(); } + + set(slot, new Item(item.getId(), item.getAmount() - amount)); + return amount; } return 0; } int removed = 0; - for (int slot = 0; slot < capacity; slot++) { - Item item = items[slot]; - if (item != null && item.getId() == id) { - set(slot, null); - removed++; + stopFiringEvents(); + + try { + for (int slot = 0; slot < capacity; slot++) { + Item item = items[slot]; + if (item != null && item.getId() == id) { + set(slot, null); + + if (++removed >= amount) { + break; + } + } } - if (removed >= amount) { - break; + } finally { + startFiringEvents(); + + if (removed > 0) { + notifyItemsUpdated(); } } diff --git a/src/org/apollo/net/release/r317/FifthNpcActionMessageDecoder.java b/src/org/apollo/net/release/r317/FifthNpcActionMessageDecoder.java new file mode 100644 index 00000000..01096bbe --- /dev/null +++ b/src/org/apollo/net/release/r317/FifthNpcActionMessageDecoder.java @@ -0,0 +1,25 @@ +package org.apollo.net.release.r317; + +import org.apollo.game.message.impl.FifthNpcActionMessage; +import org.apollo.net.codec.game.DataOrder; +import org.apollo.net.codec.game.DataType; +import org.apollo.net.codec.game.GamePacket; +import org.apollo.net.codec.game.GamePacketReader; +import org.apollo.net.release.MessageDecoder; + +/** + * A {@link MessageDecoder} for the {@link FifthNpcActionMessage}. + * + * @author Stuart + * @author Major + */ +public final class FifthNpcActionMessageDecoder extends MessageDecoder { + + @Override + public FifthNpcActionMessage decode(GamePacket packet) { + GamePacketReader reader = new GamePacketReader(packet); + int index = (int) reader.getUnsigned(DataType.SHORT, DataOrder.LITTLE); + return new FifthNpcActionMessage(index); + } + +} \ No newline at end of file diff --git a/src/org/apollo/net/release/r317/FourthNpcActionMessageDecoder.java b/src/org/apollo/net/release/r317/FourthNpcActionMessageDecoder.java new file mode 100644 index 00000000..f208cdac --- /dev/null +++ b/src/org/apollo/net/release/r317/FourthNpcActionMessageDecoder.java @@ -0,0 +1,24 @@ +package org.apollo.net.release.r317; + +import org.apollo.game.message.impl.FourthNpcActionMessage; +import org.apollo.net.codec.game.DataType; +import org.apollo.net.codec.game.GamePacket; +import org.apollo.net.codec.game.GamePacketReader; +import org.apollo.net.release.MessageDecoder; + +/** + * A {@link MessageDecoder} for the {@link FourthNpcActionMessage}. + * + * @author Stuart + * @author Major + */ +public final class FourthNpcActionMessageDecoder extends MessageDecoder { + + @Override + public FourthNpcActionMessage decode(GamePacket packet) { + GamePacketReader reader = new GamePacketReader(packet); + int index = (int) reader.getUnsigned(DataType.SHORT); + return new FourthNpcActionMessage(index); + } + +} \ No newline at end of file diff --git a/src/org/apollo/net/release/r317/HintIconMessageEncoder.java b/src/org/apollo/net/release/r317/HintIconMessageEncoder.java index b8be56c3..8df96da4 100644 --- a/src/org/apollo/net/release/r317/HintIconMessageEncoder.java +++ b/src/org/apollo/net/release/r317/HintIconMessageEncoder.java @@ -28,6 +28,8 @@ public final class HintIconMessageEncoder extends MessageEncoder