Automatically inject name of block and item RPC devices.
This commit is contained in:
@@ -5,6 +5,7 @@ import li.cil.oc2.api.bus.device.rpc.RPCMethod;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
@@ -42,6 +43,17 @@ public final class ObjectDevice implements RPCDevice {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new object device with methods in the specified object and the
|
||||
* specified list of type names.
|
||||
*
|
||||
* @param object the object containing methods provided by this device.
|
||||
* @param typeNames the type names of the device.
|
||||
*/
|
||||
public ObjectDevice(final Object object, final String... typeNames) {
|
||||
this(object, Arrays.asList(typeNames));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new object device with methods in the specified object and the specified
|
||||
* type name. For convenience, the type name may be {@code null}, in which case using
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package li.cil.oc2.common.bus;
|
||||
|
||||
import li.cil.oc2.api.bus.device.rpc.RPCDevice;
|
||||
import li.cil.oc2.common.bus.device.rpc.TypeNameRPCDevice;
|
||||
import li.cil.oc2.common.bus.device.util.ItemDeviceInfo;
|
||||
import li.cil.oc2.common.util.ItemDeviceUtils;
|
||||
import li.cil.oc2.common.util.NBTTagIds;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
@@ -24,6 +27,7 @@ public class ItemHandlerDeviceBusElement extends AbstractGroupingItemDeviceBusEl
|
||||
public void updateDevices(final int slot, final ItemStack stack) {
|
||||
if (!stack.isEmpty()) {
|
||||
final HashSet<ItemDeviceInfo> newDevices = new HashSet<>(deviceLookup.apply(stack));
|
||||
insertItemNameDevice(stack, newDevices);
|
||||
importDeviceDataFromItemStack(stack, newDevices);
|
||||
setDevicesForGroup(slot, newDevices);
|
||||
} else {
|
||||
@@ -65,4 +69,13 @@ public class ItemHandlerDeviceBusElement extends AbstractGroupingItemDeviceBusEl
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void insertItemNameDevice(final ItemStack stack, final HashSet<ItemDeviceInfo> devices) {
|
||||
if (devices.stream().anyMatch(info -> info.device instanceof RPCDevice)) {
|
||||
final ResourceLocation registryName = stack.getItem().getRegistryName();
|
||||
if (registryName != null) {
|
||||
devices.add(new ItemDeviceInfo(null, new TypeNameRPCDevice(registryName.toString())));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package li.cil.oc2.common.bus;
|
||||
import li.cil.oc2.api.bus.BlockDeviceBusElement;
|
||||
import li.cil.oc2.api.bus.DeviceBus;
|
||||
import li.cil.oc2.api.bus.DeviceBusElement;
|
||||
import li.cil.oc2.api.bus.device.rpc.RPCDevice;
|
||||
import li.cil.oc2.common.bus.device.rpc.TypeNameRPCDevice;
|
||||
import li.cil.oc2.common.bus.device.util.BlockDeviceInfo;
|
||||
import li.cil.oc2.common.bus.device.util.Devices;
|
||||
import li.cil.oc2.common.capabilities.Capabilities;
|
||||
@@ -101,6 +103,8 @@ public class TileEntityDeviceBusElement extends AbstractGroupingBlockDeviceBusEl
|
||||
}
|
||||
}
|
||||
|
||||
insertBlockNameDevice(world, pos, newDevices);
|
||||
|
||||
setDevicesForGroup(index, newDevices);
|
||||
}
|
||||
|
||||
@@ -153,4 +157,13 @@ public class TileEntityDeviceBusElement extends AbstractGroupingBlockDeviceBusEl
|
||||
capability.ifPresent(DeviceBus::scheduleScan);
|
||||
}
|
||||
}
|
||||
|
||||
private void insertBlockNameDevice(final World world, final BlockPos pos, final HashSet<BlockDeviceInfo> devices) {
|
||||
if (devices.stream().anyMatch(info -> info.device instanceof RPCDevice)) {
|
||||
final String blockName = WorldUtils.getBlockName(world, pos);
|
||||
if (blockName != null) {
|
||||
devices.add(new BlockDeviceInfo(null, new TypeNameRPCDevice(blockName)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import li.cil.oc2.api.bus.device.object.Callbacks;
|
||||
import li.cil.oc2.api.bus.device.object.ObjectDevice;
|
||||
import li.cil.oc2.api.bus.device.provider.BlockDeviceQuery;
|
||||
import li.cil.oc2.common.bus.device.provider.util.AbstractBlockDeviceProvider;
|
||||
import li.cil.oc2.common.util.WorldUtils;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
@@ -29,7 +28,6 @@ public final class BlockStateDeviceProvider extends AbstractBlockDeviceProvider
|
||||
return LazyOptional.empty();
|
||||
}
|
||||
|
||||
final String typeName = WorldUtils.getBlockName(world, position);
|
||||
return LazyOptional.of(() -> new ObjectDevice(block, typeName));
|
||||
return LazyOptional.of(() -> new ObjectDevice(block));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,10 +12,6 @@ import net.minecraftforge.common.util.LazyOptional;
|
||||
import net.minecraftforge.energy.IEnergyStorage;
|
||||
|
||||
public final class EnergyStorageBlockDeviceProvider extends AbstractTileEntityCapabilityDeviceProvider<IEnergyStorage, TileEntity> {
|
||||
private static final String ENERGY_STORAGE_TYPE_NAME = "energyStorage";
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
public EnergyStorageBlockDeviceProvider() {
|
||||
super(() -> Capabilities.ENERGY_STORAGE);
|
||||
}
|
||||
@@ -24,7 +20,7 @@ public final class EnergyStorageBlockDeviceProvider extends AbstractTileEntityCa
|
||||
|
||||
@Override
|
||||
protected LazyOptional<Device> getBlockDevice(final BlockDeviceQuery query, final IEnergyStorage value) {
|
||||
return LazyOptional.of(() -> new ObjectDevice(new EnergyStorageDevice(value), ENERGY_STORAGE_TYPE_NAME));
|
||||
return LazyOptional.of(() -> new ObjectDevice(new EnergyStorageDevice(value), "energy_storage"));
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -13,10 +13,6 @@ import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.capability.IFluidHandler;
|
||||
|
||||
public final class FluidHandlerBlockDeviceProvider extends AbstractTileEntityCapabilityDeviceProvider<IFluidHandler, TileEntity> {
|
||||
private static final String FLUID_HANDLER_TYPE_NAME = "fluidHandler";
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
public FluidHandlerBlockDeviceProvider() {
|
||||
super(() -> Capabilities.FLUID_HANDLER);
|
||||
}
|
||||
@@ -25,7 +21,7 @@ public final class FluidHandlerBlockDeviceProvider extends AbstractTileEntityCap
|
||||
|
||||
@Override
|
||||
protected LazyOptional<Device> getBlockDevice(final BlockDeviceQuery query, final IFluidHandler value) {
|
||||
return LazyOptional.of(() -> new ObjectDevice(new FluidHandlerDevice(value), FLUID_HANDLER_TYPE_NAME));
|
||||
return LazyOptional.of(() -> new ObjectDevice(new FluidHandlerDevice(value), "fluid_handler"));
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -13,10 +13,6 @@ import net.minecraftforge.common.util.LazyOptional;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
|
||||
public final class ItemHandlerBlockDeviceProvider extends AbstractTileEntityCapabilityDeviceProvider<IItemHandler, TileEntity> {
|
||||
private static final String ITEM_HANDLER_TYPE_NAME = "itemHandler";
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
public ItemHandlerBlockDeviceProvider() {
|
||||
super(() -> Capabilities.ITEM_HANDLER);
|
||||
}
|
||||
@@ -25,7 +21,7 @@ public final class ItemHandlerBlockDeviceProvider extends AbstractTileEntityCapa
|
||||
|
||||
@Override
|
||||
protected LazyOptional<Device> getBlockDevice(final BlockDeviceQuery query, final IItemHandler value) {
|
||||
return LazyOptional.of(() -> new ObjectDevice(new ItemHandlerDevice(value), ITEM_HANDLER_TYPE_NAME));
|
||||
return LazyOptional.of(() -> new ObjectDevice(new ItemHandlerDevice(value), "item_handler"));
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -5,7 +5,6 @@ import li.cil.oc2.api.bus.device.object.Callbacks;
|
||||
import li.cil.oc2.api.bus.device.object.ObjectDevice;
|
||||
import li.cil.oc2.api.bus.device.provider.BlockDeviceQuery;
|
||||
import li.cil.oc2.common.bus.device.provider.util.AbstractTileEntityDeviceProvider;
|
||||
import li.cil.oc2.common.util.WorldUtils;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraftforge.common.util.LazyOptional;
|
||||
|
||||
@@ -13,10 +12,7 @@ public final class TileEntityDeviceProvider extends AbstractTileEntityDeviceProv
|
||||
@Override
|
||||
public LazyOptional<Device> getBlockDevice(final BlockDeviceQuery query, final TileEntity tileEntity) {
|
||||
if (Callbacks.hasMethods(tileEntity)) {
|
||||
return LazyOptional.of(() -> {
|
||||
final String typeName = WorldUtils.getBlockName(query.getWorld(), query.getQueryPosition());
|
||||
return new ObjectDevice(tileEntity, typeName);
|
||||
});
|
||||
return LazyOptional.of(() -> new ObjectDevice(tileEntity));
|
||||
} else {
|
||||
return LazyOptional.empty();
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ public final class RPCDeviceList implements RPCDevice {
|
||||
return devices.stream()
|
||||
.map(RPCDevice::getTypeNames)
|
||||
.flatMap(Collection::stream)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package li.cil.oc2.common.bus.device.rpc;
|
||||
|
||||
import li.cil.oc2.api.bus.device.ItemDevice;
|
||||
import li.cil.oc2.api.bus.device.rpc.RPCDevice;
|
||||
import li.cil.oc2.api.bus.device.rpc.RPCMethod;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public final class TypeNameRPCDevice implements RPCDevice, ItemDevice {
|
||||
private final String typeName;
|
||||
|
||||
public TypeNameRPCDevice(final String typeName) {
|
||||
this.typeName = typeName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTypeNames() {
|
||||
return Collections.singletonList(typeName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<RPCMethod> getMethods() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
@@ -3,15 +3,16 @@ package li.cil.oc2.common.bus.device.util;
|
||||
import li.cil.oc2.api.bus.device.Device;
|
||||
import net.minecraftforge.registries.IForgeRegistryEntry;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Objects;
|
||||
|
||||
public abstract class AbstractDeviceInfo<TProvider extends IForgeRegistryEntry<TProvider>, TDevice extends Device> {
|
||||
public final TProvider provider;
|
||||
@Nullable public final TProvider provider;
|
||||
public final TDevice device;
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
protected AbstractDeviceInfo(final TProvider provider, final TDevice device) {
|
||||
protected AbstractDeviceInfo(@Nullable final TProvider provider, final TDevice device) {
|
||||
this.provider = provider;
|
||||
this.device = device;
|
||||
}
|
||||
@@ -23,7 +24,7 @@ public abstract class AbstractDeviceInfo<TProvider extends IForgeRegistryEntry<T
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
final AbstractDeviceInfo<?, ?> that = (AbstractDeviceInfo<?, ?>) o;
|
||||
return provider.equals(that.provider) && device.equals(that.device);
|
||||
return Objects.equals(provider, that.provider) && device.equals(that.device);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -3,8 +3,10 @@ package li.cil.oc2.common.bus.device.util;
|
||||
import li.cil.oc2.api.bus.device.Device;
|
||||
import li.cil.oc2.api.bus.device.provider.BlockDeviceProvider;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public final class BlockDeviceInfo extends AbstractDeviceInfo<BlockDeviceProvider, Device> {
|
||||
public BlockDeviceInfo(final BlockDeviceProvider blockDeviceProvider, final Device device) {
|
||||
public BlockDeviceInfo(@Nullable final BlockDeviceProvider blockDeviceProvider, final Device device) {
|
||||
super(blockDeviceProvider, device);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,10 @@ package li.cil.oc2.common.bus.device.util;
|
||||
import li.cil.oc2.api.bus.device.ItemDevice;
|
||||
import li.cil.oc2.api.bus.device.provider.ItemDeviceProvider;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class ItemDeviceInfo extends AbstractDeviceInfo<ItemDeviceProvider, ItemDevice> {
|
||||
public ItemDeviceInfo(final ItemDeviceProvider itemDeviceProvider, final ItemDevice device) {
|
||||
public ItemDeviceInfo(@Nullable final ItemDeviceProvider itemDeviceProvider, final ItemDevice device) {
|
||||
super(itemDeviceProvider, device);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user