diff --git a/common/src/main/java/dev/architectury/core/RegistryEntry.java b/common/src/main/java/dev/architectury/core/RegistryEntry.java index 31d93c05..0add96fa 100644 --- a/common/src/main/java/dev/architectury/core/RegistryEntry.java +++ b/common/src/main/java/dev/architectury/core/RegistryEntry.java @@ -19,8 +19,41 @@ package dev.architectury.core; +import com.google.common.reflect.TypeToken; +import dev.architectury.injectables.annotations.PlatformOnly; +import net.minecraft.resources.ResourceLocation; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; + /** - * An entry in registries, will extend {@code ForgeRegistryEntry} on forge. + * An entry in registries, will implement methods from {@code IForgeRegistryEntry}. */ public class RegistryEntry { + private final TypeToken token = new TypeToken(getClass()) { + }; + private ResourceLocation registryName = null; + + @ApiStatus.Internal + @PlatformOnly(PlatformOnly.FORGE) + public T setRegistryName(ResourceLocation name) { + if (registryName != null) { + throw new IllegalStateException("Tried to override registry name from previous " + registryName + " to " + name); + } + + registryName = name; + return (T) this; + } + + @Nullable + @ApiStatus.Internal + @PlatformOnly(PlatformOnly.FORGE) + public ResourceLocation getRegistryName() { + return registryName; + } + + @ApiStatus.Internal + @PlatformOnly(PlatformOnly.FORGE) + public Class getRegistryType() { + return (Class) token.getRawType(); + } } diff --git a/forge/src/main/java/dev/architectury/mixin/forge/MixinRegistryEntry.java b/forge/src/main/java/dev/architectury/mixin/forge/MixinRegistryEntry.java deleted file mode 100644 index a2f7bac5..00000000 --- a/forge/src/main/java/dev/architectury/mixin/forge/MixinRegistryEntry.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * This file is part of architectury. - * Copyright (C) 2020, 2021 architectury - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package dev.architectury.mixin.forge; - -import dev.architectury.core.RegistryEntry; -import org.spongepowered.asm.mixin.Mixin; - -@Mixin(RegistryEntry.class) -public class MixinRegistryEntry { -} diff --git a/forge/src/main/java/dev/architectury/plugin/forge/ArchitecturyMixinPlugin.java b/forge/src/main/java/dev/architectury/plugin/forge/ArchitecturyMixinPlugin.java index 39709cf2..a494db79 100644 --- a/forge/src/main/java/dev/architectury/plugin/forge/ArchitecturyMixinPlugin.java +++ b/forge/src/main/java/dev/architectury/plugin/forge/ArchitecturyMixinPlugin.java @@ -19,16 +19,11 @@ package dev.architectury.plugin.forge; -import org.objectweb.asm.Opcodes; -import org.objectweb.asm.tree.AbstractInsnNode; import org.objectweb.asm.tree.ClassNode; -import org.objectweb.asm.tree.MethodInsnNode; -import org.objectweb.asm.tree.MethodNode; import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; import org.spongepowered.asm.mixin.extensibility.IMixinInfo; import java.util.List; -import java.util.Objects; import java.util.Set; public class ArchitecturyMixinPlugin implements IMixinConfigPlugin { @@ -59,26 +54,7 @@ public class ArchitecturyMixinPlugin implements IMixinConfigPlugin { @Override public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { - // Inject our own sugar - switch (mixinClassName) { - case "dev.architectury.mixin.forge.MixinRegistryEntry": - targetClass.superName = "net/minecraftforge/registries/ForgeRegistryEntry"; - for (MethodNode method : targetClass.methods) { - if (Objects.equals(method.name, "")) { - for (AbstractInsnNode insnNode : method.instructions) { - if (insnNode.getOpcode() == Opcodes.INVOKESPECIAL && insnNode instanceof MethodInsnNode) { - MethodInsnNode node = (MethodInsnNode) insnNode; - if (Objects.equals(node.name, "") && Objects.equals(node.owner, "java/lang/Object")) { - node.owner = "net/minecraftforge/registries/ForgeRegistryEntry"; - break; - } - } - } - } - } - targetClass.signature = ";>Lnet/minecraftforge/registries/ForgeRegistryEntry;"; - break; - } + } @Override diff --git a/forge/src/main/resources/architectury.mixins.json b/forge/src/main/resources/architectury.mixins.json index b19e37bf..489fe7cc 100644 --- a/forge/src/main/resources/architectury.mixins.json +++ b/forge/src/main/resources/architectury.mixins.json @@ -11,7 +11,6 @@ "MixinChunkSerializer", "MixinFallingBlockEntity", "MixinItemExtension", - "MixinRegistryEntry", "MixinWorldEvent" ], "injectors": { diff --git a/testmod-common/src/main/java/dev/architectury/test/recipes/TestRecipeSerializer.java b/testmod-common/src/main/java/dev/architectury/test/recipes/TestRecipeSerializer.java new file mode 100644 index 00000000..a2e3c4e6 --- /dev/null +++ b/testmod-common/src/main/java/dev/architectury/test/recipes/TestRecipeSerializer.java @@ -0,0 +1,53 @@ +/* + * This file is part of architectury. + * Copyright (C) 2020, 2021 architectury + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package dev.architectury.test.recipes; + +import com.google.gson.JsonObject; +import dev.architectury.core.RegistryEntry; +import dev.architectury.platform.Platform; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.crafting.CustomRecipe; +import net.minecraft.world.item.crafting.FireworkRocketRecipe; +import net.minecraft.world.item.crafting.RecipeSerializer; + +import java.util.Objects; + +public class TestRecipeSerializer extends RegistryEntry> implements RecipeSerializer { + public TestRecipeSerializer() { + if (Platform.isForge() && !Objects.equals(getRegistryType(), RecipeSerializer.class)) { + throw new IllegalStateException("getRegistryType() must be of type " + RecipeSerializer.class.getName()); + } + } + + @Override + public CustomRecipe fromJson(ResourceLocation id, JsonObject json) { + return new FireworkRocketRecipe(id); + } + + @Override + public CustomRecipe fromNetwork(ResourceLocation id, FriendlyByteBuf buf) { + return new FireworkRocketRecipe(id); + } + + @Override + public void toNetwork(FriendlyByteBuf buf, CustomRecipe recipe) { + } +} diff --git a/testmod-common/src/main/java/dev/architectury/test/registry/TestRegistries.java b/testmod-common/src/main/java/dev/architectury/test/registry/TestRegistries.java index 55c0326b..8caae8dc 100644 --- a/testmod-common/src/main/java/dev/architectury/test/registry/TestRegistries.java +++ b/testmod-common/src/main/java/dev/architectury/test/registry/TestRegistries.java @@ -26,6 +26,7 @@ import dev.architectury.registry.registries.DeferredRegister; import dev.architectury.registry.registries.RegistrySupplier; import dev.architectury.test.TestMod; import dev.architectury.test.entity.TestEntity; +import dev.architectury.test.recipes.TestRecipeSerializer; import dev.architectury.test.registry.objects.EquippableTickingItem; import dev.architectury.test.tab.TestCreativeTabs; import net.minecraft.core.BlockPos; @@ -37,6 +38,8 @@ import net.minecraft.world.entity.EntityType; import net.minecraft.world.food.FoodProperties; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.Item; +import net.minecraft.world.item.crafting.CustomRecipe; +import net.minecraft.world.item.crafting.RecipeSerializer; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; @@ -51,9 +54,11 @@ public class TestRegistries { public static final DeferredRegister BLOCKS = DeferredRegister.create(TestMod.MOD_ID, Registry.BLOCK_REGISTRY); public static final DeferredRegister> ENTITY_TYPES = DeferredRegister.create(TestMod.MOD_ID, Registry.ENTITY_TYPE_REGISTRY); public static final DeferredRegister MOB_EFFECTS = DeferredRegister.create(TestMod.MOD_ID, Registry.MOB_EFFECT_REGISTRY); + public static final DeferredRegister> RECIPE_SERIALIZERS = DeferredRegister.create(TestMod.MOD_ID, Registry.RECIPE_SERIALIZER_REGISTRY); public static final RegistrySupplier TEST_EFFECT = MOB_EFFECTS.register("test_effect", () -> - new MobEffect(MobEffectCategory.NEUTRAL, 0x123456) {}); + new MobEffect(MobEffectCategory.NEUTRAL, 0x123456) { + }); public static final RegistrySupplier TEST_ITEM = ITEMS.register("test_item", () -> new Item(new Item.Properties().tab(TestCreativeTabs.TEST_TAB))); @@ -83,10 +88,13 @@ public class TestRegistries { public static final RegistrySupplier> TEST_ENTITY = ENTITY_TYPES.register("test_entity", () -> TestEntity.TYPE); + public static final RegistrySupplier> TEST_SERIALIZER = RECIPE_SERIALIZERS.register("test_serializer", TestRecipeSerializer::new); + public static void initialize() { MOB_EFFECTS.register(); BLOCKS.register(); ITEMS.register(); ENTITY_TYPES.register(); + RECIPE_SERIALIZERS.register(); } }