Make serialization of grouped bus element more flexible. Pull out device collection to util method.

This commit is contained in:
Florian Nücke
2021-08-03 02:53:36 +02:00
parent a3edc7e216
commit 716c850e44
5 changed files with 41 additions and 30 deletions

View File

@@ -11,6 +11,7 @@ import net.minecraftforge.registries.IForgeRegistryEntry;
import java.util.*;
public abstract class AbstractGroupingDeviceBusElement<TProvider extends IForgeRegistryEntry<TProvider>, TDeviceInfo extends AbstractDeviceInfo<TProvider, ?>> extends AbstractDeviceBusElement {
private static final String GROUPS_TAG_NAME = "groups";
private static final String GROUP_ID_TAG_NAME = "groupId";
private static final String GROUP_DATA_TAG_NAME = "groupData";
@@ -45,7 +46,7 @@ public abstract class AbstractGroupingDeviceBusElement<TProvider extends IForgeR
return groups.get(index);
}
public ListNBT save() {
public CompoundNBT save() {
final ListNBT listTag = new ListNBT();
for (int i = 0; i < groupCount; i++) {
saveGroup(i);
@@ -57,13 +58,18 @@ public abstract class AbstractGroupingDeviceBusElement<TProvider extends IForgeR
listTag.add(sideTag);
}
return listTag;
final CompoundNBT tag = new CompoundNBT();
tag.put(GROUPS_TAG_NAME, listTag);
return tag;
}
public void load(final ListNBT nbt) {
final int count = Math.min(groupCount, nbt.size());
public void load(final CompoundNBT tag) {
final ListNBT listTag = tag.getList(GROUPS_TAG_NAME, NBTTagIds.TAG_COMPOUND);
final int count = Math.min(groupCount, listTag.size());
for (int i = 0; i < count; i++) {
final CompoundNBT sideTag = nbt.getCompound(i);
final CompoundNBT sideTag = listTag.getCompound(i);
if (sideTag.hasUUID(GROUP_ID_TAG_NAME)) {
groupIds[i] = sideTag.getUUID(GROUP_ID_TAG_NAME);

View File

@@ -95,19 +95,9 @@ public class TileEntityDeviceBusElement extends AbstractGroupingBlockDeviceBusEl
return;
}
final HashSet<BlockDeviceInfo> newDevices = collectDevices(world, pos, direction);
final int index = direction.get3DDataValue();
final HashSet<BlockDeviceInfo> newDevices = new HashSet<>();
if (canDetectDevicesTowards(direction)) {
final BlockDeviceQuery query = Devices.makeQuery(world, pos, direction);
for (final LazyOptional<BlockDeviceInfo> deviceInfo : Devices.getDevices(query)) {
deviceInfo.ifPresent(newDevices::add);
deviceInfo.addListener(unused -> handleNeighborChanged(pos));
}
}
collectSyntheticDevices(world, pos, direction, newDevices);
setDevicesForGroup(index, newDevices);
}
@@ -133,7 +123,22 @@ public class TileEntityDeviceBusElement extends AbstractGroupingBlockDeviceBusEl
return canScanContinueTowards(direction);
}
protected void collectSyntheticDevices(final World world, final BlockPos pos, final Direction direction, final HashSet<BlockDeviceInfo> devices) {
protected HashSet<BlockDeviceInfo> collectDevices(final World world, final BlockPos pos, @Nullable final Direction direction) {
final HashSet<BlockDeviceInfo> newDevices = new HashSet<>();
if (canDetectDevicesTowards(direction)) {
final BlockDeviceQuery query = Devices.makeQuery(world, pos, direction);
for (final LazyOptional<BlockDeviceInfo> deviceInfo : Devices.getDevices(query)) {
deviceInfo.ifPresent(newDevices::add);
deviceInfo.addListener(unused -> handleNeighborChanged(pos));
}
}
collectSyntheticDevices(world, pos, direction, newDevices);
return newDevices;
}
protected void collectSyntheticDevices(final World world, final BlockPos pos, @Nullable final Direction direction, final HashSet<BlockDeviceInfo> devices) {
final String blockName = WorldUtils.getBlockName(world, pos);
if (blockName != null) {
devices.add(new BlockDeviceInfo(null, new TypeNameRPCDevice(blockName)));

View File

@@ -50,7 +50,7 @@ public class DeviceItemStackHandler extends FixedSizeItemStackHandler {
return super.serializeNBT();
}
public ListNBT saveDevices() {
public CompoundNBT saveDevices() {
return busElement.save();
}
@@ -58,7 +58,7 @@ public class DeviceItemStackHandler extends FixedSizeItemStackHandler {
super.deserializeNBT(tag);
}
public void loadDevices(final ListNBT tag) {
public void loadDevices(final CompoundNBT tag) {
busElement.load(tag);
for (int slot = 0; slot < getSlots(); slot++) {
busElement.updateDevices(slot, getStackInSlot(slot));

View File

@@ -188,7 +188,7 @@ public final class BusCableTileEntity extends AbstractTileEntity {
@Override
public void load(final BlockState state, final CompoundNBT tag) {
super.load(state, tag);
busElement.load(tag.getList(BUS_ELEMENT_TAG_NAME, NBTTagIds.TAG_COMPOUND));
busElement.load(tag.getCompound(BUS_ELEMENT_TAG_NAME));
deserializeInterfaceNames(tag.getList(INTERFACE_NAMES_TAG_NAME, NBTTagIds.TAG_STRING));
facade = ItemStack.of(tag.getCompound(FACADE_TAG_NAME));
}
@@ -252,11 +252,13 @@ public final class BusCableTileEntity extends AbstractTileEntity {
}
@Override
protected void collectSyntheticDevices(final World world, final BlockPos pos, final Direction direction, final HashSet<BlockDeviceInfo> devices) {
protected void collectSyntheticDevices(final World world, final BlockPos pos, @Nullable final Direction direction, final HashSet<BlockDeviceInfo> devices) {
super.collectSyntheticDevices(world, pos, direction, devices);
final String interfaceName = interfaceNames[direction.get3DDataValue()];
if (!StringUtils.isNullOrEmpty(interfaceName)) {
devices.add(new BlockDeviceInfo(null, new TypeNameRPCDevice(interfaceName)));
if (direction != null) {
final String interfaceName = interfaceNames[direction.get3DDataValue()];
if (!StringUtils.isNullOrEmpty(interfaceName)) {
devices.add(new BlockDeviceInfo(null, new TypeNameRPCDevice(interfaceName)));
}
}
}

View File

@@ -9,7 +9,6 @@ import li.cil.oc2.common.bus.AbstractDeviceBusElement;
import li.cil.oc2.common.bus.device.util.ItemDeviceInfo;
import li.cil.oc2.common.container.DeviceItemStackHandler;
import li.cil.oc2.common.container.TypedDeviceItemStackHandler;
import li.cil.oc2.common.util.NBTTagIds;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraftforge.common.util.LazyOptional;
@@ -135,9 +134,8 @@ public abstract class AbstractVMItemStackHandlers implements VMItemStackHandlers
}
public void saveDevices(final CompoundNBT tag) {
itemHandlers.forEach((deviceType, handler) -> {
tag.put(deviceType.getRegistryName().toString(), handler.saveDevices());
});
itemHandlers.forEach((deviceType, handler) ->
tag.put(deviceType.getRegistryName().toString(), handler.saveDevices()));
}
public CompoundNBT saveDevices() {
@@ -148,7 +146,7 @@ public abstract class AbstractVMItemStackHandlers implements VMItemStackHandlers
public void loadDevices(final CompoundNBT tag) {
itemHandlers.forEach((deviceType, handler) ->
handler.loadDevices(tag.getList(deviceType.getRegistryName().toString(), NBTTagIds.TAG_COMPOUND)));
handler.loadDevices(tag.getCompound(deviceType.getRegistryName().toString())));
}
///////////////////////////////////////////////////////////////////