mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-09-07 04:47:55 +00:00
Use Fabric's Item Lookup for registering media providers
We'll switch to capabilities on the (Neo)Forge side for 1.21, when the capability system is less painful, and then fully deprecate in 1.21.4.
This commit is contained in:
@@ -11,7 +11,6 @@ import dan200.computercraft.api.filesystem.Mount;
|
||||
import dan200.computercraft.api.filesystem.WritableMount;
|
||||
import dan200.computercraft.api.lua.GenericSource;
|
||||
import dan200.computercraft.api.lua.ILuaAPIFactory;
|
||||
import dan200.computercraft.api.media.MediaProvider;
|
||||
import dan200.computercraft.api.media.PrintoutContents;
|
||||
import dan200.computercraft.api.network.PacketNetwork;
|
||||
import dan200.computercraft.api.network.wired.WiredElement;
|
||||
@@ -93,11 +92,6 @@ public abstract class AbstractComputerCraftAPI implements ComputerCraftAPIServic
|
||||
return BundledRedstone.getDefaultOutput(world, pos, side);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void registerMediaProvider(MediaProvider provider) {
|
||||
MediaProviders.register(provider);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final PacketNetwork getWirelessNetwork(MinecraftServer server) {
|
||||
return ServerContext.get(server).wirelessNetwork();
|
||||
|
@@ -1,46 +0,0 @@
|
||||
// Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission.
|
||||
//
|
||||
// SPDX-License-Identifier: LicenseRef-CCPL
|
||||
|
||||
package dan200.computercraft.impl;
|
||||
|
||||
import dan200.computercraft.api.media.IMedia;
|
||||
import dan200.computercraft.api.media.MediaProvider;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
public final class MediaProviders {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(MediaProviders.class);
|
||||
|
||||
private static final Set<MediaProvider> providers = new LinkedHashSet<>();
|
||||
|
||||
private MediaProviders() {
|
||||
}
|
||||
|
||||
public static synchronized void register(MediaProvider provider) {
|
||||
Objects.requireNonNull(provider, "provider cannot be null");
|
||||
providers.add(provider);
|
||||
}
|
||||
|
||||
public static @Nullable IMedia get(ItemStack stack) {
|
||||
if (stack.isEmpty()) return null;
|
||||
|
||||
// Try the handlers in order:
|
||||
for (var mediaProvider : providers) {
|
||||
try {
|
||||
var media = mediaProvider.getMedia(stack);
|
||||
if (media != null) return media;
|
||||
} catch (Exception e) {
|
||||
// Mod misbehaved, ignore it
|
||||
LOG.error("Media provider " + mediaProvider + " errored.", e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -4,9 +4,9 @@
|
||||
|
||||
package dan200.computercraft.shared.peripheral.diskdrive;
|
||||
|
||||
import dan200.computercraft.impl.MediaProviders;
|
||||
import dan200.computercraft.shared.ModRegistry;
|
||||
import dan200.computercraft.shared.common.HorizontalContainerBlock;
|
||||
import dan200.computercraft.shared.platform.PlatformHelper;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
@@ -50,7 +50,7 @@ public class DiskDriveBlock extends HorizontalContainerBlock {
|
||||
var disk = player.getItemInHand(hand);
|
||||
if (disk.isEmpty()) return InteractionResult.PASS;
|
||||
|
||||
if (!level.isClientSide && drive.getDiskStack().isEmpty() && MediaProviders.get(disk) != null) {
|
||||
if (!level.isClientSide && drive.getDiskStack().isEmpty() && PlatformHelper.get().getMedia(disk) != null) {
|
||||
drive.setDiskStack(disk.split(1));
|
||||
}
|
||||
return InteractionResult.sidedSuccess(level.isClientSide);
|
||||
|
@@ -5,7 +5,7 @@
|
||||
package dan200.computercraft.shared.peripheral.diskdrive;
|
||||
|
||||
import dan200.computercraft.api.media.IMedia;
|
||||
import dan200.computercraft.impl.MediaProviders;
|
||||
import dan200.computercraft.shared.platform.PlatformHelper;
|
||||
import net.minecraft.sounds.SoundEvent;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
@@ -23,7 +23,7 @@ record MediaStack(ItemStack stack, @Nullable IMedia media) {
|
||||
if (stack.isEmpty()) return EMPTY;
|
||||
|
||||
var freshStack = stack.copy();
|
||||
return new MediaStack(freshStack, MediaProviders.get(freshStack));
|
||||
return new MediaStack(freshStack, PlatformHelper.get().getMedia(freshStack));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
@@ -7,6 +7,7 @@ package dan200.computercraft.shared.platform;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import com.mojang.brigadier.arguments.ArgumentType;
|
||||
import dan200.computercraft.api.media.IMedia;
|
||||
import dan200.computercraft.api.network.wired.WiredElement;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.shared.config.ConfigFile;
|
||||
@@ -424,4 +425,13 @@ public interface PlatformHelper extends dan200.computercraft.impl.PlatformHelper
|
||||
default boolean canClickRunClientCommand() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a {@link IMedia} instance for an item stack.
|
||||
*
|
||||
* @param stack The stack to look up the media for.
|
||||
* @return The media instance, or {@code null} if not found.
|
||||
*/
|
||||
@Nullable
|
||||
IMedia getMedia(ItemStack stack);
|
||||
}
|
||||
|
@@ -8,6 +8,8 @@ import com.google.auto.service.AutoService;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import com.mojang.brigadier.arguments.ArgumentType;
|
||||
import dan200.computercraft.api.media.IMedia;
|
||||
import dan200.computercraft.api.media.MediaProvider;
|
||||
import dan200.computercraft.api.network.wired.WiredElement;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.impl.AbstractComputerCraftAPI;
|
||||
@@ -227,6 +229,11 @@ public class TestPlatformHelper extends AbstractComputerCraftAPI implements Plat
|
||||
throw new UnsupportedOperationException("Cannot interact with the world inside tests");
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable IMedia getMedia(ItemStack stack) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ContainerTransfer.Slotted wrapContainer(Container container) {
|
||||
throw new UnsupportedOperationException("Cannot wrap container");
|
||||
@@ -248,6 +255,11 @@ public class TestPlatformHelper extends AbstractComputerCraftAPI implements Plat
|
||||
throw new UnsupportedOperationException("Cannot interact with the world inside tests");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerMediaProvider(MediaProvider provider) {
|
||||
throw new UnsupportedOperationException("Cannot register media providers inside tests");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInstalledVersion() {
|
||||
return "1.0";
|
||||
|
Reference in New Issue
Block a user