use of net.minecraft.block.SoundType 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;
}
use of net.minecraft.block.SoundType in project ImmersiveEngineering by BluSunrize.
the class ItemBlockIEBase method onItemUse.
@Override
public EnumActionResult onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
IBlockState iblockstate = world.getBlockState(pos);
Block block = iblockstate.getBlock();
if (!block.isReplaceable(world, pos))
pos = pos.offset(side);
if (stack.stackSize > 0 && player.canPlayerEdit(pos, side, stack) && canBlockBePlaced(world, pos, side, stack)) {
int i = this.getMetadata(stack.getMetadata());
IBlockState iblockstate1 = this.block.onBlockPlaced(world, pos, side, hitX, hitY, hitZ, i, player);
if (placeBlockAt(stack, player, world, pos, side, hitX, hitY, hitZ, iblockstate1)) {
SoundType soundtype = world.getBlockState(pos).getBlock().getSoundType(world.getBlockState(pos), world, pos, player);
world.playSound(player, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
if (!player.capabilities.isCreativeMode)
--stack.stackSize;
}
return EnumActionResult.SUCCESS;
}
return EnumActionResult.FAIL;
}
use of net.minecraft.block.SoundType in project AgriCraft by AgriCraft.
the class ItemCrop method onItemUseFirst.
// This is called when you right click with this item in hand.
@Override
public EnumActionResult onItemUseFirst(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) {
// Skip if remote, or clicked wrong face of block.
if (world.isRemote || side != EnumFacing.UP) {
return EnumActionResult.PASS;
}
// Fetch information.
final BlockPos cropPos = pos.up();
// Test if placement is valid.
if (!world.isAirBlock(cropPos)) {
return EnumActionResult.FAIL;
}
// Test if soil is valid.
if (!AgriApi.getSoilRegistry().contains(world.getBlockState(pos))) {
return EnumActionResult.FAIL;
}
// Set the block to a crop.
world.setBlockState(pos.up(), AgriBlocks.getInstance().CROP.getDefaultState());
// Remove the crop used from the stack.
stack.stackSize = player.capabilities.isCreativeMode ? stack.stackSize : stack.stackSize - 1;
// Handle sneak placing of crosscrops.
if (player.isSneaking() && stack.stackSize > 0) {
WorldHelper.getTile(world, pos.add(0, 1, 0), TileEntityCrop.class).ifPresent(c -> {
c.setCrossCrop(true);
stack.stackSize = player.capabilities.isCreativeMode ? stack.stackSize : stack.stackSize - 1;
});
}
// Play placement sound.
SoundType type = Blocks.LEAVES.getSoundType();
world.playSound(null, (double) ((float) cropPos.getX() + 0.5F), (double) ((float) cropPos.getY() + 0.5F), (double) ((float) cropPos.getZ() + 0.5F), type.getPlaceSound(), SoundCategory.PLAYERS, (type.getVolume() + 1.0F) / 4.0F, type.getPitch() * 0.8F);
// Action was a success.
return EnumActionResult.SUCCESS;
}
use of net.minecraft.block.SoundType in project RecurrentComplex by Ivorforce.
the class ItemBlockGenericSpace method onItemUse.
@Override
public EnumActionResult onItemUse(EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
ItemStack stack = playerIn.getHeldItem(hand);
IBlockState iblockstate = worldIn.getBlockState(pos);
Block block = iblockstate.getBlock();
if (// Only this was changed
!block.isReplaceable(worldIn, pos) || block == getBlock()) {
pos = pos.offset(facing);
}
if (stack.getCount() != 0 && playerIn.canPlayerEdit(pos, facing, stack) && worldIn.mayPlace(this.block, pos, false, facing, null)) {
int i = this.getMetadata(stack.getMetadata());
IBlockState iblockstate1 = this.block.getStateForPlacement(worldIn, pos, facing, hitX, hitY, hitZ, i, playerIn, EnumHand.MAIN_HAND);
if (placeBlockAt(stack, playerIn, worldIn, pos, facing, hitX, hitY, hitZ, iblockstate1)) {
SoundType soundtype = worldIn.getBlockState(pos).getBlock().getSoundType(worldIn.getBlockState(pos), worldIn, pos, playerIn);
worldIn.playSound(playerIn, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
stack.shrink(1);
}
return EnumActionResult.SUCCESS;
} else {
return EnumActionResult.FAIL;
}
}
use of net.minecraft.block.SoundType in project Railcraft by Railcraft.
the class RCSoundHandler method onPlaySound.
//TODO: test, catch PlaySoundAtEntityEvent?
@SubscribeEvent
public void onPlaySound(PlaySoundEvent event) {
ISound soundEvent = event.getSound();
ResourceLocation soundResource = soundEvent.getSoundLocation();
if (SoundHelper.matchesSoundResource(soundResource, "null")) {
event.setResultSound(null);
} else if (SoundHelper.matchesSoundResource(soundResource, "override")) {
World world = Railcraft.getProxy().getClientWorld();
if (world != null) {
float x = soundEvent.getXPosF();
float y = soundEvent.getYPosF();
float z = soundEvent.getZPosF();
BlockPos pos = new BlockPos(x, y, z);
String soundPath = soundEvent.getSoundLocation().getResourcePath();
SoundType blockSound = SoundRegistry.getBlockSound(world, pos);
if (blockSound == null) {
if (soundPath.contains("place")) {
//Play sound later to adjust for the block not being there yet.
event.getManager().playDelayedSound(event.getSound(), 3);
} else if (soundPath.contains("step")) {
blockSound = SoundRegistry.getBlockSound(world, pos.down());
}
}
if (blockSound != null) {
SoundEvent newSound = SoundHelper.matchSoundEvent(soundResource, blockSound);
ObfuscationReflectionHelper.setPrivateValue(PositionedSound.class, (PositionedSound) soundEvent, newSound.getSoundName(), 3);
} else {
event.setResultSound(null);
}
}
}
}
Aggregations