Skip to content

Commit

Permalink
WIP MI Compat
Browse files Browse the repository at this point in the history
  • Loading branch information
Leclowndu93150 committed Oct 6, 2024
1 parent 66ceddc commit aaa5f07
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ configurations {
dependencies {
runtimeOnly "curse.maven:mekanism-268560:5680395"
runtimeOnly "curse.maven:cloth-config-348521:5729127"
runtimeOnly "curse.maven:modern-industrialization-405388:5729622"
implementation "curse.maven:modern-industrialization-405388:5729622"
runtimeOnly "curse.maven:mystical-agriculture-246640:5774649"
runtimeOnly "curse.maven:cucumber-272335:5774468"
runtimeOnly "curse.maven:jei-238222:5781938"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.portingdeadmods.moreplates.config.MPConfig;
import com.portingdeadmods.moreplates.datagen.DynamicDataPack;
import com.portingdeadmods.moreplates.datagen.DynamicResourcePack;
import com.portingdeadmods.moreplates.datagen.compat.MIDynamicDataPack;
import com.portingdeadmods.moreplates.registries.MPCreativeTabs;
import com.portingdeadmods.moreplates.registries.MPItems;
import net.minecraft.core.registries.BuiltInRegistries;
import net.neoforged.fml.ModList;
import net.neoforged.fml.loading.FMLLoader;
import net.neoforged.neoforge.event.BuildCreativeModeTabContentsEvent;
import org.apache.logging.log4j.LogManager;
Expand All @@ -30,6 +32,12 @@ public MorePlatesMod(IEventBus modEventBus, ModContainer modContainer) {
modEventBus.addListener(MorePlatesMod::onCreativeTab);
DynamicResourcePack.init();
DynamicDataPack.INSTANCE.register();
/*
if(ModList.get().isLoaded("modern_industrialization")) {
LOGGER.info("Modern Industrialization detected, adding compatibility");
MIDynamicDataPack.INSTANCE.register();
}
*/
}

public static void onCreativeTab(BuildCreativeModeTabContentsEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.serialization.Decoder;
import com.portingdeadmods.moreplates.MorePlatesMod;
import com.portingdeadmods.moreplates.config.MPConfig;
import com.portingdeadmods.moreplates.registries.MPItems;
Expand All @@ -22,7 +21,6 @@
import net.minecraft.server.packs.repository.Pack;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Recipe;
import net.neoforged.neoforge.common.conditions.ICondition;
import org.apache.logging.log4j.Logger;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package com.portingdeadmods.moreplates.datagen.compat;
import aztech.modern_industrialization.machines.init.MIMachineRecipeTypes;
import aztech.modern_industrialization.machines.recipe.MachineRecipeBuilder;
import aztech.modern_industrialization.machines.recipe.MachineRecipeType;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.portingdeadmods.moreplates.MorePlatesMod;
import com.portingdeadmods.moreplates.config.MPConfig;
import com.portingdeadmods.moreplates.datagen.DynamicDataPack;
import com.portingdeadmods.moreplates.registries.MPItems;
import net.mehvahdjukaar.moonlight.api.platform.PlatHelper;
import net.mehvahdjukaar.moonlight.api.resources.SimpleTagBuilder;
import net.mehvahdjukaar.moonlight.api.resources.pack.DynServerResourcesGenerator;
import net.minecraft.advancements.Advancement;
import net.minecraft.advancements.AdvancementHolder;
import net.minecraft.advancements.critereon.InventoryChangeTrigger;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.data.recipes.RecipeCategory;
import net.minecraft.data.recipes.RecipeOutput;
import net.minecraft.data.recipes.ShapedRecipeBuilder;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.repository.Pack;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.level.ItemLike;
import net.neoforged.neoforge.common.conditions.ICondition;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class MIDynamicDataPack extends DynServerResourcesGenerator {

public static final MIDynamicDataPack INSTANCE = new MIDynamicDataPack();

public MIDynamicDataPack() {
super(new net.mehvahdjukaar.moonlight.api.resources.pack.DynamicDataPack(ResourceLocation.fromNamespaceAndPath(MorePlatesMod.MODID,"mi_generated_pack"), Pack.Position.TOP, false, false));
this.dynamicPack.setGenerateDebugResources(PlatHelper.isDev());
}

@Override
public Logger getLogger() {
return MorePlatesMod.LOGGER;
}

@Override
public boolean dependsOnLoadedPacks() {
return true;
}

@Override
public void regenerateDynamicAssets(ResourceManager manager) {
BuiltInRegistries.ITEM.forEach((item) -> {
ResourceLocation itemId = BuiltInRegistries.ITEM.getKey(item);

// Plates
if (MorePlatesMod.MODID.equals(itemId.getNamespace()) && itemId.getPath().contains("plate")) {
String rawName = itemId.getPath();
ResourceLocation inputIngot = MPConfig.getIngotFromPlate(itemId);
if (inputIngot == null) return;

Item inputItem = BuiltInRegistries.ITEM.get(inputIngot);

MachineRecipeBuilder recipeBuilder = new MachineRecipeBuilder(MIMachineRecipeTypes.COMPRESSOR, 16, 100)
.addItemInput((ItemLike) inputItem,1)
.addItemOutput(item, 1);

recipeBuilder.offerTo(new RecipeOutput() {
@Override
public Advancement.@NotNull Builder advancement() {
return Advancement.Builder.advancement();
}

@Override
public void accept(@NotNull ResourceLocation resourceLocation, @NotNull Recipe<?> recipe, @Nullable AdvancementHolder advancementHolder, ICondition... iConditions) {
dynamicPack.addRecipe(recipe, resourceLocation);
}
},"compressor/moreplates/"+itemId.getPath());
}

// Gears
if (MorePlatesMod.MODID.equals(itemId.getNamespace()) && itemId.getPath().contains("gear")) {
String rawName = itemId.getPath();
ResourceLocation inputIngot = MPConfig.getIngotFromGear(itemId);
if (inputIngot == null) return;

Item inputItem = BuiltInRegistries.ITEM.get(inputIngot);

MachineRecipeBuilder recipeBuilder = new MachineRecipeBuilder(MIMachineRecipeTypes.ASSEMBLER, 16, 100)
.addItemInput((ItemLike) inputItem,4)
.addItemOutput(item, 1);

recipeBuilder.offerTo(new RecipeOutput() {
@Override
public Advancement.@NotNull Builder advancement() {
return Advancement.Builder.advancement();
}

@Override
public void accept(@NotNull ResourceLocation resourceLocation, @NotNull Recipe<?> recipe, @Nullable AdvancementHolder advancementHolder, ICondition... iConditions) {
dynamicPack.addRecipe(recipe, resourceLocation);
}
},"assembler/moreplates/"+itemId.getPath());
}

// Rods
if (MorePlatesMod.MODID.equals(itemId.getNamespace()) && itemId.getPath().contains("rod")) {
String rawName = itemId.getPath();
ResourceLocation inputIngot = MPConfig.getIngotFromRod(itemId);
if (inputIngot == null) return;

Item inputItem = BuiltInRegistries.ITEM.get(inputIngot);


ShapedRecipeBuilder recipeBuilder = ShapedRecipeBuilder.shaped(RecipeCategory.MISC, item, 2)
.pattern("H")
.pattern("I")
.pattern("I")
.define('H', MPItems.HAMMER)
.define('I', inputItem)
.unlockedBy("has_item", InventoryChangeTrigger.TriggerInstance.hasItems(inputItem));

recipeBuilder.save(new RecipeOutput() {
@Override
public Advancement.@NotNull Builder advancement() {
return Advancement.Builder.advancement();
}

@Override
public void accept(@NotNull ResourceLocation resourceLocation, @NotNull Recipe<?> recipe, @Nullable AdvancementHolder advancementHolder, ICondition... iConditions) {
dynamicPack.addRecipe(recipe, resourceLocation);
}
});
}

});

}

private static void removeNullEntries(JsonObject jsonObject) {
jsonObject.entrySet().removeIf(entry -> entry.getValue().isJsonNull());

jsonObject.entrySet().forEach(entry -> {
JsonElement element = entry.getValue();
if (element.isJsonObject()) {
removeNullEntries(element.getAsJsonObject());
} else if (element.isJsonArray()) {
removeNullEntries(element.getAsJsonArray());
}
});
}

private static void removeNullEntries(JsonArray jsonArray) {
JsonArray newArray = new JsonArray();
jsonArray.forEach(element -> {
if (!element.isJsonNull()) {
if (element.isJsonObject()) {
removeNullEntries(element.getAsJsonObject().getAsJsonArray());
} else if (element.isJsonArray()) {
removeNullEntries(element.getAsJsonArray());
}
newArray.add(element);
}
});
for (int i = 0; i < jsonArray.size(); i++) jsonArray.remove(i);
jsonArray.addAll(newArray);
}
}

0 comments on commit aaa5f07

Please sign in to comment.