mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-03 00:38:21 +00:00
Add item dropping codecs.
This commit is contained in:
@@ -7,4 +7,4 @@ package org.apollo.game.event;
|
||||
*/
|
||||
public abstract class Event {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package org.apollo.game.event.impl;
|
||||
|
||||
import org.apollo.game.event.Event;
|
||||
import org.apollo.game.model.Position;
|
||||
|
||||
/**
|
||||
* An {@link Event} which tells the client to focus on a specific {@link Position} (on which an action should be
|
||||
* performed).
|
||||
*
|
||||
* @author Chris Fletcher
|
||||
*/
|
||||
public final class PositionEvent extends Event {
|
||||
|
||||
/**
|
||||
* The base position.
|
||||
*/
|
||||
private final Position base;
|
||||
|
||||
/**
|
||||
* The target position.
|
||||
*/
|
||||
private final Position position;
|
||||
|
||||
/**
|
||||
* Creates a new focus position event.
|
||||
*
|
||||
* @param base The base from which the position is being focused on.
|
||||
* @param position The position to focus on.
|
||||
*/
|
||||
public PositionEvent(Position base, Position position) {
|
||||
this.base = base;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the base position.
|
||||
*
|
||||
* @return The position.
|
||||
*/
|
||||
public Position getBase() {
|
||||
return base;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the position to focus on.
|
||||
*
|
||||
* @return The target position.
|
||||
*/
|
||||
public Position getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package org.apollo.game.event.impl;
|
||||
|
||||
import org.apollo.game.event.Event;
|
||||
import org.apollo.game.model.Item;
|
||||
import org.apollo.game.model.def.ItemDefinition;
|
||||
|
||||
/**
|
||||
* An {@link Event} sent to tell the client to display an item on a tile.
|
||||
*
|
||||
* @author Major
|
||||
*/
|
||||
public final class SetTileItemEvent extends Event {
|
||||
|
||||
/**
|
||||
* The {@link Item}.
|
||||
*/
|
||||
private final Item item;
|
||||
|
||||
/**
|
||||
* The offset from the client's base position.
|
||||
*/
|
||||
private final int positionOffset;
|
||||
|
||||
/**
|
||||
* The previous amount of the item (if it is being updated).
|
||||
*/
|
||||
private final int previousAmount;
|
||||
|
||||
/**
|
||||
* Indicates whether the item is stackable.
|
||||
*/
|
||||
private final boolean stackable;
|
||||
|
||||
/**
|
||||
* Indicates whether an existing item is being updated.
|
||||
*/
|
||||
private final boolean updating;
|
||||
|
||||
/**
|
||||
* Creates a new event that will remove the item with the specified id from the tile.
|
||||
*
|
||||
* @param id The id of the item.
|
||||
*/
|
||||
public SetTileItemEvent(int id) {
|
||||
this(id, 0, ItemDefinition.lookup(id).isStackable(), false, 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new event that will add an item with the specified id and amount to the tile.
|
||||
*
|
||||
* @param id The id of the item.
|
||||
* @param amount The amount of the item.
|
||||
*/
|
||||
public SetTileItemEvent(int id, int amount) {
|
||||
this(id, amount, ItemDefinition.lookup(id).isStackable(), false, 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new set tile item event.
|
||||
*
|
||||
* @param id The id of the item.
|
||||
* @param amount The new amount of the item.
|
||||
* @param stackable Whether the item is stackable or not.
|
||||
* @param positionOffset The offset from the client's base position.
|
||||
* @param updating If the item is being updated or not.
|
||||
* @param previousAmount The previous amount of the item.
|
||||
*/
|
||||
public SetTileItemEvent(int id, int amount, boolean stackable, boolean updating, int previousAmount,
|
||||
int positionOffset) {
|
||||
if (id < 0 || amount < 0 || previousAmount < 0) {
|
||||
throw new IllegalArgumentException("The id, amount, and previous amount must be 0 or greater.");
|
||||
}
|
||||
this.item = new Item(id, amount);
|
||||
this.stackable = stackable;
|
||||
this.updating = updating;
|
||||
this.previousAmount = previousAmount;
|
||||
this.positionOffset = positionOffset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new event that updates the previous amount of the item.
|
||||
*
|
||||
* @param id The id of the item.
|
||||
* @param amount The amount of the item.
|
||||
* @param previousAmount The previous amount of the item.
|
||||
*/
|
||||
public SetTileItemEvent(int id, int amount, int previousAmount) {
|
||||
this(id, amount, true, true, previousAmount, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the amount of the item.
|
||||
*
|
||||
* @return The amount.
|
||||
*/
|
||||
public int getAmount() {
|
||||
return item.getAmount();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the id of the item.
|
||||
*
|
||||
* @return The item.
|
||||
*/
|
||||
public int getId() {
|
||||
return item.getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the offset from the client's base position.
|
||||
*
|
||||
* @return The offset.
|
||||
*/
|
||||
public int getPositionOffset() {
|
||||
return positionOffset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the previous amount of the item.
|
||||
*
|
||||
* @return The previous amount.
|
||||
*/
|
||||
public int getPreviousAmount() {
|
||||
return previousAmount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether this item is stackable or not.
|
||||
*
|
||||
* @return {@code true} if the item is stackable, otherwise {@code false}.
|
||||
*/
|
||||
public boolean isStackable() {
|
||||
return stackable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether this item is being updated or not.
|
||||
*
|
||||
* @return {@code true} if the item is being updated, otherwise {@code false}.
|
||||
*/
|
||||
public boolean isUpdating() {
|
||||
return updating;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package org.apollo.game.event.impl;
|
||||
|
||||
import org.apollo.game.event.Event;
|
||||
import org.apollo.game.model.Position;
|
||||
|
||||
/**
|
||||
* An {@link Event} sent by the client indicating a request to pick up an item on a tile.
|
||||
*
|
||||
* @author Major
|
||||
*/
|
||||
public final class TakeTileItemEvent extends Event {
|
||||
|
||||
/**
|
||||
* The id of the item.
|
||||
*/
|
||||
private final int id;
|
||||
|
||||
/**
|
||||
* The position of the tile.
|
||||
*/
|
||||
private final Position position;
|
||||
|
||||
/**
|
||||
* Creates a new take tile item event.
|
||||
*
|
||||
* @param id The id of the item.
|
||||
* @param position The position of the tile.
|
||||
*/
|
||||
public TakeTileItemEvent(int id, Position position) {
|
||||
this.id = id;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the id of the item.
|
||||
*
|
||||
* @return The id.
|
||||
*/
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the position of the tile.
|
||||
*
|
||||
* @return The position.
|
||||
*/
|
||||
public Position getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.apollo.net.release.r317;
|
||||
|
||||
import org.apollo.game.event.impl.PositionEvent;
|
||||
import org.apollo.game.model.Position;
|
||||
import org.apollo.net.codec.game.DataTransformation;
|
||||
import org.apollo.net.codec.game.DataType;
|
||||
import org.apollo.net.codec.game.GamePacket;
|
||||
import org.apollo.net.codec.game.GamePacketBuilder;
|
||||
import org.apollo.net.release.EventEncoder;
|
||||
|
||||
/**
|
||||
* An {@link EventEncoder} for the {@link PositionEvent}.
|
||||
*
|
||||
* @author Chris Fletcher
|
||||
*/
|
||||
final class PositionEventEncoder extends EventEncoder<PositionEvent> {
|
||||
|
||||
@Override
|
||||
public GamePacket encode(PositionEvent event) {
|
||||
GamePacketBuilder builder = new GamePacketBuilder(85);
|
||||
Position base = event.getBase(), pos = event.getPosition();
|
||||
|
||||
builder.put(DataType.BYTE, DataTransformation.NEGATE, pos.getLocalY(base));
|
||||
builder.put(DataType.BYTE, DataTransformation.NEGATE, pos.getLocalX(base));
|
||||
|
||||
return builder.toGamePacket();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,8 +10,10 @@ import org.apollo.game.event.impl.NpcSynchronizationEvent;
|
||||
import org.apollo.game.event.impl.OpenInterfaceEvent;
|
||||
import org.apollo.game.event.impl.OpenInterfaceSidebarEvent;
|
||||
import org.apollo.game.event.impl.PlayerSynchronizationEvent;
|
||||
import org.apollo.game.event.impl.PositionEvent;
|
||||
import org.apollo.game.event.impl.RegionChangeEvent;
|
||||
import org.apollo.game.event.impl.ServerMessageEvent;
|
||||
import org.apollo.game.event.impl.SetTileItemEvent;
|
||||
import org.apollo.game.event.impl.SetWidgetItemModelEvent;
|
||||
import org.apollo.game.event.impl.SetWidgetModelAnimationEvent;
|
||||
import org.apollo.game.event.impl.SetWidgetNpcModelEvent;
|
||||
@@ -122,6 +124,7 @@ public final class Release317 extends Release {
|
||||
register(72, new FirstNpcActionEventDecoder());
|
||||
register(155, new SecondNpcActionEventDecoder());
|
||||
register(17, new ThirdNpcActionEventDecoder());
|
||||
register(236, new TakeTileItemEventDecoder());
|
||||
|
||||
// register encoders
|
||||
register(IdAssignmentEvent.class, new IdAssignmentEventEncoder());
|
||||
@@ -146,6 +149,8 @@ public final class Release317 extends Release {
|
||||
register(SetWidgetModelAnimationEvent.class, new SetWidgetModelAnimationEventEncoder());
|
||||
register(ConfigEvent.class, new ConfigEventEncoder());
|
||||
register(DisplayTabInterfaceEvent.class, new DisplayTabInterfaceEventEncoder());
|
||||
register(SetTileItemEvent.class, new SetTileItemEventEncoder());
|
||||
register(PositionEvent.class, new PositionEventEncoder());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.apollo.net.release.r317;
|
||||
|
||||
import org.apollo.game.event.impl.SetTileItemEvent;
|
||||
import org.apollo.net.codec.game.DataOrder;
|
||||
import org.apollo.net.codec.game.DataTransformation;
|
||||
import org.apollo.net.codec.game.DataType;
|
||||
import org.apollo.net.codec.game.GamePacket;
|
||||
import org.apollo.net.codec.game.GamePacketBuilder;
|
||||
import org.apollo.net.release.EventEncoder;
|
||||
|
||||
/**
|
||||
* An {@link EventEncoder} for the {@link SetTileItemEvent}.
|
||||
*
|
||||
* @author Major
|
||||
*/
|
||||
public class SetTileItemEventEncoder extends EventEncoder<SetTileItemEvent> {
|
||||
|
||||
@Override
|
||||
public GamePacket encode(SetTileItemEvent event) {
|
||||
GamePacketBuilder builder = null;
|
||||
if (event.getAmount() == 0) { // remove the item.
|
||||
builder = new GamePacketBuilder(156);
|
||||
|
||||
builder.put(DataType.BYTE, DataTransformation.ADD, event.getPositionOffset());
|
||||
builder.put(DataType.SHORT, event.getId());
|
||||
} else if (!event.isUpdating()) { // sending a new item
|
||||
builder = new GamePacketBuilder(44);
|
||||
|
||||
builder.put(DataType.SHORT, DataOrder.LITTLE, DataTransformation.ADD, event.getId());
|
||||
builder.put(DataType.SHORT, event.getAmount());
|
||||
builder.put(DataType.BYTE, event.getPositionOffset());
|
||||
} else { // updating an already displayed item
|
||||
builder = new GamePacketBuilder(84);
|
||||
|
||||
builder.put(DataType.BYTE, event.getPositionOffset());
|
||||
builder.put(DataType.SHORT, event.getId());
|
||||
builder.put(DataType.SHORT, event.getPreviousAmount());
|
||||
builder.put(DataType.SHORT, event.getAmount());
|
||||
}
|
||||
return builder.toGamePacket();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.apollo.net.release.r317;
|
||||
|
||||
import org.apollo.game.event.impl.TakeTileItemEvent;
|
||||
import org.apollo.game.model.Position;
|
||||
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.EventDecoder;
|
||||
|
||||
/**
|
||||
* An {@link EventDecoder} for the {@link TakeTileItemEvent}.
|
||||
*
|
||||
* @author Major
|
||||
*/
|
||||
public class TakeTileItemEventDecoder extends EventDecoder<TakeTileItemEvent> {
|
||||
|
||||
@Override
|
||||
public TakeTileItemEvent decode(GamePacket packet) {
|
||||
GamePacketReader reader = new GamePacketReader(packet);
|
||||
int y = (int) reader.getUnsigned(DataType.SHORT, DataOrder.LITTLE);
|
||||
int id = (int) reader.getUnsigned(DataType.SHORT);
|
||||
int x = (int) reader.getUnsigned(DataType.SHORT, DataOrder.LITTLE);
|
||||
return new TakeTileItemEvent(id, new Position(x, y));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.apollo.net.release.r377;
|
||||
|
||||
import org.apollo.game.event.impl.PositionEvent;
|
||||
import org.apollo.game.model.Position;
|
||||
import org.apollo.net.codec.game.DataTransformation;
|
||||
import org.apollo.net.codec.game.DataType;
|
||||
import org.apollo.net.codec.game.GamePacket;
|
||||
import org.apollo.net.codec.game.GamePacketBuilder;
|
||||
import org.apollo.net.release.EventEncoder;
|
||||
|
||||
/**
|
||||
* An {@link EventEncoder} for the {@link PositionEvent}.
|
||||
*
|
||||
* @author Chris Fletcher
|
||||
* @author Major
|
||||
*/
|
||||
final class PositionEventEncoder extends EventEncoder<PositionEvent> {
|
||||
|
||||
@Override
|
||||
public GamePacket encode(PositionEvent event) {
|
||||
GamePacketBuilder builder = new GamePacketBuilder(75);
|
||||
Position base = event.getBase(), pos = event.getPosition();
|
||||
|
||||
int x = pos.getLocalX(base);
|
||||
builder.put(DataType.BYTE, DataTransformation.NEGATE, x);
|
||||
int y = pos.getLocalY(base);
|
||||
builder.put(DataType.BYTE, DataTransformation.ADD, y);
|
||||
|
||||
return builder.toGamePacket();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,8 +10,10 @@ import org.apollo.game.event.impl.NpcSynchronizationEvent;
|
||||
import org.apollo.game.event.impl.OpenInterfaceEvent;
|
||||
import org.apollo.game.event.impl.OpenInterfaceSidebarEvent;
|
||||
import org.apollo.game.event.impl.PlayerSynchronizationEvent;
|
||||
import org.apollo.game.event.impl.PositionEvent;
|
||||
import org.apollo.game.event.impl.RegionChangeEvent;
|
||||
import org.apollo.game.event.impl.ServerMessageEvent;
|
||||
import org.apollo.game.event.impl.SetTileItemEvent;
|
||||
import org.apollo.game.event.impl.SetWidgetItemModelEvent;
|
||||
import org.apollo.game.event.impl.SetWidgetModelAnimationEvent;
|
||||
import org.apollo.game.event.impl.SetWidgetNpcModelEvent;
|
||||
@@ -120,6 +122,7 @@ public final class Release377 extends Release {
|
||||
register(67, new FirstNpcActionEventDecoder());
|
||||
register(112, new SecondNpcActionEventDecoder());
|
||||
register(13, new ThirdNpcActionEventDecoder());
|
||||
register(71, new TakeTileItemEventDecoder());
|
||||
|
||||
// register encoders
|
||||
register(IdAssignmentEvent.class, new IdAssignmentEventEncoder());
|
||||
@@ -144,6 +147,8 @@ public final class Release377 extends Release {
|
||||
register(SetWidgetModelAnimationEvent.class, new SetWidgetModelAnimationEventEncoder());
|
||||
register(ConfigEvent.class, new ConfigEventEncoder());
|
||||
register(DisplayTabInterfaceEvent.class, new DisplayTabInterfaceEventEncoder());
|
||||
register(SetTileItemEvent.class, new SetTileItemEventEncoder());
|
||||
register(PositionEvent.class, new PositionEventEncoder());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package org.apollo.net.release.r377;
|
||||
|
||||
import org.apollo.game.event.impl.SetTileItemEvent;
|
||||
import org.apollo.net.codec.game.DataTransformation;
|
||||
import org.apollo.net.codec.game.DataType;
|
||||
import org.apollo.net.codec.game.GamePacket;
|
||||
import org.apollo.net.codec.game.GamePacketBuilder;
|
||||
import org.apollo.net.release.EventEncoder;
|
||||
|
||||
/**
|
||||
* An {@link EventEncoder} for the {@link SetTileItemEvent}.
|
||||
*
|
||||
* @author Major
|
||||
*/
|
||||
public class SetTileItemEventEncoder extends EventEncoder<SetTileItemEvent> {
|
||||
|
||||
@Override
|
||||
public GamePacket encode(SetTileItemEvent event) {
|
||||
GamePacketBuilder builder = null;
|
||||
if (event.getAmount() == 0) { // remove the item.
|
||||
builder = new GamePacketBuilder(208);
|
||||
|
||||
builder.put(DataType.SHORT, DataTransformation.ADD, event.getId());
|
||||
builder.put(DataType.BYTE, DataTransformation.ADD, event.getPositionOffset());
|
||||
} else if (!event.isUpdating()) { // sending a new item
|
||||
builder = new GamePacketBuilder(107);
|
||||
|
||||
builder.put(DataType.SHORT, event.getId());
|
||||
builder.put(DataType.BYTE, DataTransformation.NEGATE, event.getPositionOffset());
|
||||
builder.put(DataType.SHORT, DataTransformation.ADD, event.getAmount());
|
||||
} else { // updating an already displayed item
|
||||
builder = new GamePacketBuilder(121);
|
||||
|
||||
builder.put(DataType.BYTE, event.getPositionOffset());
|
||||
builder.put(DataType.SHORT, event.getId());
|
||||
builder.put(DataType.SHORT, event.getPreviousAmount());
|
||||
builder.put(DataType.SHORT, event.getAmount());
|
||||
}
|
||||
return builder.toGamePacket();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.apollo.net.release.r377;
|
||||
|
||||
import org.apollo.game.event.impl.TakeTileItemEvent;
|
||||
import org.apollo.game.model.Position;
|
||||
import org.apollo.net.codec.game.DataOrder;
|
||||
import org.apollo.net.codec.game.DataTransformation;
|
||||
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.EventDecoder;
|
||||
|
||||
/**
|
||||
* An {@link EventDecoder} for the {@link TakeTileItemEvent}.
|
||||
*
|
||||
* @author Major
|
||||
*/
|
||||
public class TakeTileItemEventDecoder extends EventDecoder<TakeTileItemEvent> {
|
||||
|
||||
@Override
|
||||
public TakeTileItemEvent decode(GamePacket packet) {
|
||||
GamePacketReader reader = new GamePacketReader(packet);
|
||||
int id = (int) reader.getUnsigned(DataType.SHORT, DataOrder.LITTLE, DataTransformation.ADD);
|
||||
int x = (int) reader.getUnsigned(DataType.SHORT, DataOrder.LITTLE, DataTransformation.ADD);
|
||||
int y = (int) reader.getUnsigned(DataType.SHORT, DataTransformation.ADD);
|
||||
return new TakeTileItemEvent(id, new Position(x, y));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user