Skip to content

Commit

Permalink
Make scarves show their colors on the item icon!
Browse files Browse the repository at this point in the history
  • Loading branch information
falkreon committed Jan 2, 2024
1 parent 3f96248 commit 2965459
Show file tree
Hide file tree
Showing 5 changed files with 276 additions and 4 deletions.
60 changes: 60 additions & 0 deletions src/main/java/blue/endless/scarves/client/ScarvesClient.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,60 @@
package blue.endless.scarves.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import blue.endless.scarves.ScarvesBlocks;
import blue.endless.scarves.ScarvesItems;
import blue.endless.scarves.ghost.GhostInventoryNetworking;
import blue.endless.scarves.gui.ScarfStaplerGuiDescription;
import blue.endless.scarves.gui.ScarfTableGuiDescription;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents;
import net.minecraft.block.Blocks;
import net.minecraft.block.MapColor;
import net.minecraft.client.gui.screen.ingame.HandledScreens;
import net.minecraft.client.render.LightmapTextureManager;
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.LightType;

public class ScarvesClient implements ClientModInitializer {
public static final double SCARF_GRAVITY = -0.01;
public static final int UNCOLORED_SCARF_TINT = 0xFF_ddc8ab;

private static Map<String, Integer> ALT_COLORS = new HashMap<>();

static {
ALT_COLORS.put("minecraft:block/white_wool", Blocks.WHITE_WOOL.getDefaultMapColor().getRenderColor(MapColor.Brightness.NORMAL));
ALT_COLORS.put("minecraft:block/orange_wool", Blocks.ORANGE_WOOL.getDefaultMapColor().getRenderColor(MapColor.Brightness.NORMAL));
ALT_COLORS.put("minecraft:block/magenta_wool", Blocks.MAGENTA_WOOL.getDefaultMapColor().getRenderColor(MapColor.Brightness.NORMAL));
ALT_COLORS.put("minecraft:block/light_blue_wool", Blocks.LIGHT_BLUE_WOOL.getDefaultMapColor().getRenderColor(MapColor.Brightness.NORMAL));
ALT_COLORS.put("minecraft:block/yellow_wool", Blocks.YELLOW_WOOL.getDefaultMapColor().getRenderColor(MapColor.Brightness.NORMAL));
ALT_COLORS.put("minecraft:block/lime_wool", Blocks.LIME_WOOL.getDefaultMapColor().getRenderColor(MapColor.Brightness.NORMAL));
ALT_COLORS.put("minecraft:block/pink_wool", Blocks.PINK_WOOL.getDefaultMapColor().getRenderColor(MapColor.Brightness.NORMAL));
ALT_COLORS.put("minecraft:block/gray_wool", Blocks.GRAY_WOOL.getDefaultMapColor().getRenderColor(MapColor.Brightness.NORMAL));
ALT_COLORS.put("minecraft:block/light_gray_wool", Blocks.LIGHT_GRAY_WOOL.getDefaultMapColor().getRenderColor(MapColor.Brightness.NORMAL));
ALT_COLORS.put("minecraft:block/cyan_wool", Blocks.CYAN_WOOL.getDefaultMapColor().getRenderColor(MapColor.Brightness.NORMAL));
ALT_COLORS.put("minecraft:block/purple_wool", Blocks.PURPLE_WOOL.getDefaultMapColor().getRenderColor(MapColor.Brightness.NORMAL));
ALT_COLORS.put("minecraft:block/blue_wool", Blocks.BLUE_WOOL.getDefaultMapColor().getRenderColor(MapColor.Brightness.NORMAL));
ALT_COLORS.put("minecraft:block/brown_wool", Blocks.BROWN_WOOL.getDefaultMapColor().getRenderColor(MapColor.Brightness.NORMAL));
ALT_COLORS.put("minecraft:block/green_wool", Blocks.GREEN_WOOL.getDefaultMapColor().getRenderColor(MapColor.Brightness.NORMAL));
ALT_COLORS.put("minecraft:block/red_wool", Blocks.RED_WOOL.getDefaultMapColor().getRenderColor(MapColor.Brightness.NORMAL));
ALT_COLORS.put("minecraft:block/black_wool", Blocks.BLACK_WOOL.getDefaultMapColor().getRenderColor(MapColor.Brightness.NORMAL));
ALT_COLORS.put("minecraft:block/water_still", 0xFF_3f51eb);
ALT_COLORS.put("minecraft:block/lava_still", 0xFF_ff5100);
ALT_COLORS.put("minecraft:block/redstone_block", 0xFF_b50000);
ALT_COLORS.put("minecraft:block/glowstone", 0xFF_f6c921);
ALT_COLORS.put("minecraft:block/bedrock", 0xFF_4a4a4a);
}

@Override
public void onInitializeClient() {
Expand All @@ -27,6 +64,29 @@ public void onInitializeClient() {

HandledScreens.<ScarfStaplerGuiDescription, ScarfStaplerScreen>register(ScarvesBlocks.SCARF_STAPLER_SCREEN_HANDLER, (gui, inventory, title) -> new ScarfStaplerScreen(gui, inventory, title));
HandledScreens.<ScarfTableGuiDescription, ScarfTableScreen>register(ScarvesBlocks.SCARF_TABLE_SCREEN_HANDLER, (gui, inventory, title) -> new ScarfTableScreen(gui, inventory, title));

ColorProviderRegistry.ITEM.register(ScarvesClient::getScarfTint, ScarvesItems.SCARF);
}

public static int getScarfTint(ItemStack stack, int index) {
NbtCompound tag = stack.getNbt();
if (tag == null) return UNCOLORED_SCARF_TINT;
NbtList leftScarf = tag.getList("LeftScarf", NbtElement.COMPOUND_TYPE);
NbtList rightScarf = tag.getList("RightScarf", NbtElement.COMPOUND_TYPE);
NbtList scarf = (leftScarf == null || leftScarf.size() == 0) ? rightScarf : leftScarf;
if (scarf.size() == 0) return UNCOLORED_SCARF_TINT;

NbtCompound square = scarf.getCompound(index % scarf.size());

int color = (square.contains("Color", NbtElement.INT_TYPE)) ? square.getInt("Color") : 0xFF_FFFFFF;
if (color == 0xFF_FFFFFF) {
try {
String id = square.getString("Id");
return ALT_COLORS.getOrDefault(id, 0xFF_FFFFFF);
} catch (Throwable t) {}
}

return color;
}

public static void beforeEntities(WorldRenderContext ctx) {
Expand Down
214 changes: 210 additions & 4 deletions src/main/resources/assets/scarves/models/item/scarf.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,212 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "scarves:item/scarf"
}
"textures": {
"0": "scarves:item/scarf",
"particle": "scarves:item/scarf"
},
"gui_light": "front",
"elements": [
{
"from": [4, 6, 8],
"to": [8, 8, 9],
"faces": {
"north": {"uv": [8, 8, 12, 10], "texture": "#0", "tintindex": 0},
"south": {"uv": [8, 8, 12, 10], "texture": "#0", "tintindex": 0},
"west": {"uv": [11, 8, 12, 10], "texture": "#0", "tintindex": 0},
"up": {"uv": [8, 8, 12, 9], "texture": "#0", "tintindex": 0},
"down": {"uv": [8, 9, 12, 10], "texture": "#0", "tintindex": 0}
}
},
{
"from": [8, 6, 8],
"to": [13, 8, 9],
"faces": {
"north": {"uv": [3, 8, 8, 10], "texture": "#0", "tintindex": 1},
"east": {"uv": [3, 8, 4, 10], "texture": "#0", "tintindex": 1},
"south": {"uv": [3, 8, 8, 10], "texture": "#0", "tintindex": 1},
"up": {"uv": [3, 8, 8, 9], "texture": "#0", "tintindex": 1},
"down": {"uv": [3, 9, 8, 10], "texture": "#0", "tintindex": 1}
}
},
{
"from": [13, 7, 8],
"to": [14, 10, 9],
"faces": {
"north": {"uv": [2, 6, 3, 9], "texture": "#0", "tintindex": 2},
"east": {"uv": [2, 6, 3, 9], "texture": "#0", "tintindex": 2},
"south": {"uv": [2, 6, 3, 9], "texture": "#0", "tintindex": 2},
"up": {"uv": [2, 6, 3, 7], "texture": "#0", "tintindex": 2},
"down": {"uv": [2, 8, 3, 9], "texture": "#0", "tintindex": 2}
}
},
{
"from": [11, 8, 8],
"to": [13, 11, 9],
"faces": {
"north": {"uv": [3, 5, 5, 8], "texture": "#0", "tintindex": 2},
"east": {"uv": [3, 5, 4, 8], "texture": "#0", "tintindex": 2},
"south": {"uv": [5, 5, 3, 8], "texture": "#0", "tintindex": 2},
"west": {"uv": [4, 5, 5, 8], "texture": "#0", "tintindex": 2},
"up": {"uv": [3, 5, 5, 6], "rotation": 180, "texture": "#0", "tintindex": 2}
}
},
{
"from": [7, 9, 8],
"to": [11, 11, 9],
"faces": {
"north": {"uv": [5, 5, 9, 7], "texture": "#0", "tintindex": 3},
"south": {"uv": [5, 5, 9, 7], "texture": "#0", "tintindex": 3},
"up": {"uv": [5, 5, 9, 6], "texture": "#0", "tintindex": 3},
"down": {"uv": [5, 6, 9, 7], "texture": "#0", "tintindex": 3}
}
},
{
"from": [4, 9, 8],
"to": [7, 11, 9],
"faces": {
"north": {"uv": [9, 5, 12, 7], "texture": "#0", "tintindex": 4},
"south": {"uv": [9, 5, 12, 7], "texture": "#0", "tintindex": 4},
"west": {"uv": [11, 5, 12, 7], "texture": "#0", "tintindex": 4},
"up": {"uv": [9, 5, 12, 6], "texture": "#0", "tintindex": 4},
"down": {"uv": [9, 6, 12, 7], "texture": "#0", "tintindex": 4}
}
},
{
"from": [2, 7, 8],
"to": [4, 10, 9],
"faces": {
"north": {"uv": [12, 6, 14, 9], "texture": "#0", "tintindex": 5},
"south": {"uv": [14, 6, 12, 9], "texture": "#0", "tintindex": 5},
"west": {"uv": [13, 6, 14, 9], "texture": "#0", "tintindex": 5},
"up": {"uv": [12, 6, 14, 7], "rotation": 180, "texture": "#0", "tintindex": 5},
"down": {"uv": [12, 8, 14, 9], "texture": "#0", "tintindex": 5}
}
},
{
"from": [4, 8, 8],
"to": [6, 9, 9],
"faces": {
"north": {"uv": [10, 7, 12, 8], "texture": "#0", "tintindex": 5},
"east": {"uv": [10, 7, 11, 8], "texture": "#0", "tintindex": 5},
"south": {"uv": [10, 7, 12, 8], "texture": "#0", "tintindex": 5}
}
},
{
"from": [2, 2, 8],
"to": [7, 4, 9],
"faces": {
"north": {"uv": [9, 12, 14, 14], "texture": "#0", "tintindex": 7},
"east": {"uv": [9, 12, 10, 14], "texture": "#0", "tintindex": 7},
"south": {"uv": [14, 12, 9, 14], "texture": "#0", "tintindex": 7},
"west": {"uv": [13, 12, 14, 14], "texture": "#0", "tintindex": 7},
"up": {"uv": [9, 12, 14, 13], "texture": "#0", "tintindex": 7},
"down": {"uv": [9, 13, 14, 14], "texture": "#0", "tintindex": 7}
}
},
{
"from": [2, 1, 8],
"to": [3, 2, 9],
"faces": {
"north": {"uv": [13, 14, 14, 15], "texture": "#0", "tintindex": 7},
"east": {"uv": [13, 14, 14, 15], "texture": "#0", "tintindex": 7},
"south": {"uv": [13, 14, 14, 15], "texture": "#0", "tintindex": 7},
"west": {"uv": [13, 14, 14, 15], "texture": "#0", "tintindex": 7},
"down": {"uv": [13, 14, 14, 15], "texture": "#0", "tintindex": 7}
}
},
{
"from": [2, 4, 8],
"to": [3, 5, 9],
"faces": {
"north": {"uv": [12, 10, 13, 11], "texture": "#0", "tintindex": 6},
"south": {"uv": [12, 10, 13, 11], "texture": "#0", "tintindex": 6},
"west": {"uv": [12, 10, 13, 11], "texture": "#0", "tintindex": 6},
"up": {"uv": [12, 10, 13, 11], "texture": "#0", "tintindex": 6}
}
},
{
"from": [4, 1, 8],
"to": [5, 2, 9],
"faces": {
"north": {"uv": [11, 14, 12, 15], "texture": "#0", "tintindex": 7},
"east": {"uv": [11, 14, 12, 15], "texture": "#0", "tintindex": 7},
"south": {"uv": [11, 14, 12, 15], "texture": "#0", "tintindex": 7},
"west": {"uv": [11, 14, 12, 15], "texture": "#0", "tintindex": 7},
"down": {"uv": [11, 14, 12, 15], "texture": "#0", "tintindex": 7}
}
},
{
"from": [6, 1, 8],
"to": [7, 2, 9],
"faces": {
"north": {"uv": [9, 14, 10, 15], "texture": "#0", "tintindex": 7},
"east": {"uv": [9, 14, 10, 15], "texture": "#0", "tintindex": 7},
"south": {"uv": [9, 14, 10, 15], "texture": "#0", "tintindex": 7},
"west": {"uv": [9, 14, 10, 15], "texture": "#0", "tintindex": 7},
"down": {"uv": [9, 14, 10, 15], "texture": "#0", "tintindex": 7}
}
},
{
"from": [3, 4, 8],
"to": [6, 6, 9],
"faces": {
"north": {"uv": [10, 10, 13, 12], "texture": "#0", "tintindex": 6},
"east": {"uv": [10, 10, 11, 12], "texture": "#0", "tintindex": 6},
"south": {"uv": [13, 10, 10, 12], "texture": "#0", "tintindex": 6},
"west": {"uv": [12, 10, 13, 12], "texture": "#0", "tintindex": 6},
"up": {"uv": [10, 10, 13, 11], "texture": "#0", "tintindex": 6}
}
}
],
"display": {
"thirdperson_righthand": {
"rotation": [0, 90, 30],
"translation": [0, 0.75, -0.75],
"scale": [0.5, 0.5, 0.5]
},
"thirdperson_lefthand": {
"rotation": [0, 90, 30],
"translation": [0, 0.75, -0.75],
"scale": [0.5, 0.5, 0.5]
},
"firstperson_righthand": {
"rotation": [0, 142, 0],
"translation": [1.75, 1.75, 0],
"scale": [0.5, 0.5, 0.5]
},
"firstperson_lefthand": {
"rotation": [0, 142, 0],
"translation": [1.75, 1.75, 0],
"scale": [0.5, 0.5, 0.5]
},
"ground": {
"translation": [0, 3, 0],
"scale": [0.75, 0.75, 0.75]
},
"gui": {
"rotation": [0, 180, 0],
"translation": [0, 1.5, 0]
},
"head": {
"rotation": [-68, 0, 0],
"translation": [0, -0.5, 2],
"scale": [1.51, 3.6, 1]
},
"fixed": {
"translation": [0, 0, -0.25]
}
},
"groups": [
{
"name": "Loop",
"origin": [0, 0, 0],
"color": 0,
"children": [0, 1, 2, 3, 4, 5, 6, 7]
},
{
"name": "Tassel",
"origin": [0, 0, 0],
"color": 0,
"children": [8, 9, 10, 11, 12, 13]
}
]
}
6 changes: 6 additions & 0 deletions src/main/resources/assets/scarves/models/item/scarf_old.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "scarves:item/scarf"
}
}
Binary file modified src/main/resources/assets/scarves/textures/item/scarf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2965459

Please sign in to comment.