Skip to content

Commit

Permalink
fix action and minion task order serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheaterpaul committed Feb 25, 2024
1 parent af9a64e commit 9d7c31d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public interface IVampirismEntityRegistry {
/**
* Get the {@link de.teamlapen.vampirism.api.datamaps.IEntityBlood} for the given creature.
*
* @return the explicit entry or a calculated entry if one exists.
* @return the explicit entry
*/
@Nullable
IEntityBlood getEntry(PathfinderMob creature);
Expand Down
77 changes: 46 additions & 31 deletions src/main/java/de/teamlapen/vampirism/client/ClientConfigHelper.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package de.teamlapen.vampirism.client;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.gson.TypeAdapter;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
Expand Down Expand Up @@ -36,11 +33,16 @@
public class ClientConfigHelper {

public static final Gson GSON = new GsonBuilder()
.registerTypeHierarchyAdapter(List.class, new IActionListTypeAdapter())
.registerTypeAdapter(TypeToken.getParameterized(List.class, IAction.class).getType(), new IActionListTypeAdapter())
.registerTypeAdapter(TypeToken.getParameterized(List.class, SelectMinionTaskRadialScreen.Entry.class).getType(), new EntryListTypeAdapter())
.registerTypeHierarchyAdapter(ResourceLocation.class, new ResourceLocationTypeAdapter())
.registerTypeHierarchyAdapter(SelectMinionTaskRadialScreen.Entry.class, new EntryTypeAdapter())
.create();

@SuppressWarnings("unchecked")
private static final TypeToken<Map<ResourceLocation, List<IAction<?>>>> ACTION_TOKEN = (TypeToken<Map<ResourceLocation, List<IAction<?>>>>) TypeToken.getParameterized(Map.class, ResourceLocation.class, TypeToken.getParameterized(List.class, IAction.class).getType());
@SuppressWarnings("unchecked")
private static final TypeToken<Map<ResourceLocation, List<SelectMinionTaskRadialScreen.Entry>>> MINION_TASK_TOKEN = (TypeToken<Map<ResourceLocation, List<SelectMinionTaskRadialScreen.Entry>>>) TypeToken.getParameterized(Map.class, ResourceLocation.class, TypeToken.getParameterized(List.class, SelectMinionTaskRadialScreen.Entry.class).getType());

/**
* Dummy task order identifier id no faction is given, but this should never happen
*/
Expand All @@ -62,15 +64,15 @@ public static void onConfigChanged(@NotNull ModConfigEvent event) {
if (VampirismConfig.isClientConfigSpec(event.getConfig().getSpec())) {
try {
String string = VampirismConfig.CLIENT.actionOrder.get();
ACTION_ORDER = Objects.requireNonNullElseGet(GSON.fromJson(string, new TypeToken<>() {}), HashMap::new);
ACTION_ORDER = Objects.requireNonNullElseGet(GSON.fromJson(string, ACTION_TOKEN), HashMap::new);
} catch (JsonSyntaxException | IllegalArgumentException e) {
VampirismConfig.LOGGER.error("Failed to parse action order config", e);
VampirismConfig.CLIENT.actionOrder.set(VampirismConfig.CLIENT.actionOrder.getDefault());
ACTION_ORDER = new HashMap<>();
}
try {
String string = VampirismConfig.CLIENT.minionTaskOrder.get();
MINION_TASK_ORDER = Objects.requireNonNullElseGet(GSON.fromJson(string, new TypeToken<>() {}), HashMap::new);
MINION_TASK_ORDER = Objects.requireNonNullElseGet(GSON.fromJson(string, MINION_TASK_TOKEN), HashMap::new);
} catch (JsonSyntaxException | IllegalArgumentException e) {
VampirismConfig.LOGGER.error("Failed to parse minion task order config", e);
VampirismConfig.CLIENT.minionTaskOrder.set(VampirismConfig.CLIENT.minionTaskOrder.getDefault());
Expand All @@ -87,7 +89,7 @@ public static void onConfigChanged(@NotNull ModConfigEvent event) {
*/
public static boolean testActions(Object string) {
try {
GSON.fromJson((String) string, new TypeToken<Map<ResourceLocation, List<IAction<?>>>>() {});
GSON.fromJson((String) string, ACTION_TOKEN);
} catch (JsonSyntaxException | ClassCastException | IllegalArgumentException e) {
return false;
}
Expand All @@ -101,7 +103,7 @@ public static boolean testActions(Object string) {
*/
public static boolean testTasks(Object string) {
try {
GSON.fromJson((String) string, new TypeToken<Map<ResourceLocation, List<SelectMinionTaskRadialScreen.Entry>>>() {});
GSON.fromJson((String) string, MINION_TASK_TOKEN);
} catch (JsonSyntaxException | ClassCastException | IllegalArgumentException e) {
return false;
}
Expand Down Expand Up @@ -174,8 +176,12 @@ public static List<SelectMinionTaskRadialScreen.Entry> getDefaultMinionTaskOrder
*/
public static void saveActionOrder(@NotNull ResourceLocation id, @NotNull List<IAction<?>> actions) {
ACTION_ORDER.put(id, actions);
String object = GSON.toJson(ACTION_ORDER);
VampirismConfig.CLIENT.actionOrder.set(object);
try {
String object = GSON.toJson(ACTION_ORDER, ACTION_TOKEN.getType());
VampirismConfig.CLIENT.actionOrder.set(object);
} catch (JsonParseException e) {
VampirismConfig.LOGGER.error("Failed to save action order", e);
}
}

/**
Expand All @@ -186,8 +192,12 @@ public static void saveActionOrder(@NotNull ResourceLocation id, @NotNull List<
*/
public static void saveMinionTaskOrder(@Nullable IFaction<?> faction, @NotNull List<SelectMinionTaskRadialScreen.Entry> tasks) {
MINION_TASK_ORDER.put(Optional.ofNullable(faction).map(IFaction::getID).orElse(NONE), tasks);
String object = GSON.toJson(MINION_TASK_ORDER);
VampirismConfig.CLIENT.minionTaskOrder.set(object);
try {
String object = GSON.toJson(MINION_TASK_ORDER, MINION_TASK_TOKEN.getType());
VampirismConfig.CLIENT.minionTaskOrder.set(object);
} catch (JsonParseException e) {
VampirismConfig.LOGGER.error("Failed to save minion task order", e);
}

}

Expand Down Expand Up @@ -219,30 +229,35 @@ public void write(@NotNull JsonWriter out, @Nullable List<IAction<?>> value) thr
}
}

/**
* Gson type adapter for {@link SelectMinionTaskRadialScreen.Entry}
*/
private static final class EntryTypeAdapter extends TypeAdapter<SelectMinionTaskRadialScreen.Entry> {
private static final class EntryListTypeAdapter extends TypeAdapter<List<SelectMinionTaskRadialScreen.Entry>> {

@Override
public void write(JsonWriter out, @Nullable SelectMinionTaskRadialScreen.Entry value) throws IOException {
if (value == null) {
out.nullValue();
return;
public @NotNull List<SelectMinionTaskRadialScreen.Entry> read(@NotNull JsonReader in) throws IOException {
List<SelectMinionTaskRadialScreen.Entry> actions = new ArrayList<>();
in.beginArray();
while (in.hasNext()) {
ResourceLocation resourceLocation = new ResourceLocation(in.nextString());
IMinionTask<?, ?> minionTask = RegUtil.getMinionTask(resourceLocation);
SelectMinionTaskRadialScreen.Entry entry = SelectMinionTaskRadialScreen.CUSTOM_ENTRIES.get(resourceLocation);
if (entry != null) {
actions.add(entry);
} else if (minionTask != null) {
actions.add(new SelectMinionTaskRadialScreen.Entry(minionTask));
}
}
out.value(value.getId().toString());
in.endArray();
return actions;
}

@Override
public SelectMinionTaskRadialScreen.Entry read(JsonReader in) throws IOException {
ResourceLocation id = new ResourceLocation(in.nextString());
IMinionTask<?, ?> minionTask = RegUtil.getMinionTask(id);
if (minionTask != null) {
return new SelectMinionTaskRadialScreen.Entry(minionTask);
} else {
return SelectMinionTaskRadialScreen.CUSTOM_ENTRIES.get(id);
public void write(@NotNull JsonWriter out, @Nullable List<SelectMinionTaskRadialScreen.Entry> value) throws IOException {
out.beginArray();
if (value != null) {
for (SelectMinionTaskRadialScreen.Entry action : value) {
out.value(action.getId().toString());
}
}
out.endArray();
}
}

}

0 comments on commit 9d7c31d

Please sign in to comment.