package com.thebrokenrail.energonrelics.item; import com.thebrokenrail.energonrelics.EnergonRelics; import com.thebrokenrail.energonrelics.energy.core.EnergyReceiverBlockEntity; import com.thebrokenrail.energonrelics.energy.helper.EnergyGenerator; import net.minecraft.block.entity.BlockEntity; import net.minecraft.item.Item; import net.minecraft.item.ItemUsageContext; import net.minecraft.text.LiteralText; import net.minecraft.text.MutableText; import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; import net.minecraft.util.ActionResult; import net.minecraft.util.Formatting; import net.minecraft.world.World; import java.util.Objects; public class MultimeterItem extends Item { public MultimeterItem() { super(new Settings().maxCount(1).group(EnergonRelics.ITEM_GROUP)); } private Text numberToText(long value) { String str; if (value >= Long.MAX_VALUE) { str = "\u221e"; } else if (value == -1) { str = "N/A"; } else { str = String.valueOf(value); } return new LiteralText(str).formatted(Formatting.WHITE); } @Override public ActionResult useOnBlock(ItemUsageContext context) { World world = context.getWorld(); BlockEntity entity = world.getBlockEntity(context.getBlockPos()); boolean success = false; MutableText text = new LiteralText(""); if (entity instanceof EnergyGenerator) { if (!world.isClient() && context.getPlayer() != null) { text.append(new TranslatableText("text." + EnergonRelics.NAMESPACE + ".energy_available", numberToText(((EnergyGenerator) entity).getDisplayEnergy())).formatted(Formatting.YELLOW)); } success = true; } if (entity instanceof EnergyReceiverBlockEntity) { if (!world.isClient() && context.getPlayer() != null) { if (success) { text.append(" "); } text.append(new TranslatableText("text." + EnergonRelics.NAMESPACE + ".energy_required", numberToText(((EnergyReceiverBlockEntity) entity).getTotalCost())).formatted(Formatting.YELLOW)); } success = true; } if (success) { if (!world.isClient()) { Objects.requireNonNull(context.getPlayer()).sendMessage(text, true); } return ActionResult.SUCCESS; } else { return ActionResult.PASS; } } }