1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-27 17:34:48 +00:00

Update to Forge's latest registry API

Forge 4.0.18 deprecated a lot of methods and moved where
RegistryEvent.NewRegistry lives, so we needed to update. This does break
the CC API a little bit (sorry!) though given Forge 1.18.2 is still in
flux, that's probably inevitable.
This commit is contained in:
Jonathan Coates 2022-03-19 08:45:14 +00:00
parent 78cce4981a
commit e6339b2847
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
12 changed files with 53 additions and 40 deletions

View File

@ -6,5 +6,5 @@ mod_version=1.100.3
# Minecraft properties (update mods.toml when changing) # Minecraft properties (update mods.toml when changing)
mc_version=1.18.2 mc_version=1.18.2
mapping_version=2022.02.13 mapping_version=2022.02.13
forge_version=40.0.2 forge_version=40.0.19
# NO SERIOUSLY, UPDATE mods.toml WHEN CHANGING # NO SERIOUSLY, UPDATE mods.toml WHEN CHANGING

View File

@ -25,6 +25,6 @@ public abstract class PocketUpgradeDataProvider extends UpgradeDataProvider<IPoc
{ {
public PocketUpgradeDataProvider( @Nonnull DataGenerator generator ) public PocketUpgradeDataProvider( @Nonnull DataGenerator generator )
{ {
super( generator, "Pocket Computer Upgrades", "computercraft/pocket_upgrades", PocketUpgradeSerialiser.TYPE ); super( generator, "Pocket Computer Upgrades", "computercraft/pocket_upgrades", PocketUpgradeSerialiser.REGISTRY_ID );
} }
} }

View File

