Skip to content

Commit

Permalink
Codestyle Things
Browse files Browse the repository at this point in the history
  • Loading branch information
Hermanoid committed Feb 3, 2024
1 parent 91e7d2b commit bc7074d
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 81 deletions.
3 changes: 0 additions & 3 deletions src/main/java/com/hermanoid/nerd/CommonProxy.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/hermanoid/nerd/NEI_NERD_Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/hermanoid/nerd/NotEnoughRecipeDumps.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/com/hermanoid/nerd/RecipeDumper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -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
Expand All @@ -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<BaseRecipeDumper> dumpers;
if (DumperRegistry.containsKey(handlerId)) {
dumpers = DumperRegistry.get(handlerId);
}else{
} else {
dumpers = DumperRegistry.get(BaseRecipeDumper.FALLBACK_DUMPER_NAME);
}

Expand All @@ -109,7 +109,8 @@ private JsonObject extractJsonRecipeData(QueryResult queryResult) {

public Stream<JsonObject> getQueryDumps(List<ItemStack> 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);
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public abstract class BaseRecipeDumper {

public final static String FALLBACK_DUMPER_NAME = "<FALLBACK>";

public abstract JsonElement dump(ICraftingHandler handler, int recipeIndex);

public abstract String[] getCompatibleHandlers();
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/com/hermanoid/nerd/dumpers/DumperRegistry.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
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<String, BaseRecipeDumper> dumperMap = HashMultimap.create();
protected static RecipeDumpContext context;

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<BaseRecipeDumper> get(String key){
public static Collection<BaseRecipeDumper> get(String key) {
return dumperMap.get(key);
}

Expand Down
27 changes: 12 additions & 15 deletions src/main/java/com/hermanoid/nerd/dumpers/GTDefaultRecipeDumper.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,47 @@
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<String> badFields = ImmutableList.of(
// Unnecessary/bulky info
"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<Type> badTypes = ImmutableList.of(
GT_NEI_DefaultHandler.class, Materials.class
);
private static final List<Type> 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;
}
}
Expand All @@ -56,7 +53,7 @@ public String[] getCompatibleHandlers() {

@Override
public String getSlug() {
return "greg_data";
return "gtDefault";
}

}
18 changes: 11 additions & 7 deletions src/main/java/com/hermanoid/nerd/dumpers/GenericDumper.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
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;
import com.google.gson.JsonObject;
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<PositionedStack> stacks){
private JsonArray dumpItemStackList(Iterable<PositionedStack> 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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@
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;
import net.minecraft.util.RegistryNamespaced;
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 {
Expand All @@ -35,7 +35,6 @@ public class RecipeDumpContext {
public Map<String, JsonObject> dumpedItems = new HashMap<>();
public Map<String, JsonObject> dumpedFluids = new HashMap<>();


private final static Gson gson;
static {
List<String> badFields = ImmutableList.of(
Expand All @@ -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<Type> 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
Expand Down Expand Up @@ -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<String> standardNbtKeys
= new HashSet<>(Arrays.asList("id", "Count", "Damage"));
private final static HashSet<String> standardNbtKeys = new HashSet<>(Arrays.asList("id", "Count", "Damage"));

// Gets a minimal identifier for an item
// Most data (names, etc) is stored separately, once
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> badFields;
private final Set<Type> badTypes;

public SetExclusionStrategy(Set<String> badFields, Set<Type> badTypes){
this.badFields=badFields;
this.badTypes=badTypes;
public SetExclusionStrategy(Set<String> badFields, Set<Type> badTypes) {
this.badFields = badFields;
this.badTypes = badTypes;
}

@Override
public boolean shouldSkipField(FieldAttributes f) {

Expand Down
Loading

0 comments on commit bc7074d

Please sign in to comment.