88 lines
3.1 KiB
Java
88 lines
3.1 KiB
Java
package com.thebrokenrail.energonrelics.item;
|
|
|
|
import com.thebrokenrail.energonrelics.EnergonRelics;
|
|
import com.thebrokenrail.energonrelics.component.NetworkComponent;
|
|
import com.thebrokenrail.energonrelics.energy.core.EnergyReceiverBlockEntity;
|
|
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.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;
|
|
}
|
|
|
|
@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", String.valueOf(id)).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));
|
|
}
|
|
return ActionResult.SUCCESS;
|
|
} else {
|
|
return ActionResult.PASS;
|
|
}
|
|
}
|
|
}
|