Reimplement ForgeHooks#onPlaceItemIntoWorld and remove old place events

This commit is contained in:
tterrag committed 2019-04-23 20:42:08 -04:00
1 parent ca973f053f
commit a86317efc8
6 files changed
+21 -117

No files matched your search

@@ -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)
{
@@ -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);
@@ -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