Skip to content

Commit

Permalink
Merge pull request #1 from TheSilkMiner/fix/use-callbacks-for-registr…
Browse files Browse the repository at this point in the history
…ation

Rework mod to use Neo Callbacks
  • Loading branch information
Leclowndu93150 authored Oct 6, 2024
2 parents a9a71b4 + cee4ad5 commit 5939a49
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.portingdeadmods.moreplates.config.MPConfig;
import com.portingdeadmods.moreplates.datagen.DynamicDataPack;
import com.portingdeadmods.moreplates.datagen.DynamicPack;
import com.portingdeadmods.moreplates.datagen.DynamicResourcePack;
import com.portingdeadmods.moreplates.registries.MPCreativeTabs;
import com.portingdeadmods.moreplates.registries.MPItems;
import net.minecraft.core.registries.BuiltInRegistries;
Expand All @@ -28,7 +28,7 @@ public MorePlatesMod(IEventBus modEventBus, ModContainer modContainer) {
MPItems.ITEMS.register(modEventBus);
MPCreativeTabs.CREATIVE_MODE_TABS.register(modEventBus);
modEventBus.addListener(MorePlatesMod::onCreativeTab);
DynamicPack.init();
DynamicResourcePack.init();
DynamicDataPack.INSTANCE.register();
}

Expand Down
40 changes: 17 additions & 23 deletions src/main/java/com/portingdeadmods/moreplates/config/MPConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
import com.portingdeadmods.moreplates.MorePlatesMod;
import net.neoforged.fml.loading.FMLLoader;

Expand All @@ -14,14 +15,15 @@
import java.util.*;

