Skip to content

Commit 446b92c

Browse files
Refactor GeyserMapping classes to have an ABC with helper method to create codec, fix datagen module
1 parent 7a771da commit 446b92c

7 files changed

Lines changed: 83 additions & 81 deletions

File tree

client/src/main/java/org/geysermc/rainbow/client/command/SoundNamespaceSuggestionProvider.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ protected SoundNamespaceSuggestionProvider() {}
1818

1919
@Override
2020
public CompletableFuture<Suggestions> getSuggestions(CommandContext<FabricClientCommandSource> context, SuggestionsBuilder builder) {
21-
2221
return SharedSuggestionProvider.suggest(((SoundManagerAccessor) context.getSource().getClient().getSoundManager()).rainbow$getRawRegistrations().keySet().stream()
2322
.filter(namespace -> !Rainbow.isVanilla(namespace)), builder);
2423
}

datagen/src/main/java/org/geysermc/rainbow/datagen/RainbowDataProvider.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import net.minecraft.client.renderer.block.dispatch.BlockStateModel;
1616
import net.minecraft.client.renderer.block.dispatch.BlockStateModelDispatcher;
1717
import net.minecraft.client.renderer.item.ClientItem;
18+
import net.minecraft.client.resources.WaypointStyle;
1819
import net.minecraft.client.resources.metadata.animation.AnimationMetadataSection;
1920
import net.minecraft.client.resources.model.EquipmentClientInfo;
2021
import net.minecraft.client.resources.model.ResolvedModel;
@@ -42,6 +43,7 @@
4243
import net.minecraft.world.item.equipment.EquipmentAsset;
4344
import net.minecraft.world.level.block.Block;
4445
import net.minecraft.world.level.block.state.BlockState;
46+
import net.minecraft.world.waypoints.WaypointStyleAsset;
4547
import org.geysermc.rainbow.Rainbow;
4648
import org.geysermc.rainbow.RainbowIO;
4749
import org.geysermc.rainbow.datagen.accessor.SoundsProviderDataAccessor;
@@ -103,7 +105,7 @@ protected RainbowDataProvider(FabricPackOutput output, CompletableFuture<HolderL
103105

104106
protected RainbowDataProvider(FabricPackOutput output, CompletableFuture<HolderLookup.Provider> registries, String packName,
105107
FabricModelProvider modelProvider) {
106-
this(output, registries, packName, new Providers(modelProvider, Optional.empty(), Optional.empty(), Map.of()));
108+
this(output, registries, packName, new Providers(modelProvider, Optional.empty(), Optional.empty(), Map.of(), Map.of()));
107109
}
108110

109111
protected RainbowDataProvider(FabricPackOutput output, CompletableFuture<HolderLookup.Provider> registries, FabricModelProvider modelProvider) {
@@ -160,20 +162,24 @@ public static class Providers {
160162
private final Optional<List<FabricLanguageProvider>> languages;
161163
private final Optional<FabricSoundsProvider> sounds;
162164
private final Map<ResourceKey<EquipmentAsset>, EquipmentClientInfo> equipmentInfos;
165+
private final Map<ResourceKey<WaypointStyleAsset>, WaypointStyle> waypointStyles;
163166

164167
public Providers(FabricModelProvider models, Optional<List<FabricLanguageProvider>> languages,
165168
Optional<FabricSoundsProvider> sounds,
166-
Map<ResourceKey<EquipmentAsset>, EquipmentClientInfo> equipmentInfos) {
169+
Map<ResourceKey<EquipmentAsset>, EquipmentClientInfo> equipmentInfos,
170+
Map<ResourceKey<WaypointStyleAsset>, WaypointStyle> waypointStyles) {
167171
this.models = models;
168172
this.languages = languages;
169173
this.sounds = sounds;
170174
this.equipmentInfos = equipmentInfos;
175+
this.waypointStyles = waypointStyles;
171176
}
172177

173178
public static Providers create(FabricModelProvider models, Optional<FabricLanguageProvider> languages,
174179
Optional<FabricSoundsProvider> sounds,
175-
Map<ResourceKey<EquipmentAsset>, EquipmentClientInfo> equipmentInfos) {
176-
return new Providers(models, languages.map(List::of), sounds, equipmentInfos);
180+
Map<ResourceKey<EquipmentAsset>, EquipmentClientInfo> equipmentInfos,
181+
Map<ResourceKey<WaypointStyleAsset>, WaypointStyle> waypointStyles) {
182+
return new Providers(models, languages.map(List::of), sounds, equipmentInfos, waypointStyles);
177183
}
178184

179185
private Map<Item, ClientItem> getItemInfos() {
@@ -338,6 +344,11 @@ public Optional<EquipmentClientInfo> getEquipmentInfo(ResourceKey<EquipmentAsset
338344
return Optional.ofNullable(providers.equipmentInfos.get(key));
339345
}
340346

347+
@Override
348+
public Optional<WaypointStyle> getWaypointStyle(ResourceKey<WaypointStyleAsset> key) {
349+
return Optional.ofNullable(providers.waypointStyles.get(key));
350+
}
351+
341352
@Override
342353
public Optional<TextureResource> getTexture(@Nullable Identifier atlas, Identifier identifier) {
343354
// We don't care about atlas since there are none loaded at datagen
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.geysermc.rainbow.definition;
2+
3+
import com.mojang.serialization.Codec;
4+
import com.mojang.serialization.codecs.RecordCodecBuilder;
5+
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
6+
import org.geysermc.rainbow.CodecUtil;
7+
8+
import java.util.Collections;
9+
import java.util.Map;
10+
import java.util.function.Function;
11+
12+
public abstract class AbstractGeyserMappings<K, V> {
13+
private final Map<K, V> mappings = new Object2ObjectOpenHashMap<>();
14+
15+
protected AbstractGeyserMappings() {}
16+
17+
protected AbstractGeyserMappings(Map<K, V> mappings) {
18+
this.mappings.putAll(mappings);
19+
}
20+
21+
protected void map(K key, V value) {
22+
mappings.put(key, value);
23+
}
24+
25+
protected static <K, V, T extends AbstractGeyserMappings<K, V>> Codec<T> createCodec(String type, int formatVersion,
26+
Codec<K> keyCodec, Codec<V> valueCodec,
27+
Function<Map<K, V>, T> constructor) {
28+
return RecordCodecBuilder.create(instance ->
29+
instance.group(
30+
CodecUtil.unitVerifyCodec(Codec.INT, "format_version", formatVersion),
31+
Codec.unboundedMap(keyCodec, valueCodec).fieldOf(type).forGetter(T::mappings)
32+
).apply(instance, (_, mappings) -> constructor.apply(mappings))
33+
);
34+
}
35+
36+
public Map<K, V> mappings() {
37+
return Collections.unmodifiableMap(mappings);
38+
}
39+
40+
public int size() {
41+
return mappings.size();
42+
}
43+
}

rainbow/src/main/java/org/geysermc/rainbow/definition/block/GeyserBlockMappings.java

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,32 @@
22

33
import com.mojang.serialization.Codec;
44
import com.mojang.serialization.DataResult;
5-
import com.mojang.serialization.codecs.RecordCodecBuilder;
6-
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
75
import net.minecraft.core.Holder;
86
import net.minecraft.core.registries.BuiltInRegistries;
97
import net.minecraft.world.level.block.Block;
108
import net.minecraft.world.level.block.Blocks;
11-
import org.geysermc.rainbow.CodecUtil;
9+
import org.geysermc.rainbow.definition.AbstractGeyserMappings;
1210

13-
import java.util.Collections;
1411
import java.util.Map;
1512

16-
public class GeyserBlockMappings {
13+
public final class GeyserBlockMappings extends AbstractGeyserMappings<Holder<Block>, GeyserBlockMapping> {
1714
// Inspired by Item.CODEC
1815
private static final Codec<Holder<Block>> BLOCK_CODEC = BuiltInRegistries.BLOCK
1916
.holderByNameCodec()
2017
.validate(block -> block.is(Blocks.AIR.builtInRegistryHolder()) ? DataResult.error(() -> "Block must not be minecraft:air") : DataResult.success(block));
21-
public static final Codec<GeyserBlockMappings> CODEC = RecordCodecBuilder.create(instance ->
22-
instance.group(
23-
CodecUtil.unitVerifyCodec(Codec.INT, "format_version", 1),
24-
Codec.unboundedMap(BLOCK_CODEC, GeyserBlockMapping.CODEC).fieldOf("blocks").forGetter(GeyserBlockMappings::mappings)
25-
).apply(instance, (_, mappings) -> new GeyserBlockMappings(mappings))
26-
);
27-
28-
private final Map<Holder<Block>, GeyserBlockMapping> mappings = new Object2ObjectOpenHashMap<>();
18+
public static final Codec<GeyserBlockMappings> CODEC = createCodec("blocks", 1, BLOCK_CODEC, GeyserBlockMapping.CODEC, GeyserBlockMappings::new);
2919

3020
public GeyserBlockMappings() {}
3121

3222
private GeyserBlockMappings(Map<Holder<Block>, GeyserBlockMapping> mappings) {
33-
this.mappings.putAll(mappings);
23+
super(mappings);
3424
}
3525

3626
public void map(Holder<Block> block, GeyserBlockMapping.Builder mapping) {
3727
map(block, mapping.build());
3828
}
3929

30+
@Override
4031
public void map(Holder<Block> block, GeyserBlockMapping mapping) {
4132
if (block.value().getStateDefinition().isSingletonState() && (mapping.base().isEmpty() || !mapping.stateOverrides().isEmpty())) {
4233
throw new IllegalArgumentException("mapping must have a base and must not have state overrides because the base block only has a single state");
@@ -46,25 +37,22 @@ public void map(Holder<Block> block, GeyserBlockMapping mapping) {
4637
throw new IllegalArgumentException("mapping must have at least a single state override, as onlyOverrideStates is set to true");
4738
}
4839

49-
GeyserBlockMapping existing = mappings.get(block);
40+
GeyserBlockMapping existing = mappings().get(block);
5041
if (existing != null) {
5142
if (existing.onlyOverrideStates() && mapping.onlyOverrideStates()) {
52-
mappings.put(block, existing.mergeStateOverrides(mapping));
43+
super.map(block, existing.mergeStateOverrides(mapping));
5344
} else {
5445
throw new IllegalStateException("tried to register existing mapping for block " + block + ", and was unable to merge");
5546
}
5647
} else {
57-
mappings.put(block, mapping);
48+
super.map(block, mapping);
5849
}
5950
}
6051

52+
@Override
6153
public int size() {
62-
return mappings.values().stream()
54+
return mappings().values().stream()
6355
.mapToInt(mapping -> (mapping.base().isPresent() ? 1 : 0) + mapping.stateOverrides().size())
6456
.sum();
6557
}
66-
67-
public Map<Holder<Block>, GeyserBlockMapping> mappings() {
68-
return Collections.unmodifiableMap(mappings);
69-
}
7058
}

rainbow/src/main/java/org/geysermc/rainbow/definition/item/GeyserItemMappings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import java.util.Optional;
1818
import java.util.function.Function;
1919

20-
public class GeyserItemMappings {
20+
public final class GeyserItemMappings {
2121
private static final Codec<Map<Holder<Item>, Collection<GeyserItemMapping>>> MAPPINGS_CODEC = Codec.unboundedMap(Item.CODEC, GeyserItemMapping.MODEL_SAFE_CODEC.listOf().xmap(Function.identity(), ArrayList::new));
2222

2323
public static final Codec<GeyserItemMappings> CODEC = RecordCodecBuilder.create(instance ->

rainbow/src/main/java/org/geysermc/rainbow/definition/skull/GeyserSkullMappings.java

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,23 @@
66
import com.mojang.authlib.properties.PropertyMap;
77
import com.mojang.datafixers.util.Either;
88
import com.mojang.serialization.Codec;
9-
import com.mojang.serialization.codecs.RecordCodecBuilder;
10-
import net.minecraft.util.ExtraCodecs;
119
import net.minecraft.util.StringRepresentable;
1210
import net.minecraft.world.item.component.ResolvableProfile;
13-
import org.geysermc.rainbow.CodecUtil;
11+
import org.geysermc.rainbow.definition.AbstractGeyserMappings;
1412
import org.geysermc.rainbow.mixin.ResolvableProfileAccessor;
1513
import org.jspecify.annotations.Nullable;
1614

1715
import java.util.ArrayList;
18-
import java.util.Collections;
19-
import java.util.EnumMap;
2016
import java.util.List;
2117
import java.util.Map;
22-
import java.util.Objects;
2318

24-
public final class GeyserSkullMappings {
25-
public static final Codec<GeyserSkullMappings> CODEC = RecordCodecBuilder.create(instance ->
26-
instance.group(
27-
CodecUtil.unitVerifyCodec(Codec.INT, "format_version", 1),
28-
Codec.unboundedMap(SkullTextureType.CODEC, ExtraCodecs.NON_EMPTY_STRING.listOf()).fieldOf("skulls").forGetter(GeyserSkullMappings::mappings)
29-
).apply(instance, (_, mappings) -> new GeyserSkullMappings(mappings))
30-
);
31-
32-
private final Map<SkullTextureType, List<String>> mappings = new EnumMap<>(SkullTextureType.class);
19+
public final class GeyserSkullMappings extends AbstractGeyserMappings<GeyserSkullMappings.SkullTextureType, List<String>> {
20+
public static final Codec<GeyserSkullMappings> CODEC = createCodec("skulls", 1, SkullTextureType.CODEC, Codec.STRING.listOf(), GeyserSkullMappings::new);
3321

3422
public GeyserSkullMappings() {}
3523

3624
private GeyserSkullMappings(Map<SkullTextureType, List<String>> mappings) {
37-
this.mappings.putAll(mappings);
25+
super(mappings);
3826
}
3927

4028
public boolean withProfile(ResolvableProfile profile) {
@@ -63,25 +51,17 @@ public boolean withProfile(ResolvableProfile profile) {
6351
}
6452

6553
private boolean withTexture(SkullTextureType type, String texture) {
66-
List<String> textures = mappings.get(type);
54+
List<String> textures = mappings().get(type);
6755
if (textures == null) {
6856
textures = new ArrayList<>();
69-
mappings.put(type, textures);
57+
map(type, textures);
7058
} else if (textures.contains(texture)) {
7159
return false;
7260
}
7361
textures.add(texture);
7462
return true;
7563
}
7664

77-
public Map<SkullTextureType, List<String>> mappings() {
78-
return Collections.unmodifiableMap(mappings);
79-
}
80-
81-
public int size(SkullTextureType type) {
82-
return Objects.requireNonNullElseGet(mappings.get(type), List::of).size();
83-
}
84-
8565
private static @Nullable Property getTexture(GameProfile profile) {
8666
return getTexture(profile.properties());
8767
}
Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,24 @@
11
package org.geysermc.rainbow.definition.waypoint;
22

33
import com.mojang.serialization.Codec;
4-
import com.mojang.serialization.codecs.RecordCodecBuilder;
5-
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
64
import net.minecraft.client.resources.WaypointStyle;
75
import net.minecraft.resources.Identifier;
8-
import org.geysermc.rainbow.CodecUtil;
6+
import org.geysermc.rainbow.definition.AbstractGeyserMappings;
97

10-
import java.util.Collections;
118
import java.util.Map;
129

13-
public final class GeyserWaypointStyleMappings {
14-
public static final Codec<GeyserWaypointStyleMappings> CODEC = RecordCodecBuilder.create(instance ->
15-
instance.group(
16-
CodecUtil.unitVerifyCodec(Codec.INT, "format_version", 1),
17-
Codec.unboundedMap(Identifier.CODEC, WaypointStyle.CODEC).fieldOf("waypoint_styles").forGetter(GeyserWaypointStyleMappings::mappings)
18-
).apply(instance, (_, mappings) -> new GeyserWaypointStyleMappings(mappings))
19-
);
20-
21-
private final Map<Identifier, WaypointStyle> mappings = new Object2ObjectOpenHashMap<>();
10+
public final class GeyserWaypointStyleMappings extends AbstractGeyserMappings<Identifier, WaypointStyle> {
11+
public static final Codec<GeyserWaypointStyleMappings> CODEC = AbstractGeyserMappings.createCodec("waypoint_styles", 1,
12+
Identifier.CODEC, WaypointStyle.CODEC, GeyserWaypointStyleMappings::new);
2213

2314
public GeyserWaypointStyleMappings() {}
2415

2516
private GeyserWaypointStyleMappings(Map<Identifier, WaypointStyle> mappings) {
26-
this.mappings.putAll(mappings);
27-
}
28-
29-
public void map(Identifier identifier, WaypointStyle style) {
30-
if (mappings.containsKey(identifier)) {
31-
throw new IllegalStateException("tried to map waypoint style for " + identifier + " twice");
32-
}
33-
mappings.put(identifier, style);
34-
}
35-
36-
public Map<Identifier, WaypointStyle> mappings() {
37-
return Collections.unmodifiableMap(mappings);
17+
super(mappings);
3818
}
3919

40-
public int size() {
41-
return mappings.size();
20+
@Override
21+
public void map(Identifier key, WaypointStyle value) {
22+
super.map(key, value);
4223
}
4324
}

0 commit comments

Comments
 (0)