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.
EnergonRelics/src/main/java/com/thebrokenrail/energonrelics/item/NetworkChipItem.java

96 lines
3.4 KiB
Java

package com.thebrokenrail.energonrelics.item;
import com.thebrokenrail.energonrelics.EnergonRelics;
import com.thebrokenrail.energonrelics.api.block.entity.core.EnergyReceiverBlockEntity;
import com.thebrokenrail.energonrelics.component.NetworkComponent;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.Entity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.world.ServerWorld;
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.world.World;
import java.util.List;
public class NetworkChipItem extends Item {
public NetworkChipItem() {
super(new Settings().maxCount(1).group(EnergonRelics.ITEM_GROUP));
}
private void setID(ItemStack stack, int id) {
CompoundTag tag = stack.getOrCreateTag();
tag.putInt("NetworkID", id);
}
public int getID(ItemStack stack) {
CompoundTag tag = stack.getOrCreateTag();
if (tag.contains("NetworkID")) {
return tag.getInt("NetworkID");
} else {
return -1;
}
}
private int getOrCreateID(ItemStack stack, NetworkComponent component) {
int id = getID(stack);
if (id == -1) {
id = component.create();
setID(stack, id);
}
return id;
}
public ItemStack create(int id) {
ItemStack stack = new ItemStack(this);
setID(stack, id);
return 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()) {
ServerWorld serverWorld = (ServerWorld) world;
NetworkComponent component = NetworkComponent.getInstance(serverWorld);
getOrCreateID(stack, component);
}
}
@Override
@Environment(EnvType.CLIENT)
public void appendTooltip(ItemStack stack, World world, List<Text> tooltip, TooltipContext context) {
super.appendTooltip(stack, world, tooltip, context);
int id = getID(stack);
if (id != -1) {
tooltip.add(new TranslatableText("item." + EnergonRelics.NAMESPACE + ".network_chip.tooltip", new LiteralText(String.valueOf(id)).formatted(Formatting.GRAY)).formatted(Formatting.GRAY));
}
}
@Override
public ActionResult useOnBlock(ItemUsageContext context) {
World world = context.getWorld();
BlockEntity entity = world.getBlockEntity(context.getBlockPos());
if (entity instanceof EnergyReceiverBlockEntity) {
if (!world.isClient()) {
ServerWorld serverWorld = (ServerWorld) world;
NetworkComponent component = NetworkComponent.getInstance(serverWorld);
((EnergyReceiverBlockEntity) entity).toggle(getOrCreateID(context.getStack(), component));
}
EnergonRelics.playBeep(world, context.getBlockPos());
return ActionResult.SUCCESS;
} else {
return ActionResult.PASS;
}
}
}