public class MPConfig {
private record IngotPlatePair(ResourceLocation ingot, ResourceLocation plate) {}

private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private static final File configFile = new File("config/moreplates/ingot_plate_pairs.json");
private static final File generatorConfigFile = new File("config/moreplates/moreplates-generator.json");
private static final List<String> generatorValues = new ArrayList<>();
private static Set<String> registeredIngotPlatePairs = new HashSet<>();
private static Map<String, String> ingotToPlateMap = new HashMap<>();
private static Map<String, String> plateToIngotMap = new HashMap<>();
private static Set<IngotPlatePair> registeredIngotPlatePairs = new HashSet<>();
private static Map<ResourceLocation, ResourceLocation> ingotToPlateMap = new HashMap<>();
private static Map<ResourceLocation, ResourceLocation> plateToIngotMap = new HashMap<>();

public static void loadConfig() {
FMLLoader.getGamePath().resolve("config").resolve(MorePlatesMod.MODID).toFile().mkdirs();
Expand All @@ -33,9 +35,9 @@ public static void loadConfig() {
if (pairs != null) {
for (var entry : pairs) {
JsonObject pair = entry.getAsJsonObject();
String ingot = pair.get("ingot").getAsString();
String plate = pair.get("plate").getAsString();
registeredIngotPlatePairs.add(ingot + ":" + plate);
ResourceLocation ingot = new ResourceLocation(pair.get("ingot").getAsString());
ResourceLocation plate = new ResourceLocation(pair.get("plate").getAsString());
registeredIngotPlatePairs.add(new IngotPlatePair(ingot, plate));
ingotToPlateMap.put(ingot, plate);
plateToIngotMap.put(plate, ingot);
}
Expand Down Expand Up @@ -79,8 +81,8 @@ public static void loadGeneratorConfig() {
}
}

public static void saveIngotPlatePair(String ingotId, String plateId) {
String pairKey = ingotId + ":" + plateId;
public static void saveIngotPlatePair(ResourceLocation ingotId, ResourceLocation plateId) {
IngotPlatePair pairKey = new IngotPlatePair(ingotId, plateId);

if (!registeredIngotPlatePairs.contains(pairKey)) {
registeredIngotPlatePairs.add(pairKey);
Expand All @@ -90,18 +92,10 @@ public static void saveIngotPlatePair(String ingotId, String plateId) {
JsonObject json = new JsonObject();
JsonArray pairsArray = new JsonArray();

for (String pair : registeredIngotPlatePairs) {
int colonIndex = pair.indexOf(':');
String ingot = pair.substring(0, colonIndex);
String remaining = pair.substring(colonIndex + 1);

colonIndex = remaining.indexOf(':');
ingotId = remaining.substring(0, colonIndex);
plateId = remaining.substring(colonIndex + 1);

for (IngotPlatePair pair : registeredIngotPlatePairs) {
JsonObject pairObject = new JsonObject();
pairObject.addProperty("ingot", ingot + ":" + ingotId);
pairObject.addProperty("plate", plateId);
pairObject.addProperty("ingot", pair.ingot().toString());
pairObject.addProperty("plate", pair.plate().toString());
pairsArray.add(pairObject);
}

Expand All @@ -119,13 +113,13 @@ public static List<String> getGeneratorValues() {
return generatorValues;
}

public static String getPlateFromIngot(String ingotId) {
// Method to get the plate from the given ingot
public static ResourceLocation getPlateFromIngot(ResourceLocation ingotId) {
return ingotToPlateMap.get(ingotId);
}

public static String getIngotFromPlate(String plateId) {
// Method to get the ingot from the given plate
public static ResourceLocation getIngotFromPlate(ResourceLocation plateId) {
return plateToIngotMap.get(plateId);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,31 @@ public boolean dependsOnLoadedPacks() {
@Override
public void regenerateDynamicAssets(ResourceManager manager) {
BuiltInRegistries.ITEM.forEach((item) -> {
if (item.getDescriptionId().contains(MorePlatesMod.MODID) && item.getDescriptionId().contains("plate")) {
// Get the raw item name without prefixes
String rawName = item.getDescriptionId()
.replace("item.", "")
.replace(MorePlatesMod.MODID + ":", "")
.replace("moreplates.", "");

String fullName = MorePlatesMod.MODID + ":" + rawName;
String fullNameOutput = MorePlatesMod.MODID+":"+rawName;
String fullNameInput = MPConfig.getIngotFromPlate(fullNameOutput);
if(fullNameInput == null) return;
String namespace = fullNameInput.split(":")[0];
String path = fullNameInput.split(":")[1];

ItemStack inputItem = BuiltInRegistries.ITEM.get(ResourceLocation.fromNamespaceAndPath(namespace,path)).getDefaultInstance();

SimpleTagBuilder tagBuilder = SimpleTagBuilder.of(ResourceLocation.fromNamespaceAndPath("c", "plates/"+rawName.replace("_plate", "")));
ResourceLocation itemId = BuiltInRegistries.ITEM.getKey(item);
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);

ItemStack inputItemStack = BuiltInRegistries.ITEM.get(ResourceLocation.fromNamespaceAndPath(namespace, path)).getDefaultInstance();

SimpleTagBuilder tagBuilder = SimpleTagBuilder.of(ResourceLocation.fromNamespaceAndPath("c", "plates/" + rawName.replace("_plate", "")));
SimpleTagBuilder generalTagBuilder = SimpleTagBuilder.of(ResourceLocation.fromNamespaceAndPath("c", "plates"));
tagBuilder.addEntry(item);
generalTagBuilder.addEntry(item);
dynamicPack.addTag(tagBuilder, Registries.ITEM);
dynamicPack.addTag(generalTagBuilder, Registries.ITEM);

Item ingot = inputItem.getItem();
Item ingot = inputItemStack.getItem();
ShapedRecipeBuilder recipeBuilder = ShapedRecipeBuilder.shaped(RecipeCategory.MISC, item)
.pattern("AB")
.pattern("B ")
.define('A', MPItems.HAMMER)
.define('B', ingot)
.unlockedBy("has_item", InventoryChangeTrigger.TriggerInstance.hasItems(ingot));
.define('B', inputItem)
.unlockedBy("has_item", InventoryChangeTrigger.TriggerInstance.hasItems(inputItem));

recipeBuilder.save(new RecipeOutput() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.repository.Pack;
import net.minecraft.server.packs.resources.ResourceManager;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger;

public class DynamicPack {
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DynamicResourcePack {
public static void init() {
ClientAssetsGenerator generator = new ClientAssetsGenerator();
generator.register();
Expand All @@ -29,63 +33,52 @@ protected ClientAssetsGenerator() {
@Override
public void regenerateDynamicAssets(ResourceManager manager) {
BuiltInRegistries.ITEM.forEach((item) -> {
if (item.getDescriptionId().contains(MorePlatesMod.MODID) && item.getDescriptionId().contains("plate")) {
String rawName = item.getDescriptionId()
.replace("item.", "")
.replace(MorePlatesMod.MODID + ":", "")
.replace("moreplates.", "");

ResourceLocation modelLocation = ResourceLocation.fromNamespaceAndPath(MorePlatesMod.MODID, rawName);
ResourceLocation itemId = BuiltInRegistries.ITEM.getKey(item);
if (MorePlatesMod.MODID.equals(itemId.getNamespace()) && itemId.getPath().contains("plate")) {
String rawName = itemId.getPath();
ResourceLocation textureLocation = ResourceLocation.fromNamespaceAndPath(MorePlatesMod.MODID, "item/" + rawName);
String texture = MorePlatesMod.MODID + ":item/" + rawName;
String doubleTexture = MorePlatesMod.MODID + ":item/double_sign";
String hotSignTexture = MorePlatesMod.MODID + ":item/hot_sign";

String ingotId = MPConfig.getIngotFromPlate(MorePlatesMod.MODID + ":" + rawName);
ResourceLocation ingotId = MPConfig.getIngotFromPlate(itemId);
if(ingotId != null){
String[] parts = ingotId.split(":", 2);
String itemNamespace = parts[0];
String itemId = parts[1];
ResourceLocation ingotTexture = ResourceLocation.fromNamespaceAndPath(itemNamespace, "item/" + itemId);
ResourceLocation ingotTexture = ResourceLocation.fromNamespaceAndPath(ingotId.getNamespace(), "item/" + ingotId.getPath());
TextureImage newPlateTexture = TextureUtils.createRecoloredTexture(manager, ingotTexture);
this.dynamicPack.addAndCloseTexture(textureLocation, newPlateTexture);
}

JsonObject model = new JsonObject();
model.addProperty("parent", "item/generated");
JsonObject textures = new JsonObject();
textures.addProperty("layer0", texture);
textures.addProperty("layer0", textureLocation.toString());

// TODO: Condition is always false
if(rawName.contains("double") && !rawName.contains("hot")){
String doubleTexture = MorePlatesMod.MODID + ":item/double_sign";
textures.addProperty("layer1", doubleTexture);
}

// TODO: Condition is always false
if(rawName.contains("hot") && !rawName.contains("double")){
String hotSignTexture = MorePlatesMod.MODID + ":item/hot_sign";
textures.addProperty("layer1", hotSignTexture);
}
model.add("textures", textures);
//Recipes
this.dynamicPack.addItemModel(modelLocation, model);
this.dynamicPack.addItemModel(itemId, model);
}
});
}

@Override
public void addDynamicTranslations(AfterLanguageLoadEvent languageEvent) {
BuiltInRegistries.ITEM.forEach((item) -> {
if (item.getDescriptionId().contains(MorePlatesMod.MODID)) {
String rawName = item.getDescriptionId().replace("item.", "").replace(MorePlatesMod.MODID + ":", "");

String[] words = rawName.replace("moreplates.", "").split("_");
StringBuilder formattedName = new StringBuilder();

for (String word : words) {
formattedName.append(Character.toUpperCase(word.charAt(0)))
.append(word.substring(1)).append(" ");
}
ResourceLocation itemId = BuiltInRegistries.ITEM.getKey(item);
if (MorePlatesMod.MODID.equals(itemId.getNamespace())) {
String rawName = itemId.getPath();

String languageKey = "item." + rawName;
String formattedDisplayName = formattedName.toString().trim();
String languageKey = "item." + MorePlatesMod.MODID + '.' + rawName;
String formattedName = Stream.of(rawName.split("_")).map(StringUtils::capitalize).collect(Collectors.joining(" "));

languageEvent.addEntry(languageKey, formattedDisplayName);
languageEvent.addEntry(languageKey, formattedName);
}
});
}
Expand Down
43 changes: 30 additions & 13 deletions src/main/java/com/portingdeadmods/moreplates/events/MPEvents.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
package com.portingdeadmods.moreplates.events;

import com.portingdeadmods.moreplates.MorePlatesMod;
import com.portingdeadmods.moreplates.config.MPConfig;
import net.minecraft.core.registries.BuiltInRegistries;
import com.portingdeadmods.moreplates.utils.IngotUtil;
import com.portingdeadmods.moreplates.utils.PlateUtil;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.registries.ModifyRegistriesEvent;
import net.neoforged.neoforge.registries.RegisterEvent;
import net.neoforged.neoforge.registries.callback.AddCallback;

import java.util.Set;
import java.util.function.BiConsumer;


@EventBusSubscriber(modid = MorePlatesMod.MODID, bus = EventBusSubscriber.Bus.MOD)
public class MPEvents {
private static class ItemRegistrationCallback implements AddCallback<Item> {
@Override
public void onAdd(Registry<Item> registry, int id, ResourceKey<Item> key, Item value) {
registerPlateFor(key.location(), false, (plateId, plate) -> Registry.register(registry, plateId, plate));
}
}

@SubscribeEvent
public static void modifyRegistriesEvent(ModifyRegistriesEvent event) {
event.getRegistry(Registries.ITEM).addCallback(new ItemRegistrationCallback());
}

@SubscribeEvent
public static void onRegisterItems(RegisterEvent event) {
event.register(Registries.ITEM, helper -> {
BuiltInRegistries.ITEM.stream().forEach(item -> {
ResourceLocation itemID = BuiltInRegistries.ITEM.getKey(item);
if (itemID.getPath().contains("ingot") && item.getDescriptionId().contains("minecraft") && !itemID.getPath().contains("block")) {
String plateName = itemID.getPath().replace("ingot", "plate");
helper.register(ResourceLocation.fromNamespaceAndPath(MorePlatesMod.MODID, plateName),
new Item(new Item.Properties()));
MPConfig.saveIngotPlatePair(itemID.getNamespace() + ":" + itemID.getPath(), MorePlatesMod.MODID + ":" + ResourceLocation.fromNamespaceAndPath(MorePlatesMod.MODID, plateName).getPath());
}
});
});
event.register(Registries.ITEM, helper -> Set.copyOf(event.getRegistry().keySet()).forEach(key -> registerPlateFor(key, true, helper::register)));
}

private static void registerPlateFor(ResourceLocation id, boolean onlyVanilla, BiConsumer<ResourceLocation, Item> regCallback) {
// Avoid accidental recursion when we register our own thing multiple times
if (!MorePlatesMod.MODID.equals(id.getNamespace()) && IngotUtil.isValidIngot(id, onlyVanilla)) {
String ingotType = IngotUtil.getIngotType(id);
PlateUtil.registerPlateIfNotYetDone(ingotType, id, regCallback);
}
}
}

This file was deleted.

Loading

0 comments on commit 5939a49

Please sign in to comment.