Add EntityPlaceEvent (#5057)

Co-authored-by: tterrag <tterrag1098@gmail.com>
This commit is contained in:
Christopher Krierandtterrag authored and tterrag committed 2019-04-23 19:58:09 -04:00
1 parent 119e4a6924
commit ca973f053f
8 files changed
+188 -15

No files matched your search

@@ -119,6 +119,8 @@ import net.minecraftforge.event.entity.player.UseHoeEvent;
import net.minecraftforge.event.furnace.FurnaceFuelBurnTimeEvent;
import net.minecraftforge.event.terraingen.ChunkGeneratorEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.event.world.BlockEvent.EntityMultiPlaceEvent;
import net.minecraftforge.event.world.BlockEvent.EntityPlaceEvent;
import net.minecraftforge.event.world.BlockEvent.CreateFluidSourceEvent;
import net.minecraftforge.event.world.BlockEvent.MultiPlaceEvent;
import net.minecraftforge.event.world.BlockEvent.NeighborNotifyEvent;
@@ -133,6 +135,14 @@ import net.minecraftforge.eventbus.api.Event.Result;
public class ForgeEventFactory
{
public static boolean onMultiBlockPlace(@Nullable Entity entity, List<BlockSnapshot> blockSnapshots, EnumFacing direction)
{
BlockSnapshot snap = blockSnapshots.get(0);
IBlockState placedAgainst = snap.getWorld().getBlockState(snap.getPos().offset(direction.getOpposite()));
EntityMultiPlaceEvent event = new EntityMultiPlaceEvent(blockSnapshots, placedAgainst, entity);
return MinecraftForge.EVENT_BUS.post(event);
}
public static MultiPlaceEvent onPlayerMultiBlockPlace(EntityPlayer player, List<BlockSnapshot> blockSnapshots, EnumFacing direction, EnumHand hand)
{
BlockSnapshot snap = blockSnapshots.get(0);
@@ -142,6 +152,13 @@ public class ForgeEventFactory
return event;
}
public static boolean onBlockPlace(@Nullable Entity entity, @Nonnull BlockSnapshot blockSnapshot, @Nonnull EnumFacing direction)
{
IBlockState placedAgainst = blockSnapshot.getWorld().getBlockState(blockSnapshot.getPos().offset(direction.getOpposite()));
EntityPlaceEvent event = new BlockEvent.EntityPlaceEvent(blockSnapshot, placedAgainst, entity);
return MinecraftForge.EVENT_BUS.post(event);
}
public static PlaceEvent onPlayerBlockPlace(@Nonnull EntityPlayer player, @Nonnull BlockSnapshot blockSnapshot, @Nonnull EnumFacing direction, @Nonnull EnumHand hand)
{
IBlockState placedAgainst = blockSnapshot.getWorld().getBlockState(blockSnapshot.getPos().offset(direction.getOpposite()));
@@ -43,6 +43,7 @@ import net.minecraftforge.eventbus.api.Event;
import com.google.common.collect.ImmutableList;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.minecraftforge.eventbus.api.Event.HasResult;
@@ -166,26 +167,55 @@ public class BlockEvent extends Event
}
}
/**
* Called when a block is placed.
*
* If a Block Place event is cancelled, the block will not be placed.
*/
@Cancelable
public static class EntityPlaceEvent extends BlockEvent
{
private final Entity entity;
private final BlockSnapshot blockSnapshot;
private final IBlockState placedBlock;
private final IBlockState placedAgainst;
public EntityPlaceEvent(@Nonnull BlockSnapshot blockSnapshot, @Nonnull IBlockState placedAgainst, @Nullable Entity entity)
{
super(blockSnapshot.getWorld(), blockSnapshot.getPos(), !(entity instanceof EntityPlayer) ? blockSnapshot.getReplacedBlock() : blockSnapshot.getCurrentBlock());
this.entity = entity;
this.blockSnapshot = blockSnapshot;
this.placedBlock = !(entity instanceof EntityPlayer) ? blockSnapshot.getReplacedBlock() : blockSnapshot.getCurrentBlock();
this.placedAgainst = placedAgainst;
if (DEBUG)
{
System.out.printf("Created EntityPlaceEvent - [PlacedBlock: %s ][PlacedAgainst: %s ][Entity: %s ]\n", getPlacedBlock(), placedAgainst, entity);
}
}
@Nullable
public Entity getEntity() { return entity; }
public BlockSnapshot getBlockSnapshot() { return blockSnapshot; }
public IBlockState getPlacedBlock() { return placedBlock; }
public IBlockState getPlacedAgainst() { return placedAgainst; }
}
/**
* Called when a block is placed by a player.
*
* If a Block Place event is cancelled, the block will not be placed.
*/
@Cancelable
public static class PlaceEvent extends BlockEvent
@Deprecated // Remove in 1.13
public static class PlaceEvent extends EntityPlaceEvent
{
private final EntityPlayer player;
private final BlockSnapshot blockSnapshot;
private final IBlockState placedBlock;
private final IBlockState placedAgainst;
private final EnumHand hand;
public PlaceEvent(@Nonnull BlockSnapshot blockSnapshot, @Nonnull IBlockState placedAgainst, @Nonnull EntityPlayer player, @Nonnull EnumHand hand) {
super(blockSnapshot.getWorld(), blockSnapshot.getPos(), blockSnapshot.getCurrentBlock());
super(blockSnapshot, placedAgainst, player);
this.player = player;
this.blockSnapshot = blockSnapshot;
this.placedBlock = blockSnapshot.getCurrentBlock();
this.placedAgainst = placedAgainst;
this.hand = hand;
if (DEBUG)
{
@@ -194,12 +224,46 @@ public class BlockEvent extends Event
}
public EntityPlayer getPlayer() { return player; }
public BlockSnapshot getBlockSnapshot() { return blockSnapshot; }
public IBlockState getPlacedBlock() { return placedBlock; }
public IBlockState getPlacedAgainst() { return placedAgainst; }
@Nonnull
@Deprecated
public ItemStack getItemInHand() { return player.getHeldItem(hand); }
public EnumHand getHand() { return hand; }
}
/**
* Fired when a single block placement triggers the
* creation of multiple blocks(e.g. placing a bed block). The block returned
* by {@link #state} and its related methods is the block where
* the placed block would exist if the placement only affected a single
* block.
*/
@Cancelable
public static class EntityMultiPlaceEvent extends EntityPlaceEvent
{
private final List<BlockSnapshot> blockSnapshots;
public EntityMultiPlaceEvent(@Nonnull List<BlockSnapshot> blockSnapshots, @Nonnull IBlockState placedAgainst, @Nullable Entity entity) {
super(blockSnapshots.get(0), placedAgainst, entity);
this.blockSnapshots = ImmutableList.copyOf(blockSnapshots);
if (DEBUG)
{
System.out.printf("Created EntityMultiPlaceEvent - [PlacedAgainst: %s ][Entity: %s ]\n", placedAgainst, entity);
}
}
/**
* Gets a list of BlockSnapshots for all blocks which were replaced by the
* placement of the new blocks. Most of these blocks will just be of type AIR.
*
* @return immutable list of replaced BlockSnapshots
*/
public List<BlockSnapshot> getReplacedBlockSnapshots()
{
return blockSnapshots;
}
}
/**
* Fired when a single block placement action of a player triggers the
* creation of multiple blocks(e.g. placing a bed block). The block returned