package com.thebrokenrail.twine.item; import com.thebrokenrail.twine.Twine; import com.thebrokenrail.twine.component.StageDataComponent; import com.thebrokenrail.twine.util.ItemUtil; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.item.TooltipContext; import net.minecraft.entity.Entity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.server.world.ServerWorld; import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; import net.minecraft.text.LiteralText; 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.TypedActionResult; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import java.util.List; public class DivinerItem extends Item { private static final Text STAGE_COUNT_TEXT = new LiteralText(String.valueOf(Twine.STAGE_COUNT)).formatted(Formatting.WHITE); public DivinerItem() { super(new Settings().group(Twine.ITEM_GROUP).maxCount(1)); } @Override public TypedActionResult use(World world, PlayerEntity user, Hand hand) { ItemStack stack = user.getStackInHand(hand); if (!world.isClient()) { StageDataComponent component = StageDataComponent.getFromWorld((ServerWorld) world); BlockPos pos = user.getBlockPos(); int effectiveStage = component.findEffectiveStageOfChunk((ServerWorld) world, pos); StageDataComponent.StageData[] personalData = component.getData(user.getUuid()); int personalStage = StageDataComponent.findPersonalStageOfChunk(pos, personalData); user.sendMessage(new TranslatableText("chat.twine.diviner_info_1", new LiteralText(String.valueOf(effectiveStage + 1)).formatted(Formatting.WHITE), STAGE_COUNT_TEXT).formatted(Formatting.YELLOW), false); user.sendMessage(new TranslatableText("chat.twine.diviner_info_2", (effectiveStage + 1 >= Twine.STAGE_COUNT ? new TranslatableText("chat.twine.diviner_info_never") : new LiteralText(String.valueOf(Twine.STAGE_TIME - component.findEffectiveTimeOfChunk((ServerWorld) world, pos)))).formatted(Formatting.WHITE)).formatted(Formatting.YELLOW), false); user.sendMessage(new TranslatableText("chat.twine.diviner_info_3", component.findEffectiveCauseOfChunk((ServerWorld) world, pos).shallowCopy().formatted(Formatting.WHITE)).formatted(Formatting.YELLOW), false); user.sendMessage(new TranslatableText("chat.twine.diviner_info_4", new LiteralText(String.valueOf(personalStage + 1)).formatted(Formatting.WHITE), STAGE_COUNT_TEXT).formatted(Formatting.YELLOW), false); user.sendMessage(new TranslatableText("chat.twine.diviner_info_5", new LiteralText(String.valueOf(personalData[personalStage].x)).formatted(Formatting.WHITE), new LiteralText(String.valueOf(personalData[personalStage].z)).formatted(Formatting.WHITE)).formatted(Formatting.YELLOW), false); user.sendMessage(new TranslatableText("chat.twine.diviner_info_6", (personalStage + 1 >= Twine.STAGE_COUNT ? new TranslatableText("chat.twine.diviner_info_never") : new LiteralText(String.valueOf(Twine.STAGE_TIME - personalData[personalStage].time))).formatted(Formatting.WHITE)).formatted(Formatting.YELLOW), false); world.playSound(null, user.getBlockPos(), SoundEvents.BLOCK_ENCHANTMENT_TABLE_USE, SoundCategory.PLAYERS, 1f, 1f); } return new TypedActionResult<>(ActionResult.SUCCESS, stack); } @Override public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) { super.inventoryTick(stack, world, entity, slot, selected); if (!world.isClient() && entity instanceof PlayerEntity) { StageDataComponent component = StageDataComponent.getFromWorld((ServerWorld) world); BlockPos pos = entity.getBlockPos(); int effectiveStage = component.findEffectiveStageOfChunk((ServerWorld) world, pos); ((PlayerEntity) entity).sendMessage(new TranslatableText("chat.twine.diviner_info_1", new LiteralText(String.valueOf(effectiveStage + 1)).formatted(Formatting.WHITE), STAGE_COUNT_TEXT).formatted(Formatting.YELLOW), true); } } @Override @Environment(EnvType.CLIENT) public void appendTooltip(ItemStack stack, World world, List tooltip, TooltipContext context) { super.appendTooltip(stack, world, tooltip, context); ItemUtil.addTooltip("item", "diviner", 2, tooltip); } }