From 20889020d76a1790c0cf7a531bb8fb39d7e4e473 Mon Sep 17 00:00:00 2001 From: Guido Gloor Date: Sat, 14 Oct 2023 12:44:48 +0200 Subject: [PATCH] Refactored color provider into its own class, also blueified high grass --- .../ch/haslo/bluegrass/BlueGrassClient.java | 57 +---------------- .../bluegrass/BlueGrassColorProvider.java | 63 +++++++++++++++++++ 2 files changed, 66 insertions(+), 54 deletions(-) create mode 100644 src/client/java/ch/haslo/bluegrass/BlueGrassColorProvider.java diff --git a/src/client/java/ch/haslo/bluegrass/BlueGrassClient.java b/src/client/java/ch/haslo/bluegrass/BlueGrassClient.java index 622de18..d38d9c3 100644 --- a/src/client/java/ch/haslo/bluegrass/BlueGrassClient.java +++ b/src/client/java/ch/haslo/bluegrass/BlueGrassClient.java @@ -14,17 +14,8 @@ import net.fabricmc.fabric.api.client.model.loading.v1.ModelLoadingPlugin; import net.fabricmc.fabric.api.client.model.loading.v1.ModelModifier; import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry; -import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.color.block.BlockColorProvider; import net.minecraft.client.util.ModelIdentifier; -import net.minecraft.registry.entry.RegistryEntry; -import net.minecraft.util.math.BlockPos; -import net.minecraft.world.BlockRenderView; -import net.minecraft.world.World; -import net.minecraft.world.biome.Biome; -import org.jetbrains.annotations.Nullable; public class BlueGrassClient implements ClientModInitializer { @Override @@ -42,51 +33,9 @@ public void onInitializeClient() { return model; })); System.out.println("Registering ColorProviderRegistry"); - ColorProviderRegistry.BLOCK.register(new GrassColorProvider(), Blocks.GRASS_BLOCK); - ColorProviderRegistry.BLOCK.register(new GrassColorProvider(), Blocks.GRASS); + ColorProviderRegistry.BLOCK.register(new BlueGrassColorProvider(), Blocks.GRASS_BLOCK); + ColorProviderRegistry.BLOCK.register(new BlueGrassColorProvider(), Blocks.GRASS); + ColorProviderRegistry.BLOCK.register(new BlueGrassColorProvider(), Blocks.TALL_GRASS); System.out.println("BlueGrassClient initialized"); } - - private static class GrassColorProvider implements BlockColorProvider { - @Override - public int getColor(BlockState state, @Nullable BlockRenderView world, @Nullable BlockPos blockPos, int tintIndex) { - // System.out.println("Getting color somewhere (sorry about the spam)"); - assert blockPos != null; - World minecraftWorld = MinecraftClient.getInstance().world; - assert minecraftWorld != null; - int totalRed = 0; - int totalGreen = 0; - int totalBlue = 0; - // System.out.println("Getting color values"); - for (int xOffset = -1; xOffset <= 1; ++xOffset) { - for (int zOffset = -1; zOffset <= 1; ++zOffset) { - RegistryEntry registryEntry = minecraftWorld.getBiome(blockPos.add(xOffset, 0, zOffset)); - Biome biome = registryEntry.value(); - int grassColor = biome.getGrassColorAt(blockPos.getX() + xOffset, blockPos.getZ() + zOffset); - // System.out.println("Grass Color: 0x" + Integer.toHexString(grassColor)); - totalRed += (grassColor & 16711680) >> 16; - totalGreen += (grassColor & 65280) >> 8; - totalBlue += grassColor & 255; - } - } - int averageRed = totalRed / 9; - int averageGreen = totalGreen / 9; - int averageBlue = totalBlue / 9; - int scaledRed = Math.min(255, (int) (averageRed / 1.5)); - int scaledBlue = Math.min(255, (int) (averageGreen * 1.5)); - int scaledGreen = Math.min(255, (int) (averageBlue / 1.5)); - // System.out.println("Color values RGB average: " + - // averageRed + "," + - // averageGreen + "," + - // averageBlue + "" + - // ", RGB scaled: " + - // scaledRed + "," + - // scaledGreen + "," + - // scaledBlue - // ); - int color = 0xFF000000 | scaledRed << 16 | scaledGreen << 8 | scaledBlue; - // System.out.println("returning: 0x" + Integer.toHexString(color)); - return color; - } - } } diff --git a/src/client/java/ch/haslo/bluegrass/BlueGrassColorProvider.java b/src/client/java/ch/haslo/bluegrass/BlueGrassColorProvider.java new file mode 100644 index 0000000..fde7a4d --- /dev/null +++ b/src/client/java/ch/haslo/bluegrass/BlueGrassColorProvider.java @@ -0,0 +1,63 @@ +/* + * Copyright © 2023 haslo + * + * This file is part of BlueGrass. + * + * Licensed under the MIT license. For more information, + * see the LICENSE file. + */ + +package ch.haslo.bluegrass; + +import net.minecraft.block.BlockState; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.color.block.BlockColorProvider; +import net.minecraft.registry.entry.RegistryEntry; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.BlockRenderView; +import net.minecraft.world.World; +import net.minecraft.world.biome.Biome; +import org.jetbrains.annotations.Nullable; + +class BlueGrassColorProvider implements BlockColorProvider { + @Override + public int getColor(BlockState state, @Nullable BlockRenderView world, @Nullable BlockPos blockPos, int tintIndex) { + // System.out.println("Getting color somewhere (sorry about the spam)"); + assert blockPos != null; + World minecraftWorld = MinecraftClient.getInstance().world; + assert minecraftWorld != null; + int totalRed = 0; + int totalGreen = 0; + int totalBlue = 0; + // System.out.println("Getting color values"); + for (int xOffset = -1; xOffset <= 1; ++xOffset) { + for (int zOffset = -1; zOffset <= 1; ++zOffset) { + RegistryEntry registryEntry = minecraftWorld.getBiome(blockPos.add(xOffset, 0, zOffset)); + Biome biome = registryEntry.value(); + int grassColor = biome.getGrassColorAt(blockPos.getX() + xOffset, blockPos.getZ() + zOffset); + // System.out.println("Grass Color: 0x" + Integer.toHexString(grassColor)); + totalRed += (grassColor & 16711680) >> 16; + totalGreen += (grassColor & 65280) >> 8; + totalBlue += grassColor & 255; + } + } + int averageRed = totalRed / 9; + int averageGreen = totalGreen / 9; + int averageBlue = totalBlue / 9; + int scaledRed = Math.min(255, (int) (averageRed / 1.5)); + int scaledBlue = Math.min(255, (int) (averageGreen * 1.5)); + int scaledGreen = Math.min(255, (int) (averageBlue / 1.5)); + // System.out.println("Color values RGB average: " + + // averageRed + "," + + // averageGreen + "," + + // averageBlue + "" + + // ", RGB scaled: " + + // scaledRed + "," + + // scaledGreen + "," + + // scaledBlue + // ); + int color = 0xFF000000 | scaledRed << 16 | scaledGreen << 8 | scaledBlue; + // System.out.println("returning: 0x" + Integer.toHexString(color)); + return color; + } +}