From 2392a4c1841587babd2295261e052f34a94b2f45 Mon Sep 17 00:00:00 2001 From: tterrag Date: Wed, 25 Sep 2019 18:01:43 -0400 Subject: [PATCH] Fix new flower pot ctor not being usable with deferred register --- .../minecraft/block/FlowerPotBlock.java.patch | 75 ++++++++----------- .../META-INF/fieldtomethodtransformers.js | 12 +++ .../debug/block/FlowerPotTest.java | 40 +++++----- 3 files changed, 68 insertions(+), 59 deletions(-) diff --git a/patches/minecraft/net/minecraft/block/FlowerPotBlock.java.patch b/patches/minecraft/net/minecraft/block/FlowerPotBlock.java.patch index e92decbe7..845d37032 100644 --- a/patches/minecraft/net/minecraft/block/FlowerPotBlock.java.patch +++ b/patches/minecraft/net/minecraft/block/FlowerPotBlock.java.patch @@ -1,19 +1,19 @@ --- a/net/minecraft/block/FlowerPotBlock.java +++ b/net/minecraft/block/FlowerPotBlock.java -@@ -19,14 +19,35 @@ - import net.minecraft.world.World; - - public class FlowerPotBlock extends Block { -- private static final Map field_196451_b = Maps.newHashMap(); +@@ -23,10 +23,37 @@ protected static final VoxelShape field_196450_a = Block.func_208617_a(5.0D, 0.0D, 5.0D, 11.0D, 6.0D, 11.0D); -- private final Block field_196452_c; -+ private final FlowerPotBlock emptyPot; -+ private final Map, net.minecraftforge.registries.IRegistryDelegate> fullPots; -+ private final net.minecraftforge.registries.IRegistryDelegate field_196452_c; + private final Block field_196452_c; ++ private final Map> fullPots; ++ private final java.util.function.Supplier emptyPot; ++ private final java.util.function.Supplier flowerDelegate; ++ + @Deprecated // Mods should use the constructor below public FlowerPotBlock(Block p_i48395_1_, Block.Properties p_i48395_2_) { -+ this((FlowerPotBlock) Blocks.field_150457_bL, p_i48395_1_, p_i48395_2_); ++ this(Blocks.field_150457_bL == null ? null : () -> (FlowerPotBlock) Blocks.field_150457_bL.delegate.get(), () -> p_i48395_1_.delegate.get(), p_i48395_2_); ++ if (Blocks.field_150457_bL != null) { ++ ((FlowerPotBlock)Blocks.field_150457_bL).addPlant(p_i48395_1_.getRegistryName(), () -> this); ++ } + } + + /** @@ -24,65 +24,56 @@ + * @param p_i48395_1_ The flower block. + * @param p_i48395_2_ + */ -+ public FlowerPotBlock(FlowerPotBlock emptyPot, Block p_i48395_1_, Block.Properties p_i48395_2_) { ++ public FlowerPotBlock(@javax.annotation.Nullable java.util.function.Supplier emptyPot, java.util.function.Supplier p_i48395_1_, Block.Properties p_i48395_2_) { super(p_i48395_2_); - this.field_196452_c = p_i48395_1_; - field_196451_b.put(p_i48395_1_, this); -+ this.field_196452_c = p_i48395_1_.delegate; ++ this.field_196452_c = null; // Unused, redirected by coremod ++ this.flowerDelegate = p_i48395_1_; + if (emptyPot == null) { + this.fullPots = Maps.newHashMap(); -+ this.emptyPot = this; ++ this.emptyPot = null; + } else { ++ this.fullPots = java.util.Collections.emptyMap(); + this.emptyPot = emptyPot; -+ this.fullPots = emptyPot.fullPots; -+ this.fullPots.put(p_i48395_1_.delegate, this.delegate); + } } public VoxelShape func_220053_a(BlockState p_220053_1_, IBlockReader p_220053_2_, BlockPos p_220053_3_, ISelectionContext p_220053_4_) { -@@ -40,9 +61,9 @@ +@@ -40,7 +67,7 @@ public boolean func_220051_a(BlockState p_220051_1_, World p_220051_2_, BlockPos p_220051_3_, PlayerEntity p_220051_4_, Hand p_220051_5_, BlockRayTraceResult p_220051_6_) { ItemStack itemstack = p_220051_4_.func_184586_b(p_220051_5_); Item item = itemstack.func_77973_b(); - Block block = item instanceof BlockItem ? field_196451_b.getOrDefault(((BlockItem)item).func_179223_d(), Blocks.field_150350_a) : Blocks.field_150350_a; -+ Block block = item instanceof BlockItem ? fullPots.getOrDefault(((BlockItem)item).func_179223_d().delegate, Blocks.field_150350_a.delegate).get() : Blocks.field_150350_a; ++ Block block = item instanceof BlockItem ? getEmptyPot().fullPots.getOrDefault(((BlockItem)item).func_179223_d().getRegistryName(), Blocks.field_150350_a.delegate).get() : Blocks.field_150350_a; boolean flag = block == Blocks.field_150350_a; -- boolean flag1 = this.field_196452_c == Blocks.field_150350_a; -+ boolean flag1 = this.field_196452_c.get() == Blocks.field_150350_a; + boolean flag1 = this.field_196452_c == Blocks.field_150350_a; if (flag != flag1) { - if (flag1) { - p_220051_2_.func_180501_a(p_220051_3_, block.func_176223_P(), 3); -@@ -51,14 +72,14 @@ - itemstack.func_190918_g(1); - } - } else { -- ItemStack itemstack1 = new ItemStack(this.field_196452_c); -+ ItemStack itemstack1 = new ItemStack(this.field_196452_c.get()); - if (itemstack.func_190926_b()) { - p_220051_4_.func_184611_a(p_220051_5_, itemstack1); - } else if (!p_220051_4_.func_191521_c(itemstack1)) { +@@ -58,7 +85,7 @@ p_220051_4_.func_71019_a(itemstack1, false); } - p_220051_2_.func_180501_a(p_220051_3_, Blocks.field_150457_bL.func_176223_P(), 3); -+ p_220051_2_.func_180501_a(p_220051_3_, emptyPot.func_176223_P(), 3); ++ p_220051_2_.func_180501_a(p_220051_3_, getEmptyPot().func_176223_P(), 3); } } -@@ -66,7 +87,7 @@ - } - - public ItemStack func_185473_a(IBlockReader p_185473_1_, BlockPos p_185473_2_, BlockState p_185473_3_) { -- return this.field_196452_c == Blocks.field_150350_a ? super.func_185473_a(p_185473_1_, p_185473_2_, p_185473_3_) : new ItemStack(this.field_196452_c); -+ return this.field_196452_c.get() == Blocks.field_150350_a ? super.func_185473_a(p_185473_1_, p_185473_2_, p_185473_3_) : new ItemStack(this.field_196452_c.get()); - } - - public BlockState func_196271_a(BlockState p_196271_1_, Direction p_196271_2_, BlockState p_196271_3_, IWorld p_196271_4_, BlockPos p_196271_5_, BlockPos p_196271_6_) { -@@ -78,6 +99,6 @@ +@@ -78,6 +105,17 @@ } public Block func_220276_d() { - return this.field_196452_c; -+ return this.field_196452_c.get(); ++ return flowerDelegate.get(); } ++ ++ public FlowerPotBlock getEmptyPot() { ++ return emptyPot == null ? this : emptyPot.get(); ++ } ++ ++ public void addPlant(net.minecraft.util.ResourceLocation flower, java.util.function.Supplier fullPot) { ++ if (getEmptyPot() != this) { ++ throw new IllegalArgumentException("Cannot add plant to non-empty pot: " + this); ++ } ++ fullPots.put(flower, fullPot); ++ } } diff --git a/src/main/resources/META-INF/fieldtomethodtransformers.js b/src/main/resources/META-INF/fieldtomethodtransformers.js index e928191b9..d36f845bd 100644 --- a/src/main/resources/META-INF/fieldtomethodtransformers.js +++ b/src/main/resources/META-INF/fieldtomethodtransformers.js @@ -49,6 +49,18 @@ function initializeCoreMod() { asmapi.redirectFieldToMethod(classNode, stateField, 'getModelState') // forge added method, doesn't need mapping return classNode; } + }, + 'flowerpotblock': { + 'target': { + 'type': 'CLASS', + 'name': 'net.minecraft.block.FlowerPotBlock' + }, + 'transformer': function(classNode) { + var asmapi=Java.type('net.minecraftforge.coremod.api.ASMAPI') + var fn = asmapi.mapField('field_196452_c') // flower - remap to mcp if necessary + asmapi.redirectFieldToMethod(classNode, fn, asmapi.mapMethod('func_220276_d')) + return classNode; + } } } } diff --git a/src/test/java/net/minecraftforge/debug/block/FlowerPotTest.java b/src/test/java/net/minecraftforge/debug/block/FlowerPotTest.java index c7d850b3c..68f529c48 100644 --- a/src/test/java/net/minecraftforge/debug/block/FlowerPotTest.java +++ b/src/test/java/net/minecraftforge/debug/block/FlowerPotTest.java @@ -25,35 +25,41 @@ import net.minecraft.block.FlowerPotBlock; import net.minecraft.item.BlockItem; import net.minecraft.item.Item; import net.minecraftforge.event.RegistryEvent; +import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.RegistryObject; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus; -import net.minecraftforge.registries.ObjectHolder; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.minecraftforge.registries.DeferredRegister; +import net.minecraftforge.registries.ForgeRegistries; @Mod(FlowerPotTest.MODID) -@Mod.EventBusSubscriber(bus = Bus.MOD) +@Mod.EventBusSubscriber(modid = FlowerPotTest.MODID, bus = Bus.MOD) public class FlowerPotTest { static final String MODID = "flower_pot_test"; static final String BLOCK_ID = "test_flower_pot"; + + private static final DeferredRegister BLOCKS = new DeferredRegister<>(ForgeRegistries.BLOCKS, MODID); + private static final DeferredRegister ITEMS = new DeferredRegister<>(ForgeRegistries.ITEMS, MODID); - @ObjectHolder(BLOCK_ID) - public static FlowerPotBlock EMPTY_FLOWER_POT; - - @ObjectHolder(BLOCK_ID) - public static FlowerPotBlock OAK_FLOWER_POT; - + public static final RegistryObject EMPTY_FLOWER_POT = BLOCKS.register(BLOCK_ID, () -> new FlowerPotBlock(null, Blocks.AIR.delegate, Block.Properties.from(Blocks.FLOWER_POT))); + public static final RegistryObject OAK_FLOWER_POT = BLOCKS.register( + BLOCK_ID + "_oak", () -> new FlowerPotBlock(EMPTY_FLOWER_POT, Blocks.OAK_SAPLING.delegate, Block.Properties.from(Blocks.FLOWER_POT))); + + static { + ITEMS.register(BLOCK_ID, () -> new BlockItem(EMPTY_FLOWER_POT.get(), new Item.Properties())); + } + @SubscribeEvent - public static void registerBlocks(RegistryEvent.Register event) - { - FlowerPotBlock empty = new FlowerPotBlock(null, Blocks.AIR, Block.Properties.from(Blocks.FLOWER_POT)); - event.getRegistry().register(empty.setRegistryName(BLOCK_ID)); - event.getRegistry().register(new FlowerPotBlock(empty, Blocks.OAK_SAPLING, Block.Properties.from(empty)).setRegistryName(BLOCK_ID + "_oak")); + public static void onItemRegister(RegistryEvent.Register event) { + EMPTY_FLOWER_POT.get().addPlant(Blocks.OAK_SAPLING.getRegistryName(), OAK_FLOWER_POT); } - @SubscribeEvent - public static void registerItems(RegistryEvent.Register event) - { - event.getRegistry().register(new BlockItem(EMPTY_FLOWER_POT, new Item.Properties()).setRegistryName(MODID, BLOCK_ID)); + public FlowerPotTest() { + IEventBus modBus = FMLJavaModLoadingContext.get().getModEventBus(); + BLOCKS.register(modBus); + ITEMS.register(modBus); } }