Skip to content

Commit

Permalink
Keycards can now be dyed
Browse files Browse the repository at this point in the history
  • Loading branch information
andersmmg committed May 25, 2024
1 parent 7af320f commit c967567
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

### Changed

- Keycards can now be dyed any color

### Deprecated

### Removed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package com.andersmmg.lockandblock.client;

import com.andersmmg.lockandblock.block.ModBlocks;
import com.andersmmg.lockandblock.item.ModItems;
import com.andersmmg.lockandblock.item.custom.KeycardItem;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry;
import net.minecraft.client.render.RenderLayer;

public class LockAndBlockClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.FORCEFIELD, RenderLayer.getTranslucent());
ColorProviderRegistry.ITEM.register((stack, tintIndex) -> {
if (stack.getItem() instanceof KeycardItem keycardItem) {
return keycardItem.getColor(stack);
}
return 0;
}, ModItems.KEYCARD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import com.andersmmg.lockandblock.block.custom.KeycardReaderBlock;
import com.andersmmg.lockandblock.item.ModItems;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.item.DyeableItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Formatting;
Expand All @@ -15,7 +17,7 @@

import java.util.List;

public class KeycardItem extends Item {
public class KeycardItem extends Item implements DyeableItem {
public KeycardItem(Settings settings) {
super(settings);
}
Expand All @@ -32,6 +34,27 @@ public static void setUuid(String uuid, ItemStack stack) {
stack.getOrCreateNbt().putString(LockAndBlock.CARD_UUID_KEY, uuid);
}

@Override
public boolean hasColor(ItemStack stack) {
return DyeableItem.super.hasColor(stack);
}

@Override
public int getColor(ItemStack stack) {
NbtCompound nbtCompound = stack.getSubNbt("display");
return nbtCompound != null && nbtCompound.contains("color", 99) ? nbtCompound.getInt("color") : 0xFFFFFF;
}

@Override
public void removeColor(ItemStack stack) {
DyeableItem.super.removeColor(stack);
}

@Override
public void setColor(ItemStack stack, int color) {
DyeableItem.super.setColor(stack, color);
}

@Override
public ActionResult useOnBlock(ItemUsageContext context) {
if (context.getPlayer().isSneaking() && context.getWorld().getBlockState(context.getBlockPos()).getBlock() instanceof KeycardReaderBlock keycardReaderBlock) {
Expand Down

0 comments on commit c967567

Please sign in to comment.