-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
462 additions
and
467 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package com.ishikyoo.leavesly; | ||
|
||
import com.ishikyoo.leavesly.property.Properties; | ||
import com.ishikyoo.leavesly.support.Support; | ||
import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry; | ||
import net.minecraft.block.Block; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.client.color.world.BiomeColors; | ||
import net.minecraft.state.property.IntProperty; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.world.BlockRenderView; | ||
import net.minecraft.world.biome.FoliageColors; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
public class ColorProvider { | ||
private static final IntProperty SNOW_LAYER = Properties.SNOW_LAYER; | ||
|
||
private static final Block[] BlockArray = Support.getBlocks(); | ||
|
||
private static final List<Block> blockStaticColorsList = new ArrayList<>(); | ||
private static final HashMap<Block, Integer> blockStaticColorsHashMap = new HashMap<>(); | ||
|
||
private static final List<Block> blockFoliageColorsList = new ArrayList<>(); | ||
private static final List<Block> blockGrassColorsList = new ArrayList<>(); | ||
private static final HashMap<Block, Double> blockColorMasksHashMap = new HashMap<>(); | ||
|
||
public static void initialize() { | ||
initializeColorMasks(); | ||
initializeColorProvides(); | ||
} | ||
|
||
private static void initializeColorMasks() { | ||
for (Block block : BlockArray) { | ||
String blockId = Support.getBlockId(block); | ||
if (blockId.equals("azalea_leaves")) | ||
addBlockStaticColor(block,0xC4FF4F, 0.5725490196); | ||
else if (blockId.equals("birch_leaves")) | ||
addBlockStaticColor(block, FoliageColors.getBirchColor(), 0.72549019607); | ||
else if (blockId.equals("cherry_leaves")) | ||
addBlockStaticColor(block,0xDEFF4C, 0.6); | ||
else if (blockId.equals("dark_oak_leaves")) | ||
addBlockFoliageColor(block, 0.72549019607); | ||
else if (blockId.equals("flowering_azalea_leaves")) | ||
addBlockStaticColor(block, 0xC4FF4F, 0.5725490196); | ||
else if (blockId.equals("jungle_leaves")) | ||
addBlockFoliageColor(block, 0.72549019607); | ||
else if (blockId.equals("mangrove_leaves")) | ||
addBlockStaticColor(block, FoliageColors.getBirchColor(), 0.70980392156); | ||
else if (blockId.equals("oak_leaves")) | ||
addBlockFoliageColor(block, 0.73725490196); | ||
else if (blockId.equals("spruce_leaves")) | ||
addBlockStaticColor(block, FoliageColors.getSpruceColor(), 0.60392156862); | ||
else if (blockId.equals("vine")) | ||
addBlockFoliageColor(block, 0.66666666666); | ||
else if (blockId.equals("short_grass")) | ||
addBlockGrassColor(block, 0.72156862745); | ||
else if (blockId.equals("tall_grass")) | ||
addBlockGrassColor(block, 0.67450980392); | ||
else if (blockId.equals("fern")) | ||
addBlockGrassColor(block, 0.64705882352); | ||
else if (blockId.equals("large_fern")) | ||
addBlockGrassColor(block, 0.67450980392); | ||
} | ||
} | ||
|
||
private static void initializeColorProvides() { | ||
Block[] staticColorBlocks = new Block[blockStaticColorsList.size()]; | ||
blockStaticColorsList.toArray(staticColorBlocks); | ||
|
||
Block[] foliageColorBlocks = new Block[blockFoliageColorsList.size()]; | ||
blockFoliageColorsList.toArray(foliageColorBlocks); | ||
|
||
Block[] grassColorBlocks = new Block[blockGrassColorsList.size()]; | ||
blockGrassColorsList.toArray(grassColorBlocks); | ||
|
||
ColorProviderRegistry.BLOCK.register(ColorProvider::getBlockSnowLayeredStaticColor, | ||
staticColorBlocks | ||
); | ||
ColorProviderRegistry.BLOCK.register(ColorProvider::getBlockSnowLayeredFoliageColor, | ||
foliageColorBlocks | ||
); | ||
ColorProviderRegistry.BLOCK.register(ColorProvider::getBlockSnowLayeredGrassColor, | ||
grassColorBlocks | ||
); | ||
} | ||
|
||
private static int getBlockSnowLayeredStaticColor(BlockState state, BlockRenderView world, BlockPos position, int index) { | ||
Block block = state.getBlock(); | ||
int color = blockStaticColorsHashMap.get(block); | ||
int snowLayer = state.get(SNOW_LAYER); | ||
double leavesSnowLayerMask = (double) snowLayer / SNOW_LAYER.getValues().size(); | ||
return getSnowLayeredColor(color, leavesSnowLayerMask); | ||
} | ||
|
||
private static int getBlockSnowLayeredFoliageColor(BlockState state, BlockRenderView world, BlockPos position, int index) { | ||
Block block = state.getBlock(); | ||
int color = getMaskedColor(BiomeColors.getFoliageColor(world, position), blockColorMasksHashMap.get(block)); | ||
int snowLayer = state.get(SNOW_LAYER); | ||
double leavesSnowLayerMask = (double) snowLayer / SNOW_LAYER.getValues().size(); | ||
return getSnowLayeredColor(color, leavesSnowLayerMask); | ||
} | ||
|
||
private static int getBlockSnowLayeredGrassColor(BlockState state, BlockRenderView world, BlockPos position, int index) { | ||
Block block = state.getBlock(); | ||
int color = getMaskedColor(BiomeColors.getGrassColor(world, position), blockColorMasksHashMap.get(block)); | ||
int snowLayer = state.get(SNOW_LAYER); | ||
double leavesSnowLayerMask = (double) snowLayer / SNOW_LAYER.getValues().size(); | ||
return getSnowLayeredColor(color, leavesSnowLayerMask); | ||
} | ||
|
||
private static void addBlockStaticColor(Block block, int color, double mask) { | ||
blockStaticColorsList.add(block); | ||
blockStaticColorsHashMap.put(block, getMaskedColor(color, mask)); | ||
} | ||
|
||
private static void addBlockFoliageColor(Block block, double mask) { | ||
blockFoliageColorsList.add(block); | ||
blockColorMasksHashMap.put(block, mask); | ||
} | ||
|
||
private static void addBlockGrassColor(Block block, double mask) { | ||
blockGrassColorsList.add(block); | ||
blockColorMasksHashMap.put(block, mask); | ||
} | ||
|
||
private static int getMaskedColor(int color, double mask) { | ||
int r = (int) Math.round((color >> 16 & 0xff) * mask); | ||
int g = (int) Math.round((color >> 8 & 0xff) * mask); | ||
int b = (int) Math.round((color & 0xff) * mask); | ||
return r << 16 | g << 8 | b; | ||
} | ||
private static int getSnowLayeredColor(int color, double mask) { | ||
int r = (color >> 16 & 0xff); | ||
int g = (color >> 8 & 0xff); | ||
int b = (color & 0xff); | ||
r += (int) Math.round((0xff - r) * mask); | ||
g += (int) Math.round((0xff - g) * mask); | ||
b += (int) Math.round((0xff - b) * mask); | ||
return r << 16 | g << 8 | b; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.