Skip to content

Commit

Permalink
Rods,Gears and Plates ! 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Leclowndu93150 committed Oct 6, 2024
1 parent 0cdb6b0 commit 4878fc8
Show file tree
Hide file tree
Showing 9 changed files with 336 additions and 61 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ mod_id=moreplates
# The human-readable display name for the mod.
mod_name=More Plates
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=MIT
mod_license=All Rights Reserved
# The mod version. See https://semver.org/
mod_version=0.1.0
mod_version=1.0.0
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
Expand Down
130 changes: 128 additions & 2 deletions src/main/java/com/portingdeadmods/moreplates/config/MPConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,24 @@

public class MPConfig {
private record IngotPlatePair(ResourceLocation ingot, ResourceLocation plate) {}
private record IngotGearPair(ResourceLocation ingot, ResourceLocation gear) {}
private record IngotRodPair(ResourceLocation ingot, ResourceLocation rod) {}

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 ingotGearConfigFile = new File("config/moreplates/ingot_gear_pairs.json");
private static final File ingotRodConfigFile = new File("config/moreplates/ingot_rod_pairs.json");
private static final File generatorConfigFile = new File("config/moreplates/moreplates-generator.json");
private static final List<String> generatorValues = new ArrayList<>();
private static final Set<IngotPlatePair> registeredIngotPlatePairs = new HashSet<>();
private static final Set<IngotGearPair> registeredIngotGearPairs = new HashSet<>();
private static final Set<IngotRodPair> registeredIngotRodPairs = new HashSet<>();
private static final Map<ResourceLocation, ResourceLocation> ingotToPlateMap = new HashMap<>();
private static final Map<ResourceLocation, ResourceLocation> plateToIngotMap = new HashMap<>();
private static final Map<ResourceLocation, ResourceLocation> ingotToGearMap = new HashMap<>();
private static final Map<ResourceLocation, ResourceLocation> gearToIngotMap = new HashMap<>();
private static final Map<ResourceLocation, ResourceLocation> ingotToRodMap = new HashMap<>();
private static final Map<ResourceLocation, ResourceLocation> rodToIngotMap = new HashMap<>();

public static void loadConfig() {
FMLLoader.getGamePath().resolve("config").resolve(MorePlatesMod.MODID).toFile().mkdirs();
Expand All @@ -48,6 +58,52 @@ public static void loadConfig() {
}
}

public static void loadIngotGearConfig() {
FMLLoader.getGamePath().resolve("config").resolve(MorePlatesMod.MODID).toFile().mkdirs();
if (ingotGearConfigFile.exists()) {
try (FileReader reader = new FileReader(ingotGearConfigFile)) {
JsonObject json = gson.fromJson(reader, JsonObject.class);
JsonArray pairs = json.getAsJsonArray("ingot_gear_pairs");

if (pairs != null) {
for (var entry : pairs) {
JsonObject pair = entry.getAsJsonObject();
ResourceLocation ingot = ResourceLocation.parse(pair.get("ingot").getAsString());
ResourceLocation gear = ResourceLocation.parse(pair.get("gear").getAsString());
registeredIngotGearPairs.add(new IngotGearPair(ingot, gear));
ingotToGearMap.put(ingot, gear);
gearToIngotMap.put(gear, ingot);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static void loadIngotRodConfig() {
FMLLoader.getGamePath().resolve("config").resolve(MorePlatesMod.MODID).toFile().mkdirs();
if (ingotRodConfigFile.exists()) {
try (FileReader reader = new FileReader(ingotRodConfigFile)) {
JsonObject json = gson.fromJson(reader, JsonObject.class);
JsonArray pairs = json.getAsJsonArray("ingot_rod_pairs");

if (pairs != null) {
for (var entry : pairs) {
JsonObject pair = entry.getAsJsonObject();
ResourceLocation ingot = ResourceLocation.parse(pair.get("ingot").getAsString());
ResourceLocation rod = ResourceLocation.parse(pair.get("rod").getAsString());
registeredIngotRodPairs.add(new IngotRodPair(ingot, rod));
ingotToRodMap.put(ingot, rod);
rodToIngotMap.put(rod, ingot);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static void loadGeneratorConfig() {
FMLLoader.getGamePath().resolve("config").resolve(MorePlatesMod.MODID).toFile().mkdirs();

Expand Down Expand Up @@ -108,17 +164,87 @@ public static void saveIngotPlatePair(ResourceLocation ingotId, ResourceLocation
}
}

public static void saveIngotGearPair(ResourceLocation ingotId, ResourceLocation gearId) {
IngotGearPair pairKey = new IngotGearPair(ingotId, gearId);

if (!registeredIngotGearPairs.contains(pairKey)) {
registeredIngotGearPairs.add(pairKey);
ingotToGearMap.put(ingotId, gearId);
gearToIngotMap.put(gearId, ingotId);

JsonObject json = new JsonObject();
JsonArray pairsArray = new JsonArray();

for (IngotGearPair pair : registeredIngotGearPairs) {
JsonObject pairObject = new JsonObject();
pairObject.addProperty("ingot", pair.ingot().toString());
pairObject.addProperty("gear", pair.gear().toString());
pairsArray.add(pairObject);
}

json.add("ingot_gear_pairs", pairsArray);

try (FileWriter writer = new FileWriter(ingotGearConfigFile)) {
gson.toJson(json, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static void saveIngotRodPair(ResourceLocation ingotId, ResourceLocation rodId) {
IngotRodPair pairKey = new IngotRodPair(ingotId, rodId);

if (!registeredIngotRodPairs.contains(pairKey)) {
registeredIngotRodPairs.add(pairKey);
ingotToRodMap.put(ingotId, rodId);
rodToIngotMap.put(rodId, ingotId);

JsonObject json = new JsonObject();
JsonArray pairsArray = new JsonArray();

for (IngotRodPair pair : registeredIngotRodPairs) {
JsonObject pairObject = new JsonObject();
pairObject.addProperty("ingot", pair.ingot().toString());
pairObject.addProperty("rod", pair.rod().toString());
pairsArray.add(pairObject);
}

json.add("ingot_rod_pairs", pairsArray);

try (FileWriter writer = new FileWriter(ingotRodConfigFile)) {
gson.toJson(json, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static List<String> getGeneratorValues() {
return generatorValues;
}

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

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

public static ResourceLocation getGearFromIngot(ResourceLocation ingotId) {
return ingotToGearMap.get(ingotId);
}

public static ResourceLocation getIngotFromGear(ResourceLocation gearId) {
return gearToIngotMap.get(gearId);
}

public static ResourceLocation getRodFromIngot(ResourceLocation ingotId) {
return ingotToRodMap.get(ingotId);
}

public static ResourceLocation getIngotFromRod(ResourceLocation rodId) {
return rodToIngotMap.get(rodId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,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 All @@ -37,8 +36,6 @@ public DynamicDataPack() {
this.dynamicPack.setGenerateDebugResources(PlatHelper.isDev());
}



@Override
public Logger getLogger() {
return MorePlatesMod.LOGGER;
Expand All @@ -53,11 +50,12 @@ public boolean dependsOnLoadedPacks() {
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;
if (inputIngot == null) return;

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

Expand Down Expand Up @@ -87,10 +85,80 @@ public void accept(@NotNull ResourceLocation resourceLocation, @NotNull Recipe<?
}
});
}

// 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);

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

ShapedRecipeBuilder recipeBuilder = ShapedRecipeBuilder.shaped(RecipeCategory.MISC, item)
.pattern(" I ")
.pattern("I I")
.pattern(" I ")
.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);
}
});
}

// 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);

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

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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,55 @@ protected ClientAssetsGenerator() {
public void regenerateDynamicAssets(ResourceManager manager) {
BuiltInRegistries.ITEM.forEach((item) -> {
ResourceLocation itemId = BuiltInRegistries.ITEM.getKey(item);
if (MorePlatesMod.MODID.equals(itemId.getNamespace()) && itemId.getPath().contains("plate")) {
if (MorePlatesMod.MODID.equals(itemId.getNamespace()) && (itemId.getPath().contains("plate") || itemId.getPath().contains("gear") || itemId.getPath().contains("rod"))) {
String rawName = itemId.getPath();
ResourceLocation textureLocation = ResourceLocation.fromNamespaceAndPath(MorePlatesMod.MODID, "item/" + rawName);

ResourceLocation ingotId = MPConfig.getIngotFromPlate(itemId);
if(ingotId != null){
ResourceLocation ingotTexture = ResourceLocation.fromNamespaceAndPath(ingotId.getNamespace(), "item/" + ingotId.getPath());
TextureImage newPlateTexture = TextureUtils.createRecoloredTexture(manager, ingotTexture);
this.dynamicPack.addAndCloseTexture(textureLocation, newPlateTexture);
if (itemId.getPath().contains("plate")) {
ResourceLocation ingotId = MPConfig.getIngotFromPlate(itemId);
if (ingotId != null) {
ResourceLocation ingotTexture = ResourceLocation.fromNamespaceAndPath(ingotId.getNamespace(), "item/" + ingotId.getPath());
ResourceLocation plateTexture = ResourceLocation.fromNamespaceAndPath(MorePlatesMod.MODID,"item/base_plate");
TextureImage newPlateTexture = TextureUtils.createRecoloredTexture(manager, plateTexture, ingotTexture);
this.dynamicPack.addAndCloseTexture(textureLocation, newPlateTexture);
}
}

if (itemId.getPath().contains("gear")) {
ResourceLocation ingotId = MPConfig.getIngotFromGear(itemId);
if (ingotId != null) {
ResourceLocation ingotTexture = ResourceLocation.fromNamespaceAndPath(ingotId.getNamespace(), "item/" + ingotId.getPath());
ResourceLocation gearTexture = ResourceLocation.fromNamespaceAndPath(MorePlatesMod.MODID,"item/base_gear");
TextureImage newGearTexture = TextureUtils.createRecoloredTexture(manager, gearTexture, ingotTexture);
this.dynamicPack.addAndCloseTexture(textureLocation, newGearTexture);
}
}

if (itemId.getPath().contains("rod")) {
ResourceLocation ingotId = MPConfig.getIngotFromRod(itemId);
if (ingotId != null) {
ResourceLocation ingotTexture = ResourceLocation.fromNamespaceAndPath(ingotId.getNamespace(), "item/" + ingotId.getPath());
ResourceLocation rodTexture = ResourceLocation.fromNamespaceAndPath(MorePlatesMod.MODID,"item/base_rod");
TextureImage newRodTexture = TextureUtils.createRecoloredTexture(manager, rodTexture, ingotTexture);
this.dynamicPack.addAndCloseTexture(textureLocation, newRodTexture);
}
}

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

// TODO: Condition is always false
if(rawName.contains("double") && !rawName.contains("hot")){
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")){
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(itemId, model);
}
});
Expand All @@ -83,8 +103,6 @@ public void addDynamicTranslations(AfterLanguageLoadEvent languageEvent) {
});
}



@Override
public Logger getLogger() {
return MorePlatesMod.LOGGER;
Expand Down
Loading

0 comments on commit 4878fc8

Please sign in to comment.