mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-22 13:09:59 +00:00
Add EntityPlaceEvent (#5057)
This commit is contained in:
1 parent
cc95c40b5b
commit
f3d364397d
9 files changed
+209
-33
No files matched your search
@@ -121,6 +121,8 @@ import net.minecraftforge.event.furnace.FurnaceFuelBurnTimeEvent;
|
||||
import net.minecraftforge.event.terraingen.ChunkGeneratorEvent;
|
||||
import net.minecraftforge.event.terraingen.PopulateChunkEvent;
|
||||
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;
|
||||
@@ -139,6 +141,15 @@ import javax.annotation.Nullable;
|
||||
public class ForgeEventFactory
|
||||
{
|
||||
|
||||
public static EntityMultiPlaceEvent 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);
|
||||
MinecraftForge.EVENT_BUS.post(event);
|
||||
return event;
|
||||
}
|
||||
|
||||
public static MultiPlaceEvent onPlayerMultiBlockPlace(EntityPlayer player, List<BlockSnapshot> blockSnapshots, EnumFacing direction, EnumHand hand)
|
||||
{
|
||||
BlockSnapshot snap = blockSnapshots.get(0);
|
||||
@@ -148,6 +159,15 @@ public class ForgeEventFactory
|
||||
return event;
|
||||
}
|
||||
|
||||
public static EntityPlaceEvent 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);
|
||||
MinecraftForge.EVENT_BUS.post(event);
|
||||
return 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()));
|
||||
|
||||
@@ -22,13 +22,10 @@ package net.minecraftforge.event.world;
|
||||
import java.util.EnumSet;
|
||||
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;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.init.Enchantments;
|
||||
import net.minecraft.item.ItemStack;
|
||||
@@ -44,6 +41,7 @@ import net.minecraftforge.fml.common.eventhandler.Event;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class BlockEvent extends Event
|
||||
{
|
||||
@@ -165,26 +163,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)
|
||||
{
|
||||
@@ -196,12 +223,43 @@ public class BlockEvent extends Event
|
||||
@Nonnull
|
||||
@Deprecated
|
||||
public ItemStack getItemInHand() { return player.getHeldItem(hand); }
|
||||
public BlockSnapshot getBlockSnapshot() { return blockSnapshot; }
|
||||
public IBlockState getPlacedBlock() { return placedBlock; }
|
||||
public IBlockState getPlacedAgainst() { return placedAgainst; }
|
||||
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
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2018.
|
||||
*
|
||||
* 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.entity.item.EntityFallingBlock;
|
||||
import net.minecraftforge.event.world.BlockEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
|
||||
@Mod(modid = BasePlaceEventTest.MOD_ID, name = "BaseBlockPlaceEvent test mod", version = "1.0", acceptableRemoteVersions = "*")
|
||||
@Mod.EventBusSubscriber
|
||||
public class BasePlaceEventTest
|
||||
{
|
||||
static final String MOD_ID = "base_block_place_event_test";
|
||||
static final boolean ENABLED = true;
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onBlockPlaced(BlockEvent.EntityPlaceEvent event)
|
||||
{
|
||||
if (!ENABLED)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.getEntity() instanceof EntityFallingBlock) {
|
||||
event.setCanceled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user