Add API to give devices a reference to their context, to flag stuff as dirty e.g.
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
package li.cil.oc2.api.bus;
|
||||
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.world.level.LevelAccessor;
|
||||
import net.minecraft.world.level.Level;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@@ -21,7 +21,7 @@ public interface BlockDeviceBusElement extends DeviceBusElement {
|
||||
* @return the level the bus lives in.
|
||||
*/
|
||||
@Nullable
|
||||
LevelAccessor getLevel();
|
||||
Level getLevel();
|
||||
|
||||
/**
|
||||
* The position of this bus element.
|
||||
|
||||
@@ -8,6 +8,8 @@ import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraftforge.common.util.INBTSerializable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Base interface for objects that can be registered as devices on a {@link DeviceBus}.
|
||||
* <p>
|
||||
@@ -21,11 +23,39 @@ import net.minecraftforge.common.util.INBTSerializable;
|
||||
* detected.
|
||||
*/
|
||||
public interface Device extends INBTSerializable<CompoundTag> {
|
||||
/**
|
||||
* Called when the device is picked up by a {@link DeviceContainer}.
|
||||
* <p>
|
||||
* This only means the device will be hosted by the specified container, it has no implication
|
||||
* on whether the device is actually used by a computer, or whether that computer is running.
|
||||
*
|
||||
* @param container the container hosting this device.
|
||||
*/
|
||||
default void setDeviceContainer(@Nullable final DeviceContainer container) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Called to serialize this device into its container's persistent storage.
|
||||
* <p>
|
||||
* When using this method, it may be necessary to call {@link DeviceContainer#setChanged()},
|
||||
* similar to how it is necessary to call {@link BlockEntity#setChanged()} when the serialized
|
||||
* representation of it changed. Unless this is called, no guarantee is made this method will
|
||||
* be called.
|
||||
*
|
||||
* @return the serialized state of this device.
|
||||
*/
|
||||
@Override
|
||||
default CompoundTag serializeNBT() {
|
||||
return new CompoundTag();
|
||||
}
|
||||
|
||||
/**
|
||||
* Called to deserialize this device from its container's persistent storage.
|
||||
* <p>
|
||||
* The passed tag will be what was last returned by {@link #serializeNBT()}.
|
||||
*
|
||||
* @param tag the serialized state of this device.
|
||||
*/
|
||||
@Override
|
||||
default void deserializeNBT(final CompoundTag tag) {
|
||||
}
|
||||
|
||||
18
src/main/java/li/cil/oc2/api/bus/device/DeviceContainer.java
Normal file
18
src/main/java/li/cil/oc2/api/bus/device/DeviceContainer.java
Normal file
@@ -0,0 +1,18 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
|
||||
package li.cil.oc2.api.bus.device;
|
||||
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
|
||||
/**
|
||||
* Back-channel for devices to send messages to their containing context, such as Bus Interfaces.
|
||||
*/
|
||||
public interface DeviceContainer {
|
||||
/**
|
||||
* Notifies the container managing a device it is managing has changed.
|
||||
* <p>
|
||||
* This should be called by devices when they need to be serialized. It is the equivalent of the
|
||||
* {@link BlockEntity#setChanged()} method for {@link Device}s .
|
||||
*/
|
||||
void setChanged();
|
||||
}
|
||||
@@ -38,6 +38,17 @@ public abstract class AbstractBlockDeviceBusElement extends AbstractGroupingDevi
|
||||
super(Constants.BLOCK_FACE_COUNT);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
// DeviceContainer
|
||||
|
||||
@Override
|
||||
public void setChanged() {
|
||||
final Level level = getLevel();
|
||||
if (level != null) {
|
||||
ChunkUtils.setLazyUnsaved(level, getPosition());
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
// DeviceBusElement
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
package li.cil.oc2.common.bus;
|
||||
|
||||
import li.cil.oc2.api.bus.device.Device;
|
||||
import li.cil.oc2.api.bus.device.DeviceContainer;
|
||||
import li.cil.oc2.common.util.NBTTagIds;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.ListTag;
|
||||
@@ -10,7 +11,7 @@ import net.minecraft.nbt.ListTag;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.*;
|
||||
|
||||
public abstract class AbstractGroupingDeviceBusElement<TEntry extends AbstractGroupingDeviceBusElement.Entry, TQuery> extends AbstractDeviceBusElement {
|
||||
public abstract class AbstractGroupingDeviceBusElement<TEntry extends AbstractGroupingDeviceBusElement.Entry, TQuery> extends AbstractDeviceBusElement implements DeviceContainer {
|
||||
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";
|
||||
@@ -170,9 +171,11 @@ public abstract class AbstractGroupingDeviceBusElement<TEntry extends AbstractGr
|
||||
}
|
||||
|
||||
protected void onEntryAdded(final TEntry entry) {
|
||||
entry.getDevice().setDeviceContainer(this);
|
||||
}
|
||||
|
||||
protected void onEntryRemoved(final TEntry entry) {
|
||||
entry.getDevice().setDeviceContainer(null);
|
||||
}
|
||||
|
||||
protected void onEntryRemoved(final String dataKey, final CompoundTag data, @Nullable final TQuery query) {
|
||||
|
||||
@@ -171,6 +171,11 @@ public abstract class AbstractVMItemStackHandlers implements VMItemStackHandlers
|
||||
super(groupCount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setChanged() {
|
||||
onChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ItemDeviceQuery makeQuery(final ItemStack stack) {
|
||||
return AbstractVMItemStackHandlers.this.makeQuery(stack);
|
||||
|
||||
Reference in New Issue
Block a user