Make FlowerPotBlock extensible and reusable for mods

- Change static lookup map to an instance variable
- Add an instance variable for the "empty pot" for the current block
- Keep one instance of the lookup map between the empty pot and all its full versions
- Convert everything to use delegates
This commit is contained in:
tterrag 2019-09-17 19:57:58 -04:00
parent af8074cc39
commit 6fda1e968d
8 changed files with 160 additions and 5 deletions

View File

@ -1,11 +1,88 @@
--- a/net/minecraft/block/FlowerPotBlock.java
+++ b/net/minecraft/block/FlowerPotBlock.java
@@ -19,7 +19,7 @@
@@ -19,14 +19,35 @@
import net.minecraft.world.World;
public class FlowerPotBlock extends Block {
- private static final Map<Block, Block> field_196451_b = Maps.newHashMap();
+ private static final Map<Block, Block> field_196451_b = Maps.newHashMap(); //TODO: Delegates
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 Block field_196452_c;
+ private final FlowerPotBlock emptyPot;
+ private final Map<net.minecraftforge.registries.IRegistryDelegate<Block>, net.minecraftforge.registries.IRegistryDelegate<Block>> fullPots;
+ private final net.minecraftforge.registries.IRegistryDelegate<Block> field_196452_c;
+ @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_);
+ }
+
+ /**
+ * For mod use, eliminates the need to extend this class, and prevents modded
+ * flower pots from altering vanilla behavior.
+ *
+ * @param emptyPot The empty pot for this pot, or null for self.
+ * @param p_i48395_1_ The flower block.
+ * @param p_i48395_2_
+ */
+ public FlowerPotBlock(FlowerPotBlock emptyPot, Block 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;
+ if (emptyPot == null) {
+ this.fullPots = Maps.newHashMap();
+ this.emptyPot = this;
+ } else {
+ 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 @@
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;
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;
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)) {
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);
}
}
@@ -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 @@
}
public Block func_220276_d() {
- return this.field_196452_c;
+ return this.field_196452_c.get();
}
}

View File

@ -1,5 +1,5 @@
net/minecraft/block/FireBlock.tryCatchFire(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;ILjava/util/Random;ILnet/minecraft/util/Direction;)V=|p_176536_1_,p_176536_2_,p_176536_3_,p_176536_4_,p_176536_5_,face
net/minecraft/block/FlowerPotBlock.<init>(Lnet/minecraft/block/FlowerPotBlock;Lnet/minecraft/block/Block;Lnet/minecraft/block/Block$Properties;)V=|emptyPot,p_i48395_1_,p_i48395_2_
net/minecraft/block/RedstoneWireBlock.canConnectTo(Lnet/minecraft/block/BlockState;Lnet/minecraft/world/IBlockReader;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/util/Direction;)Z=|p_176343_0_,world,pos,p_176343_1_
net/minecraft/block/PoweredRailBlock.<init>(Lnet/minecraft/block/Block$Properties;Z)=|p_i48349_1_,isActivator

View File

@ -0,0 +1,59 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2019.
*
* This library 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 version 2.1
* of the License.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.debug.block;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
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.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;
import net.minecraftforge.registries.ObjectHolder;
@Mod(FlowerPotTest.MODID)
@Mod.EventBusSubscriber(bus = Bus.MOD)
public class FlowerPotTest
{
static final String MODID = "flower_pot_test";
static final String BLOCK_ID = "test_flower_pot";
@ObjectHolder(BLOCK_ID)
public static FlowerPotBlock EMPTY_FLOWER_POT;
@ObjectHolder(BLOCK_ID)
public static FlowerPotBlock OAK_FLOWER_POT;
@SubscribeEvent
public static void registerBlocks(RegistryEvent.Register<Block> 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"));
}
@SubscribeEvent
public static void registerItems(RegistryEvent.Register<Item> event)
{
event.getRegistry().register(new BlockItem(EMPTY_FLOWER_POT, new Item.Properties()).setRegistryName(MODID, BLOCK_ID));
}
}

View File

@ -27,10 +27,11 @@ import net.minecraft.item.Item;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;
import net.minecraftforge.registries.ObjectHolder;
@Mod(StickyBlockTest.MODID)
@Mod.EventBusSubscriber
@Mod.EventBusSubscriber(bus = Bus.MOD)
public class StickyBlockTest
{
static final String MODID = "custom_slime_block_test";

View File

@ -51,3 +51,5 @@ loaderVersion="[28,)"
modId="data_gen_test"
[[mods]]
modId="piston_event_test"
[[mods]]
modId="flower_pot_test"

View File

@ -0,0 +1,5 @@
{
"variants": {
"": { "model": "block/flower_pot" }
}
}

View File

@ -0,0 +1,5 @@
{
"variants": {
"": { "model": "block/potted_oak_sapling" }
}
}

View File

@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "item/flower_pot"
}
}