Skip to content

refactor: Config format v1.1 #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: root
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5ec7870
refactor: [WIP] Merge custom gen with basic gen
null2264 Apr 7, 2025
81e9ed2
refactor: Rename notNullOr -> elvis
null2264 Apr 7, 2025
48cf093
refactor: Use 'neighbours' to hold modifier
null2264 Apr 8, 2025
c1253ce
sync: Merge branch 'root' into dev/config-v1.1
null2264 Apr 9, 2025
5f29dc8
feat: Add Builder API for WeightedBlock to make constructing result a…
null2264 Apr 9, 2025
2774acf
fix: Basalt's default modifier should be soul soil
null2264 Apr 9, 2025
e8c596f
fix: Don't mix up the formatting
null2264 Apr 9, 2025
54b5c8c
fix: Not sure why this still here, should already been replaced by Ge…
null2264 Apr 9, 2025
842cbbd
chore: Deprecate CustomGen class
null2264 Apr 9, 2025
dd40284
test: Auto generate test config if gametest is enabled
null2264 Apr 9, 2025
8472a6c
fix: "since" on `@Deprecated` annotation didn't exist on Java 8
null2264 Apr 10, 2025
ff8319c
fix: "cannot find symbol"
null2264 Apr 10, 2025
4343d72
refactor: Reduce repetition trying to load/reload config
null2264 Apr 10, 2025
55e6ae5
fix: Backport some Java 16 API to Java 8
null2264 Apr 10, 2025
dd71342
fix(manifold): Use the full path
null2264 Apr 10, 2025
0007a0f
fix: The built-in parseBoolean is not as good as I had hoped for...
null2264 Apr 10, 2025
de665b5
fix: Fix example comment
null2264 Apr 10, 2025
f9e8305
fix(plugin/builtin): Accidentally copy the entire thing instead of sp…
null2264 Apr 10, 2025
71b0a1e
docs: Primer [skip ci]
null2264 Apr 10, 2025
e5ebc1d
docs: Typo [skip ci]
null2264 Apr 10, 2025
3405b27
sync: Merge branch 'root' into dev/config-v1.1
null2264 Apr 11, 2025
4560313
fix: Warn the user about the change
null2264 Apr 11, 2025
e2e256a
fix(config): Add link to migration documentation
null2264 Apr 21, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions .github/primers/config/1.1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Config Format v1.1

