From 442632262f20e74873f3c856f8fb364e32fc2190 Mon Sep 17 00:00:00 2001 From: Martin Prokoph Date: Wed, 31 Jul 2024 20:50:09 +0200 Subject: [PATCH] Colored bird bath water on Polymer - Thanks to @Patbox for implementing the necessary changes in Polymer this fast! --- gradle.properties | 2 +- .../motschen/decorative/block/BirdBath.java | 2 +- .../model/ItemDisplayBirdBathModel.java | 27 ++++++++++++++----- .../motschen/decorative/util/ColorUtil.java | 9 ++++--- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/gradle.properties b/gradle.properties index 613ac44..7d6523d 100755 --- a/gradle.properties +++ b/gradle.properties @@ -17,5 +17,5 @@ org.gradle.jvmargs=-Xmx2G fabric_version=0.100.7+1.21 midnightlib_version=1.5.7-fabric patchouli_version=1.18.2-67-FABRIC - polymer_version=0.9.7+1.21 + polymer_version=0.9.8+1.21 factorytools_version=0.3.1+1.21 diff --git a/src/main/java/eu/midnightdust/motschen/decorative/block/BirdBath.java b/src/main/java/eu/midnightdust/motschen/decorative/block/BirdBath.java index 52fb7f4..b2db114 100755 --- a/src/main/java/eu/midnightdust/motschen/decorative/block/BirdBath.java +++ b/src/main/java/eu/midnightdust/motschen/decorative/block/BirdBath.java @@ -137,6 +137,6 @@ public class BirdBath extends AbstractCauldronBlock implements FactoryBlock { @Override public @Nullable ElementHolder createElementHolder(ServerWorld world, BlockPos pos, BlockState initialBlockState) { - return new ItemDisplayBirdBathModel(initialBlockState, pos, world); + return new ItemDisplayBirdBathModel(initialBlockState); } } diff --git a/src/main/java/eu/midnightdust/motschen/decorative/polymer/model/ItemDisplayBirdBathModel.java b/src/main/java/eu/midnightdust/motschen/decorative/polymer/model/ItemDisplayBirdBathModel.java index 34f4257..578b2a9 100644 --- a/src/main/java/eu/midnightdust/motschen/decorative/polymer/model/ItemDisplayBirdBathModel.java +++ b/src/main/java/eu/midnightdust/motschen/decorative/polymer/model/ItemDisplayBirdBathModel.java @@ -7,6 +7,7 @@ import eu.pb4.factorytools.api.resourcepack.BaseItemProvider; import eu.pb4.factorytools.api.virtualentity.BlockModel; import eu.pb4.factorytools.api.virtualentity.ItemDisplayElementUtil; import eu.pb4.polymer.virtualentity.api.attachment.BlockAwareAttachment; +import eu.pb4.polymer.virtualentity.api.attachment.ChunkAttachment; import eu.pb4.polymer.virtualentity.api.attachment.HolderAttachment; import eu.pb4.polymer.virtualentity.api.elements.ItemDisplayElement; import net.minecraft.block.BlockState; @@ -19,6 +20,7 @@ import net.minecraft.potion.Potions; import net.minecraft.server.world.ServerWorld; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; +import org.jetbrains.annotations.Nullable; import org.joml.Vector3f; import java.util.List; @@ -29,6 +31,7 @@ import static eu.midnightdust.motschen.decorative.DecorativeMain.id; public class ItemDisplayBirdBathModel extends BlockModel { private final ItemDisplayElement main; private final ItemDisplayElement water; + private int waterColor = ColorUtil.convertRgbToArgb(4159204); public static ItemStack STONE; public static ItemStack WATER; @@ -37,20 +40,16 @@ public class ItemDisplayBirdBathModel extends BlockModel { WATER = BaseItemProvider.requestModel(Items.POTION, id("block/polymer/bird_bath_water")); } - public ItemDisplayBirdBathModel(BlockState state, BlockPos pos, ServerWorld world) { + public ItemDisplayBirdBathModel(BlockState state) { this.main = ItemDisplayElementUtil.createSimple(STONE); this.main.setDisplaySize(1, 1); this.main.setScale(new Vector3f(2)); this.main.setViewRange(0.75f * (DecorativeConfig.viewDistance / 100f)); this.addElement(this.main); - // TODO: Get actual biome color when it's implemented in Polymer - // int baseColor = ColorUtil.getWaterColor(world, pos); - int color = ColorUtil.convertRgbToArgb(4159204); - WATER.applyComponentsFrom(ComponentMap.builder().add(DataComponentTypes.POTION_CONTENTS, new PotionContentsComponent(Optional.of(Potions.WATER), Optional.of(color), List.of())).build()); - this.water = ItemDisplayElementUtil.createSimple(WATER); + this.water = ItemDisplayElementUtil.createSimple(); this.water.setDisplaySize(1, 1); - this.water.setScale(new Vector3f(2)); + this.water.setScale(new Vector3f(state.get(BirdBath.LEVEL) >= 2 ? 1.9f : 1.65f)); this.water.setOffset(new Vec3d(0d, -0.025 * (3-(state.get(BirdBath.LEVEL))), 0d)); this.water.setViewRange(state.get(BirdBath.LEVEL) != 0 ? (0.75f * (DecorativeConfig.viewDistance / 100f)) : 0); this.addElement(this.water); @@ -61,10 +60,24 @@ public class ItemDisplayBirdBathModel extends BlockModel { if (updateType == BlockAwareAttachment.BLOCK_STATE_UPDATE) { var state = this.blockState(); this.water.setViewRange(state.get(BirdBath.LEVEL) != 0 ? (0.75f * (DecorativeConfig.viewDistance / 100f)) : 0); + this.water.setScale(new Vector3f(state.get(BirdBath.LEVEL) >= 2 ? 1.9f : 1.65f)); this.water.setOffset(new Vec3d(0d, -0.025 * (3-(state.get(BirdBath.LEVEL))), 0d)); this.tick(); } } + @Override + protected void onAttachmentSet(HolderAttachment attachment, @Nullable HolderAttachment oldAttachment) { + if (attachment instanceof ChunkAttachment chunkAttachment) { + this.waterColor = ColorUtil.convertRgbToArgb(ColorUtil.getWaterColor(chunkAttachment.getChunk(), blockPos())); + this.water.setItem(getColoredWater()); + } + } + private ItemStack getColoredWater() { + var colWater = WATER.copy(); + colWater.applyComponentsFrom(ComponentMap.builder().add(DataComponentTypes.POTION_CONTENTS, + new PotionContentsComponent(Optional.of(Potions.WATER), Optional.of(this.waterColor), List.of())).build()); + return colWater; + } } diff --git a/src/main/java/eu/midnightdust/motschen/decorative/util/ColorUtil.java b/src/main/java/eu/midnightdust/motschen/decorative/util/ColorUtil.java index 911b1ac..fd15a74 100644 --- a/src/main/java/eu/midnightdust/motschen/decorative/util/ColorUtil.java +++ b/src/main/java/eu/midnightdust/motschen/decorative/util/ColorUtil.java @@ -1,7 +1,8 @@ package eu.midnightdust.motschen.decorative.util; -import net.minecraft.server.world.ServerWorld; import net.minecraft.util.math.BlockPos; +import net.minecraft.world.biome.source.BiomeCoords; +import net.minecraft.world.chunk.Chunk; import java.util.Arrays; @@ -14,8 +15,10 @@ public class ColorUtil { return (alpha << 24) | (red << 16) | (green << 8) | blue; } - public static int getWaterColor(ServerWorld world, BlockPos pos) { - var biome = world.getBiome(pos); + public static int getWaterColor(Chunk chunk, BlockPos pos) { + var biome = chunk.getBiomeForNoiseGen(BiomeCoords.fromBlock(pos.getX()), + BiomeCoords.fromBlock(pos.getY()), + BiomeCoords.fromBlock(pos.getZ())); return biome == null ? 4159204 : biome.value().getWaterColor(); }