Provide access to container tile entity/entity in item device queries.
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package li.cil.oc2.api.bus.device.provider;
|
||||
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Device query for an item stack.
|
||||
@@ -8,6 +12,20 @@ import net.minecraft.item.ItemStack;
|
||||
* @see ItemDeviceProvider
|
||||
*/
|
||||
public interface ItemDeviceQuery {
|
||||
/**
|
||||
* The {@link TileEntity} that holds the item this query is for.
|
||||
*
|
||||
* @return the {@link TileEntity} hosting the device, if any.
|
||||
*/
|
||||
Optional<TileEntity> getContainerTileEntity();
|
||||
|
||||
/**
|
||||
* The {@link Entity} that holds the item this query is for.
|
||||
*
|
||||
* @return the {@link Entity} hosting the device, if any.
|
||||
*/
|
||||
Optional<Entity> getContainerEntity();
|
||||
|
||||
/**
|
||||
* The item stack this query is performed for.
|
||||
*
|
||||
|
||||
@@ -16,6 +16,7 @@ import li.cil.oc2.common.block.ComputerBlock;
|
||||
import li.cil.oc2.common.bus.AbstractDeviceBusController;
|
||||
import li.cil.oc2.common.bus.TileEntityDeviceBusController;
|
||||
import li.cil.oc2.common.bus.TileEntityDeviceBusElement;
|
||||
import li.cil.oc2.common.bus.device.Devices;
|
||||
import li.cil.oc2.common.bus.device.ItemDeviceInfo;
|
||||
import li.cil.oc2.common.capabilities.Capabilities;
|
||||
import li.cil.oc2.common.container.DeviceItemStackHandler;
|
||||
@@ -38,6 +39,7 @@ import li.cil.sedna.device.serial.UART16550A;
|
||||
import li.cil.sedna.device.virtio.VirtIOFileSystemDevice;
|
||||
import li.cil.sedna.fs.HostFileSystem;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraft.tileentity.ITickableTileEntity;
|
||||
import net.minecraft.util.Direction;
|
||||
@@ -107,10 +109,11 @@ public final class ComputerTileEntity extends AbstractTileEntity implements ITic
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
private final DeviceItemStackHandler memoryItemHandler = new TypedDeviceItemStackHandler(MEMORY_SLOTS, DeviceTypes.MEMORY);
|
||||
private final DeviceItemStackHandler hardDriveItemHandler = new TypedDeviceItemStackHandler(HARD_DRIVE_SLOTS, DeviceTypes.HARD_DRIVE);
|
||||
private final DeviceItemStackHandler flashMemoryItemHandler = new TypedDeviceItemStackHandler(FLASH_MEMORY_SLOTS, DeviceTypes.FLASH_MEMORY);
|
||||
private final DeviceItemStackHandler cardItemHandler = new TypedDeviceItemStackHandler(CARD_SLOTS, DeviceTypes.CARD);
|
||||
private final DeviceItemStackHandler memoryItemHandler = new TypedDeviceItemStackHandler(MEMORY_SLOTS, this::getDevices, DeviceTypes.MEMORY);
|
||||
private final DeviceItemStackHandler hardDriveItemHandler = new TypedDeviceItemStackHandler(HARD_DRIVE_SLOTS, this::getDevices, DeviceTypes.HARD_DRIVE);
|
||||
private final DeviceItemStackHandler flashMemoryItemHandler = new TypedDeviceItemStackHandler(FLASH_MEMORY_SLOTS, this::getDevices, DeviceTypes.FLASH_MEMORY);
|
||||
private final DeviceItemStackHandler cardItemHandler = new TypedDeviceItemStackHandler(CARD_SLOTS, this::getDevices, DeviceTypes.CARD);
|
||||
|
||||
private final IItemHandler itemHandlers = new CombinedInvWrapper(memoryItemHandler, hardDriveItemHandler, flashMemoryItemHandler, cardItemHandler);
|
||||
|
||||
private final Terminal terminal = new Terminal();
|
||||
@@ -480,6 +483,10 @@ public final class ComputerTileEntity extends AbstractTileEntity implements ITic
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
private List<ItemDeviceInfo> getDevices(final ItemStack stack) {
|
||||
return Devices.getDevices(this, stack);
|
||||
}
|
||||
|
||||
private void setBusState(final AbstractDeviceBusController.BusState value) {
|
||||
if (value == busState) {
|
||||
return;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package li.cil.oc2.common.bus;
|
||||
|
||||
import li.cil.oc2.common.bus.device.Devices;
|
||||
import li.cil.oc2.common.bus.device.ItemDeviceInfo;
|
||||
import li.cil.oc2.common.util.ItemDeviceUtils;
|
||||
import li.cil.oc2.common.util.NBTTagIds;
|
||||
@@ -9,17 +8,22 @@ import net.minecraft.nbt.CompoundNBT;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ItemHandlerDeviceBusElement extends AbstractGroupingItemDeviceBusElement {
|
||||
public ItemHandlerDeviceBusElement(final int slotCount) {
|
||||
private final Function<ItemStack, List<ItemDeviceInfo>> deviceLookup;
|
||||
|
||||
public ItemHandlerDeviceBusElement(final int slotCount, final Function<ItemStack, List<ItemDeviceInfo>> deviceLookup) {
|
||||
super(slotCount);
|
||||
this.deviceLookup = deviceLookup;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
public void updateDevices(final int slot, final ItemStack stack) {
|
||||
if (!stack.isEmpty()) {
|
||||
final HashSet<ItemDeviceInfo> newDevices = new HashSet<>(Devices.getDevices(stack));
|
||||
final HashSet<ItemDeviceInfo> newDevices = new HashSet<>(deviceLookup.apply(stack));
|
||||
importDeviceDataFromItemStack(stack, newDevices);
|
||||
setDevicesForGroup(slot, newDevices);
|
||||
} else {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package li.cil.oc2.common.container;
|
||||
|
||||
import li.cil.oc2.common.bus.ItemHandlerDeviceBusElement;
|
||||
import li.cil.oc2.common.bus.device.ItemDeviceInfo;
|
||||
import li.cil.oc2.common.util.NBTTagIds;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
@@ -8,6 +9,9 @@ import net.minecraft.util.NonNullList;
|
||||
import net.minecraftforge.items.ItemStackHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class DeviceItemStackHandler extends ItemStackHandler {
|
||||
private static final String BUS_ELEMENT_NBT_TAG_NAME = "busElement";
|
||||
|
||||
@@ -17,13 +21,13 @@ public class DeviceItemStackHandler extends ItemStackHandler {
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
public DeviceItemStackHandler(final int size) {
|
||||
this(NonNullList.withSize(size, ItemStack.EMPTY));
|
||||
public DeviceItemStackHandler(final int size, final Function<ItemStack, List<ItemDeviceInfo>> deviceLookup) {
|
||||
this(NonNullList.withSize(size, ItemStack.EMPTY), deviceLookup);
|
||||
}
|
||||
|
||||
public DeviceItemStackHandler(final NonNullList<ItemStack> stacks) {
|
||||
public DeviceItemStackHandler(final NonNullList<ItemStack> stacks, final Function<ItemStack, List<ItemDeviceInfo>> deviceLookup) {
|
||||
super(stacks);
|
||||
this.busElement = new ItemHandlerDeviceBusElement(getSlots());
|
||||
this.busElement = new ItemHandlerDeviceBusElement(getSlots(), deviceLookup);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -2,13 +2,17 @@ package li.cil.oc2.common.container;
|
||||
|
||||
import li.cil.oc2.api.bus.device.DeviceType;
|
||||
import li.cil.oc2.common.bus.device.Devices;
|
||||
import li.cil.oc2.common.bus.device.ItemDeviceInfo;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class TypedDeviceItemStackHandler extends DeviceItemStackHandler {
|
||||
private final DeviceType deviceType;
|
||||
|
||||
public TypedDeviceItemStackHandler(final int size, final DeviceType deviceType) {
|
||||
super(size);
|
||||
public TypedDeviceItemStackHandler(final int size, final Function<ItemStack, List<ItemDeviceInfo>> deviceLookup, final DeviceType deviceType) {
|
||||
super(size, deviceLookup);
|
||||
this.deviceType = deviceType;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user