Skip to content

Commit

Permalink
test: tint stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Zepalesque committed Jun 24, 2024
1 parent dcb2177 commit 9f3da36
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/zepalesque/zenith/Zenith.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
47 changes: 22 additions & 25 deletions src/main/java/net/zepalesque/zenith/api/biometint/BiomeTint.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,36 @@
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<Biome, Integer> dataMap;
private final Map<Biome, Integer> tints = new HashMap<>();
private final Map<ResourceKey<Biome>, Integer> serverTints = new HashMap<>();
private final int defaultColor;
@Nonnull
private Optional<Integer> defaultOverride = Optional.empty();
private boolean initialized = false;

/**
* @param loc The location that will be used for the {@link DataMapType}. Preferably should be the same as this tint's registered ID.
* @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<Biome, Integer> 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.
Expand All @@ -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());
}

/**
Expand All @@ -58,6 +49,7 @@ public int getColor(Biome biome) {
public void clear() {
this.tints.clear();
this.initialized = false;
this.defaultOverride = Optional.empty();
}

/**
Expand All @@ -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<Biome, Integer> 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<ResourceKey<Biome>, Integer> toSync() {
return this.serverTints;
}

public Optional<Integer> getOverride() {
return this.defaultOverride;
}

public void markInitialized() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public ZenithDataMapGen(PackOutput output, CompletableFuture<HolderLookup.Provid

@Override
protected void gather() {
Builder<Integer, Biome> 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<Integer, Biome> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -32,10 +34,10 @@ public static void updateTints(PlayerEvent.PlayerLoggedInEvent event) {
Map<ResourceLocation, Map<ResourceLocation, Integer>> map = new HashMap<>();
BiomeTints.TINT_REGISTRY.forEach(tint -> {
ResourceLocation loc = BiomeTints.TINT_REGISTRY.getKey(tint);
Map<ResourceLocation, Integer> tints = registry.getDataMap(tint.getDataMap()).entrySet().stream().collect(Collectors.toMap(
Map<ResourceLocation, Integer> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,17 +17,23 @@

import javax.annotation.Nullable;
import java.util.Map;
import java.util.Optional;

public record BiomeTintSyncPacket(Map<ResourceLocation, Map<ResourceLocation, Integer>> types) implements CustomPacketPayload {
public record BiomeTintSyncPacket(Map<ResourceLocation, Pair<Map<ResourceLocation, Integer>, Optional<Integer>>> 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<ResourceLocation, Map<ResourceLocation, Integer>> map = buf.readMap(FriendlyByteBuf::readResourceLocation, b1 -> b1.readMap(FriendlyByteBuf::readResourceLocation, FriendlyByteBuf::readInt));
Map<ResourceLocation, Map<ResourceLocation, Integer>> map =
buf.readMap(
FriendlyByteBuf::readResourceLocation,
b1 -> b1.readMap(FriendlyByteBuf::readResourceLocation, FriendlyByteBuf::readInt));
return new BiomeTintSyncPacket(map);
}

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/net/zepalesque/zenith/util/codec/ZenithCodecs.java
Original file line number Diff line number Diff line change
@@ -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<Map<Holder<Biome>, Integer>> BIOME_COLOR_MAP = ExtraCodecs.strictUnboundedMap(Biome.CODEC, Codec.INT);

}
Original file line number Diff line number Diff line change
@@ -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<BiomeTint> tintHolder, Map<Holder<Biome>, Integer> colors, Optional<Integer> defaultOverride) implements BiomeModifier {


public static final Codec<BiomeTintMapModifier> 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> 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<? extends BiomeModifier> codec() {
return CODEC;
}

public record MusicOperator(Optional<Holder<SoundEvent>> sound, Optional<Integer> minDelay, Optional<Integer> maxDelay, Optional<Boolean> replaceCurrent) implements UnaryOperator<Music> {

public static final Codec<MusicOperator> 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> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefaultFoliageSettings> settings, Map<Holder<Biome>, Integer> grassMap, Map<Holder<Biome>, Integer> foliageMap) implements BiomeModifier {

public static final Codec<Map<Holder<Biome>, Integer>> MAP_CODEC = ExtraCodecs.strictUnboundedMap(Biome.CODEC, Codec.INT);

public static final Codec<FoliageModifier> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefaultSkySettings> settings, Map<Holder<Biome>, Integer> skyMap, Map<Holder<Biome>, Integer> fogMap) implements BiomeModifier {

public static final Codec<Map<Holder<Biome>, Integer>> MAP_CODEC = ExtraCodecs.strictUnboundedMap(Biome.CODEC, Codec.INT);

public static final Codec<SkiesModifier> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefaultWaterSettings> settings, Map<Holder<Biome>, Integer> waterMap, Map<Holder<Biome>, Integer> fogMap) implements BiomeModifier {

public static final Codec<Map<Holder<Biome>, Integer>> MAP_CODEC = ExtraCodecs.strictUnboundedMap(Biome.CODEC, Codec.INT);

public static final Codec<WaterModifier> 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
Expand Down

0 comments on commit 9f3da36

Please sign in to comment.