package de.ellpeck.naturesaura.items; import de.ellpeck.naturesaura.api.NaturesAuraAPI; import de.ellpeck.naturesaura.entities.EntityEffectInhibitor; import de.ellpeck.naturesaura.reg.IColorProvidingItem; import net.minecraft.client.color.item.ItemColor; import net.minecraft.core.NonNullList; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.InteractionResult; import net.minecraft.world.item.CreativeModeTab; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.context.UseOnContext; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; public class ItemEffectPowder extends ItemImpl implements IColorProvidingItem { public ItemEffectPowder() { super("effect_powder"); } public static ResourceLocation getEffect(ItemStack stack) { if (!stack.hasTag()) return null; var effect = stack.getTag().getString("effect"); if (effect.isEmpty()) return null; return new ResourceLocation(effect); } public static ItemStack setEffect(ItemStack stack, ResourceLocation effect) { stack.getOrCreateTag().putString("effect", effect != null ? effect.toString() : ""); return stack; } @Override public InteractionResult useOn(UseOnContext context) { var level = context.getLevel(); if (!level.isClientSide) { var hit = context.getClickLocation(); var stack = context.getPlayer().getItemInHand(context.getHand()); EntityEffectInhibitor.place(level, stack, hit.x, hit.y + 1, hit.z); stack.setCount(0); } return InteractionResult.SUCCESS; } @Override public void fillItemCategory(CreativeModeTab tab, NonNullList items) { if (this.allowedIn(tab)) { for (var effect : NaturesAuraAPI.EFFECT_POWDERS.keySet()) { var stack = new ItemStack(this); ItemEffectPowder.setEffect(stack, effect); items.add(stack); } } } @Override public Component getName(ItemStack stack) { return Component.translatable(this.getDescriptionId(stack) + "." + ItemEffectPowder.getEffect(stack)); } @Override @OnlyIn(Dist.CLIENT) public ItemColor getItemColor() { return (stack, tintIndex) -> NaturesAuraAPI.EFFECT_POWDERS.getOrDefault(ItemEffectPowder.getEffect(stack), 0xFFFFFF); } }