use of net.minecraftforge.common.util.BlockSnapshot in project MinecraftForge by MinecraftForge.
the class ForgeHooks method onPlaceItemIntoWorld.
public static EnumActionResult onPlaceItemIntoWorld(@Nonnull ItemStack itemstack, @Nonnull EntityPlayer player, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull EnumFacing side, float hitX, float hitY, float hitZ, @Nonnull EnumHand hand) {
// handle all placement events here
int meta = itemstack.getItemDamage();
int size = itemstack.getCount();
NBTTagCompound nbt = null;
if (itemstack.getTagCompound() != null) {
nbt = (NBTTagCompound) itemstack.getTagCompound().copy();
}
if (// if not bucket
!(itemstack.getItem() instanceof ItemBucket)) {
world.captureBlockSnapshots = true;
}
EnumActionResult ret = itemstack.getItem().onItemUse(player, world, pos, hand, side, hitX, hitY, hitZ);
world.captureBlockSnapshots = false;
if (ret == EnumActionResult.SUCCESS) {
// save new item data
int newMeta = itemstack.getItemDamage();
int newSize = itemstack.getCount();
NBTTagCompound newNBT = null;
if (itemstack.getTagCompound() != null) {
newNBT = (NBTTagCompound) itemstack.getTagCompound().copy();
}
net.minecraftforge.event.world.BlockEvent.PlaceEvent placeEvent = null;
@SuppressWarnings("unchecked") List<net.minecraftforge.common.util.BlockSnapshot> blockSnapshots = (List<BlockSnapshot>) world.capturedBlockSnapshots.clone();
world.capturedBlockSnapshots.clear();
// make sure to set pre-placement item data for event
itemstack.setItemDamage(meta);
itemstack.setCount(size);
if (nbt != null) {
itemstack.setTagCompound(nbt);
}
if (blockSnapshots.size() > 1) {
placeEvent = ForgeEventFactory.onPlayerMultiBlockPlace(player, blockSnapshots, side, hand);
} else if (blockSnapshots.size() == 1) {
placeEvent = ForgeEventFactory.onPlayerBlockPlace(player, blockSnapshots.get(0), side, hand);
}
if (placeEvent != null && (placeEvent.isCanceled())) {
// cancel placement
ret = EnumActionResult.FAIL;
// revert back all captured blocks
for (net.minecraftforge.common.util.BlockSnapshot blocksnapshot : blockSnapshots) {
world.restoringBlockSnapshots = true;
blocksnapshot.restore(true, false);
world.restoringBlockSnapshots = false;
}
} else {
// Change the stack to its new content
itemstack.setItemDamage(newMeta);
itemstack.setCount(newSize);
if (nbt != null) {
itemstack.setTagCompound(newNBT);
}
for (BlockSnapshot snap : blockSnapshots) {
int updateFlag = snap.getFlag();
IBlockState oldBlock = snap.getReplacedBlock();
IBlockState newBlock = world.getBlockState(snap.getPos());
if (// Containers get placed automatically
!newBlock.getBlock().hasTileEntity(newBlock)) {
newBlock.getBlock().onBlockAdded(world, snap.getPos(), newBlock);
}
world.markAndNotifyBlock(snap.getPos(), null, oldBlock, newBlock, updateFlag);
}
player.addStat(StatList.getObjectUseStats(itemstack.getItem()));
}
}
world.capturedBlockSnapshots.clear();
return ret;
}
use of net.minecraftforge.common.util.BlockSnapshot in project MinecraftForge by MinecraftForge.
the class ForgeEventFactory method onPlayerMultiBlockPlace.
public static MultiPlaceEvent onPlayerMultiBlockPlace(EntityPlayer player, List<BlockSnapshot> blockSnapshots, EnumFacing direction, @Nullable 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;
}
use of net.minecraftforge.common.util.BlockSnapshot in project Overloaded by CJ-MC-Mods.
the class PlayerInteractionUtil method placeBlock.
public static boolean placeBlock(@Nonnull ItemStack searchStack, @Nonnull EntityPlayerMP player, @Nonnull World worldIn, @Nonnull BlockPos newPosition, @Nonnull EnumFacing facing, @Nonnull IEnergyStorage energy, float hitX, float hitY, float hitZ) {
// Can we place a block at this Pos
ItemBlock itemBlock = ((ItemBlock) searchStack.getItem());
if (!worldIn.mayPlace(itemBlock.getBlock(), newPosition, false, facing, null)) {
return false;
}
BlockEvent.PlaceEvent event = ForgeEventFactory.onPlayerBlockPlace(player, new BlockSnapshot(worldIn, newPosition, worldIn.getBlockState(newPosition)), facing, EnumHand.MAIN_HAND);
if (event.isCanceled())
return false;
long distance = Math.round(player.getPosition().getDistance(newPosition.getX(), newPosition.getY(), newPosition.getZ()));
long cost = OverloadedConfig.multiToolConfig.placeBaseCost + OverloadedConfig.multiToolConfig.costPerMeterAway * distance;
if (cost > Integer.MAX_VALUE || cost < 0 || energy.getEnergyStored() < cost)
return false;
IItemHandler inventory = player.getCapability(ITEM_HANDLER_CAPABILITY, EnumFacing.UP);
int foundStackSlot = findItemStack(searchStack, inventory);
if (foundStackSlot == -1) {
System.out.println("Stack not found");
return false;
}
System.out.println("Stack found at index: " + foundStackSlot);
ItemStack foundStack = inventory.extractItem(foundStackSlot, 1, player.capabilities.isCreativeMode);
int i = itemBlock.getMetadata(foundStack.getMetadata());
IBlockState iblockstate1 = itemBlock.block.getStateForPlacement(worldIn, newPosition, facing, hitX, hitY, hitZ, i, player, EnumHand.MAIN_HAND);
if (itemBlock.placeBlockAt(foundStack, player, worldIn, newPosition, facing, hitX, hitY, hitZ, iblockstate1)) {
SoundType soundtype = worldIn.getBlockState(newPosition).getBlock().getSoundType(worldIn.getBlockState(newPosition), worldIn, newPosition, player);
worldIn.playSound(null, newPosition, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
energy.extractEnergy((int) cost, false);
System.out.println("Place Block Successs");
return true;
}
System.out.println("Place Block Failed");
inventory.insertItem(foundStackSlot, foundStack, player.capabilities.isCreativeMode);
return false;
}
Aggregations