@ -5,10 +5,13 @@
*/ */
package dan200.computercraft.api.pocket; package dan200.computercraft.api.pocket;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.upgrades.IUpgradeBase; import dan200.computercraft.api.upgrades.IUpgradeBase;
import dan200.computercraft.api.upgrades.UpgradeSerialiser; import dan200.computercraft.api.upgrades.UpgradeSerialiser;
import dan200.computercraft.internal.upgrades.SerialiserWithCraftingItem; import dan200.computercraft.internal.upgrades.SerialiserWithCraftingItem;
import dan200.computercraft.internal.upgrades.SimpleSerialiser; import dan200.computercraft.internal.upgrades.SimpleSerialiser;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.SimpleRecipeSerializer; import net.minecraft.world.item.crafting.SimpleRecipeSerializer;
@ -33,11 +36,12 @@ import java.util.function.Function;
public interface PocketUpgradeSerialiser<T extends IPocketUpgrade> extends UpgradeSerialiser<T, PocketUpgradeSerialiser<?>> public interface PocketUpgradeSerialiser<T extends IPocketUpgrade> extends UpgradeSerialiser<T, PocketUpgradeSerialiser<?>>
{ {
/** /**
* A {@link Class} representing an abstract {@link PocketUpgradeSerialiser}. This is largely intended for use with * The ID for the associated registry.
* Forge Registry methods/classes, such as {@link DeferredRegister} and {@link RegistryManager#getRegistry(Class)}. *
* This is largely intended for use with Forge Registry methods/classes, such as {@link DeferredRegister} and
* {@link RegistryManager#getRegistry(ResourceKey)}.
*/ */
@SuppressWarnings( "unchecked" ) ResourceKey<Registry<PocketUpgradeSerialiser<?>>> REGISTRY_ID = ResourceKey.createRegistryKey( new ResourceLocation( ComputerCraft.MOD_ID, "pocket_upgrade_serialiser" ) );
Class<PocketUpgradeSerialiser<?>> TYPE = (Class<PocketUpgradeSerialiser<?>>) (Class<?>) PocketUpgradeSerialiser.class;
/** /**
* A convenient base class to inherit to implement {@link PocketUpgradeSerialiser}. * A convenient base class to inherit to implement {@link PocketUpgradeSerialiser}.

View File

@ -34,7 +34,7 @@ public abstract class TurtleUpgradeDataProvider extends UpgradeDataProvider<ITur
public TurtleUpgradeDataProvider( DataGenerator generator ) public TurtleUpgradeDataProvider( DataGenerator generator )
{ {
super( generator, "Turtle Upgrades", "computercraft/turtle_upgrades", TurtleUpgradeSerialiser.TYPE ); super( generator, "Turtle Upgrades", "computercraft/turtle_upgrades", TurtleUpgradeSerialiser.REGISTRY_ID );
} }
/** /**

View File

@ -5,10 +5,13 @@
*/ */
package dan200.computercraft.api.turtle; package dan200.computercraft.api.turtle;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.upgrades.IUpgradeBase; import dan200.computercraft.api.upgrades.IUpgradeBase;
import dan200.computercraft.api.upgrades.UpgradeSerialiser; import dan200.computercraft.api.upgrades.UpgradeSerialiser;
import dan200.computercraft.internal.upgrades.SerialiserWithCraftingItem; import dan200.computercraft.internal.upgrades.SerialiserWithCraftingItem;
import dan200.computercraft.internal.upgrades.SimpleSerialiser; import dan200.computercraft.internal.upgrades.SimpleSerialiser;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeSerializer; import net.minecraft.world.item.crafting.RecipeSerializer;
@ -61,11 +64,12 @@ import java.util.function.Function;
public interface TurtleUpgradeSerialiser<T extends ITurtleUpgrade> extends UpgradeSerialiser<T, TurtleUpgradeSerialiser<?>> public interface TurtleUpgradeSerialiser<T extends ITurtleUpgrade> extends UpgradeSerialiser<T, TurtleUpgradeSerialiser<?>>
{ {
/** /**
* A {@link Class} representing an abstract {@link TurtleUpgradeSerialiser}. This is largely intended for use with * The ID for the associated registry.
* Forge Registry methods/classes, such as {@link DeferredRegister} and {@link RegistryManager#getRegistry(Class)}. *
* This is largely intended for use with Forge Registry methods/classes, such as {@link DeferredRegister} and
* {@link RegistryManager#getRegistry(ResourceKey)}.
*/ */
@SuppressWarnings( "unchecked" ) ResourceKey<Registry<TurtleUpgradeSerialiser<?>>> REGISTRY_ID = ResourceKey.createRegistryKey( new ResourceLocation( ComputerCraft.MOD_ID, "turtle_upgrade_serialiser" ) );
Class<TurtleUpgradeSerialiser<?>> TYPE = (Class<TurtleUpgradeSerialiser<?>>) (Class<?>) TurtleUpgradeSerialiser.class;
/** /**
* A convenient base class to inherit to implement {@link TurtleUpgradeSerialiser}. * A convenient base class to inherit to implement {@link TurtleUpgradeSerialiser}.

View File

@ -12,9 +12,11 @@ import com.google.gson.JsonParseException;
import dan200.computercraft.api.turtle.TurtleUpgradeSerialiser; import dan200.computercraft.api.turtle.TurtleUpgradeSerialiser;
import dan200.computercraft.internal.upgrades.SerialiserWithCraftingItem; import dan200.computercraft.internal.upgrades.SerialiserWithCraftingItem;
import dan200.computercraft.internal.upgrades.SimpleSerialiser; import dan200.computercraft.internal.upgrades.SimpleSerialiser;
import net.minecraft.core.Registry;
import net.minecraft.data.DataGenerator; import net.minecraft.data.DataGenerator;
import net.minecraft.data.DataProvider; import net.minecraft.data.DataProvider;
import net.minecraft.data.HashCache; import net.minecraft.data.HashCache;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item; import net.minecraft.world.item.Item;
import net.minecraftforge.registries.RegistryManager; import net.minecraftforge.registries.RegistryManager;
@ -43,16 +45,16 @@ public abstract class UpgradeDataProvider<T extends IUpgradeBase, R extends Upgr
private final DataGenerator generator; private final DataGenerator generator;
private final String name; private final String name;
private final String folder; private final String folder;
private final Class<R> klass; private final ResourceKey<Registry<R>> registry;
private List<T> upgrades; private List<T> upgrades;
protected UpgradeDataProvider( @Nonnull DataGenerator generator, @Nonnull String name, @Nonnull String folder, @Nonnull Class<R> klass ) protected UpgradeDataProvider( @Nonnull DataGenerator generator, @Nonnull String name, @Nonnull String folder, @Nonnull ResourceKey<Registry<R>> registry )
{ {
this.generator = generator; this.generator = generator;
this.name = name; this.name = name;
this.folder = folder; this.folder = folder;
this.klass = klass; this.registry = registry;
} }
/** /**
@ -155,8 +157,8 @@ public abstract class UpgradeDataProvider<T extends IUpgradeBase, R extends Upgr
@Nonnull @Nonnull
public final R existingSerialiser( @Nonnull ResourceLocation id ) public final R existingSerialiser( @Nonnull ResourceLocation id )
{ {
var result = RegistryManager.ACTIVE.getRegistry( klass ).getValue( id ); var result = RegistryManager.ACTIVE.getRegistry( registry ).getValue( id );
if( result == null ) throw new IllegalArgumentException( "No such serialiser " + klass ); if( result == null ) throw new IllegalArgumentException( "No such serialiser " + registry );
return result; return result;
} }

View File

@ -14,7 +14,7 @@ import java.util.stream.Stream;
public final class PocketUpgrades public final class PocketUpgrades
{ {
private static final UpgradeManager<PocketUpgradeSerialiser<?>, IPocketUpgrade> registry = new UpgradeManager<>( private static final UpgradeManager<PocketUpgradeSerialiser<?>, IPocketUpgrade> registry = new UpgradeManager<>(
"pocket computer upgrade", "computercraft/pocket_upgrades", PocketUpgradeSerialiser.TYPE "pocket computer upgrade", "computercraft/pocket_upgrades", PocketUpgradeSerialiser.REGISTRY_ID
); );
private PocketUpgrades() {} private PocketUpgrades() {}

View File

@ -94,10 +94,7 @@ import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.registries.DeferredRegister; import net.minecraftforge.registries.*;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryBuilder;
import net.minecraftforge.registries.RegistryObject;
import java.util.function.BiFunction; import java.util.function.BiFunction;
@ -256,7 +253,7 @@ public final class Registry
public static class ModTurtleSerialisers public static class ModTurtleSerialisers
{ {
static final DeferredRegister<TurtleUpgradeSerialiser<?>> SERIALISERS = DeferredRegister.create( TurtleUpgradeSerialiser.TYPE, ComputerCraft.MOD_ID ); static final DeferredRegister<TurtleUpgradeSerialiser<?>> SERIALISERS = DeferredRegister.create( TurtleUpgradeSerialiser.REGISTRY_ID.location(), ComputerCraft.MOD_ID );
public static final RegistryObject<TurtleUpgradeSerialiser<TurtleSpeaker>> SPEAKER = public static final RegistryObject<TurtleUpgradeSerialiser<TurtleSpeaker>> SPEAKER =
SERIALISERS.register( "speaker", () -> TurtleUpgradeSerialiser.simpleWithCustomItem( TurtleSpeaker::new ) ); SERIALISERS.register( "speaker", () -> TurtleUpgradeSerialiser.simpleWithCustomItem( TurtleSpeaker::new ) );
@ -272,7 +269,7 @@ public final class Registry
public static class ModPocketUpgradeSerialisers public static class ModPocketUpgradeSerialisers
{ {
static final DeferredRegister<PocketUpgradeSerialiser<?>> SERIALISERS = DeferredRegister.create( PocketUpgradeSerialiser.TYPE, ComputerCraft.MOD_ID ); static final DeferredRegister<PocketUpgradeSerialiser<?>> SERIALISERS = DeferredRegister.create( PocketUpgradeSerialiser.REGISTRY_ID, ComputerCraft.MOD_ID );
public static final RegistryObject<PocketUpgradeSerialiser<PocketSpeaker>> SPEAKER = public static final RegistryObject<PocketUpgradeSerialiser<PocketSpeaker>> SPEAKER =
SERIALISERS.register( "speaker", () -> PocketUpgradeSerialiser.simpleWithCustomItem( PocketSpeaker::new ) ); SERIALISERS.register( "speaker", () -> PocketUpgradeSerialiser.simpleWithCustomItem( PocketSpeaker::new ) );
@ -312,17 +309,21 @@ public final class Registry
} }
@SubscribeEvent @SubscribeEvent
public static void registerRegistries( RegistryEvent.NewRegistry event ) public static void registerRegistries( NewRegistryEvent event )
{ {
new RegistryBuilder<TurtleUpgradeSerialiser<?>>() @SuppressWarnings( "unchecked" )
.setName( new ResourceLocation( ComputerCraft.MOD_ID, "turtle_upgrade_serialiser" ) ) Class<TurtleUpgradeSerialiser<?>> turtleType = (Class<TurtleUpgradeSerialiser<?>>) (Class<?>) TurtleUpgradeSerialiser.class;
.setType( TurtleUpgradeSerialiser.TYPE ) event.create( new RegistryBuilder<TurtleUpgradeSerialiser<?>>()
.disableSaving().disableSync().create(); .setName( TurtleUpgradeSerialiser.REGISTRY_ID.location() )
.setType( turtleType )
.disableSaving().disableSync() );
new RegistryBuilder<PocketUpgradeSerialiser<?>>() @SuppressWarnings( "unchecked" )
.setName( new ResourceLocation( ComputerCraft.MOD_ID, "pocket_upgrade_serialiser" ) ) Class<PocketUpgradeSerialiser<?>> pocketType = (Class<PocketUpgradeSerialiser<?>>) (Class<?>) PocketUpgradeSerialiser.class;
.setType( PocketUpgradeSerialiser.TYPE ) event.create( new RegistryBuilder<PocketUpgradeSerialiser<?>>()
.disableSaving().disableSync().create(); .setName( PocketUpgradeSerialiser.REGISTRY_ID.location() )
.setType( pocketType )
.disableSaving().disableSync() );
} }
@SubscribeEvent @SubscribeEvent

View File

@ -14,7 +14,7 @@ import java.util.stream.Stream;
public final class TurtleUpgrades public final class TurtleUpgrades
{ {
private static final UpgradeManager<TurtleUpgradeSerialiser<?>, ITurtleUpgrade> registry = new UpgradeManager<>( private static final UpgradeManager<TurtleUpgradeSerialiser<?>, ITurtleUpgrade> registry = new UpgradeManager<>(
"turtle upgrade", "computercraft/turtle_upgrades", TurtleUpgradeSerialiser.TYPE "turtle upgrade", "computercraft/turtle_upgrades", TurtleUpgradeSerialiser.REGISTRY_ID
); );
private TurtleUpgrades() {} private TurtleUpgrades() {}

View File

@ -9,6 +9,8 @@ import com.google.gson.*;
import dan200.computercraft.ComputerCraft; import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.upgrades.IUpgradeBase; import dan200.computercraft.api.upgrades.IUpgradeBase;
import dan200.computercraft.api.upgrades.UpgradeSerialiser; import dan200.computercraft.api.upgrades.UpgradeSerialiser;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager; import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener; import net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener;
@ -45,16 +47,16 @@ public class UpgradeManager<R extends UpgradeSerialiser<? extends T, R>, T exten
) {} ) {}
private final String kind; private final String kind;
private final Class<R> klass; private final ResourceKey<Registry<R>> registry;
private Map<String, UpgradeWrapper<R, T>> current = Collections.emptyMap(); private Map<String, UpgradeWrapper<R, T>> current = Collections.emptyMap();
private Map<T, UpgradeWrapper<R, T>> currentWrappers = Collections.emptyMap(); private Map<T, UpgradeWrapper<R, T>> currentWrappers = Collections.emptyMap();
public UpgradeManager( @Nonnull String kind, @Nonnull String path, @Nonnull Class<R> klass ) public UpgradeManager( @Nonnull String kind, @Nonnull String path, @Nonnull ResourceKey<Registry<R>> registry )
{ {
super( GSON, path ); super( GSON, path );
this.kind = kind; this.kind = kind;
this.klass = klass; this.registry = registry;
} }
@Nullable @Nullable
@ -126,7 +128,7 @@ public class UpgradeManager<R extends UpgradeSerialiser<? extends T, R>, T exten
var root = GsonHelper.convertToJsonObject( json, "top element" ); var root = GsonHelper.convertToJsonObject( json, "top element" );
var serialiserId = new ResourceLocation( GsonHelper.getAsString( root, "type" ) ); var serialiserId = new ResourceLocation( GsonHelper.getAsString( root, "type" ) );
var serialiser = RegistryManager.ACTIVE.getRegistry( klass ).getValue( serialiserId ); var serialiser = RegistryManager.ACTIVE.getRegistry( registry ).getValue( serialiserId );
if( serialiser == null ) throw new JsonSyntaxException( "Unknown upgrade type '" + serialiserId + "'" ); if( serialiser == null ) throw new JsonSyntaxException( "Unknown upgrade type '" + serialiserId + "'" );
// TODO: Can we track which mod this resource came from and use that instead? It's theoretically possible, // TODO: Can we track which mod this resource came from and use that instead? It's theoretically possible,

View File

@ -42,8 +42,8 @@ public class UpgradesLoadedMessage implements NetworkMessage
public UpgradesLoadedMessage( @Nonnull FriendlyByteBuf buf ) public UpgradesLoadedMessage( @Nonnull FriendlyByteBuf buf )
{ {
turtleUpgrades = fromBytes( buf, RegistryManager.ACTIVE.getRegistry( TurtleUpgradeSerialiser.TYPE ) ); turtleUpgrades = fromBytes( buf, RegistryManager.ACTIVE.getRegistry( TurtleUpgradeSerialiser.REGISTRY_ID ) );
pocketUpgrades = fromBytes( buf, RegistryManager.ACTIVE.getRegistry( PocketUpgradeSerialiser.TYPE ) ); pocketUpgrades = fromBytes( buf, RegistryManager.ACTIVE.getRegistry( PocketUpgradeSerialiser.REGISTRY_ID ) );
} }
private <R extends UpgradeSerialiser<? extends T, R>, T extends IUpgradeBase> Map<String, UpgradeManager.UpgradeWrapper<R, T>> fromBytes( private <R extends UpgradeSerialiser<? extends T, R>, T extends IUpgradeBase> Map<String, UpgradeManager.UpgradeWrapper<R, T>> fromBytes(

View File

@ -21,6 +21,6 @@ CC: Tweaked is a fork of ComputerCraft, adding programmable computers, turtles a
[[dependencies.computercraft]] [[dependencies.computercraft]]
modId="forge" modId="forge"
mandatory=true mandatory=true
versionRange="[40.0.2,41)" versionRange="[40.0.19,41)"
ordering="NONE" ordering="NONE"
side="BOTH" side="BOTH"