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.
Twine/src/main/java/com/thebrokenrail/twine/item/DivinerItem.java

79 lines
4.6 KiB
Java
Raw Normal View History

package com.thebrokenrail.twine.item;
import com.thebrokenrail.twine.Twine;
import com.thebrokenrail.twine.component.StageDataComponent;
2020-06-17 21:57:52 +00:00
import com.thebrokenrail.twine.util.ItemUtil;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.item.TooltipContext;
2020-06-18 22:25:33 +00:00
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;
2020-06-18 00:19:08 +00:00
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.LiteralText;
2020-06-17 21:57:52 +00:00
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;
2020-06-15 20:29:28 +00:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
2020-06-17 21:57:52 +00:00
import java.util.List;
public class DivinerItem extends Item {
2020-06-18 22:25:33 +00:00
private static final Text STAGE_COUNT_TEXT = new LiteralText(String.valueOf(Twine.STAGE_COUNT)).formatted(Formatting.WHITE);
public DivinerItem() {
2020-06-18 22:25:33 +00:00
super(new Settings().group(Twine.ITEM_GROUP).maxCount(1));
}
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
ItemStack stack = user.getStackInHand(hand);
if (!world.isClient()) {
StageDataComponent component = StageDataComponent.getFromWorld((ServerWorld) world);
2020-06-15 20:29:28 +00:00
BlockPos pos = user.getBlockPos();
2020-06-18 22:25:33 +00:00
int effectiveStage = component.findEffectiveStageOfChunk((ServerWorld) world, pos);
StageDataComponent.StageData[] personalData = component.getData(user.getUuid());
2020-06-18 22:25:33 +00:00
int personalStage = StageDataComponent.findPersonalStageOfChunk(pos, personalData);
2020-06-18 22:25:33 +00:00
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);
2020-06-18 00:19:08 +00:00
user.sendMessage(new TranslatableText("chat.twine.diviner_info_3", component.findEffectiveCauseOfChunk((ServerWorld) world, pos).shallowCopy().formatted(Formatting.WHITE)).formatted(Formatting.YELLOW), false);
2020-06-18 22:25:33 +00:00
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);
2020-06-18 00:19:08 +00:00
world.playSound(null, user.getBlockPos(), SoundEvents.BLOCK_ENCHANTMENT_TABLE_USE, SoundCategory.PLAYERS, 1f, 1f);
}
return new TypedActionResult<>(ActionResult.SUCCESS, stack);
}
2020-06-17 21:57:52 +00:00
2020-06-18 22:25:33 +00:00
@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);
}
}
2020-06-17 21:57:52 +00:00
@Override
@Environment(EnvType.CLIENT)
public void appendTooltip(ItemStack stack, World world, List<Text> tooltip, TooltipContext context) {
super.appendTooltip(stack, world, tooltip, context);
ItemUtil.addTooltip("item", "diviner", 2, tooltip);
}
}