Reimplement ForgeHooks#onPlaceItemIntoWorld and remove old place events

This commit is contained in:
tterrag 2019-04-23 20:42:08 -04:00
parent ca973f053f
commit a86317efc8
6 changed files with 21 additions and 117 deletions

View File

@ -1,18 +1,6 @@
--- a/net/minecraft/item/ItemBlock.java
+++ b/net/minecraft/item/ItemBlock.java
@@ -40,9 +40,10 @@
return EnumActionResult.FAIL;
} else {
IBlockState iblockstate = this.func_195945_b(p_195942_1_);
+ IBlockState currentState = p_195942_1_.func_195991_k().func_180495_p(p_195942_1_.func_195995_a());
if (iblockstate == null) {
return EnumActionResult.FAIL;
- } else if (!this.func_195941_b(p_195942_1_, iblockstate)) {
+ } else if (!net.minecraftforge.event.ForgeEventFactory.onBlockPlace(p_195942_1_.func_195999_j(), new net.minecraftforge.common.util.BlockSnapshot(p_195942_1_.func_195991_k(), p_195942_1_.func_195995_a(), currentState), net.minecraft.util.EnumFacing.UP) && !this.func_195941_b(p_195942_1_, iblockstate)) {
return EnumActionResult.FAIL;
} else {
BlockPos blockpos = p_195942_1_.func_195995_a();
@@ -59,7 +60,7 @@
@@ -59,7 +59,7 @@
}
}
@ -21,7 +9,7 @@
world.func_184133_a(entityplayer, blockpos, soundtype.func_185841_e(), SoundCategory.BLOCKS, (soundtype.func_185843_a() + 1.0F) / 2.0F, soundtype.func_185847_b() * 0.8F);
itemstack.func_190918_g(1);
return EnumActionResult.SUCCESS;
@@ -134,6 +135,10 @@
@@ -134,6 +134,10 @@
}
public Block func_179223_d() {

View File

@ -8,7 +8,7 @@
+ // special case for handling block placement with water lilies
+ net.minecraftforge.common.util.BlockSnapshot blocksnapshot = net.minecraftforge.common.util.BlockSnapshot.getBlockSnapshot(p_77659_1_, blockpos1);
p_77659_1_.func_180501_a(blockpos1, Blocks.field_196651_dG.func_176223_P(), 11);
+ if (net.minecraftforge.event.ForgeEventFactory.onPlayerBlockPlace(p_77659_2_, blocksnapshot, net.minecraft.util.EnumFacing.UP, p_77659_3_).isCanceled()) {
+ if (net.minecraftforge.event.ForgeEventFactory.onBlockPlace(p_77659_2_, blocksnapshot, net.minecraft.util.EnumFacing.UP)) {
+ blocksnapshot.restore(true, false);
+ return new ActionResult<ItemStack>(EnumActionResult.FAIL, itemstack);
+ }

View File

@ -74,6 +74,7 @@
- BlockPos blockpos = p_196084_1_.func_195995_a();
- BlockWorldState blockworldstate = new BlockWorldState(p_196084_1_.func_195991_k(), blockpos, false);
- if (entityplayer != null && !entityplayer.field_71075_bZ.field_75099_e && !this.func_206847_b(p_196084_1_.func_195991_k().func_205772_D(), blockworldstate)) {
+ if (!p_196084_1_.field_196006_g.field_72995_K) return net.minecraftforge.common.ForgeHooks.onPlaceItemIntoWorld(p_196084_1_);
+ return onItemUse(p_196084_1_, (c) -> func_77973_b().func_195939_a(p_196084_1_));
+ }
+
@ -82,7 +83,6 @@
+ }
+
+ private EnumActionResult onItemUse(ItemUseContext context, java.util.function.Function<ItemUseContext, EnumActionResult> callback) {
+ //if (!context.world.isRemote) return net.minecraftforge.common.ForgeHooks.onPlaceItemIntoWorld(context);
+ EntityPlayer entityplayer = context.func_195999_j();
+ BlockPos blockpos = context.func_195995_a();
+ BlockWorldState blockworldstate = new BlockWorldState(context.func_195991_k(), blockpos, false);

View File

@ -66,6 +66,7 @@ import net.minecraft.inventory.ContainerRepair;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemAxe;
import net.minecraft.item.ItemBucket;
import net.minecraft.item.ItemEnchantedBook;
import net.minecraft.item.ItemPickaxe;
import net.minecraft.item.ItemPotion;
@ -73,6 +74,7 @@ import net.minecraft.item.ItemSpade;
import net.minecraft.item.ItemSpawnEgg;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemTippedArrow;
import net.minecraft.item.ItemUseContext;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetHandlerPlayServer;
@ -80,6 +82,7 @@ import net.minecraft.network.Packet;
import net.minecraft.network.play.server.SPacketBlockChange;
import net.minecraft.potion.PotionType;
import net.minecraft.potion.PotionUtils;
import net.minecraft.stats.StatList;
import net.minecraft.tags.Tag;
import net.minecraft.tileentity.MobSpawnerBaseLogic;
import net.minecraft.tileentity.TileEntity;
@ -110,6 +113,7 @@ import net.minecraft.world.storage.loot.LootEntry;
import net.minecraft.world.storage.loot.LootTable;
import net.minecraft.world.storage.loot.LootTableManager;
import net.minecraft.world.storage.loot.conditions.LootCondition;
import net.minecraftforge.common.util.BlockSnapshot;
import net.minecraftforge.event.AnvilUpdateEvent;
import net.minecraftforge.event.DifficultyChangeEvent;
import net.minecraftforge.event.ForgeEventFactory;
@ -608,19 +612,17 @@ public class ForgeHooks
return event.isCanceled() ? -1 : event.getExpToDrop();
}
/* TODO: Talk to Sponge folk about World rollbacks.
public static EnumActionResult onPlaceItemIntoWorld(@Nonnull ItemUseContext context)
{
ItemStack itemstack = context.getItem();
World world = context.getWorld();
// handle all placement events here
int meta = itemstack.getItemDamage();
int size = itemstack.getCount();
NBTTagCompound nbt = null;
if (itemstack.getTagCompound() != null)
if (itemstack.hasTag())
{
nbt = itemstack.getTagCompound().copy();
nbt = itemstack.getTag().copy();
}
if (!(itemstack.getItem() instanceof ItemBucket)) // if not bucket
@ -634,39 +636,37 @@ public class ForgeHooks
if (ret == EnumActionResult.SUCCESS)
{
// save new item data
int newMeta = itemstack.getItemDamage();
int newSize = itemstack.getCount();
NBTTagCompound newNBT = null;
if (itemstack.getTagCompound() != null)
if (itemstack.hasTag())
{
newNBT = itemstack.getTagCompound().copy();
newNBT = itemstack.getTag().copy();
}
BlockEvent.PlaceEvent placeEvent = null;
@SuppressWarnings("unchecked")
List<BlockSnapshot> blockSnapshots = (List<BlockSnapshot>)world.capturedBlockSnapshots.clone();
world.capturedBlockSnapshots.clear();
// make sure to set pre-placement item data for event
itemstack.setDamage(meta);
itemstack.setCount(size);
if (nbt != null)
{
itemstack.setTagCompound(nbt);
itemstack.setTag(nbt);
}
EntityPlayer player = context.getPlayer();
EnumFacing side = context.getFace();
boolean eventResult = false;
if (blockSnapshots.size() > 1)
{
placeEvent = ForgeEventFactory.onPlayerMultiBlockPlace(player, blockSnapshots, side, hand);
eventResult = ForgeEventFactory.onMultiBlockPlace(player, blockSnapshots, side);
}
else if (blockSnapshots.size() == 1)
{
placeEvent = ForgeEventFactory.onPlayerBlockPlace(player, blockSnapshots.get(0), side, hand);
eventResult = ForgeEventFactory.onBlockPlace(player, blockSnapshots.get(0), side);
}
if (placeEvent != null && placeEvent.isCanceled())
if (eventResult)
{
ret = EnumActionResult.FAIL; // cancel placement
// revert back all captured blocks
@ -680,11 +680,10 @@ public class ForgeHooks
else
{
// Change the stack to its new content
itemstack.setDamage(newMeta);
itemstack.setCount(newSize);
if (nbt != null)
{
itemstack.setTagCompound(newNBT);
itemstack.setTag(newNBT);
}
for (BlockSnapshot snap : blockSnapshots)
@ -694,19 +693,18 @@ public class ForgeHooks
IBlockState newBlock = world.getBlockState(snap.getPos());
if (!newBlock.getBlock().hasTileEntity(newBlock)) // Containers get placed automatically
{
newBlock.getBlock().onBlockAdded(world, snap.getPos(), newBlock);
newBlock.onBlockAdded(world, snap.getPos(), oldBlock);
}
world.markAndNotifyBlock(snap.getPos(), null, oldBlock, newBlock, updateFlag);
}
player.addStat(StatList.getObjectUseStats(itemstack.getItem()));
player.addStat(StatList.ITEM_USED.get(itemstack.getItem()));
}
}
world.capturedBlockSnapshots.clear();
return ret;
}
*/
public static boolean onAnvilChange(ContainerRepair container, @Nonnull ItemStack left, @Nonnull ItemStack right, IInventory outputSlot, String name, int baseCost)
{

View File

@ -119,12 +119,10 @@ 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.CreateFluidSourceEvent;
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;
import net.minecraftforge.event.world.BlockEvent.PlaceEvent;
import net.minecraftforge.event.world.ExplosionEvent;
import net.minecraftforge.event.world.GetCollisionBoxesEvent;
import net.minecraftforge.event.world.SaplingGrowTreeEvent;
@ -143,15 +141,6 @@ public class ForgeEventFactory
return MinecraftForge.EVENT_BUS.post(event);
}
public static MultiPlaceEvent onPlayerMultiBlockPlace(EntityPlayer player, List<BlockSnapshot> blockSnapshots, EnumFacing direction, EnumHand hand)
{
BlockSnapshot snap = blockSnapshots.get(0);
IBlockState placedAgainst = snap.getWorld().getBlockState(snap.getPos().offset(direction.getOpposite()));
MultiPlaceEvent event = new MultiPlaceEvent(blockSnapshots, placedAgainst, player, hand);
MinecraftForge.EVENT_BUS.post(event);
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()));
@ -159,14 +148,6 @@ public class ForgeEventFactory
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()));
PlaceEvent event = new PlaceEvent(blockSnapshot, placedAgainst, player, hand);
MinecraftForge.EVENT_BUS.post(event);
return event;
}
public static NeighborNotifyEvent onNeighborNotify(World world, BlockPos pos, IBlockState state, EnumSet<EnumFacing> notifiedSides, boolean forceRedstoneUpdate)
{
NeighborNotifyEvent event = new NeighborNotifyEvent(world, pos, state, notifiedSides, forceRedstoneUpdate);

View File

@ -201,35 +201,6 @@ public class BlockEvent extends Event
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
@Deprecated // Remove in 1.13
public static class PlaceEvent extends EntityPlaceEvent
{
private final EntityPlayer player;
private final EnumHand hand;
public PlaceEvent(@Nonnull BlockSnapshot blockSnapshot, @Nonnull IBlockState placedAgainst, @Nonnull EntityPlayer player, @Nonnull EnumHand hand) {
super(blockSnapshot, placedAgainst, player);
this.player = player;
this.hand = hand;
if (DEBUG)
{
System.out.printf("Created PlaceEvent - [PlacedBlock: %s ][PlacedAgainst: %s ][ItemStack: %s ][Player: %s ][Hand: %s]\n", getPlacedBlock(), placedAgainst, player.getHeldItem(hand), player, hand);
}
}
public EntityPlayer getPlayer() { return player; }
@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
@ -263,40 +234,6 @@ public class BlockEvent extends Event
}
}
/**
* 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
* 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 MultiPlaceEvent extends PlaceEvent
{
private final List<BlockSnapshot> blockSnapshots;
public MultiPlaceEvent(@Nonnull List<BlockSnapshot> blockSnapshots, @Nonnull IBlockState placedAgainst, @Nonnull EntityPlayer player, @Nonnull EnumHand hand) {
super(blockSnapshots.get(0), placedAgainst, player, hand);
this.blockSnapshots = ImmutableList.copyOf(blockSnapshots);
if (DEBUG)
{
System.out.printf("Created MultiPlaceEvent - [PlacedAgainst: %s ][ItemInHand: %s ][Player: %s ]\n", placedAgainst, player.getHeldItem(hand), player);
}
}
/**
* 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 physics update occurs on a block. This event acts as
* a way for mods to detect physics updates, in the same way a BUD switch