To make it less confusing for users to configure custom generator, I decided to merge `customGen` with basic generators (`cobbleGen`/`stoneGen`/`basaltGen`). This format is a partial port of [GH-53](https://github.com/null2264/CobbleGen/issues/53).

> [!NOTE]
> If you still want to use format v1.0, you can set `formatVersion` to `1.0` (do note that it'll be removed completely at some point):
>
> ```json5
> {
> "formatVersion": "1.0",
> "cobbleGen": [
> ...
> ],
> "customGen": {
> ...
> }
> }
> ```

## v1.0

Previously in v1.0, you'd define custom generators inside `customGen`, but most people got confused about the `cobbleGen`/`stoneGen`/`basaltGen` and thought it's a name for the custom generator.

```json5
{
"cobbleGen": [
{
"id": "minecraft:cobblestone",
"weight": 100.0
}
],
"customGen": {
// This is the most confusing part for most people
"cobbleGen": {
"minecraft:bedrock": [
{
"id": "#minecraft:ores",
"weight": 100.0
}
]
}
}
}
```

## v1.1

Now in v1.1, all you need to do to define a custom generator is by adding `modifier` value inside the "result block", similar to how you set the generation's `weight`.

```json5
{
"cobbleGen": [
{
"id": "minecraft:cobblestone",
"weight": 100.0
},
{
"id": "#minecraft:ores",
"weight": 100.0,
"modifier": "minecraft:bedrock"
}
]
}
```
50 changes: 0 additions & 50 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,56 +134,6 @@ jobs:
pauseOnLostFocus:false
EOF

cat <<EOF >> run/test-${{ matrix.mc }}-${{ matrix.loader }}/config/cobblegen.json5
{
"formatVersion": "1.0",
"cobbleGen": [
{
"id": "minecraft:bedrock",
"weight": 100.0,
},
],
"stoneGen": [
{
"id": "minecraft:bedrock",
"weight": 100.0,
},
],
"basaltGen": [
{
"id": "minecraft:bedrock",
"weight": 100.0,
},
],
"customGen": {
"cobbleGen": {
"minecraft:bedrock": [
{
"id": "minecraft:bedrock",
"weight": 100.0,
},
],
},
"stoneGen": {
"minecraft:bedrock": [
{
"id": "minecraft:bedrock",
"weight": 100.0,
},
],
},
"basaltGen": {
"minecraft:bedrock": [
{
"id": "minecraft:bedrock",
"weight": 100.0,
},
],
},
},
}
EOF

json="$(cat ./headlessmc.json)"
url="$(echo $json | jq -r '.assets[].browser_download_url' | grep .jar | grep launcher | grep -v jfx | grep -v wrapper)"
curl -Ls -o "headlessmc-launcher.jar" $url
Expand Down
41 changes: 25 additions & 16 deletions cobblegen/src/main/java/io/github/null2264/cobblegen/CobbleGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import io.github.null2264.cobblegen.compat.LoaderCompat;
import io.github.null2264.cobblegen.compat.TextCompat;
import io.github.null2264.cobblegen.data.config.Config;
import io.github.null2264.cobblegen.data.config.ConfigData;
import io.github.null2264.cobblegen.data.config.ConfigMetaData;
import io.github.null2264.cobblegen.data.model.CGRegistry;
Expand All @@ -14,7 +15,6 @@
import java.io.File;
import java.nio.file.Path;

import static io.github.null2264.cobblegen.data.config.ConfigHelper.loadConfig;
import static io.github.null2264.cobblegen.util.Constants.OP_LEVEL_GAMEMASTERS;

#if FORGE
Expand All @@ -39,12 +39,13 @@ public class CobbleGen
private static final Path configPath = LoaderCompat.getConfigDir();
private static final File configFile = new File(configPath + File.separator + MOD_ID + ".json5");
private static final File metaConfigFile = new File(configPath + File.separator + MOD_ID + "-meta.json5");
private static final Config.Factory<ConfigMetaData> metaConfigFactory = new ConfigMetaData.Factory();
@ApiStatus.Internal
public static ConfigMetaData META_CONFIG = loadConfig(false, metaConfigFile, null, new ConfigMetaData(), ConfigMetaData.class);
public static ConfigMetaData META_CONFIG = metaConfigFactory.load(metaConfigFile);

public CobbleGen() {
// Force config to be generated when loading up the game instead of having to load a world
loadConfig(false, configFile, null, ConfigData.defaultConfig(), ConfigData.class);
new ConfigData.Factory().load(configFile);
#if FORGE && MC>=11801 && MC<12105
// I was gonna do RegisterGameTestsEvent like a normal person, but there's a check that I need to bypass otherwise Forge won't register my test
net.minecraft.gametest.framework.GameTestRegistry.register(io.github.null2264.cobblegen.gametest.BlockGenerationTest.class);
Expand All @@ -59,19 +60,27 @@ public void onInitialize() {}
public static void initCommands(CommandDispatcher<CommandSourceStack> dispatcher) {
CGLog.info("Registering command...");
dispatcher.register(
LiteralArgumentBuilder.<CommandSourceStack>literal("cobblegen")
.then(LiteralArgumentBuilder.<CommandSourceStack>literal("reload-meta").requires(arg -> arg.hasPermission(OP_LEVEL_GAMEMASTERS)).executes(c -> {
CGLog.info("Reloading meta config...");
META_CONFIG = loadConfig(true, metaConfigFile, META_CONFIG, new ConfigMetaData(), ConfigMetaData.class);
c.getSource().sendSuccess(
#if MC>=12001
() ->
#endif
TextCompat.literal("Meta config has been reloaded"), false
);
CGLog.info("Meta config has been reloaded");
return 0;
}))
LiteralArgumentBuilder.<CommandSourceStack>literal("cobblegen")
.then(LiteralArgumentBuilder.<CommandSourceStack>literal("reload-meta").requires(arg -> arg.hasPermission(OP_LEVEL_GAMEMASTERS)).executes(c -> {
META_CONFIG = metaConfigFactory.reload(metaConfigFile);
c.getSource().sendSuccess(
#if MC>=12001
() ->
#endif
TextCompat.literal("Meta config has been reloaded"), false
);
return 0;
}))
.then(LiteralArgumentBuilder.<CommandSourceStack>literal("reload").requires(arg -> arg.hasPermission(OP_LEVEL_GAMEMASTERS)).executes(c -> {
FLUID_INTERACTION.reload();
c.getSource().sendSuccess(
#if MC>=12001
() ->
#endif
TextCompat.literal("Generator config has been reloaded"), false
);
return 0;
}))
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.null2264.cobblegen;

import io.github.null2264.cobblegen.CobbleGen;
import io.github.null2264.cobblegen.data.CGRegistryImpl;
import io.github.null2264.cobblegen.data.model.CGRegistry;
import io.github.null2264.cobblegen.data.model.Generator;
Expand All @@ -27,7 +26,7 @@

import static io.github.null2264.cobblegen.compat.CollectionCompat.listOf;
import static io.github.null2264.cobblegen.util.Constants.LAVA_FIZZ;
import static io.github.null2264.cobblegen.util.Util.notNullOr;
import static io.github.null2264.cobblegen.util.Util.elvis;

/**
* Replacement for BlockGenerator. This will act like Vanilla's registry system
Expand Down Expand Up @@ -60,7 +59,7 @@ public Map<Fluid, List<Generator>> getLocalGenerators() {

@NotNull
public Map<Fluid, List<Generator>> getGenerators() {
return notNullOr(serverGeneratorMap, generatorMap);
return elvis(serverGeneratorMap, generatorMap);
}

@ApiStatus.Internal
Expand Down Expand Up @@ -143,7 +142,6 @@ public boolean interact(LevelAccessor level, BlockPos pos, BlockState state, boo

for (Generator generator : generators) {
if (!generator.check(level, pos, state, fromTop)) continue;
if (fromTop && generator.getType() != GeneratorType.STONE) continue;

final Optional<BlockState> result = generator.tryGenerate(level, pos, state);
if (result.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@ public static <K, V> Map<K, V> mapOf(K key, V value) {
public static <T> List<T> streamToList(Stream<T> stream) {
return (List<T>) Collections.unmodifiableList(new ArrayList<>(Arrays.asList(stream.toArray())));
}
}

public static <T> List<T> mergeList(List<T> list, List<T> list2) {
List<T> rt = new ArrayList<>();
rt.addAll(list);
rt.addAll(list2);
return rt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import java.util.List;
import java.util.Objects;

import static io.github.null2264.cobblegen.compat.CollectionCompat.streamToList;

/**
* Class to holds modifier as Map key
*/
Expand All @@ -15,7 +13,7 @@ public class CGModifier {

public CGModifier(List<CGIdentifier> modifiers) {
if (modifiers.size() >= 4) throw new IllegalArgumentException("Cannot have more than 4 modifiers");
this.modifiers = streamToList(modifiers.stream().sorted());
this.modifiers = modifiers.stream().sorted().toList();
}

public void writeToBuf(FriendlyByteBuf buf) {
Expand All @@ -38,4 +36,4 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(modifiers);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
package io.github.null2264.cobblegen.data.config;

public interface Config {}
import java.io.File;

public interface Config {
interface Factory<T extends Config> {
T load(File file);
T reload(File file);
}
}
Loading
Loading