Ver Fonte

Remove hacks around RegistryEntry (#144)

shedaniel há 3 anos atrás
pai
commit
452d0d1bb9

+ 34 - 1
common/src/main/java/me/shedaniel/architectury/core/RegistryEntry.java

@@ -19,8 +19,41 @@
 
 package me.shedaniel.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<T> {
+    private final TypeToken<T> token = new TypeToken<T>(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<T> getRegistryType() {
+        return (Class<T>) token.getRawType();
+    }
 }

+ 0 - 27
forge/src/main/java/me/shedaniel/architectury/mixin/forge/MixinRegistryEntry.java

@@ -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 me.shedaniel.architectury.mixin.forge;
-
-import me.shedaniel.architectury.core.RegistryEntry;
-import org.spongepowered.asm.mixin.Mixin;
-
-@Mixin(RegistryEntry.class)
-public class MixinRegistryEntry<T> {
-}

+ 1 - 25
forge/src/main/java/me/shedaniel/architectury/plugin/forge/ArchitecturyMixinPlugin.java

@@ -19,16 +19,11 @@
 
 package me.shedaniel.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 "me.shedaniel.architectury.mixin.forge.MixinRegistryEntry":
-                targetClass.superName = "net/minecraftforge/registries/ForgeRegistryEntry";
-                for (MethodNode method : targetClass.methods) {
-                    if (Objects.equals(method.name, "<init>")) {
-                        for (AbstractInsnNode insnNode : method.instructions) {
-                            if (insnNode.getOpcode() == Opcodes.INVOKESPECIAL && insnNode instanceof MethodInsnNode) {
-                                MethodInsnNode node = (MethodInsnNode) insnNode;
-                                if (Objects.equals(node.name, "<init>") && Objects.equals(node.owner, "java/lang/Object")) {
-                                    node.owner = "net/minecraftforge/registries/ForgeRegistryEntry";
-                                    break;
-                                }
-                            }
-                        }
-                    }
-                }
-                targetClass.signature = "<T::Lnet/minecraftforge/registries/IForgeRegistryEntry<TT;>;>Lnet/minecraftforge/registries/ForgeRegistryEntry<TT;>;";
-                break;
-        }
+        
     }
     
     @Override

+ 0 - 1
forge/src/main/resources/architectury.mixins.json

@@ -20,7 +20,6 @@
     "MixinBlockEntityExtension",
     "MixinChunkSerializer",
     "MixinItemExtension",
-    "MixinRegistryEntry",
     "MixinWorldEvent",
     "MobSpawnSettingsBuilderAccessor"
   ],

+ 53 - 0
testmod-common/src/main/java/me/shedaniel/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 me.shedaniel.architectury.test.recipes;
+
+import com.google.gson.JsonObject;
+import me.shedaniel.architectury.core.RegistryEntry;
+import me.shedaniel.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<RecipeSerializer<?>> implements RecipeSerializer<CustomRecipe> {
+    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) {
+    }
+}

+ 9 - 1
testmod-common/src/main/java/me/shedaniel/architectury/test/registry/TestRegistries.java

@@ -26,6 +26,7 @@ import me.shedaniel.architectury.registry.DeferredRegister;
 import me.shedaniel.architectury.registry.RegistrySupplier;
 import me.shedaniel.architectury.test.TestMod;
 import me.shedaniel.architectury.test.entity.TestEntity;
+import me.shedaniel.architectury.test.recipes.TestRecipeSerializer;
 import me.shedaniel.architectury.test.registry.objects.EquippableTickingItem;
 import me.shedaniel.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<Block> BLOCKS = DeferredRegister.create(TestMod.MOD_ID, Registry.BLOCK_REGISTRY);
     public static final DeferredRegister<EntityType<?>> ENTITY_TYPES = DeferredRegister.create(TestMod.MOD_ID, Registry.ENTITY_TYPE_REGISTRY);
     public static final DeferredRegister<MobEffect> MOB_EFFECTS = DeferredRegister.create(TestMod.MOD_ID, Registry.MOB_EFFECT_REGISTRY);
+    public static final DeferredRegister<RecipeSerializer<?>> RECIPE_SERIALIZERS = DeferredRegister.create(TestMod.MOD_ID, Registry.RECIPE_SERIALIZER_REGISTRY);
     
     public static final RegistrySupplier<MobEffect> TEST_EFFECT = MOB_EFFECTS.register("test_effect", () ->
-            new MobEffect(MobEffectCategory.NEUTRAL, 0x123456) {});
+            new MobEffect(MobEffectCategory.NEUTRAL, 0x123456) {
+            });
     
     public static final RegistrySupplier<Item> 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<EntityType<TestEntity>> TEST_ENTITY = ENTITY_TYPES.register("test_entity", () -> TestEntity.TYPE);
     
+    public static final RegistrySupplier<RecipeSerializer<CustomRecipe>> 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();
     }
 }