Improve HintIconMessage.

This commit is contained in:
Major-
2015-02-26 03:24:08 +00:00
parent cd5eafe7ac
commit 4d452dbf1e
4 changed files with 66 additions and 34 deletions
@@ -1,5 +1,6 @@
package org.apollo.game.message.impl;
import java.util.NoSuchElementException;
import java.util.Optional;
import org.apollo.game.message.Message;
@@ -15,14 +16,44 @@ public final class HintIconMessage extends Message {
// TODO identify the other types and use an enum.
/**
* The hint icon type for Npcs.
* The type of a HintIcon.
*/
public static final int NPC_TYPE = 1;
public enum Type {
/**
* The hint icon type for Players.
*/
public static final int PLAYER_TYPE = 10;
/**
* A HintIcon that hovers over an Npc.
*/
NPC(1),
/**
* A HintIcon that hovers over a Player.
*/
PLAYER(10);
/**
* The integer value of this type.
*/
private final int value;
/**
* Creates the Type.
*
* @param value The value.
*/
private Type(int value) {
this.value = value;
}
/**
* Gets the value of this type.
*
* @return The value.
*/
public int getValue() {
return value;
}
}
/**
* Creates a HintIconMessage for the Npc with the specified index.
@@ -31,7 +62,7 @@ public final class HintIconMessage extends Message {
* @return The HintIconMessage.
*/
public static HintIconMessage forNpc(int index) {
return new HintIconMessage(NPC_TYPE, Optional.of(index), Optional.empty());
return new HintIconMessage(Type.NPC, Optional.of(index), Optional.empty());
}
/**
@@ -41,11 +72,11 @@ public final class HintIconMessage extends Message {
* @return The HintIconMessage.
*/
public static HintIconMessage forPlayer(int index) {
return new HintIconMessage(PLAYER_TYPE, Optional.of(index), Optional.empty());
return new HintIconMessage(Type.PLAYER, Optional.of(index), Optional.empty());
}
/**
* The index of the entity, if applicable.
* The index of the Mob, if applicable.
*/
private final Optional<Integer> index;
@@ -55,18 +86,18 @@ public final class HintIconMessage extends Message {
private final Optional<Position> position;
/**
* The type of entity this HintIconMessage is directed at.
* The Type of entity this HintIconMessage is directed at.
*/
private final int type;
private final Type type;
/**
* Creates the HintIconMessage.
*
* @param type The type of entity this HintIconMessage is directed at.
* @param index The index of the entity, if applicable.
* @param type The {@link Type} of this HintIconMessage.
* @param index The index of the Mob, if applicable.
* @param position The Position of the tile, if applicable.
*/
private HintIconMessage(int type, Optional<Integer> index, Optional<Position> position) {
private HintIconMessage(Type type, Optional<Integer> index, Optional<Position> position) {
this.type = type;
this.index = index;
this.position = position;
@@ -76,18 +107,20 @@ public final class HintIconMessage extends Message {
* Gets the index of the entity, if applicable.
*
* @return The index.
* @throws NoSuchElementException If no index is available for this HintIcon.
*/
public Optional<Integer> getIndex() {
return index;
public int getIndex() {
return index.get();
}
/**
* Gets the {@link Position} of the tile, if applicable.
*
* @return The Position.
* @throws NoSuchElementException If no Position is available for this HintIcon.
*/
public Optional<Position> getPosition() {
return position;
public Position getPosition() {
return position.get();
}
/**
@@ -95,7 +128,7 @@ public final class HintIconMessage extends Message {
*
* @return The type.
*/
public int getType() {
public Type getType() {
return type;
}
@@ -16,13 +16,13 @@ public final class HintIconMessageEncoder extends MessageEncoder<HintIconMessage
@Override
public GamePacket encode(HintIconMessage message) {
GamePacketBuilder builder = new GamePacketBuilder(254);
int type = message.getType();
builder.put(DataType.BYTE, type);
HintIconMessage.Type type = message.getType();
builder.put(DataType.BYTE, type.getValue());
switch (type) {
case HintIconMessage.NPC_TYPE:
case HintIconMessage.PLAYER_TYPE:
builder.put(DataType.SHORT, message.getIndex().get());
case NPC:
case PLAYER:
builder.put(DataType.SHORT, message.getIndex());
builder.put(DataType.TRI_BYTE, 0); // Dummy bytes
break;
default:
@@ -16,13 +16,13 @@ public final class HintIconMessageEncoder extends MessageEncoder<HintIconMessage
@Override
public GamePacket encode(HintIconMessage message) {
GamePacketBuilder builder = new GamePacketBuilder(199);
int type = message.getType();
builder.put(DataType.BYTE, type);
HintIconMessage.Type type = message.getType();
builder.put(DataType.BYTE, type.getValue());
switch (type) {
case HintIconMessage.NPC_TYPE:
case HintIconMessage.PLAYER_TYPE:
builder.put(DataType.SHORT, message.getIndex().get());
case NPC:
case PLAYER:
builder.put(DataType.SHORT, message.getIndex());
builder.put(DataType.TRI_BYTE, 0); // Dummy bytes
break;
default:
@@ -41,7 +41,6 @@ import org.apollo.game.message.impl.UpdateTileItemMessage;
import org.apollo.game.message.impl.UpdateWeightMessage;
import org.apollo.net.meta.PacketMetaDataGroup;
import org.apollo.net.release.Release;
import org.apollo.net.release.r317.HintIconMessageEncoder;
/**
* A {@link Release} implementation for the 377 protocol.
@@ -128,8 +127,8 @@ public final class Release377 extends Release {
register(1, new ItemOnItemMessageDecoder());
register(36, new MagicOnItemMessageDecoder());
register(31, new MagicOnPlayerMessageDecoder());
register(104, new MagicOnNpcMessageDecoder());
register(31, new MagicOnPlayerMessageDecoder());
register(104, new MagicOnNpcMessageDecoder());
register(187, new FocusUpdateMessageDecoder());
register(19, new MouseClickedMessageDecoder());
@@ -144,8 +143,8 @@ public final class Release377 extends Release {
register(112, new FirstNpcActionMessageDecoder());
register(67, new SecondNpcActionMessageDecoder());
register(13, new ThirdNpcActionMessageDecoder());
register(42, new FourthNpcActionMessageDecoder());
register(8, new FifthNpcActionMessageDecoder());
register(42, new FourthNpcActionMessageDecoder());
register(8, new FifthNpcActionMessageDecoder());
register(71, new TakeTileItemMessageDecoder());
register(152, new ItemOnObjectMessageDecoder());