From bc7074d748dfe719931937da0e385b12d3eb5215 Mon Sep 17 00:00:00 2001 From: Lucas Niewohner Date: Sat, 3 Feb 2024 15:39:41 -0700 Subject: [PATCH] Codestyle Things --- .../java/com/hermanoid/nerd/CommonProxy.java | 3 -- .../com/hermanoid/nerd/NEI_NERD_Config.java | 2 +- .../hermanoid/nerd/NotEnoughRecipeDumps.java | 3 -- .../java/com/hermanoid/nerd/RecipeDumper.java | 18 +++++---- .../nerd/dumpers/BaseRecipeDumper.java | 1 + .../nerd/dumpers/DumperRegistry.java | 13 ++++--- .../nerd/dumpers/GTDefaultRecipeDumper.java | 27 ++++++-------- .../hermanoid/nerd/dumpers/GenericDumper.java | 18 +++++---- .../RecipeDumpContext.java | 30 +++++++-------- .../SetExclusionStrategy.java | 13 ++++--- .../nerd/stack_serialization/SluggerGson.java | 37 ++++++++++--------- 11 files changed, 84 insertions(+), 81 deletions(-) diff --git a/src/main/java/com/hermanoid/nerd/CommonProxy.java b/src/main/java/com/hermanoid/nerd/CommonProxy.java index 8c575cd..9a28242 100644 --- a/src/main/java/com/hermanoid/nerd/CommonProxy.java +++ b/src/main/java/com/hermanoid/nerd/CommonProxy.java @@ -1,9 +1,6 @@ package com.hermanoid.nerd; -import cpw.mods.fml.common.event.FMLInitializationEvent; -import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; -import cpw.mods.fml.common.event.FMLServerStartingEvent; public class CommonProxy { diff --git a/src/main/java/com/hermanoid/nerd/NEI_NERD_Config.java b/src/main/java/com/hermanoid/nerd/NEI_NERD_Config.java index 7a1a2a6..54a2144 100644 --- a/src/main/java/com/hermanoid/nerd/NEI_NERD_Config.java +++ b/src/main/java/com/hermanoid/nerd/NEI_NERD_Config.java @@ -2,10 +2,10 @@ import com.hermanoid.nerd.dumpers.DumperRegistry; import com.hermanoid.nerd.dumpers.GTDefaultRecipeDumper; +import com.hermanoid.nerd.dumpers.GenericDumper; import codechicken.nei.api.API; import codechicken.nei.api.IConfigureNEI; -import com.hermanoid.nerd.dumpers.GenericDumper; // This class is automatically discovered by a system in NotEnoughItems @SuppressWarnings("unused") diff --git a/src/main/java/com/hermanoid/nerd/NotEnoughRecipeDumps.java b/src/main/java/com/hermanoid/nerd/NotEnoughRecipeDumps.java index 30bd5f0..7d3146c 100644 --- a/src/main/java/com/hermanoid/nerd/NotEnoughRecipeDumps.java +++ b/src/main/java/com/hermanoid/nerd/NotEnoughRecipeDumps.java @@ -5,10 +5,7 @@ import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.SidedProxy; -import cpw.mods.fml.common.event.FMLInitializationEvent; -import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; -import cpw.mods.fml.common.event.FMLServerStartingEvent; @Mod(modid = Tags.MODID, version = Tags.VERSION, name = Tags.MODNAME, acceptedMinecraftVersions = "[1.7.10]") public class NotEnoughRecipeDumps { diff --git a/src/main/java/com/hermanoid/nerd/RecipeDumper.java b/src/main/java/com/hermanoid/nerd/RecipeDumper.java index 8149209..6f83994 100644 --- a/src/main/java/com/hermanoid/nerd/RecipeDumper.java +++ b/src/main/java/com/hermanoid/nerd/RecipeDumper.java @@ -8,8 +8,6 @@ import java.util.TimerTask; import java.util.stream.Stream; -import com.hermanoid.nerd.dumpers.DumperRegistry; -import com.hermanoid.nerd.stack_serialization.RecipeDumpContext; import net.minecraft.item.ItemStack; import net.minecraft.util.ChatComponentTranslation; @@ -21,6 +19,8 @@ import com.google.gson.JsonObject; import com.google.gson.stream.JsonWriter; import com.hermanoid.nerd.dumpers.BaseRecipeDumper; +import com.hermanoid.nerd.dumpers.DumperRegistry; +import com.hermanoid.nerd.stack_serialization.RecipeDumpContext; import codechicken.core.CommonUtils; import codechicken.nei.ItemList; @@ -41,6 +41,7 @@ public class RecipeDumper extends DataDumper { private boolean dumpActive = false; private final Timer timer = new Timer(); private RecipeDumpContext context = null; + public RecipeDumper(String name) { super(name); } @@ -64,13 +65,12 @@ private QueryResult performQuery(ItemStack targetStack) { return result; } - private JsonObject extractJsonRecipeData(QueryResult queryResult) { // Gather item details (don't grab everything... you can dump items if you want more details) // These columns will be repeated many times in the output, so don't write more than needed. JsonObject queryDump = new JsonObject(); - queryDump.add("query_item", context.getMinimalItemDump(queryResult.targetStack)); + queryDump.add("queryItem", context.getMinimalItemDump(queryResult.targetStack)); JsonArray handlerDumpArr = new JsonArray(); // Perform the Query @@ -82,12 +82,12 @@ private JsonObject extractJsonRecipeData(QueryResult queryResult) { String handlerId = handler.getHandlerId(); handlerDump.addProperty("id", handlerId); handlerDump.addProperty("name", handler.getRecipeName()); - handlerDump.addProperty("tab_name", handler.getRecipeTabName()); + handlerDump.addProperty("tabName", handler.getRecipeTabName()); Iterable dumpers; if (DumperRegistry.containsKey(handlerId)) { dumpers = DumperRegistry.get(handlerId); - }else{ + } else { dumpers = DumperRegistry.get(BaseRecipeDumper.FALLBACK_DUMPER_NAME); } @@ -109,7 +109,8 @@ private JsonObject extractJsonRecipeData(QueryResult queryResult) { public Stream getQueryDumps(List items) { // Parallelization doesn't help a *lot* but it is like a 2x speedup so I'll take it - // Update yeahhhh so parallelization works with some mods but in the larger GTNH modpack, some handlers don't react well + // Update yeahhhh so parallelization works with some mods but in the larger GTNH modpack, some handlers don't + // react well return items.stream() .map(this::performQuery) .map(this::extractJsonRecipeData); @@ -197,7 +198,8 @@ private void dumpContext(RecipeDumpContext context) { FileWriter writer = new FileWriter(file); JsonWriter jsonWriter = new JsonWriter(writer); - // Use a jsonWriter dump because the FileWriter seems to chop off the end of the (very large) dump().toString() + // Use a jsonWriter dump because the FileWriter seems to chop off the end of the (very large) + // dump().toString() new Gson().toJson(context.dump(), jsonWriter); jsonWriter.close(); writer.close(); diff --git a/src/main/java/com/hermanoid/nerd/dumpers/BaseRecipeDumper.java b/src/main/java/com/hermanoid/nerd/dumpers/BaseRecipeDumper.java index ddf1b5c..1bde663 100644 --- a/src/main/java/com/hermanoid/nerd/dumpers/BaseRecipeDumper.java +++ b/src/main/java/com/hermanoid/nerd/dumpers/BaseRecipeDumper.java @@ -8,6 +8,7 @@ public abstract class BaseRecipeDumper { public final static String FALLBACK_DUMPER_NAME = ""; + public abstract JsonElement dump(ICraftingHandler handler, int recipeIndex); public abstract String[] getCompatibleHandlers(); diff --git a/src/main/java/com/hermanoid/nerd/dumpers/DumperRegistry.java b/src/main/java/com/hermanoid/nerd/dumpers/DumperRegistry.java index ea807ee..79f43e4 100644 --- a/src/main/java/com/hermanoid/nerd/dumpers/DumperRegistry.java +++ b/src/main/java/com/hermanoid/nerd/dumpers/DumperRegistry.java @@ -1,12 +1,13 @@ package com.hermanoid.nerd.dumpers; +import java.util.Collection; + import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; import com.hermanoid.nerd.stack_serialization.RecipeDumpContext; -import java.util.Collection; - public class DumperRegistry { + protected static Multimap dumperMap = HashMultimap.create(); protected static RecipeDumpContext context; @@ -14,17 +15,17 @@ public static void registerDumper(BaseRecipeDumper dumper) { for (String id : dumper.getCompatibleHandlers()) dumperMap.put(id, dumper); } - public static void setContext(RecipeDumpContext context){ - for(BaseRecipeDumper dumper : dumperMap.values()){ + public static void setContext(RecipeDumpContext context) { + for (BaseRecipeDumper dumper : dumperMap.values()) { dumper.setContext(context); } } - public static boolean containsKey(String key){ + public static boolean containsKey(String key) { return dumperMap.containsKey(key); } - public static Collection get(String key){ + public static Collection get(String key) { return dumperMap.get(key); } diff --git a/src/main/java/com/hermanoid/nerd/dumpers/GTDefaultRecipeDumper.java b/src/main/java/com/hermanoid/nerd/dumpers/GTDefaultRecipeDumper.java index 4fcf7c6..637898e 100644 --- a/src/main/java/com/hermanoid/nerd/dumpers/GTDefaultRecipeDumper.java +++ b/src/main/java/com/hermanoid/nerd/dumpers/GTDefaultRecipeDumper.java @@ -1,19 +1,19 @@ package com.hermanoid.nerd.dumpers; +import java.lang.reflect.Type; +import java.util.List; + import com.google.common.collect.ImmutableList; import com.google.gson.Gson; import com.google.gson.JsonElement; import com.hermanoid.nerd.stack_serialization.RecipeDumpContext; +import com.hermanoid.nerd.stack_serialization.SluggerGson; import codechicken.nei.recipe.ICraftingHandler; -import com.hermanoid.nerd.stack_serialization.SluggerGson; import gregtech.api.enums.Materials; import gregtech.api.util.GT_Recipe; import gregtech.nei.GT_NEI_DefaultHandler; -import java.lang.reflect.Type; -import java.util.List; - public class GTDefaultRecipeDumper extends BaseRecipeDumper { private static final List badFields = ImmutableList.of( @@ -21,30 +21,27 @@ public class GTDefaultRecipeDumper extends BaseRecipeDumper { "recipeCategory", "stackTraces", "owners" - // Some recipes are GT_Recipe_WithAlt, which have more evil ItemStacks we can't serialize. - // TODO: Remove this comment if new serialization now works with mOreDictAlt -// "mOreDictAlt" ); - private static final List badTypes = ImmutableList.of( - GT_NEI_DefaultHandler.class, Materials.class - ); + private static final List badTypes = ImmutableList.of(GT_NEI_DefaultHandler.class, Materials.class); private Gson gson; @Override - public void setContext(RecipeDumpContext context){ + public void setContext(RecipeDumpContext context) { super.setContext(context); - gson = SluggerGson.gsonBuilder(context, badFields, badTypes).create(); + gson = SluggerGson.gsonBuilder(context, badFields, badTypes) + .create(); } @Override public JsonElement dump(ICraftingHandler handler, int recipeIndex) { GT_NEI_DefaultHandler gthandler = (GT_NEI_DefaultHandler) handler; - GT_Recipe recipe = gthandler.getCache().get(recipeIndex).mRecipe; + GT_Recipe recipe = ((GT_NEI_DefaultHandler.CachedDefaultRecipe) gthandler.arecipes.get(recipeIndex)).mRecipe; try { return gson.toJsonTree(recipe); } catch (Exception e) { - System.out.println("GTDefaultRecipeDumper GSON Serialization failed for handler " + handler.getRecipeName()); + System.out + .println("GTDefaultRecipeDumper GSON Serialization failed for handler " + handler.getRecipeName()); return null; } } @@ -56,7 +53,7 @@ public String[] getCompatibleHandlers() { @Override public String getSlug() { - return "greg_data"; + return "gtDefault"; } } diff --git a/src/main/java/com/hermanoid/nerd/dumpers/GenericDumper.java b/src/main/java/com/hermanoid/nerd/dumpers/GenericDumper.java index 13d8623..95904a1 100644 --- a/src/main/java/com/hermanoid/nerd/dumpers/GenericDumper.java +++ b/src/main/java/com/hermanoid/nerd/dumpers/GenericDumper.java @@ -1,7 +1,5 @@ package com.hermanoid.nerd.dumpers; -import codechicken.nei.PositionedStack; -import codechicken.nei.recipe.ICraftingHandler; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; @@ -9,22 +7,28 @@ import com.hermanoid.nerd.stack_serialization.RecipeDumpContext; import com.hermanoid.nerd.stack_serialization.SluggerGson; +import codechicken.nei.PositionedStack; +import codechicken.nei.recipe.ICraftingHandler; + public class GenericDumper extends BaseRecipeDumper { + private Gson gson = null; @Override - public void setContext(RecipeDumpContext context){ + public void setContext(RecipeDumpContext context) { super.setContext(context); - gson = SluggerGson.gsonBuilder(context).create(); + gson = SluggerGson.gsonBuilder(context) + .create(); } - private JsonArray dumpItemStackList(Iterable stacks){ + private JsonArray dumpItemStackList(Iterable stacks) { JsonArray arr = new JsonArray(); - for (PositionedStack stack : stacks){ + for (PositionedStack stack : stacks) { arr.add(gson.toJsonTree(stack.item)); } return arr; } + @Override public JsonElement dump(ICraftingHandler handler, int recipeIndex) { assert gson != null; @@ -41,7 +45,7 @@ public JsonElement dump(ICraftingHandler handler, int recipeIndex) { @Override public String[] getCompatibleHandlers() { - return new String[]{ FALLBACK_DUMPER_NAME }; + return new String[] { FALLBACK_DUMPER_NAME }; } @Override diff --git a/src/main/java/com/hermanoid/nerd/stack_serialization/RecipeDumpContext.java b/src/main/java/com/hermanoid/nerd/stack_serialization/RecipeDumpContext.java index efc1c32..140f8cc 100644 --- a/src/main/java/com/hermanoid/nerd/stack_serialization/RecipeDumpContext.java +++ b/src/main/java/com/hermanoid/nerd/stack_serialization/RecipeDumpContext.java @@ -8,9 +8,6 @@ import java.util.List; import java.util.Map; -import com.google.common.collect.ImmutableList; -import com.google.gson.GsonBuilder; -import gregtech.api.enums.Materials; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -18,13 +15,16 @@ import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidStack; +import com.google.common.collect.ImmutableList; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; import com.google.gson.reflect.TypeToken; import codechicken.nei.util.NBTJson; +import gregtech.api.enums.Materials; import gregtech.common.fluid.GT_Fluid; public class RecipeDumpContext { @@ -35,7 +35,6 @@ public class RecipeDumpContext { public Map dumpedItems = new HashMap<>(); public Map dumpedFluids = new HashMap<>(); - private final static Gson gson; static { List badFields = ImmutableList.of( @@ -50,13 +49,11 @@ public class RecipeDumpContext { // The automatic serializer doesn't like the rarity enum, I dunno why "rarity", // I don't think a fluid's corresponding block is useful, and it causes breaky recursion - "block" - ); + "block"); List badTypes = Collections.singletonList(Materials.class); - SetExclusionStrategy exclusionStrategy = - new SetExclusionStrategy( - new HashSet<>(badFields), - new HashSet<>(badTypes)); + SetExclusionStrategy exclusionStrategy = new SetExclusionStrategy( + new HashSet<>(badFields), + new HashSet<>(badTypes)); gson = new GsonBuilder() // We might be only doing serializations, but GSON will still create // a type adapter and get stuck in nasty recursion/type access land @@ -96,14 +93,16 @@ public JsonObject fluidToDetailedJson(Fluid src) { fluid = (JsonObject) gson.toJsonTree(src, fluidType); } // Manually serialize rarity bc wierdness - fluid.addProperty("rarity", src.getRarity().toString()); + fluid.addProperty( + "rarity", + src.getRarity() + .toString()); // Slap on some info that's only available via method calls fluid.addProperty("id", src.getID()); return fluid; } - private final static HashSet standardNbtKeys - = new HashSet<>(Arrays.asList("id", "Count", "Damage")); + private final static HashSet standardNbtKeys = new HashSet<>(Arrays.asList("id", "Count", "Damage")); // Gets a minimal identifier for an item // Most data (names, etc) is stored separately, once @@ -114,7 +113,8 @@ public JsonObject fluidToDetailedJson(Fluid src) { // // There is also the matter of how many/most (but not all) fluids in GTNewHorizons have a corresponding item-based // "FluidDisplay" provided by greg, sometimes as an ingredient and sometimes as an "otherStack" - // Some recipes have one or the other and I haven't a clue what decides whether a recipe gets one, the other, or both + // Some recipes have one or the other and I haven't a clue what decides whether a recipe gets one, the other, or + // both // I'll leave resolving combining fluids+item displays to the consumer of the dump // However, to (pretty dramatically) cut down on export size, I'll refer to the fluid slug (stored as Damage) // instead of doing a normal NBT dump @@ -150,7 +150,7 @@ public JsonElement getMinimalItemDump(ItemStack stack) { itemObj.addProperty("itemSlug", slug); itemObj.addProperty("count", count); for (String key : standardNbtKeys) tag.removeTag(key); - if(!tag.hasNoTags()){ + if (!tag.hasNoTags()) { itemObj.add("NBT", NBTJson.toJsonObject(tag)); } return itemObj; diff --git a/src/main/java/com/hermanoid/nerd/stack_serialization/SetExclusionStrategy.java b/src/main/java/com/hermanoid/nerd/stack_serialization/SetExclusionStrategy.java index d77e629..14d5359 100644 --- a/src/main/java/com/hermanoid/nerd/stack_serialization/SetExclusionStrategy.java +++ b/src/main/java/com/hermanoid/nerd/stack_serialization/SetExclusionStrategy.java @@ -1,20 +1,21 @@ package com.hermanoid.nerd.stack_serialization; -import com.google.gson.ExclusionStrategy; -import com.google.gson.FieldAttributes; - import java.lang.reflect.Type; import java.util.Set; +import com.google.gson.ExclusionStrategy; +import com.google.gson.FieldAttributes; + public class SetExclusionStrategy implements ExclusionStrategy { private final Set badFields; private final Set badTypes; - public SetExclusionStrategy(Set badFields, Set badTypes){ - this.badFields=badFields; - this.badTypes=badTypes; + public SetExclusionStrategy(Set badFields, Set badTypes) { + this.badFields = badFields; + this.badTypes = badTypes; } + @Override public boolean shouldSkipField(FieldAttributes f) { diff --git a/src/main/java/com/hermanoid/nerd/stack_serialization/SluggerGson.java b/src/main/java/com/hermanoid/nerd/stack_serialization/SluggerGson.java index 513b624..f23f136 100644 --- a/src/main/java/com/hermanoid/nerd/stack_serialization/SluggerGson.java +++ b/src/main/java/com/hermanoid/nerd/stack_serialization/SluggerGson.java @@ -7,7 +7,6 @@ import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.FluidStack; -import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonSerializationContext; @@ -17,21 +16,27 @@ public class SluggerGson { private static class FluidStackSerializer implements JsonSerializer { + private final RecipeDumpContext context; - private FluidStackSerializer(RecipeDumpContext context){ + + private FluidStackSerializer(RecipeDumpContext context) { this.context = context; } + @Override - public JsonElement serialize(FluidStack src, Type typeOfSrc, JsonSerializationContext jcontext) { - return context.getMinimalFluidDump(src); - } - }; + public JsonElement serialize(FluidStack src, Type typeOfSrc, JsonSerializationContext jcontext) { + return context.getMinimalFluidDump(src); + } + } private static class ItemStackSerializer implements JsonSerializer { + private final RecipeDumpContext context; - private ItemStackSerializer(RecipeDumpContext context){ + + private ItemStackSerializer(RecipeDumpContext context) { this.context = context; } + @Override public JsonElement serialize(ItemStack src, Type typeOfSrc, JsonSerializationContext jcontext) { return context.getMinimalItemDump(src); @@ -40,11 +45,10 @@ public JsonElement serialize(ItemStack src, Type typeOfSrc, JsonSerializationCon /** * @return A GsonBuilder with Slug-afying Serializers installed, which - * you can add your own extensions to. + * you can add your own extensions to. */ - public static GsonBuilder gsonBuilder(RecipeDumpContext context){ - return new GsonBuilder() - .registerTypeAdapter(FluidStack.class, new FluidStackSerializer(context)) + public static GsonBuilder gsonBuilder(RecipeDumpContext context) { + return new GsonBuilder().registerTypeAdapter(FluidStack.class, new FluidStackSerializer(context)) .registerTypeAdapter(ItemStack.class, new ItemStackSerializer(context)); } @@ -52,13 +56,12 @@ public static GsonBuilder gsonBuilder(RecipeDumpContext context){ * Helper method which adds a ListExclusionStrategy to the base gsonBuilder(context) * It's a common enough use-case to deserve its own helper. */ - public static GsonBuilder gsonBuilder(RecipeDumpContext context, Collection badFields, Collection badTypes){ + public static GsonBuilder gsonBuilder(RecipeDumpContext context, Collection badFields, + Collection badTypes) { SetExclusionStrategy exclusionStrategy = new SetExclusionStrategy( - new HashSet(badFields), - new HashSet(badTypes) - ); - return gsonBuilder(context) - .addSerializationExclusionStrategy(exclusionStrategy) + new HashSet<>(badFields), + new HashSet<>(badTypes)); + return gsonBuilder(context).addSerializationExclusionStrategy(exclusionStrategy) .addDeserializationExclusionStrategy(exclusionStrategy); } }