This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
RelicCraft/src/main/java/com/thebrokenrail/reliccraft/item/GenerateRelicItem.java

73 lines
2.4 KiB
Java
Raw Permalink Normal View History

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;
2020-04-15 22:53:31 +00:00
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<ItemStack> use(World world, PlayerEntity user, Hand hand) {
ItemStack stack = user.getStackInHand(hand);
if (!world.isClient()) {
2020-04-15 22:53:31 +00:00
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<RelicData> 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<Text> tooltip, TooltipContext context) {
tooltip.add(new TranslatableText("item." + RelicCraft.NAMESPACE + ".generate_relic.tooltip").formatted(Formatting.GRAY));
}
@Override
2020-06-28 16:38:49 +00:00
public boolean hasGlint(ItemStack stack) {
return true;
}
}