Added Fluid Place Block Event for when fluid changes blocks in world. (#4627)

This commit is contained in:
LexManos 2018-06-27 12:04:23 -07:00
parent 0e4d73230b
commit 1007d3e9c5
7 changed files with 148 additions and 7 deletions

View File

@ -17,6 +17,15 @@
{
IBlockState iblockstate = p_180650_1_.func_180495_p(p_180650_2_.func_177977_b());
@@ -118,7 +119,7 @@
{
if (this.field_149764_J == Material.field_151587_i && p_180650_1_.func_180495_p(p_180650_2_.func_177977_b()).func_185904_a() == Material.field_151586_h)
{
- p_180650_1_.func_175656_a(p_180650_2_.func_177977_b(), Blocks.field_150348_b.func_176223_P());
+ p_180650_1_.func_175656_a(p_180650_2_.func_177977_b(), net.minecraftforge.event.ForgeEventFactory.fireFluidPlaceBlockEvent(p_180650_1_, p_180650_2_.func_177977_b(), p_180650_2_, Blocks.field_150348_b.func_176223_P()));
this.func_180688_d(p_180650_1_, p_180650_2_.func_177977_b());
return;
}
@@ -166,6 +167,7 @@
}
else

View File

@ -1,6 +1,6 @@
--- ../src-base/minecraft/net/minecraft/block/BlockFarmland.java
+++ ../src-work/minecraft/net/minecraft/block/BlockFarmland.java
@@ -72,9 +72,10 @@
@@ -72,7 +72,7 @@
public void func_180658_a(World p_180658_1_, BlockPos p_180658_2_, Entity p_180658_3_, float p_180658_4_)
{
@ -9,9 +9,7 @@
{
func_190970_b(p_180658_1_, p_180658_2_);
}
super.func_180658_a(p_180658_1_, p_180658_2_, p_180658_3_, p_180658_4_);
@@ -95,7 +96,7 @@
@@ -95,7 +95,7 @@
private boolean func_176529_d(World p_176529_1_, BlockPos p_176529_2_)
{
Block block = p_176529_1_.func_180495_p(p_176529_2_.func_177984_a()).func_177230_c();

View File

@ -1,5 +1,22 @@
--- ../src-base/minecraft/net/minecraft/block/BlockLiquid.java
+++ ../src-work/minecraft/net/minecraft/block/BlockLiquid.java
@@ -284,14 +284,14 @@
if (integer.intValue() == 0)
{
- p_176365_1_.func_175656_a(p_176365_2_, Blocks.field_150343_Z.func_176223_P());
+ p_176365_1_.func_175656_a(p_176365_2_, net.minecraftforge.event.ForgeEventFactory.fireFluidPlaceBlockEvent(p_176365_1_, p_176365_2_, p_176365_2_, Blocks.field_150343_Z.func_176223_P()));
this.func_180688_d(p_176365_1_, p_176365_2_);
return true;
}
if (integer.intValue() <= 4)
{
- p_176365_1_.func_175656_a(p_176365_2_, Blocks.field_150347_e.func_176223_P());
+ p_176365_1_.func_175656_a(p_176365_2_, net.minecraftforge.event.ForgeEventFactory.fireFluidPlaceBlockEvent(p_176365_1_, p_176365_2_, p_176365_2_, Blocks.field_150347_e.func_176223_P()));
this.func_180688_d(p_176365_1_, p_176365_2_);
return true;
}
@@ -452,4 +452,29 @@
{
return BlockFaceShape.UNDEFINED;

View File

@ -1,6 +1,6 @@
--- ../src-base/minecraft/net/minecraft/block/BlockStaticLiquid.java
+++ ../src-work/minecraft/net/minecraft/block/BlockStaticLiquid.java
@@ -52,14 +52,14 @@
@@ -52,22 +52,22 @@
{
blockpos = blockpos.func_177982_a(p_180650_4_.nextInt(3) - 1, 1, p_180650_4_.nextInt(3) - 1);
@ -18,7 +18,8 @@
{
if (this.func_176369_e(p_180650_1_, blockpos))
{
@@ -67,7 +67,7 @@
- p_180650_1_.func_175656_a(blockpos, Blocks.field_150480_ab.func_176223_P());
+ p_180650_1_.func_175656_a(blockpos, net.minecraftforge.event.ForgeEventFactory.fireFluidPlaceBlockEvent(p_180650_1_, blockpos, p_180650_2_, Blocks.field_150480_ab.func_176223_P()));
return;
}
}
@ -27,3 +28,12 @@
{
return;
}
@@ -86,7 +86,7 @@
if (p_180650_1_.func_175623_d(blockpos1.func_177984_a()) && this.func_176368_m(p_180650_1_, blockpos1))
{
- p_180650_1_.func_175656_a(blockpos1.func_177984_a(), Blocks.field_150480_ab.func_176223_P());
+ p_180650_1_.func_175656_a(blockpos1.func_177984_a(), net.minecraftforge.event.ForgeEventFactory.fireFluidPlaceBlockEvent(p_180650_1_, blockpos1.func_177984_a(), p_180650_2_, Blocks.field_150480_ab.func_176223_P()));
}
}
}

View File

@ -25,6 +25,7 @@ import java.util.List;
import java.util.Random;
import net.minecraft.block.BlockPortal;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
@ -319,6 +320,13 @@ public class ForgeEventFactory
return event.getDropChance();
}
public static IBlockState fireFluidPlaceBlockEvent(World world, BlockPos pos, BlockPos liquidPos, IBlockState state)
{
BlockEvent.FluidPlaceBlockEvent event = new BlockEvent.FluidPlaceBlockEvent(world, pos, liquidPos, state);
MinecraftForge.EVENT_BUS.post(event);
return event.getNewState();
}
public static ItemTooltipEvent onItemTooltip(ItemStack itemStack, @Nullable EntityPlayer entityPlayer, List<String> toolTip, ITooltipFlag flags)
{
ItemTooltipEvent event = new ItemTooltipEvent(itemStack, entityPlayer, toolTip, flags);

View File

@ -24,6 +24,7 @@ import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.block.BlockPortal;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
@ -127,7 +128,7 @@ public class BlockEvent extends Event
this.player = player;
if (state == null || !ForgeHooks.canHarvestBlock(state.getBlock(), player, world, pos) || // Handle empty block or player unable to break block scenario
(state.getBlock().canSilkHarvest(world, pos, world.getBlockState(pos), player) && EnchantmentHelper.getEnchantmentLevel(Enchantments.SILK_TOUCH, player.getHeldItemMainhand()) > 0)) // If the block is being silk harvested, the exp dropped is 0
(state.getBlock().canSilkHarvest(world, pos, world.getBlockState(pos), player) && EnchantmentHelper.getEnchantmentLevel(Enchantments.SILK_TOUCH, player.getHeldItemMainhand()) > 0)) // If the block is being silk harvested, the exp dropped is 0
{
this.exp = 0;
}
@ -287,6 +288,59 @@ public class BlockEvent extends Event
}
}
/**
* Fired when a liquid places a block. Use {@link #setNewState(IBlockState)} to change the result of
* a cobblestone generator or add variants of obsidian. Alternatively, you could execute
* arbitrary code when lava sets blocks on fire, even preventing it.
*
* {@link #getState()} will return the block that was originally going to be placed.
* {@link #getPos()} will return the position of the block to be changed.
*/
@Cancelable
public static class FluidPlaceBlockEvent extends BlockEvent
{
private final BlockPos liquidPos;
private IBlockState newState;
private IBlockState origState;
public FluidPlaceBlockEvent(World world, BlockPos pos, BlockPos liquidPos, IBlockState state)
{
super(world, pos, state);
this.liquidPos = liquidPos;
this.newState = state;
this.origState = world.getBlockState(pos);
}
/**
* @return The position of the liquid this event originated from. This may be the same as {@link #getPos()}.
*/
public BlockPos getLiquidPos()
{
return liquidPos;
}
/**
* @return The block state that will be placed after this event resolves.
*/
public IBlockState getNewState()
{
return newState;
}
public void setNewState(IBlockState state)
{
this.newState = state;
}
/**
* @return The state of the block to be changed before the event was fired.
*/
public IBlockState getOriginalState()
{
return origState;
}
}
/**
* Fired when a crop block grows. See subevents.
*

View File

@ -0,0 +1,45 @@
package net.minecraftforge.debug;
import net.minecraft.block.BlockStone;
import net.minecraft.init.Blocks;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.BiomeHills;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
/**
* This test will:
* - Cause lava to turn into gold when touched by water.
* - Replace the result of a cobblestone generator with granite.
* - Replace the result of a stone generator with either diamond, or emerald when in a biome where emerald spawns naturally.
* - Prevent lava from setting surrounding blocks on fire.
*/
@Mod(modid = "fluidplaceblocktest", name = "FluidPlaceBlockTest", version = "0.0.0", acceptableRemoteVersions = "*")
public class FluidPlaceBlockTest
{
private static final boolean ENABLED = false;
@Mod.EventHandler
public void preinit(FMLPreInitializationEvent event)
{
if (!ENABLED) return;
MinecraftForge.EVENT_BUS.register(FluidPlaceBlockTest.class);
}
@SubscribeEvent @SuppressWarnings("unused")
public static void onFluidPlaceBlockEvent(BlockEvent.FluidPlaceBlockEvent event)
{
if (event.getState().getBlock() == Blocks.OBSIDIAN) event.setNewState(Blocks.GOLD_BLOCK.getDefaultState());
if (event.getState().getBlock() == Blocks.COBBLESTONE) event.setNewState(Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.GRANITE));
if (event.getState() == Blocks.STONE.getDefaultState())
{
Biome biome = event.getWorld().getBiome(event.getPos());
if (biome instanceof BiomeHills) event.setNewState(Blocks.EMERALD_BLOCK.getDefaultState());
else event.setNewState(Blocks.DIAMOND_BLOCK.getDefaultState());
}
if (event.getState().getBlock() == Blocks.FIRE) event.setNewState(event.getOriginalState());
}
}