Skip to content

Commit

Permalink
[24w12a] Block and Item colors
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMCLoveMan committed Mar 28, 2024
1 parent f60ac8b commit 18cec58
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.FluidType;
import net.themcbrothers.uselessmod.UselessMod;
import net.themcbrothers.uselessmod.util.ColorUtils;

import java.util.function.Consumer;
import java.util.function.Supplier;
Expand Down Expand Up @@ -36,7 +37,8 @@ public ResourceLocation getFlowingTexture() {

@Override
public int getTintColor(FluidStack stack) {
return stack.getOrDefault(UselessDataComponents.COLOR.get(), 0xFFFFFFFF);
Integer color = stack.get(UselessDataComponents.COLOR.get());
return color != null ? ColorUtils.fullAlpha(color) : -1;
}
});
}
Expand Down
37 changes: 30 additions & 7 deletions src/main/java/net/themcbrothers/uselessmod/setup/ClientSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
import net.minecraft.client.renderer.entity.player.PlayerRenderer;
import net.minecraft.client.renderer.item.ItemProperties;
import net.minecraft.client.resources.PlayerSkin;
import net.minecraft.core.Holder;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ElytraItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.ModContainer;
Expand All @@ -39,10 +41,12 @@
import net.themcbrothers.uselessmod.client.renderer.entity.layers.UselessElytraLayer;
import net.themcbrothers.uselessmod.core.*;
import net.themcbrothers.uselessmod.util.CoffeeUtils;
import net.themcbrothers.uselessmod.util.ColorUtils;
import net.themcbrothers.uselessmod.world.level.block.UselessSkullBlock;
import net.themcbrothers.uselessmod.world.level.block.entity.CupBlockEntity;
import net.themcbrothers.uselessmod.world.level.block.entity.MachineSupplierBlockEntity;
import net.themcbrothers.uselessmod.world.level.block.entity.PaintedWoolBlockEntity;
import net.themcbrothers.uselessmod.world.level.block.entity.WallClosetBlockEntity;
import org.jetbrains.annotations.Nullable;

public class ClientSetup extends CommonSetup {
Expand Down Expand Up @@ -96,14 +100,14 @@ private void blockColors(final RegisterColorHandlersEvent.Block event) {

event.register(((state, level, pos, tintIndex) -> {
if (level != null && pos != null && level.getBlockEntity(pos) instanceof PaintedWoolBlockEntity canvas) {
return canvas.getColor();
return ColorUtils.fullAlpha(canvas.getColor());
}
return -1;
}), UselessBlocks.PAINTED_WOOL.get());

event.register((state, level, pos, tintIndex) -> {
if (level != null && pos != null && level.getBlockEntity(pos) instanceof CupBlockEntity cup) {
return cup.getCoffeeType().map(CoffeeType::getColor).orElse(-1);
return cup.getCoffeeType().map(CoffeeType::getColor).map(ColorUtils::fullAlpha).orElse(-1);
}
return -1;
}, UselessBlocks.CUP_COFFEE.get());
Expand All @@ -117,26 +121,45 @@ private void blockColors(final RegisterColorHandlersEvent.Block event) {
}
return -1;
}, UselessBlocks.MACHINE_SUPPLIER.get());

event.register((state, level, pos, tintIndex) -> {
if (level != null && pos != null &&
level.getBlockEntity(pos) instanceof WallClosetBlockEntity blockEntity) {
return colors.getColor(blockEntity.getMaterial().defaultBlockState(), level, pos, tintIndex);
}
return -1;
}, UselessBlocks.WALL_CLOSET.get());
}

private void itemColors(final RegisterColorHandlersEvent.Item event) {
final ItemColors colors = event.getItemColors();

event.register(((stack, layer) -> {
Integer color = stack.get(UselessDataComponents.COLOR.get());
return layer == 1 && color != null ? color : -1;
return layer == 1 && color != null ? ColorUtils.fullAlpha(color) : -1;
}), UselessItems.PAINT_BRUSH);

event.register(((stack, layer) -> stack.getOrDefault(UselessDataComponents.COLOR.get(), -1)), UselessBlocks.PAINTED_WOOL);
event.register(((stack, layer) -> {
Integer color = stack.get(UselessDataComponents.COLOR.get());
return color != null ? ColorUtils.fullAlpha(color) : -1;
}), UselessBlocks.PAINTED_WOOL);

event.register((stack, layer) ->
CoffeeUtils.getCoffeeType(stack).map(CoffeeType::getColor).orElse(-1), UselessBlocks.CUP_COFFEE);
event.register((stack, layer) -> CoffeeUtils.getCoffeeType(stack)
.map(CoffeeType::getColor)
.map(ColorUtils::fullAlpha)
.orElse(-1),
UselessBlocks.CUP_COFFEE);

event.register((stack, layer) -> {
BlockState mimic = stack.get(UselessDataComponents.MIMIC.get());
return mimic != null ? colors.getColor(new ItemStack(mimic.getBlock().asItem()), layer) : -1;
return mimic != null ? colors.getColor(new ItemStack(mimic.getBlock()), layer) : -1;
}, UselessBlocks.MACHINE_SUPPLIER);

event.register((stack, layer) -> {
Holder<Block> block = stack.get(UselessDataComponents.WALL_CLOSET_MATERIAL.get());
return block != null ? colors.getColor(new ItemStack(block.value()), layer) : -1;
});

event.register(new DynamicFluidContainerModel.Colors(), UselessItems.BUCKET_PAINT);
}

Expand Down
23 changes: 23 additions & 0 deletions src/main/java/net/themcbrothers/uselessmod/util/ColorUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.themcbrothers.uselessmod.util;

/**
* Some useful methods for colors
*
* @author TheMCLoveMan
*/
public class ColorUtils {
/**
* Returns the given color with a full alpha channel
*
* @param color Color
* @return Color with full alpha
*/
public static int fullAlpha(int color) {
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = color & 0xFF;
int a = 0xFF;

return (a << 24) | (r << 16) | (g << 8) | b;
}
}

0 comments on commit 18cec58

Please sign in to comment.