EnergonRelics/src/main/java/com/thebrokenrail/energonrelics/item/NetworkChipItem.java

96 lines
3.4 KiB
Java
Raw Normal View History

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;
2020-07-13 20:37:21 +00:00
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;
2020-08-03 20:36:20 +00:00
import net.minecraft.text.LiteralText;
2020-07-13 20:37:21 +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.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;
}
2020-07-19 19:12:39 +00:00
public ItemStack create(int id) {
ItemStack stack = new ItemStack(this);
setID(stack, id);
return stack;
}
2020-07-13 20:37:21 +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()) {
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) {
2020-08-03 20:36:20 +00:00
tooltip.add(new TranslatableText("item." + EnergonRelics.NAMESPACE + ".network_chip.tooltip", new LiteralText(String.valueOf(id)).formatted(Formatting.GRAY)).formatted(Formatting.GRAY));
2020-07-13 20:37:21 +00:00
}
}
@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));
}
2020-07-25 17:34:16 +00:00
EnergonRelics.playBeep(world, context.getBlockPos());
2020-07-13 20:37:21 +00:00
return ActionResult.SUCCESS;
} else {
return ActionResult.PASS;
}
}
}