1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-10 01:10:30 +00:00

Change ComponentLookup to use ServerLevel

Makes this more consistent with the rest of the peripheral code, and our
changes in 1.20.4.
This commit is contained in:
Jonathan Coates 2024-01-15 08:28:59 +00:00
parent 30dc4cb38c
commit 1d6e3f4fc0
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
4 changed files with 12 additions and 21 deletions

View File

@ -6,7 +6,7 @@ package dan200.computercraft.shared.peripheral.generic;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.world.level.Level; import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
@ -32,5 +32,5 @@ public interface ComponentLookup<C extends Runnable> {
* @return The found component, or {@code null} if not present. * @return The found component, or {@code null} if not present.
*/ */
@Nullable @Nullable
Object find(Level level, BlockPos pos, BlockState state, BlockEntity blockEntity, Direction side, C invalidate); Object find(ServerLevel level, BlockPos pos, BlockState state, BlockEntity blockEntity, Direction side, C invalidate);
} }

View File

@ -11,7 +11,7 @@ import dan200.computercraft.core.methods.PeripheralMethod;
import dan200.computercraft.shared.computer.core.ServerContext; import dan200.computercraft.shared.computer.core.ServerContext;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.world.level.Level; import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -43,7 +43,7 @@ public final class GenericPeripheralProvider<C extends Runnable> {
if (!lookups.contains(lookup)) lookups.add(lookup); if (!lookups.contains(lookup)) lookups.add(lookup);
} }
public void forEachMethod(MethodSupplier<PeripheralMethod> methods, Level level, BlockPos pos, Direction side, BlockEntity blockEntity, C invalidate, MethodSupplier.TargetedConsumer<PeripheralMethod> consumer) { public void forEachMethod(MethodSupplier<PeripheralMethod> methods, ServerLevel level, BlockPos pos, Direction side, BlockEntity blockEntity, C invalidate, MethodSupplier.TargetedConsumer<PeripheralMethod> consumer) {
methods.forEachMethod(blockEntity, consumer); methods.forEachMethod(blockEntity, consumer);
for (var lookup : lookups) { for (var lookup : lookups) {
@ -53,17 +53,11 @@ public final class GenericPeripheralProvider<C extends Runnable> {
} }
@Nullable @Nullable
public IPeripheral getPeripheral(Level level, BlockPos pos, Direction side, @Nullable BlockEntity blockEntity, C invalidate) { public IPeripheral getPeripheral(ServerLevel level, BlockPos pos, Direction side, @Nullable BlockEntity blockEntity, C invalidate) {
if (blockEntity == null) return null; if (blockEntity == null) return null;
var server = level.getServer();
if (server == null) {
LOG.warn("Fetching peripherals on a non-server level {}.", level, new IllegalStateException("Fetching peripherals on a non-server level."));
return null;
}
var builder = new GenericPeripheralBuilder(); var builder = new GenericPeripheralBuilder();
forEachMethod(ServerContext.get(server).peripheralMethods(), level, pos, side, blockEntity, invalidate, builder::addMethod); forEachMethod(ServerContext.get(level.getServer()).peripheralMethods(), level, pos, side, blockEntity, invalidate, builder::addMethod);
return builder.toPeripheral(blockEntity, side); return builder.toPeripheral(blockEntity, side);
} }
} }

View File

@ -9,7 +9,7 @@ import dan200.computercraft.shared.peripheral.generic.ComponentLookup;
import dan200.computercraft.shared.peripheral.generic.GenericPeripheralProvider; import dan200.computercraft.shared.peripheral.generic.GenericPeripheralProvider;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.world.level.Level; import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -29,7 +29,7 @@ public final class Peripherals {
genericProvider.registerLookup(lookup); genericProvider.registerLookup(lookup);
} }
public static @Nullable IPeripheral getGenericPeripheral(Level level, BlockPos pos, Direction side, @Nullable BlockEntity blockEntity, Runnable invalidate) { public static @Nullable IPeripheral getGenericPeripheral(ServerLevel level, BlockPos pos, Direction side, @Nullable BlockEntity blockEntity, Runnable invalidate) {
return genericProvider.getPeripheral(level, pos, side, blockEntity, invalidate); return genericProvider.getPeripheral(level, pos, side, blockEntity, invalidate);
} }
} }

View File

@ -12,7 +12,7 @@ import dan200.computercraft.shared.platform.InvalidateCallback;
import dan200.computercraft.shared.util.CapabilityUtil; import dan200.computercraft.shared.util.CapabilityUtil;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.world.level.Level; import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.Capability;
@ -61,7 +61,7 @@ public final class Peripherals {
private record CapabilityLookup<T>(Capability<T> capability) implements ComponentLookup<InvalidateCallback> { private record CapabilityLookup<T>(Capability<T> capability) implements ComponentLookup<InvalidateCallback> {
@Nullable @Nullable
@Override @Override
public T find(Level level, BlockPos pos, BlockState state, BlockEntity blockEntity, Direction side, InvalidateCallback invalidate) { public T find(ServerLevel level, BlockPos pos, BlockState state, BlockEntity blockEntity, Direction side, InvalidateCallback invalidate) {
return CapabilityUtil.unwrap(CapabilityUtil.getCapability(blockEntity, this.capability(), side), invalidate); return CapabilityUtil.unwrap(CapabilityUtil.getCapability(blockEntity, this.capability(), side), invalidate);
} }
} }
@ -72,12 +72,9 @@ public final class Peripherals {
} }
@Nullable @Nullable
public static IPeripheral getPeripheral(Level world, BlockPos pos, Direction side, InvalidateCallback invalidate) { public static IPeripheral getPeripheral(ServerLevel world, BlockPos pos, Direction side, InvalidateCallback invalidate) {
return world.isInWorldBounds(pos) && !world.isClientSide ? getPeripheralAt(world, pos, side, invalidate) : null; if (!world.isInWorldBounds(pos)) return null;
}
@Nullable
private static IPeripheral getPeripheralAt(Level world, BlockPos pos, Direction side, InvalidateCallback invalidate) {
var block = world.getBlockEntity(pos); var block = world.getBlockEntity(pos);
if (block != null) { if (block != null) {
var peripheral = block.getCapability(CAPABILITY_PERIPHERAL, side); var peripheral = block.getCapability(CAPABILITY_PERIPHERAL, side);