Files
src
build.gradle
gradle.properties
settings.gradle
Code
12 644
src/main/java/com/kodari/megaores/MegaOres.java
package com.kodari.megaores;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.biome.v1.BiomeModifications;
import net.fabricmc.fabric.api.biome.v1.BiomeSelectors;
import net.fabricmc.fabric.api.creativetab.v1.CreativeModeTabEvents;
import net.minecraft.core.Registry;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.tags.BlockTags;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ToolMaterial;
import net.minecraft.world.item.component.ItemLore;
import net.minecraft.world.item.equipment.ArmorMaterial;
import net.minecraft.world.item.equipment.ArmorType;
import net.minecraft.world.item.equipment.EquipmentAssets;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.levelgen.GenerationStep;
public final class MegaOres implements ModInitializer {
private static final String MOD_ID = "megaores";
private static final List<OreContent> CONTENT = new ArrayList<>();
private static Item fieldGuide;
@Override
public void onInitialize() {
registerContent();
registerCreativeTabs();
registerWorldGeneration();
}
private static void registerContent() {
List<String[]> deposits = readDeposits();
validateDeposits(deposits);
Map<String, Integer> dimensionIndices = new HashMap<>();
for (String[] row : deposits) {
String dimension = row[0];
String material = row[1];
boolean handHarvest = row[2].equals("dirt");
String prefix = switch (dimension) {
case "overworld" -> "ow";
case "nether" -> "nether";
case "end" -> "end";
default -> throw new IllegalStateException("Unknown ore dimension: " + dimension);
};
int tier = switch (dimension) {
case "overworld" -> 0;
case "nether" -> 1;
default -> 2;
};
int localIndex = dimensionIndices.merge(dimension, 1, Integer::sum) - 1;
int progressionBand = localIndex / 10;
String materialId = prefix + "_" + material;
String blockId = materialId + "_ore";
String rawId = "raw_" + materialId;
String ingotId = "ingot_" + materialId;
Identifier blockIdentifier = id(blockId);
ResourceKey<Block> blockKey = ResourceKey.create(Registries.BLOCK, blockIdentifier);
BlockBehaviour.Properties blockProperties = BlockBehaviour.Properties.of()
.strength(handHarvest ? 0.6F : dimension.equals("overworld") ? 3.0F : 4.0F, handHarvest ? 0.6F : 4.0F)
.setId(blockKey);
if (!handHarvest) {
blockProperties.requiresCorrectToolForDrops();
}
Block block = Registry.register(BuiltInRegistries.BLOCK, blockIdentifier, new Block(blockProperties));
Item blockItem = Registry.register(BuiltInRegistries.ITEM, blockIdentifier,
new BlockItem(block, new Item.Properties().setId(ResourceKey.create(Registries.ITEM, blockIdentifier))));
Item rawItem = registerItem(rawId, new Item.Properties());
Item ingot = registerItem(ingotId, new Item.Properties());
TagKey<Item> repairTag = TagKey.create(Registries.ITEM, id("tool_materials/" + materialId));
TagKey<Block> incorrectBlocks = switch (tier) {
case 0 -> BlockTags.INCORRECT_FOR_STONE_TOOL;
case 1 -> BlockTags.INCORRECT_FOR_IRON_TOOL;
default -> BlockTags.INCORRECT_FOR_DIAMOND_TOOL;
};
ToolMaterial toolMaterial = new ToolMaterial(incorrectBlocks, 400 + tier * 350 + progressionBand * 100,
5.5F + tier * 0.9F + progressionBand * 0.35F,
1.0F + tier * 0.8F + progressionBand * 0.2F, 10 + tier * 3 + progressionBand, repairTag);
Item pickaxe = registerItem(materialId + "_pickaxe",
new Item.Properties().pickaxe(toolMaterial, 1.0F, -2.8F));
Item sword = registerItem(materialId + "_sword",
new Item.Properties().sword(toolMaterial, 3.0F, -2.4F));
int[][] defense = {{2, 4, 5, 2}, {2, 5, 6, 2}, {3, 6, 8, 3}};
int[] tierDefense = defense[tier].clone();
tierDefense[2] += progressionBand / 2;
ArmorMaterial armorMaterial = new ArmorMaterial(15 + tier * 9 + progressionBand * 2,
Map.of(ArmorType.BOOTS, tierDefense[0], ArmorType.LEGGINGS, tierDefense[1],
ArmorType.CHESTPLATE, tierDefense[2], ArmorType.HELMET, tierDefense[3], ArmorType.BODY, 0),
10 + tier * 5 + progressionBand, SoundEvents.ARMOR_EQUIP_DIAMOND,
tier == 2 ? 2.0F + progressionBand * 0.2F : tier == 1 ? 1.0F + progressionBand * 0.15F : progressionBand * 0.05F,
0.0F, repairTag, EquipmentAssets.DIAMOND);
Item helmet = registerItem(materialId + "_helm",
new Item.Properties().humanoidArmor(armorMaterial, ArmorType.HELMET));
Item chestplate = registerItem(materialId + "_chestplate",
new Item.Properties().humanoidArmor(armorMaterial, ArmorType.CHESTPLATE));
Item leggings = registerItem(materialId + "_leggings",
new Item.Properties().humanoidArmor(armorMaterial, ArmorType.LEGGINGS));
Item boots = registerItem(materialId + "_boots",
new Item.Properties().humanoidArmor(armorMaterial, ArmorType.BOOTS));
CONTENT.add(new OreContent(dimension, blockId, blockItem, rawItem, ingot, pickaxe, sword,
List.of(helmet, chestplate, leggings, boots)));
}
fieldGuide = registerItem("field_guide", new Item.Properties().component(DataComponents.LORE,
new ItemLore(List.of(Component.translatable("item.megaores.field_guide.lore.1"),
Component.translatable("item.megaores.field_guide.lore.2"),
Component.translatable("item.megaores.field_guide.lore.3"),
Component.translatable("item.megaores.field_guide.lore.4"),
Component.translatable("item.megaores.field_guide.lore.5")))));
}
private static void validateDeposits(List<String[]> deposits) {
if (deposits.size() != 150) {
throw new IllegalStateException("Mega Ores must define exactly 150 deposits.");
}
Map<String, Integer> dimensionCounts = new HashMap<>();
Set<String> blockIds = new HashSet<>();
for (int index = 0; index < deposits.size(); index++) {
String[] row = deposits.get(index);
if (row.length != 3) {
throw new IllegalStateException("Invalid deposit row " + (index + 1) + ": expected dimension|material|host.");
}
String dimension = row[0];
String material = row[1];
String host = row[2];
String prefix = switch (dimension) {
case "overworld" -> "ow";
case "nether" -> "nether";
case "end" -> "end";
default -> throw new IllegalStateException("Unknown ore dimension: " + dimension);
};
boolean validHost = switch (dimension) {
case "overworld" -> host.equals("dirt") || host.equals("stone");
case "nether" -> host.equals("netherrack");
case "end" -> host.equals("end_stone");
default -> false;
};
if (!material.matches("[a-z][a-z0-9]*(?:_[a-z0-9]+)*") || !validHost) {
throw new IllegalStateException("Invalid deposit row " + (index + 1) + ".");
}
String blockId = prefix + "_" + material + "_ore";
if (!blockIds.add(blockId)) {
throw new IllegalStateException("Duplicate deposit ID: " + blockId);
}
dimensionCounts.merge(dimension, 1, Integer::sum);
}
if (!dimensionCounts.equals(Map.of("overworld", 50, "nether", 50, "end", 50))) {
throw new IllegalStateException("Mega Ores requires exactly 50 deposits in each vanilla dimension.");
}
}
private static void registerCreativeTabs() {
CreativeModeTabEvents.modifyOutputEvent(tabKey("natural_blocks")).register(output ->
CONTENT.forEach(content -> output.accept(content.blockItem())));
CreativeModeTabEvents.modifyOutputEvent(tabKey("ingredients")).register(output -> {
CONTENT.forEach(content -> {
output.accept(content.rawItem());
output.accept(content.ingot());
});
output.accept(fieldGuide);
});
CreativeModeTabEvents.modifyOutputEvent(tabKey("tools_and_utilities")).register(output ->
CONTENT.forEach(content -> output.accept(content.pickaxe())));
CreativeModeTabEvents.modifyOutputEvent(tabKey("combat")).register(output -> CONTENT.forEach(content -> {
output.accept(content.sword());
content.armor().forEach(output::accept);
}));
}
private static void registerWorldGeneration() {
for (OreContent content : CONTENT) {
ResourceKey<net.minecraft.world.level.levelgen.placement.PlacedFeature> placedFeature =
ResourceKey.create(Registries.PLACED_FEATURE, id(content.blockId()));