diff --git a/src/main/java/net/zepalesque/zenith/Zenith.java b/src/main/java/net/zepalesque/zenith/Zenith.java index f1616bb..19e1f35 100644 --- a/src/main/java/net/zepalesque/zenith/Zenith.java +++ b/src/main/java/net/zepalesque/zenith/Zenith.java @@ -113,7 +113,7 @@ private void dataSetup(GatherDataEvent event) { } private void registerDataMaps(RegisterDataMapTypesEvent event) { - BiomeTints.TINT_REGISTRY.forEach(tint -> tint.register(event)); +// BiomeTints.TINT_REGISTRY.forEach(tint -> tint.register(event)); } private void registerRegistries(NewRegistryEvent event) { diff --git a/src/main/java/net/zepalesque/zenith/api/biometint/BiomeTint.java b/src/main/java/net/zepalesque/zenith/api/biometint/BiomeTint.java index 23b2334..e12ca87 100644 --- a/src/main/java/net/zepalesque/zenith/api/biometint/BiomeTint.java +++ b/src/main/java/net/zepalesque/zenith/api/biometint/BiomeTint.java @@ -1,25 +1,26 @@ package net.zepalesque.zenith.api.biometint; -import com.mojang.serialization.Codec; -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.neoforged.neoforge.registries.DataPackRegistriesHooks; import net.neoforged.neoforge.registries.datamaps.DataMapType; -import net.neoforged.neoforge.registries.datamaps.RegisterDataMapTypesEvent; import net.zepalesque.zenith.Zenith; +import javax.annotation.Nonnull; import java.util.HashMap; import java.util.Map; +import java.util.Optional; /** * A class used to easily create biome-dependent tint systems. */ public class BiomeTint { - private final DataMapType dataMap; private final Map tints = new HashMap<>(); + private final Map, Integer> serverTints = new HashMap<>(); private final int defaultColor; + @Nonnull + private Optional defaultOverride = Optional.empty(); private boolean initialized = false; /** @@ -27,19 +28,9 @@ public class BiomeTint { * @param defaultColor The fallback color that should be used if a biome does not exist in the tint map. */ public BiomeTint(ResourceLocation loc, int defaultColor) { - this.dataMap = createTintMap(loc); this.defaultColor = defaultColor; } - /** - * Creates a {@link DataMapType} that should be used for this object. Whenever the player joins a world, this will be sent to the client to update the tint map. - * @param location The {@link ResourceLocation} that should be used for this data map. This will automatically be prefixed with "tint/". - * @return a new {@link DataMapType} - */ - private static DataMapType createTintMap(ResourceLocation location) { - return DataMapType.builder(location.withPrefix("tint/"), Registries.BIOME, Codec.INT).build(); - } - /** * Returns the color for the given biome. * @param biome The biome to check for. @@ -49,7 +40,7 @@ public int getColor(Biome biome) { if (!this.initialized) { Zenith.LOGGER.warn("Attempted to get uninitialized BiomeTint: {}!", BiomeTints.TINT_REGISTRY.getKey(this)); } - return this.tints.getOrDefault(biome, defaultColor); + return this.tints.getOrDefault(biome, getDefaultColor()); } /** @@ -58,6 +49,7 @@ public int getColor(Biome biome) { public void clear() { this.tints.clear(); this.initialized = false; + this.defaultOverride = Optional.empty(); } /** @@ -72,18 +64,23 @@ public void addTint(Biome biome, int color, ResourceLocation biomeID) { } /** - * Access for the {@link DataMapType} this object uses. - * @return The data map types. Should mainly be used for datagen purposes. + * Sets the override color for the map. Should not be called, as this is handled by Zenith. + * @param color the color for the override */ - public DataMapType getDataMap() { - return this.dataMap; + public void setOverride(int color) { + this.defaultOverride = Optional.of(color); } - /** - * Registers this object's {@link DataMapType}. Should not be called, as this is handled by Zenith. - */ - public void register(RegisterDataMapTypesEvent event) { - event.register(this.dataMap); + public int getDefaultColor() { + return this.defaultOverride.orElse(this.defaultColor); + } + + public Map, Integer> toSync() { + return this.serverTints; + } + + public Optional getOverride() { + return this.defaultOverride; } public void markInitialized() { diff --git a/src/main/java/net/zepalesque/zenith/data/generator/ZenithDataMapGen.java b/src/main/java/net/zepalesque/zenith/data/generator/ZenithDataMapGen.java index 89ccc12..157bf30 100644 --- a/src/main/java/net/zepalesque/zenith/data/generator/ZenithDataMapGen.java +++ b/src/main/java/net/zepalesque/zenith/data/generator/ZenithDataMapGen.java @@ -21,11 +21,11 @@ public ZenithDataMapGen(PackOutput output, CompletableFuture tints = this.builder(BiomeTints.EXAMPLE_TINT.get().getDataMap()); - tints.add(Biomes.PLAINS, 0x5BCEFA, false); - tints.add(Biomes.CHERRY_GROVE, 0xF5A9B8, false); - tints.add(Biomes.THE_END, 0xFFFFFF, false); - tints.add(BiomeTags.IS_MOUNTAIN, 0xF5A9B8, false); - tints.add(BiomeTags.IS_TAIGA, 0x5BCEFA, false); +// Builder tints = this.builder(BiomeTints.EXAMPLE_TINT.get().getDataMap()); +// tints.add(Biomes.PLAINS, 0x5BCEFA, false); +// tints.add(Biomes.CHERRY_GROVE, 0xF5A9B8, false); +// tints.add(Biomes.THE_END, 0xFFFFFF, false); +// tints.add(BiomeTags.IS_MOUNTAIN, 0xF5A9B8, false); +// tints.add(BiomeTags.IS_TAIGA, 0x5BCEFA, false); } } \ No newline at end of file diff --git a/src/main/java/net/zepalesque/zenith/event/listener/BiomeTintListener.java b/src/main/java/net/zepalesque/zenith/event/listener/BiomeTintListener.java index 2aa88c5..1ad1c23 100644 --- a/src/main/java/net/zepalesque/zenith/event/listener/BiomeTintListener.java +++ b/src/main/java/net/zepalesque/zenith/event/listener/BiomeTintListener.java @@ -2,6 +2,7 @@ import com.mojang.blaze3d.platform.NativeImage; +import com.mojang.datafixers.util.Pair; import net.minecraft.core.Registry; import net.minecraft.core.registries.Registries; import net.minecraft.resources.ResourceKey; @@ -20,6 +21,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.Optional; import java.util.stream.Collectors; @Mod.EventBusSubscriber(modid = Zenith.MODID) @@ -32,10 +34,10 @@ public static void updateTints(PlayerEvent.PlayerLoggedInEvent event) { Map> map = new HashMap<>(); BiomeTints.TINT_REGISTRY.forEach(tint -> { ResourceLocation loc = BiomeTints.TINT_REGISTRY.getKey(tint); - Map tints = registry.getDataMap(tint.getDataMap()).entrySet().stream().collect(Collectors.toMap( + Map tints = tint.toSync().entrySet().stream().collect(Collectors.toMap( entry -> entry.getKey().location(), Map.Entry::getValue )); - map.put(loc, tints); + map.put(loc, tints, tint.getOverride()); }); ZenithNetworking.sendToPlayer(new BiomeTintSyncPacket(map), player); } diff --git a/src/main/java/net/zepalesque/zenith/network/packet/BiomeTintSyncPacket.java b/src/main/java/net/zepalesque/zenith/network/packet/BiomeTintSyncPacket.java index 406ec64..7d2f2fd 100644 --- a/src/main/java/net/zepalesque/zenith/network/packet/BiomeTintSyncPacket.java +++ b/src/main/java/net/zepalesque/zenith/network/packet/BiomeTintSyncPacket.java @@ -1,5 +1,6 @@ package net.zepalesque.zenith.network.packet; +import com.mojang.datafixers.util.Pair; import net.minecraft.client.Minecraft; import net.minecraft.core.Registry; import net.minecraft.core.registries.Registries; @@ -16,17 +17,23 @@ import javax.annotation.Nullable; import java.util.Map; +import java.util.Optional; -public record BiomeTintSyncPacket(Map> types) implements CustomPacketPayload { +public record BiomeTintSyncPacket(Map, Optional>> types) implements CustomPacketPayload { public static final ResourceLocation ID = new ResourceLocation(Zenith.MODID, "sync_biome_tints"); public void write(FriendlyByteBuf buf) { - buf.writeMap(types, FriendlyByteBuf::writeResourceLocation, (b1, map) -> b1.writeMap(map, FriendlyByteBuf::writeResourceLocation, FriendlyByteBuf::writeInt)); + buf.writeMap(types, + FriendlyByteBuf::writeResourceLocation, + (b1, map) -> b1.writeMap(map, FriendlyByteBuf::writeResourceLocation, FriendlyByteBuf::writeInt)); } public static BiomeTintSyncPacket decode(FriendlyByteBuf buf) { - Map> map = buf.readMap(FriendlyByteBuf::readResourceLocation, b1 -> b1.readMap(FriendlyByteBuf::readResourceLocation, FriendlyByteBuf::readInt)); + Map> map = + buf.readMap( + FriendlyByteBuf::readResourceLocation, + b1 -> b1.readMap(FriendlyByteBuf::readResourceLocation, FriendlyByteBuf::readInt)); return new BiomeTintSyncPacket(map); } diff --git a/src/main/java/net/zepalesque/zenith/util/codec/ZenithCodecs.java b/src/main/java/net/zepalesque/zenith/util/codec/ZenithCodecs.java new file mode 100644 index 0000000..b43a598 --- /dev/null +++ b/src/main/java/net/zepalesque/zenith/util/codec/ZenithCodecs.java @@ -0,0 +1,14 @@ +package net.zepalesque.zenith.util.codec; + +import com.mojang.serialization.Codec; +import net.minecraft.core.Holder; +import net.minecraft.util.ExtraCodecs; +import net.minecraft.world.level.biome.Biome; + +import java.util.Map; + +public class ZenithCodecs { + + public static final Codec, Integer>> BIOME_COLOR_MAP = ExtraCodecs.strictUnboundedMap(Biome.CODEC, Codec.INT); + +} diff --git a/src/main/java/net/zepalesque/zenith/world/biome/modifier/BiomeTintMapModifier.java b/src/main/java/net/zepalesque/zenith/world/biome/modifier/BiomeTintMapModifier.java new file mode 100644 index 0000000..304cd9f --- /dev/null +++ b/src/main/java/net/zepalesque/zenith/world/biome/modifier/BiomeTintMapModifier.java @@ -0,0 +1,70 @@ +package net.zepalesque.zenith.world.biome.modifier; + +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.minecraft.core.Holder; +import net.minecraft.core.HolderSet; +import net.minecraft.resources.ResourceKey; +import net.minecraft.sounds.Music; +import net.minecraft.sounds.SoundEvent; +import net.minecraft.world.level.biome.Biome; +import net.neoforged.neoforge.common.world.BiomeModifier; +import net.neoforged.neoforge.common.world.ModifiableBiomeInfo; +import net.zepalesque.zenith.api.biometint.BiomeTint; +import net.zepalesque.zenith.api.biometint.BiomeTints; +import net.zepalesque.zenith.util.codec.ZenithCodecs; +import net.zepalesque.zenith.util.predicate.MusicPredicate; + +import javax.annotation.ParametersAreNonnullByDefault; +import java.util.Map; +import java.util.Optional; +import java.util.function.UnaryOperator; + +public record BiomeTintMapModifier(Holder tintHolder, Map, Integer> colors, Optional defaultOverride) implements BiomeModifier { + + + public static final Codec CODEC = RecordCodecBuilder.create(builder -> builder.group( + BiomeTints.TINT_REGISTRY.holderByNameCodec().fieldOf("tint_id").forGetter(BiomeTintMapModifier::tintHolder), + ZenithCodecs.BIOME_COLOR_MAP.fieldOf("colors").forGetter(BiomeTintMapModifier::colors), + Codec.INT.optionalFieldOf("default_color_override").forGetter(BiomeTintMapModifier::defaultOverride)).apply(builder, BiomeTintMapModifier::new)); + + + @Override + @ParametersAreNonnullByDefault + public void modify(Holder biome, Phase phase, ModifiableBiomeInfo.BiomeInfo.Builder builder) { + if (phase == Phase.AFTER_EVERYTHING && this.colors.containsKey(biome) && this.tintHolder.isBound()) { + BiomeTint tint = this.tintHolder.value(); + tint. + } + } + + @Override + public Codec codec() { + return CODEC; + } + + public record MusicOperator(Optional> sound, Optional minDelay, Optional maxDelay, Optional replaceCurrent) implements UnaryOperator { + + public static final Codec CODEC = RecordCodecBuilder.create(builder -> builder.group( + SoundEvent.CODEC.optionalFieldOf("sound").forGetter(MusicOperator::sound), + Codec.INT.optionalFieldOf("min_delay").forGetter(MusicOperator::minDelay), + Codec.INT.optionalFieldOf("max_delay").forGetter(MusicOperator::maxDelay), + Codec.BOOL.optionalFieldOf("replace_current").forGetter(MusicOperator::replaceCurrent)).apply(builder, MusicOperator::new)); + + @Override + public Music apply(Music music) { + if (sound.isEmpty() && minDelay.isEmpty() && maxDelay.isEmpty() && replaceCurrent.isEmpty()) { + return music; + } + Holder soundEvent = music.getEvent(); + int minimum = music.getMinDelay(); + int maximum = music.getMaxDelay(); + boolean replace = music.replaceCurrentMusic(); + if (sound.isPresent()) { soundEvent = sound.get(); } + if (minDelay.isPresent()) { minimum = minDelay.get(); } + if (maxDelay.isPresent()) { maximum = maxDelay.get(); } + if (replaceCurrent.isPresent()) { replace = replaceCurrent.get(); } + return new Music(soundEvent, minimum, maximum, replace); + } + } +} diff --git a/src/main/java/net/zepalesque/zenith/world/biome/modifier/FoliageModifier.java b/src/main/java/net/zepalesque/zenith/world/biome/modifier/FoliageModifier.java index e0e769d..4835b74 100644 --- a/src/main/java/net/zepalesque/zenith/world/biome/modifier/FoliageModifier.java +++ b/src/main/java/net/zepalesque/zenith/world/biome/modifier/FoliageModifier.java @@ -8,18 +8,17 @@ import net.minecraft.world.level.biome.Biome; import net.neoforged.neoforge.common.world.BiomeModifier; import net.neoforged.neoforge.common.world.ModifiableBiomeInfo; +import net.zepalesque.zenith.util.codec.ZenithCodecs; import java.util.Map; import java.util.Optional; public record FoliageModifier(Optional settings, Map, Integer> grassMap, Map, Integer> foliageMap) implements BiomeModifier { - public static final Codec, Integer>> MAP_CODEC = ExtraCodecs.strictUnboundedMap(Biome.CODEC, Codec.INT); - public static final Codec CODEC = RecordCodecBuilder.create(builder -> builder.group( DefaultFoliageSettings.CODEC.optionalFieldOf("default_colors").forGetter(FoliageModifier::settings), - MAP_CODEC.fieldOf("grass_map").forGetter(FoliageModifier::grassMap), - MAP_CODEC.fieldOf("foliage_map").forGetter(FoliageModifier::foliageMap)).apply(builder, FoliageModifier::new)); + ZenithCodecs.BIOME_COLOR_MAP.fieldOf("grass_map").forGetter(FoliageModifier::grassMap), + ZenithCodecs.BIOME_COLOR_MAP.fieldOf("foliage_map").forGetter(FoliageModifier::foliageMap)).apply(builder, FoliageModifier::new)); @Override diff --git a/src/main/java/net/zepalesque/zenith/world/biome/modifier/SkiesModifier.java b/src/main/java/net/zepalesque/zenith/world/biome/modifier/SkiesModifier.java index 7b6d9de..8806aa2 100644 --- a/src/main/java/net/zepalesque/zenith/world/biome/modifier/SkiesModifier.java +++ b/src/main/java/net/zepalesque/zenith/world/biome/modifier/SkiesModifier.java @@ -8,18 +8,18 @@ import net.minecraft.world.level.biome.Biome; import net.neoforged.neoforge.common.world.BiomeModifier; import net.neoforged.neoforge.common.world.ModifiableBiomeInfo; +import net.zepalesque.zenith.util.codec.ZenithCodecs; import java.util.Map; import java.util.Optional; public record SkiesModifier(Optional settings, Map, Integer> skyMap, Map, Integer> fogMap) implements BiomeModifier { - public static final Codec, Integer>> MAP_CODEC = ExtraCodecs.strictUnboundedMap(Biome.CODEC, Codec.INT); public static final Codec CODEC = RecordCodecBuilder.create(builder -> builder.group( DefaultSkySettings.CODEC.optionalFieldOf("default_colors").forGetter(SkiesModifier::settings), - MAP_CODEC.fieldOf("sky_map").forGetter(SkiesModifier::skyMap), - MAP_CODEC.fieldOf("fog_map").forGetter(SkiesModifier::fogMap)).apply(builder, SkiesModifier::new)); + ZenithCodecs.BIOME_COLOR_MAP.fieldOf("sky_map").forGetter(SkiesModifier::skyMap), + ZenithCodecs.BIOME_COLOR_MAP.fieldOf("fog_map").forGetter(SkiesModifier::fogMap)).apply(builder, SkiesModifier::new)); @Override diff --git a/src/main/java/net/zepalesque/zenith/world/biome/modifier/WaterModifier.java b/src/main/java/net/zepalesque/zenith/world/biome/modifier/WaterModifier.java index b663cf3..0461051 100644 --- a/src/main/java/net/zepalesque/zenith/world/biome/modifier/WaterModifier.java +++ b/src/main/java/net/zepalesque/zenith/world/biome/modifier/WaterModifier.java @@ -8,18 +8,17 @@ import net.minecraft.world.level.biome.Biome; import net.neoforged.neoforge.common.world.BiomeModifier; import net.neoforged.neoforge.common.world.ModifiableBiomeInfo; +import net.zepalesque.zenith.util.codec.ZenithCodecs; import java.util.Map; import java.util.Optional; public record WaterModifier(Optional settings, Map, Integer> waterMap, Map, Integer> fogMap) implements BiomeModifier { - public static final Codec, Integer>> MAP_CODEC = ExtraCodecs.strictUnboundedMap(Biome.CODEC, Codec.INT); - public static final Codec CODEC = RecordCodecBuilder.create(builder -> builder.group( DefaultWaterSettings.CODEC.optionalFieldOf("default_colors").forGetter(WaterModifier::settings), - MAP_CODEC.fieldOf("water_map").forGetter(WaterModifier::waterMap), - MAP_CODEC.fieldOf("fog_map").forGetter(WaterModifier::fogMap)).apply(builder, WaterModifier::new)); + ZenithCodecs.BIOME_COLOR_MAP.fieldOf("water_map").forGetter(WaterModifier::waterMap), + ZenithCodecs.BIOME_COLOR_MAP.fieldOf("fog_map").forGetter(WaterModifier::fogMap)).apply(builder, WaterModifier::new)); @Override