Skip to content

Commit

Permalink
Merge pull request #1 from D-Cysteine/main
Browse files Browse the repository at this point in the history
v0.4.1
  • Loading branch information
D-Cysteine authored Aug 26, 2021
2 parents 6eb0800 + 86b33a4 commit e3600e8
Show file tree
Hide file tree
Showing 32 changed files with 597 additions and 263 deletions.
10 changes: 0 additions & 10 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,6 @@ tasks.withType<Jar> {
)
)
}

// Move access transformer config into META-INF
this.rename("(neicustomdiagram_at\\.cfg)", "META-INF/$1")

// Update META-INF/MANIFEST.MF
this.manifest.attributes(
mapOf(
Pair("FMLAT", "neicustomdiagram_at.cfg")
)
)
}

val sourcesJar by tasks.creating(Jar::class) {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
neiCustomDiagramVersion=0.3.1
neiCustomDiagramVersion=0.4.1

minecraftVersion=1.7.10
forgeVersion=10.13.4.1614
Expand Down
Binary file modified libs/NotEnoughItems-1.7.10-2.1.11-GTNH-dev.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import codechicken.nei.event.NEIRegisterHandlerInfosEvent;
import com.github.dcysteine.neicustomdiagram.api.Config;
import com.github.dcysteine.neicustomdiagram.api.Logger;
import com.github.dcysteine.neicustomdiagram.api.Reflection;
import com.github.dcysteine.neicustomdiagram.api.Registry;
import com.github.dcysteine.neicustomdiagram.api.diagram.DiagramGenerator;
import com.github.dcysteine.neicustomdiagram.api.diagram.DiagramGroupInfo;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLLoadCompleteEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.relauncher.Side;
import net.minecraftforge.common.MinecraftForge;
Expand All @@ -34,16 +35,19 @@ public final class NeiCustomDiagram {
public static NeiCustomDiagram instance;

@EventHandler
public void onPreInitialization(FMLPreInitializationEvent event) {
public void onInitialization(FMLInitializationEvent event) {
if (event.getSide() != Side.CLIENT) {
return;
}
Logger.MOD.info("Mod pre-initialization starting...");
Logger.MOD.info("Mod initialization starting...");

Reflection.initialize();
Registry.initialize();
Config.initialize();
Config.saveConfig();
MinecraftForge.EVENT_BUS.register(NeiCustomDiagram.this);

Logger.MOD.info("Mod pre-initialization complete!");
Logger.MOD.info("Mod initialization complete!");
}

@EventHandler
Expand All @@ -55,18 +59,22 @@ public void onLoadComplete(FMLLoadCompleteEvent event) {

// TODO might be nice to hook into BetterLoadingScreen.
Registry.generateDiagramGroups();
Config.saveConfig();

Logger.MOD.info("Mod post-load complete!");
}

@SubscribeEvent
public void registerHandlers(NEIRegisterHandlerInfosEvent event) {
for (DiagramGenerator generator : Registry.GENERATORS) {
Logger.MOD.info("Registering handlers for diagram groups...");

for (DiagramGenerator generator : Registry.generators()) {
DiagramGroupInfo info = generator.info();
if (Config.getDiagramEnabled(info)) {
event.registerHandlerInfo(info.groupId(), MOD_NAME, MOD_ID, info::buildHandlerInfo);
Logger.MOD.info("Registered handler for diagram group [{}]!", info.groupId());
}
}

Logger.MOD.info("Registration complete!");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.dcysteine.neicustomdiagram.api;

import com.github.dcysteine.neicustomdiagram.NeiCustomDiagram;
import com.github.dcysteine.neicustomdiagram.api.diagram.DiagramGroupInfo;
import cpw.mods.fml.relauncher.FMLInjectionData;
import net.minecraftforge.common.config.Configuration;
Expand Down Expand Up @@ -47,15 +46,16 @@ public boolean get() {
// Static class.
private Config() {}

/** This method is only intended to be called during mod loading. */
/** This method is only intended to be called during mod initialization. */
public static void initialize() {
config = new Configuration(CONFIG_FILE);

// Load all options, so that they get saved if they're missing from the config.
Arrays.stream(Options.values()).forEach(Options::get);
Registry.generators().forEach(generator -> getDiagramEnabled(generator.info()));
}

/** This method is only intended to be called during mod loading. */
/** This method is only intended to be called during mod initialization. */
public static void saveConfig() {
if (config.hasChanged()) {
config.save();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.dcysteine.neicustomdiagram.api;

public final class Formatter {
public static final String INTEGER_FORMAT = "%,d";
public static final String DOUBLE_FORMAT = "%.2f";

// Static class.
private Formatter() {}

public static String formatInt(int i) {
return String.format(INTEGER_FORMAT, i);
}

public static String formatDouble(double d) {
return String.format(DOUBLE_FORMAT, d);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
/** Class that provides some convenience methods for getting translated strings. */
public final class Lang {
public static final Lang API = new Lang("neicustomdiagram.api.");
public static final Lang UTIL = new Lang("neicustomdiagram.util.");

public static final Lang FORGE_FLUID_CONTAINERS =
new Lang("neicustomdiagram.impl.forge.fluidcontainers.");
new Lang("neicustomdiagram.generators.forge.fluidcontainers.");
public static final Lang FORGE_ORE_DICTIONARY =
new Lang("neicustomdiagram.impl.forge.oredictionary.");
new Lang("neicustomdiagram.generators.forge.oredictionary.");

public static final Lang GREGTECH = new Lang("neicustomdiagram.impl.gregtech.");
public static final Lang GREGTECH = new Lang("neicustomdiagram.generators.gregtech.");
public static final Lang GREGTECH_ORE_DICTIONARY =
new Lang("neicustomdiagram.impl.gregtech.oredictionary.");
new Lang("neicustomdiagram.generators.gregtech.oredictionary.");
public static final Lang GREGTECH_ORE_PROCESSING =
new Lang("neicustomdiagram.impl.gregtech.oreprocessing.");
new Lang("neicustomdiagram.generators.gregtech.oreprocessing.");

private final String prefix;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.dcysteine.neicustomdiagram.api;

import com.google.auto.value.AutoValue;
import cpw.mods.fml.relauncher.ReflectionHelper;
import net.minecraft.client.gui.inventory.GuiContainer;

import java.lang.reflect.Field;

/** Class containing reflection accessors for private fields. */
public final class Reflection {
public static IntegerField GUI_LEFT;
public static IntegerField GUI_TOP;

@AutoValue
public abstract static class IntegerField {
public static IntegerField create(Class<?> clazz, String... fieldNames) {
Field field = ReflectionHelper.findField(clazz, fieldNames);
return new AutoValue_Reflection_IntegerField(field);
}

public abstract Field field();

public int getInt(Object obj) {
try {
return field().getInt(obj);
} catch (IllegalAccessException e) {
throw new IllegalStateException("Could not access reflection field!", e);
}
}
}

/** This method is only intended to be called during mod initialization. */
public static void initialize() {
GUI_LEFT = IntegerField.create(GuiContainer.class, "guiLeft", "field_147003_i");
GUI_TOP = IntegerField.create(GuiContainer.class, "guiTop", "field_147009_r");
}

}
111 changes: 96 additions & 15 deletions src/main/java/com/github/dcysteine/neicustomdiagram/api/Registry.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,125 @@
import com.github.dcysteine.neicustomdiagram.api.diagram.DiagramGenerator;
import com.github.dcysteine.neicustomdiagram.api.diagram.DiagramGroup;
import com.github.dcysteine.neicustomdiagram.api.diagram.DiagramGroupInfo;
import com.github.dcysteine.neicustomdiagram.impl.forge.fluidcontainers.ForgeFluidContainers;
import com.github.dcysteine.neicustomdiagram.impl.forge.oredictionary.ForgeOreDictionary;
import com.github.dcysteine.neicustomdiagram.impl.gregtech.oredictionary.GregTechOreDictionary;
import com.github.dcysteine.neicustomdiagram.impl.gregtech.oreprocessing.GregTechOreProcessing;
import com.github.dcysteine.neicustomdiagram.generators.forge.fluidcontainers.ForgeFluidContainers;
import com.github.dcysteine.neicustomdiagram.generators.forge.oredictionary.ForgeOreDictionary;
import com.github.dcysteine.neicustomdiagram.generators.gregtech.oredictionary.GregTechOreDictionary;
import com.github.dcysteine.neicustomdiagram.generators.gregtech.oreprocessing.GregTechOreProcessing;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import cpw.mods.fml.common.Loader;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/** Registry of diagram generators. Add your diagram generator here! */
public final class Registry {
public static final ImmutableList<DiagramGenerator> GENERATORS;
/** This will be prepended to all group IDs, to ensure that they are globally unique. */
public static final String GROUP_ID_PREFIX = "neicustomdiagram.diagramgroup.";

private static final List<RegistryEntry> entries = new ArrayList<>();
private static final List<DiagramGenerator> generators = new ArrayList<>();

static {
GENERATORS = ImmutableList.<DiagramGenerator>builder()
// Add your diagram generator here!
.add(new ForgeFluidContainers())
.add(new ForgeOreDictionary())
.add(new GregTechOreDictionary())
.add(new GregTechOreProcessing())
.build();
// Add your diagram generator here!
addGenerator("forge.fluidcontainers", ForgeFluidContainers::new);
addGenerator("forge.oredictionary", ForgeOreDictionary::new);
addGenerator("gregtech.oredictionary", GregTechOreDictionary::new, ModIds.GREGTECH);
addGenerator("gregtech.oreprocessing", GregTechOreProcessing::new, ModIds.GREGTECH);
}

/** For convenience, some mod IDs stored as constants. */
public static final class ModIds {
// If you're adding a new mod dependency here, don't forget to also add it to the list of
// dependencies in NeiCustomDiagram.java (if necessary).
public static final String GREGTECH = "gregtech";
public static final String BARTWORKS = "bartworks";

public static boolean isModLoaded(String modId) {
return Loader.isModLoaded(modId);
}
}

@AutoValue
protected abstract static class RegistryEntry {
protected static RegistryEntry create(
String groupIdSuffix, Function<String, DiagramGenerator> generatorConstructor,
String... hardDependencies) {
return new AutoValue_Registry_RegistryEntry(
GROUP_ID_PREFIX + groupIdSuffix, generatorConstructor,
ImmutableSet.copyOf(hardDependencies));
}

protected abstract String groupId();
protected abstract Function<String, DiagramGenerator> generatorConstructor();
protected abstract ImmutableSet<String> hardDependencies();

protected DiagramGenerator get() {
return generatorConstructor().apply(groupId());
}

protected List<String> missingDependencies() {
return hardDependencies().stream()
.filter(modId -> !ModIds.isModLoaded(modId))
.collect(Collectors.toList());
}
}

// Static class.
private Registry() {};

/** This method must be called before mod initialization. */
public static void addGenerator(
String groupIdSuffix, Function<String, DiagramGenerator> generatorConstructor,
String... hardDependencies) {
entries.add(RegistryEntry.create(groupIdSuffix, generatorConstructor, hardDependencies));
}

/** This method is only intended to be called during mod initialization. */
public static void initialize() {
Logger.MOD.info("Initializing diagram groups...");

for (RegistryEntry entry : entries) {
List<String> missingDependencies = entry.missingDependencies();
if (!missingDependencies.isEmpty()) {
Logger.MOD.warn(
"Diagram group [{}] is missing dependencies: {}",
entry.groupId(), missingDependencies);
continue;
}

generators.add(entry.get());
Logger.MOD.info("Initialized diagram group [{}]!", entry.groupId());
}

Logger.MOD.info("Initialization complete!");
}

/**
* We use this method as an accessor so that we can guarantee that callers cannot mutate
* {@link #entries}.
*/
public static ImmutableList<DiagramGenerator> generators() {
return ImmutableList.copyOf(generators);
}

public static void generateDiagramGroups() {
Logger.MOD.info("Generating diagram groups...");

for (DiagramGenerator generator : GENERATORS) {
for (DiagramGenerator generator : generators) {
DiagramGroupInfo info = generator.info();
if (!Config.getDiagramEnabled(info)) {
Logger.MOD.info("Diagram group [{}] disabled by config.", info.groupId());
continue;
}
Logger.MOD.info("Generating diagram group [{}]...", info.groupId());

Logger.MOD.info("Generating diagram group [{}]...", info.groupId());
DiagramGroup diagramGroup = generator.generate();
API.registerRecipeHandler(diagramGroup);
API.registerUsageHandler(diagramGroup);

Logger.MOD.info("Generated diagram group [{}]!", info.groupId());
}

Expand Down
Loading

0 comments on commit e3600e8

Please sign in to comment.