2020-07-13 20:37:21 +00:00
|
|
|
package com.thebrokenrail.energonrelics.item;
|
|
|
|
|
|
|
|
import com.thebrokenrail.energonrelics.EnergonRelics;
|
2020-08-04 17:06:11 +00:00
|
|
|
import com.thebrokenrail.energonrelics.api.block.entity.core.EnergyReceiverBlockEntity;
|
|
|
|
import com.thebrokenrail.energonrelics.api.block.entity.helper.EnergyGenerator;
|
2020-08-16 03:01:24 +00:00
|
|
|
import com.thebrokenrail.energonrelics.api.item.MultimeterExtra;
|
2020-07-13 20:37:21 +00:00
|
|
|
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() {
|
2020-08-20 23:03:47 +00:00
|
|
|
super(new Settings().maxCount(1).group(EnergonRelics.Items.ITEM_GROUP));
|
2020-07-13 20:37:21 +00:00
|
|
|
}
|
|
|
|
|
2020-08-03 20:36:20 +00:00
|
|
|
public static MutableText format(long value) {
|
2020-07-13 20:37:21 +00:00
|
|
|
String str;
|
|
|
|
if (value >= Long.MAX_VALUE) {
|
|
|
|
str = "\u221e";
|
2020-07-23 17:39:40 +00:00
|
|
|
} else if (value == -1) {
|
|
|
|
str = "N/A";
|
2020-07-13 20:37:21 +00:00
|
|
|
} 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("");
|
2020-07-30 19:49:18 +00:00
|
|
|
Text separator = new TranslatableText("text." + EnergonRelics.NAMESPACE + ".multimeter_separator").formatted(Formatting.YELLOW);
|
2020-07-13 20:37:21 +00:00
|
|
|
if (entity instanceof EnergyGenerator) {
|
|
|
|
if (!world.isClient() && context.getPlayer() != null) {
|
2020-08-03 20:36:20 +00:00
|
|
|
text.append(new TranslatableText("text." + EnergonRelics.NAMESPACE + ".energy_available", format(((EnergyGenerator) entity).getDisplayEnergy())).formatted(Formatting.YELLOW));
|
2020-07-13 20:37:21 +00:00
|
|
|
}
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
if (entity instanceof EnergyReceiverBlockEntity) {
|
|
|
|
if (!world.isClient() && context.getPlayer() != null) {
|
|
|
|
if (success) {
|
2020-07-30 19:49:18 +00:00
|
|
|
text.append(separator);
|
2020-07-13 20:37:21 +00:00
|
|
|
}
|
2020-08-03 20:36:20 +00:00
|
|
|
text.append(new TranslatableText("text." + EnergonRelics.NAMESPACE + ".energy_required", format(((EnergyReceiverBlockEntity) entity).getTotalCost())).formatted(Formatting.YELLOW));
|
2020-07-13 20:37:21 +00:00
|
|
|
}
|
|
|
|
success = true;
|
|
|
|
}
|
2020-07-30 19:49:18 +00:00
|
|
|
if (entity instanceof MultimeterExtra) {
|
|
|
|
if (!world.isClient() && context.getPlayer() != null) {
|
2020-08-16 03:01:24 +00:00
|
|
|
MutableText extraText = ((MultimeterExtra) entity).getExtra();
|
|
|
|
if (extraText != null) {
|
|
|
|
if (success) {
|
|
|
|
text.append(separator);
|
|
|
|
}
|
|
|
|
text.append(extraText.formatted(Formatting.YELLOW));
|
2020-07-30 19:49:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
success = true;
|
|
|
|
}
|
2020-07-13 20:37:21 +00:00
|
|
|
if (success) {
|
|
|
|
if (!world.isClient()) {
|
|
|
|
Objects.requireNonNull(context.getPlayer()).sendMessage(text, true);
|
|
|
|
}
|
2020-08-20 23:03:47 +00:00
|
|
|
EnergonRelics.Extras.playBeep(world, context.getBlockPos());
|
2020-07-13 20:37:21 +00:00
|
|
|
return ActionResult.SUCCESS;
|
|
|
|
} else {
|
|
|
|
return ActionResult.PASS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|