-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new color functions for items and blocks
- Loading branch information
1 parent
7dc2613
commit 4a07741
Showing
8 changed files
with
272 additions
and
111 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
103 changes: 103 additions & 0 deletions
103
common/src/main/java/dev/latvian/mods/kubejs/block/BlockTintFunction.java
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,103 @@ | ||
package dev.latvian.mods.kubejs.block; | ||
|
||
import dev.latvian.mods.rhino.BaseFunction; | ||
import dev.latvian.mods.rhino.Context; | ||
import dev.latvian.mods.rhino.NativeJavaObject; | ||
import dev.latvian.mods.rhino.Undefined; | ||
import dev.latvian.mods.rhino.mod.util.color.Color; | ||
import dev.latvian.mods.rhino.mod.util.color.SimpleColor; | ||
import dev.latvian.mods.rhino.mod.util.color.SimpleColorWithAlpha; | ||
import dev.latvian.mods.rhino.mod.wrapper.ColorWrapper; | ||
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap; | ||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; | ||
import net.minecraft.client.renderer.BiomeColors; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.world.level.BlockAndTintGetter; | ||
import net.minecraft.world.level.FoliageColor; | ||
import net.minecraft.world.level.GrassColor; | ||
import net.minecraft.world.level.block.RedStoneWireBlock; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
import net.minecraft.world.level.block.state.properties.BlockStateProperties; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.List; | ||
|
||
@FunctionalInterface | ||
public interface BlockTintFunction { | ||
Color getColor(BlockState state, @Nullable BlockAndTintGetter level, @Nullable BlockPos pos, int index); | ||
|
||
record Fixed(Color color) implements BlockTintFunction { | ||
@Override | ||
public Color getColor(BlockState state, @Nullable BlockAndTintGetter level, @Nullable BlockPos pos, int index) { | ||
return color; | ||
} | ||
} | ||
|
||
class Mapped implements BlockTintFunction { | ||
public final Int2ObjectMap<BlockTintFunction> map = new Int2ObjectArrayMap<>(1); | ||
|
||
@Override | ||
public Color getColor(BlockState state, @Nullable BlockAndTintGetter level, @Nullable BlockPos pos, int index) { | ||
var f = map.get(index); | ||
return f == null ? null : f.getColor(state, level, pos, index); | ||
} | ||
} | ||
|
||
BlockTintFunction GRASS = (s, l, p, i) -> new SimpleColor(l == null || p == null ? GrassColor.get(0.5, 1.0) : BiomeColors.getAverageGrassColor(l, p)); | ||
Color DEFAULT_FOLIAGE_COLOR = new SimpleColor(FoliageColor.getDefaultColor()); | ||
BlockTintFunction FOLIAGE = (s, l, p, i) -> l == null || p == null ? DEFAULT_FOLIAGE_COLOR : new SimpleColor(BiomeColors.getAverageFoliageColor(l, p)); | ||
Fixed EVERGREEN_FOLIAGE = new Fixed(new SimpleColor(FoliageColor.getEvergreenColor())); | ||
Fixed BIRCH_FOLIAGE = new Fixed(new SimpleColor(FoliageColor.getBirchColor())); | ||
Fixed MANGROVE_FOLIAGE = new Fixed(new SimpleColor(FoliageColor.getMangroveColor())); | ||
BlockTintFunction WATER = (s, l, p, i) -> l == null || p == null ? null : new SimpleColorWithAlpha(BiomeColors.getAverageWaterColor(l, p)); | ||
Color[] REDSTONE_COLORS = new Color[16]; | ||
BlockTintFunction REDSTONE = (state, level, pos, index) -> { | ||
if (REDSTONE_COLORS[0] == null) { | ||
for (int i = 0; i < REDSTONE_COLORS.length; i++) { | ||
REDSTONE_COLORS[i] = new SimpleColor(RedStoneWireBlock.getColorForPower(i)); | ||
} | ||
} | ||
|
||
return REDSTONE_COLORS[state.getValue(BlockStateProperties.POWER)]; | ||
}; | ||
|
||
@Nullable | ||
static BlockTintFunction of(Context cx, Object o) { | ||
if (o == null || Undefined.isUndefined(o)) { | ||
return null; | ||
} else if (o instanceof BlockTintFunction f) { | ||
return f; | ||
} else if (o instanceof List<?> list) { | ||
var map = new Mapped(); | ||
|
||
for (int i = 0; i < list.size(); i++) { | ||
var f = of(cx, list.get(i)); | ||
|
||
if (f != null) { | ||
map.map.put(i, f); | ||
} | ||
} | ||
|
||
return map; | ||
} else if (o instanceof CharSequence) { | ||
var f = switch (o.toString()) { | ||
case "grass" -> GRASS; | ||
case "foliage" -> FOLIAGE; | ||
case "evergreen_foliage" -> EVERGREEN_FOLIAGE; | ||
case "birch_foliage" -> BIRCH_FOLIAGE; | ||
case "mangrove_foliage" -> MANGROVE_FOLIAGE; | ||
case "water" -> WATER; | ||
case "redstone" -> REDSTONE; | ||
default -> null; | ||
}; | ||
|
||
if (f != null) { | ||
return f; | ||
} | ||
} else if (o instanceof BaseFunction function) { | ||
return (BlockTintFunction) NativeJavaObject.createInterfaceAdapter(cx, BlockTintFunction.class, function); | ||
} | ||
|
||
return new Fixed(ColorWrapper.of(o)); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
common/src/main/java/dev/latvian/mods/kubejs/client/BlockTintFunctionWrapper.java
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,16 @@ | ||
package dev.latvian.mods.kubejs.client; | ||
|
||
import dev.latvian.mods.kubejs.block.BlockTintFunction; | ||
import net.minecraft.client.color.block.BlockColor; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.world.level.BlockAndTintGetter; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public record BlockTintFunctionWrapper(BlockTintFunction function) implements BlockColor { | ||
@Override | ||
public int getColor(BlockState state, @Nullable BlockAndTintGetter level, @Nullable BlockPos pos, int index) { | ||
var c = function.getColor(state, level, pos, index); | ||
return c == null ? 0xFFFFFFFF : c.getArgbJS(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
common/src/main/java/dev/latvian/mods/kubejs/client/ItemTintFunctionWrapper.java
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,13 @@ | ||
package dev.latvian.mods.kubejs.client; | ||
|
||
import dev.latvian.mods.kubejs.item.ItemTintFunction; | ||
import net.minecraft.client.color.item.ItemColor; | ||
import net.minecraft.world.item.ItemStack; | ||
|
||
public record ItemTintFunctionWrapper(ItemTintFunction function) implements ItemColor { | ||
@Override | ||
public int getColor(ItemStack stack, int index) { | ||
var c = function.getColor(stack, index); | ||
return c == null ? 0xFFFFFFFF : c.getArgbJS(); | ||
} | ||
} |
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.