Skip to content

Commit

Permalink
Fix issue with serializing message filters/consumers
Browse files Browse the repository at this point in the history
  • Loading branch information
eggohito committed Feb 13, 2024
1 parent 1f1824d commit 586641d
Showing 1 changed file with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -382,20 +382,27 @@ public class EggolibDataTypes {

public static final SerializableDataType<EntityPose> ENTITY_POSE = SerializableDataType.enumValue(EntityPose.class);

public static final SerializableDataType<Pattern> REGEX_PATTERN = SerializableDataType.wrap(
Pattern.class,
SerializableDataTypes.STRING,
Pattern::pattern,
Pattern::compile
);

public static final SerializableDataType<MessageFilter> MESSAGE_FILTER = SerializableDataType.compound(
MessageFilter.class,
new SerializableData()
.add("filter", SerializableDataTypes.STRING)
.add("filter", REGEX_PATTERN)
.add("entity_action", ApoliDataTypes.ENTITY_ACTION, null),
data -> new MessageFilter(
Pattern.compile(data.get("filter")),
data.get("filter"),
data.get("entity_action")
),
(serializableData, messageFilter) -> {

SerializableData.Instance data = serializableData.new Instance();

data.set("filter", messageFilter.getFilter().pattern());
data.set("filter", messageFilter.getFilter());
data.set("entity_action", messageFilter.getAction());

return data;
Expand Down Expand Up @@ -429,7 +436,7 @@ else if (jsonElement instanceof JsonPrimitive jsonPrimitive && jsonPrimitive.isS
public static final SerializableDataType<MessageConsumer> MESSAGE_CONSUMER = SerializableDataType.compound(
MessageConsumer.class,
new SerializableData()
.add("filter", SerializableDataTypes.STRING)
.add("filter", REGEX_PATTERN)
.add("before_action", ApoliDataTypes.ENTITY_ACTION, null)
.add("after_action", ApoliDataTypes.ENTITY_ACTION, null),
data -> new MessageConsumer(
Expand All @@ -441,7 +448,7 @@ else if (jsonElement instanceof JsonPrimitive jsonPrimitive && jsonPrimitive.isS

SerializableData.Instance data = serializableData.new Instance();

data.set("filter", messageConsumer.getFilter().pattern());
data.set("filter", messageConsumer.getFilter());
data.set("before_action", messageConsumer.getAction(MessagePhase.BEFORE));
data.set("after_action", messageConsumer.getAction(MessagePhase.AFTER));

Expand All @@ -463,7 +470,8 @@ else if (jsonElement instanceof JsonPrimitive jsonPrimitive && jsonPrimitive.isS
}

else if (jsonElement instanceof JsonPrimitive jsonPrimitive && jsonPrimitive.isString()) {
return new MessageConsumer(Pattern.compile(jsonPrimitive.getAsString()), null, null);
Pattern pattern = REGEX_PATTERN.read(jsonPrimitive);
return new MessageConsumer(pattern, null, null);
}

else {
Expand All @@ -478,7 +486,7 @@ else if (jsonElement instanceof JsonPrimitive jsonPrimitive && jsonPrimitive.isS
public static final SerializableDataType<MessageReplacer> MESSAGE_REPLACER = SerializableDataType.compound(
MessageReplacer.class,
new SerializableData()
.add("filter", SerializableDataTypes.STRING)
.add("filter", REGEX_PATTERN)
.add("replacement", SerializableDataTypes.STRING, null)
.add("before_action", ApoliDataTypes.ENTITY_ACTION, null)
.add("after_action", ApoliDataTypes.ENTITY_ACTION, null),
Expand All @@ -492,7 +500,7 @@ else if (jsonElement instanceof JsonPrimitive jsonPrimitive && jsonPrimitive.isS

SerializableData.Instance data = serializableData.new Instance();

data.set("filter", messageReplacer.getFilter().pattern());
data.set("filter", messageReplacer.getFilter());
data.set("replacement", messageReplacer.getReplacement());
data.set("before_action", messageReplacer.getAction(MessagePhase.BEFORE));
data.set("after_action", messageReplacer.getAction(MessagePhase.AFTER));
Expand Down

0 comments on commit 586641d

Please sign in to comment.