mirror of
https://github.com/array-in-a-matrix/brainwine.git
synced 2025-04-02 11:11:58 -04:00
Default MessagePack enum values to prevent NPE
This commit is contained in:
parent
f7c99997e9
commit
5670778f9a
4 changed files with 27 additions and 2 deletions
|
@ -1,11 +1,13 @@
|
||||||
package brainwine.gameserver.entity;
|
package brainwine.gameserver.entity;
|
||||||
|
|
||||||
|
import brainwine.gameserver.msgpack.DefaultEnumValue;
|
||||||
import brainwine.gameserver.msgpack.EnumValue;
|
import brainwine.gameserver.msgpack.EnumValue;
|
||||||
import brainwine.gameserver.msgpack.RegisterEnum;
|
import brainwine.gameserver.msgpack.RegisterEnum;
|
||||||
|
|
||||||
@RegisterEnum
|
@RegisterEnum
|
||||||
public enum FacingDirection {
|
public enum FacingDirection {
|
||||||
|
|
||||||
|
@DefaultEnumValue
|
||||||
WEST(-1),
|
WEST(-1),
|
||||||
EAST(1);
|
EAST(1);
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package brainwine.gameserver.item;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
|
import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
|
||||||
|
|
||||||
|
import brainwine.gameserver.msgpack.DefaultEnumValue;
|
||||||
import brainwine.gameserver.msgpack.RegisterEnum;
|
import brainwine.gameserver.msgpack.RegisterEnum;
|
||||||
|
|
||||||
@RegisterEnum
|
@RegisterEnum
|
||||||
|
@ -12,6 +13,7 @@ public enum Layer {
|
||||||
FRONT,
|
FRONT,
|
||||||
LIQUID,
|
LIQUID,
|
||||||
|
|
||||||
|
@DefaultEnumValue
|
||||||
@JsonEnumDefaultValue
|
@JsonEnumDefaultValue
|
||||||
NONE;
|
NONE;
|
||||||
}
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package brainwine.gameserver.msgpack;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Target({ElementType.FIELD, ElementType.METHOD})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface DefaultEnumValue {}
|
|
@ -13,17 +13,28 @@ import org.msgpack.template.AbstractTemplate;
|
||||||
import org.msgpack.type.ValueType;
|
import org.msgpack.type.ValueType;
|
||||||
import org.msgpack.unpacker.Unpacker;
|
import org.msgpack.unpacker.Unpacker;
|
||||||
|
|
||||||
|
import brainwine.gameserver.msgpack.DefaultEnumValue;
|
||||||
import brainwine.gameserver.msgpack.EnumValue;
|
import brainwine.gameserver.msgpack.EnumValue;
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public class EnumTemplate<T> extends AbstractTemplate<T> {
|
public class EnumTemplate<T> extends AbstractTemplate<T> {
|
||||||
|
|
||||||
private final Map<T, Object> ids = new HashMap<>();
|
private final Map<T, Object> ids = new HashMap<>();
|
||||||
private final Map<Object, T> values = new HashMap<>();
|
private final Map<Object, T> values = new HashMap<>();
|
||||||
|
private T defaultValue;
|
||||||
|
|
||||||
public EnumTemplate(Class<T> type) {
|
public EnumTemplate(Class<T> type) {
|
||||||
T[] entries = type.getEnumConstants();
|
T[] entries = type.getEnumConstants();
|
||||||
|
|
||||||
for(Field field : type.getFields()) {
|
for(Field field : type.getFields()) {
|
||||||
|
if(field.getType() == type && field.isAnnotationPresent(DefaultEnumValue.class)) {
|
||||||
|
try {
|
||||||
|
defaultValue = (T)field.get(type);
|
||||||
|
} catch (IllegalArgumentException | IllegalAccessException e) {
|
||||||
|
throw new MessageTypeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(field.isAnnotationPresent(EnumValue.class)) {
|
if(field.isAnnotationPresent(EnumValue.class)) {
|
||||||
try {
|
try {
|
||||||
for(T entry : entries) {
|
for(T entry : entries) {
|
||||||
|
@ -85,9 +96,9 @@ public class EnumTemplate<T> extends AbstractTemplate<T> {
|
||||||
ValueType next = unpacker.getNextType();
|
ValueType next = unpacker.getNextType();
|
||||||
|
|
||||||
if(next == ValueType.INTEGER) {
|
if(next == ValueType.INTEGER) {
|
||||||
return values.get(unpacker.readInt());
|
return values.getOrDefault(unpacker.readInt(), defaultValue);
|
||||||
} else if(next == ValueType.RAW) {
|
} else if(next == ValueType.RAW) {
|
||||||
return values.get(unpacker.readString());
|
return values.getOrDefault(unpacker.readString(), defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new MessageTypeException("Unsupported enum id type");
|
throw new MessageTypeException("Unsupported enum id type");
|
||||||
|
|
Loading…
Add table
Reference in a new issue