Skip to content

Commit

Permalink
Finish Rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
UnRealDinnerbone committed Nov 7, 2023
1 parent 60b11f1 commit 3a657e1
Show file tree
Hide file tree
Showing 39 changed files with 589 additions and 882 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### 3.2.2
### 3.3.0

- Blacklist Enlightened End mod
- Rewrite mod to better support modded / custom ores
- Added /jamd reload to reload the config
- Added configs jamd/overworld.json, jamd/nether.json, jamd/end.json
- Configs now completely controls the world generation
- Added support for ore multipliers
- Added Mekanism support
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"type": "minecraft:crafting_shaped",
"category": "misc",
"key": {
"O": {
"item": "minecraft:end_stone"
},
"P": {
"item": "minecraft:diamond_pickaxe"
}
},
"pattern": [
"OOO",
"OPO",
"OOO"
],
"result": {
"item": "jamd:end_portal"
},
"show_notification": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"type": "minecraft:crafting_shaped",
"category": "misc",
"key": {
"O": {
"item": "minecraft:nether_bricks"
},
"P": {
"item": "minecraft:netherite_pickaxe"
}
},
"pattern": [
"OOO",
"OPO",
"OOO"
],
"result": {
"item": "jamd:nether_portal"
},
"show_notification": true
}
21 changes: 21 additions & 0 deletions common/src/generated/resources/data/jamd/recipes/portal_block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"type": "minecraft:crafting_shaped",
"category": "misc",
"key": {
"O": {
"item": "minecraft:obsidian"
},
"P": {
"item": "minecraft:diamond_pickaxe"
}
},
"pattern": [
"OOO",
"OPO",
"OOO"
],
"result": {
"item": "jamd:portal_block"
},
"show_notification": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,29 @@
[],
[],
[],
[]
[
"minecraft:ore_coal_upper",
"minecraft:ore_coal_lower",
"minecraft:ore_iron_upper",
"minecraft:ore_iron_middle",
"minecraft:ore_iron_small",
"minecraft:ore_gold",
"minecraft:ore_gold_lower",
"minecraft:ore_redstone",
"minecraft:ore_redstone_lower",
"minecraft:ore_diamond",
"minecraft:ore_diamond_large",
"minecraft:ore_diamond_buried",
"minecraft:ore_lapis",
"minecraft:ore_lapis_buried",
"minecraft:ore_copper",
"minecraft:underwater_magma",
"minecraft:ore_emerald",
"minecraft:ore_copper_large",
"minecraft:ore_tuff"
]
],
"has_precipitation": true,
"has_precipitation": false,
"spawn_costs": {},
"spawners": {
"ambient": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

import java.util.List;

public record ConfigCodec(List<OresCodec> ores) {
public record ConfigCodec(int oreMultiplier, List<OresCodec> ores) {
public static final Codec<ConfigCodec> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.INT.fieldOf("oreMultiplier").forGetter(ConfigCodec::oreMultiplier),
Codec.list(OresCodec.CODEC).fieldOf("ores").forGetter(ConfigCodec::ores)
).apply(instance, ConfigCodec::new));
}
82 changes: 14 additions & 68 deletions common/src/main/java/com/unrealdinnerbone/jamd/JAMD.java
Original file line number Diff line number Diff line change
@@ -1,89 +1,35 @@
package com.unrealdinnerbone.jamd;

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.JsonOps;
import com.unrealdinnerbone.jamd.util.Transformers;
import com.mojang.logging.LogUtils;
import com.unrealdinnerbone.jamd.api.FeatureTypeRegistry;
import com.unrealdinnerbone.jamd.compact.MinecraftOreCompact;
import com.unrealdinnerbone.jamd.util.OreRegistry;
import com.unrealdinnerbone.trenzalore.api.platform.Services;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.levelgen.placement.PlacedFeature;
import net.minecraft.server.MinecraftServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;

