Skip to content

Commit

Permalink
Add config migration for grass to short_grass rename
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Dec 6, 2023
1 parent 95cb66b commit 55191ab
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import io.leangen.geantyref.TypeToken;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.NodePath;
import org.spongepowered.configurate.transformation.ConfigurationTransformation;
Expand All @@ -11,8 +15,10 @@

@SuppressWarnings("unused")
public final class Advanced extends AbstractConfig {
private static final int LATEST_VERSION = 3;

Advanced(final DirectoryProvider directoryProvider) {
super(directoryProvider.dataDirectory(), Advanced.class, "advanced.yml", 2);
super(directoryProvider.dataDirectory(), Advanced.class, "advanced.yml", LATEST_VERSION);
}

@Override
Expand All @@ -33,8 +39,47 @@ protected void addVersions(ConfigurationTransformation.VersionedBuilder versione
return null;
})
.build();
final ConfigurationTransformation twoToThree = ConfigurationTransformation.builder()
.addAction(NodePath.path("world-settings"), (path, node) -> {
final Set<Object> childKeys = node.childrenMap().keySet();
for (final Object childKey : childKeys) {
final ConfigurationNode worldSection = node.node(childKey);
final List<ConfigurationNode> sections = List.of(
worldSection.node("invisible-blocks"),
worldSection.node("iterate-up-base-blocks"),
worldSection.node("color-overrides", "blocks")
);
for (final ConfigurationNode sectionNode : sections) {
if (sectionNode.isList()) {
final List<String> list = Objects.requireNonNull(sectionNode.getList(String.class));
boolean removed = false;
removed |= list.remove("minecraft:grass");
removed |= list.remove("grass");
if (removed) {
list.add("minecraft:short_grass");
sectionNode.setList(String.class, list);
}
} else if (sectionNode.isMap()) {
final TypeToken<Map<String, String>> type = new TypeToken<>() {};
final Map<String, String> map = Objects.requireNonNull(sectionNode.get(type));
@Nullable String removed;
removed = map.remove("minecraft:grass");
if (removed == null) {
removed = map.remove("grass");
}
if (removed != null) {
map.put("minecraft:short_grass", removed);
sectionNode.set(type, map);
}
}
}
}
return null;
})
.build();

versionedBuilder.addVersion(2, oneToTwo);
versionedBuilder.addVersion(LATEST_VERSION, twoToThree);
}

static Advanced config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

@SuppressWarnings("unused")
public final class Config extends AbstractConfig {
private static final int LATEST_VERSION = 2;

Config(final DirectoryProvider directoryProvider) {
super(directoryProvider.dataDirectory(), Config.class, "config.yml", 2);
super(directoryProvider.dataDirectory(), Config.class, "config.yml", LATEST_VERSION);
}

@Override
Expand All @@ -24,7 +26,7 @@ protected void addVersions(final ConfigurationTransformation.VersionedBuilder ve
})
.build();

versionedBuilder.addVersion(2, oneToTwo);
versionedBuilder.addVersion(LATEST_VERSION, oneToTwo);
}

static Config config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private void invisibleBlocks() {
List.of(
"minecraft:tall_grass",
"minecraft:fern",
"minecraft:grass",
"minecraft:short_grass",
"minecraft:large_fern"
)
).forEach(block -> this.invisibleBlocks.add(Util.requireEntry(BuiltInRegistries.BLOCK, new ResourceLocation(block))));
Expand Down

0 comments on commit 55191ab

Please sign in to comment.