package com.thebrokenrail.reliccraft.item; import com.squareup.moshi.JsonAdapter; import com.squareup.moshi.Moshi; import com.thebrokenrail.reliccraft.RelicCraft; import com.thebrokenrail.reliccraft.data.RelicData; import net.minecraft.client.item.TooltipContext; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundTag; import net.minecraft.stat.Stats; import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; import net.minecraft.util.ActionResult; import net.minecraft.util.Formatting; import net.minecraft.util.Hand; import net.minecraft.util.Rarity; import net.minecraft.util.TypedActionResult; import net.minecraft.world.World; import java.util.List; public class GenerateRelicItem extends Item { public GenerateRelicItem() { super(new Settings().group(RelicCraft.ITEM_GROUP).rarity(Rarity.UNCOMMON)); } @Override public TypedActionResult use(World world, PlayerEntity user, Hand hand) { ItemStack stack = user.getStackInHand(hand); if (!world.isClient()) { user.incrementStat(Stats.USED.getOrCreateStat(this)); RelicCraft.playRelicSound(user); ItemStack newStack; if (RANDOM.nextBoolean()) { newStack = new ItemStack(RelicCraft.ORB_ITEM); } else { newStack = new ItemStack(RelicCraft.STAFF_ITEM); } CompoundTag tag = new CompoundTag(); Moshi moshi = new Moshi.Builder().build(); JsonAdapter jsonAdapter = moshi.adapter(RelicData.class); tag.putString("RelicData", jsonAdapter.toJson(RelicData.generate(RANDOM))); newStack.setTag(tag); if (!user.isCreative()) { stack.decrement(1); } if (!user.inventory.insertStack(newStack.copy())) { user.dropItem(newStack, false); } } return new TypedActionResult<>(ActionResult.SUCCESS, stack); } @Override public void appendTooltip(ItemStack stack, World world, List tooltip, TooltipContext context) { tooltip.add(new TranslatableText("item." + RelicCraft.NAMESPACE + ".generate_relic.tooltip").formatted(Formatting.GRAY)); } @Override public boolean hasGlint(ItemStack stack) { return true; } }