public class JAMD {

private static final Logger LOGGER = LoggerFactory.getLogger(JAMD.class);

private static final Logger LOGGER = LogUtils.getLogger();
public static final Path CONFIG_FOLDER = Services.PLATFORM.getConfigPath().resolve("jamd");
public static final String MOD_ID = "jamd";
public static final Map<ResourceKey<Biome>, Map<String, PlacedFeature>> REGISTERED_FEATURES = new HashMap<>();


private static final List<String> DIM_TYPES = Arrays.asList("mining", "nether", "end");

public static void init() {
try {
createFiles();
}catch (IOException e) {
LOGGER.error("Failed to create config files", e);
return;
}
for (String dimType : DIM_TYPES) {
try {
String jsonString = Files.readString(CONFIG_FOLDER.resolve(dimType + ".json"));
JsonElement parse = new JsonParser().parse(jsonString);
DataResult<ConfigCodec> data = ConfigCodec.CODEC.parse(JsonOps.INSTANCE, parse);
if(data.error().isPresent()) {
LOGGER.error("Failed to parse config: {}", data.error().get().message());
return;
}
Optional<ConfigCodec> result = data.result();
if(result.isPresent()) {
Map<String, PlacedFeature> placedFeatureMap = new HashMap<>();
ConfigCodec configCodec = result.get();
for (OresCodec ore : configCodec.ores()) {
if(placedFeatureMap.containsKey(ore.id())) {
LOGGER.error("Duplicate ore: {} found in config {}", ore.id(), dimType);
continue;
}
placedFeatureMap.put(ore.id(), Transformers.fromConfigCodec(ore));
LOGGER.debug("Registered ore: {} for dim type: {}", ore.id(), dimType);
}
REGISTERED_FEATURES.put(ResourceKey.create(Registries.BIOME, new ResourceLocation(MOD_ID, dimType)), placedFeatureMap);
}
}catch (IOException e) {
LOGGER.error("Failed to load config for dim type: {}", dimType, e);
}

}
FeatureTypeRegistry.register("minecraft", MinecraftOreCompact::new);
}

private static void createFiles() throws IOException {
if(!Files.exists(CONFIG_FOLDER)) {
Files.createDirectories(CONFIG_FOLDER);
}
for (String dimType : DIM_TYPES) {
Path path = CONFIG_FOLDER.resolve(dimType + ".json");
if(!Files.exists(path)) {
InputStream defaultConfig = JAMD.class.getResourceAsStream("/defaults/" + dimType + ".json");
if(defaultConfig != null) {
Files.copy(defaultConfig, path);
}else {
throw new IOException("Failed to find default config for: " + dimType);
}
public static void onServerStart(MinecraftServer server) {
OreRegistry.REGISTERED_FEATURES.clear();
for (WorldType type : WorldType.TYPES) {
try {
type.exportIfNotExist(server);
} catch (IOException e) {
LOGGER.error("Failed to export config for world type: {}", type.getName(), e);
}
}
}


}
40 changes: 9 additions & 31 deletions common/src/main/java/com/unrealdinnerbone/jamd/JAMDRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.BiomeTags;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.Level;
Expand All @@ -38,24 +40,21 @@ public class JAMDRegistry implements IRegistry {

public static final RegistryEntry<Codec<? extends ChunkGenerator>> CUSTOM_FLAT_LEVEL_SOURCE = CHUNK_GENERATORS.register("mining", () -> CustomFlatLevelSource.CODEC);

public static final RegistrySet OVERWORLD = RegistrySet.of("portal_block", "portal", OverworldPortalBlock::new, OverworldBlockEntity::new);
public static final RegistrySet NETHER = RegistrySet.of("nether_portal", NetherPortalBlock::new, NetherBlockEntity::new);
public static final WorldType OVERWORLD = of("mining", "portal_block", "portal", OverworldPortalBlock::new, OverworldBlockEntity::new, BiomeTags.IS_OVERWORLD);

public static final RegistrySet END = RegistrySet.of("end_portal", EndPortalBlock::new, EndBlockEntity::new);
public static final WorldType NETHER = of("nether", "nether_portal", "nether_portal", NetherPortalBlock::new, NetherBlockEntity::new, BiomeTags.IS_NETHER);

public static final WorldType END = of("end", "end_portal", "end_portal", EndPortalBlock::new, EndBlockEntity::new, BiomeTags.IS_END);

public static class Keys {
public static final KeySet OVERWORLD = KeySet.of(new ResourceLocation(JAMD.MOD_ID, "mining"));

public static final KeySet NETHER = KeySet.of(new ResourceLocation(JAMD.MOD_ID, "nether"));

public static final KeySet END = KeySet.of(new ResourceLocation(JAMD.MOD_ID, "end"));

private static WorldType of(String name, String blockName, String tileName, Supplier<Block> blockSupplier, BiFunction<BlockPos, BlockState, PortalTileEntity> tileSupplier, TagKey<Biome> biomeTagKey) {
RegistryEntry<Block> block = BLOCKS.register(blockName, blockSupplier);
return new WorldType(name, block, ITEMS.register(blockName, () -> new BlockItem(block.get(), new Item.Properties())), TILES.register(tileName, () -> Regeneration.createBlockEntityType(tileSupplier, block.get())), biomeTagKey);
}

@Override
public void afterRegistered() {
Regeneration.addItemsToCreateTab(CreativeTabs.FUNCTIONAL_BLOCKS, List.of(OVERWORLD.item(), NETHER.item(), END.item()));
Regeneration.addItemsToCreateTab(CreativeTabs.FUNCTIONAL_BLOCKS, List.of(OVERWORLD.getItem(), NETHER.getItem(), END.getItem()));
}

@Override
Expand All @@ -68,26 +67,5 @@ public String getModID() {
return JAMD.MOD_ID;
}

public record RegistrySet(RegistryEntry<Block> block, RegistryEntry<BlockItem> item, RegistryEntry<BlockEntityType<PortalTileEntity>> blockEntity) {

private static RegistrySet of(String name, Supplier<Block> blockSupplier, BiFunction<BlockPos, BlockState, PortalTileEntity> tileSupplier) {
return of(name, name, blockSupplier, tileSupplier);
}

@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "4.0.0")
private static RegistrySet of(String blockName, String tileName, Supplier<Block> blockSupplier, BiFunction<BlockPos, BlockState, PortalTileEntity> tileSupplier) {
RegistryEntry<Block> block = BLOCKS.register(blockName, blockSupplier);
RegistryEntry<BlockItem> itemBlock = ITEMS.register(blockName, () -> new BlockItem(block.get(), new Item.Properties()));
RegistryEntry<BlockEntityType<PortalTileEntity>> tile = TILES.register(tileName, () -> Regeneration.createBlockEntityType(tileSupplier, block.get()));
return new RegistrySet(block, itemBlock, tile);
}
}

public record KeySet(ResourceKey<Level> level, ResourceKey<DimensionType> dimensionType, ResourceKey<Biome> biome) {

private static KeySet of(ResourceLocation id) {
return new KeySet(ResourceKey.create(Registries.DIMENSION, id), ResourceKey.create(Registries.DIMENSION_TYPE, id), ResourceKey.create(Registries.BIOME, id));
}
}
}
39 changes: 4 additions & 35 deletions common/src/main/java/com/unrealdinnerbone/jamd/OresCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,18 @@
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration;
import net.minecraft.world.level.levelgen.placement.PlacementModifier;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Optional;

public record OresCodec(
String id,
int oreSize,
int oreAmount,
float discardChance,
Optional<Integer> minY,
Optional<Integer> maxY,
PlacementType placementType,
List<OreConfiguration.TargetBlockState> targets) {
public record OresCodec(int oreSize, float discardChance, List<PlacementModifier> modifiers, List<OreConfiguration.TargetBlockState> targets) {
public static final Codec<OresCodec> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.STRING.fieldOf("id").forGetter(OresCodec::id),
Codec.INT.fieldOf("ore_size").forGetter(OresCodec::oreSize),
Codec.INT.fieldOf("ore_amount").forGetter(OresCodec::oreAmount),
Codec.FLOAT.fieldOf("discard_on_air_chance").forGetter(OresCodec::discardChance),
Codec.INT.optionalFieldOf("min_y").forGetter(OresCodec::minY),
Codec.INT.optionalFieldOf("max_y").forGetter(OresCodec::maxY),
PlacementType.CODEC.fieldOf("placement_type").forGetter(OresCodec::placementType),
Codec.list(PlacementModifier.CODEC).fieldOf("modifiers").forGetter(OresCodec::modifiers),
Codec.list(OreConfiguration.TargetBlockState.CODEC).fieldOf("targets").forGetter(OresCodec::targets)
).apply(instance, OresCodec::new));


public static enum PlacementType implements StringRepresentable {
UNIFORM("uniform"),
TRIANGLE("triangle"),
;

public static final Codec<PlacementType> CODEC = StringRepresentable.fromEnum(PlacementType::values);
private final String name;

PlacementType(String name) {
this.name = name;
}


@Override
@NotNull
public String getSerializedName() {
return name;
}
}
}
}
Loading

0 comments on commit 3a657e1

Please sign in to comment.