1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-11-04 23:53:01 +00:00

Construct ComponentAccesses with the BE

After embarrassing, let's do some proper work.

Rather than passing the level and position each time we call
ComponentAccess.get(), we now pass them at construction time (in the
form of the BE). This makes the consuming code a little cleaner, and is
required for the NeoForge changes in 1.20.4.
This commit is contained in:
Jonathan Coates
2024-01-14 17:46:37 +00:00
parent e6ee292850
commit be4512d1c3
10 changed files with 71 additions and 73 deletions

View File

@@ -189,13 +189,13 @@ public class PlatformHelperImpl implements PlatformHelper {
}
@Override
public ComponentAccess<IPeripheral> createPeripheralAccess(Consumer<Direction> invalidate) {
return new PeripheralAccess(invalidate);
public ComponentAccess<IPeripheral> createPeripheralAccess(BlockEntity owner, Consumer<Direction> invalidate) {
return new PeripheralAccess(owner, invalidate);
}
@Override
public ComponentAccess<WiredElement> createWiredElementAccess(Consumer<Direction> invalidate) {
return new CapabilityAccess<>(Capabilities.CAPABILITY_WIRED_ELEMENT, invalidate);
public ComponentAccess<WiredElement> createWiredElementAccess(BlockEntity owner, Consumer<Direction> invalidate) {
return new CapabilityAccess<>(owner, Capabilities.CAPABILITY_WIRED_ELEMENT, invalidate);
}
@Override
@@ -434,11 +434,12 @@ public class PlatformHelperImpl implements PlatformHelper {
}
private abstract static class ComponentAccessImpl<T> implements ComponentAccess<T> {
private final BlockEntity owner;
private final InvalidateCallback[] invalidators;
private @Nullable Level level;
private @Nullable BlockPos pos;
ComponentAccessImpl(Consumer<Direction> invalidate) {
ComponentAccessImpl(BlockEntity owner, Consumer<Direction> invalidate) {
this.owner = owner;
// Generate a cache of invalidation functions so we can guarantee we only ever have one registered per
// capability - there's no way to remove these callbacks!
var invalidators = this.invalidators = new InvalidateCallback[6];
@@ -450,19 +451,19 @@ public class PlatformHelperImpl implements PlatformHelper {
@Nullable
@Override
public T get(ServerLevel level, BlockPos pos, Direction direction) {
if (this.level != null && this.level != level) throw new IllegalStateException("Level has changed");
if (this.pos != null && this.pos != pos) throw new IllegalStateException("Position has changed");
this.level = level;
this.pos = pos;
return get(level, pos.relative(direction), direction.getOpposite(), invalidators[direction.ordinal()]);
public T get(Direction direction) {
return get(getLevel(), owner.getBlockPos().relative(direction), direction.getOpposite(), invalidators[direction.ordinal()]);
}
final ServerLevel getLevel() {
return Objects.requireNonNull((ServerLevel) owner.getLevel(), "Block entity is not in a level");
}
}
private static class PeripheralAccess extends ComponentAccessImpl<IPeripheral> {
PeripheralAccess(Consumer<Direction> invalidate) {
super(invalidate);
PeripheralAccess(BlockEntity owner, Consumer<Direction> invalidate) {
super(owner, invalidate);
}
@Nullable
@@ -475,8 +476,8 @@ public class PlatformHelperImpl implements PlatformHelper {
private static class CapabilityAccess<T> extends ComponentAccessImpl<T> {
private final Capability<T> capability;
CapabilityAccess(Capability<T> capability, Consumer<Direction> invalidate) {
super(invalidate);
CapabilityAccess(BlockEntity owner, Capability<T> capability, Consumer<Direction> invalidate) {
super(owner, invalidate);
this.capability = capability;
}