Java Code Examples for org.bukkit.entity.FallingBlock#setVelocity()

The following examples show how to use org.bukkit.entity.FallingBlock#setVelocity() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Molotov.java    From ce with GNU Lesser General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
   @Override
public void effect(Event e, ItemStack item, final int level) {
	if(e instanceof EntityDamageByEntityEvent) {
	EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) e;
	Entity target = event.getEntity();

	World world = target.getWorld();
	world.playEffect(target.getLocation(), Effect.POTION_BREAK, 10);
	double boundaries = 0.1*level;
	for(double x = boundaries; x >= -boundaries; x-=0.1)
		for(double z = boundaries; z >= -boundaries; z-=0.1) {
			FallingBlock b = world.spawnFallingBlock(target.getLocation(), Material.FIRE.getId(), (byte) 0x0);
			b.setVelocity(new Vector(x, 0.1, z));
			b.setDropItem(false);
		}
	}
}
 
Example 2
Source File: SeismicAxe.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ItemUseHandler getItemHandler() {
    return e -> {
        Player p = e.getPlayer();
        List<Block> blocks = p.getLineOfSight(null, RANGE);

        for (int i = 2; i < blocks.size(); i++) {
            Block ground = findGround(blocks.get(i));
            Location groundLocation = ground.getLocation();

            ground.getWorld().playEffect(groundLocation, Effect.STEP_SOUND, ground.getType());

            if (ground.getRelative(BlockFace.UP).getType() == Material.AIR) {
                Location loc = ground.getRelative(BlockFace.UP).getLocation().add(0.5, 0.0, 0.5);
                FallingBlock block = ground.getWorld().spawnFallingBlock(loc, ground.getBlockData());
                block.setDropItem(false);
                block.setVelocity(new Vector(0, 0.4 + i * 0.01, 0));
                block.setMetadata("seismic_axe", new FixedMetadataValue(SlimefunPlugin.instance, "fake_block"));
            }

            for (Entity n : ground.getChunk().getEntities()) {
                if (n instanceof LivingEntity && n.getType() != EntityType.ARMOR_STAND && n.getLocation().distance(groundLocation) <= 2.0D && !n.getUniqueId().equals(p.getUniqueId())) {
                    pushEntity(p, n);
                }
            }
        }

        for (int i = 0; i < 4; i++) {
            damageItem(p, e.getItem());
        }
    };
}
 
Example 3
Source File: Bombardment.java    From ce with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
   @Override
public void effect(Event e, ItemStack item, final int level) {
	if(e instanceof EntityDamageByEntityEvent) {
	EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) e;
	Entity target = event.getEntity();

	final World world = target.getWorld();
	Vector vec = new Vector(0, -5, 0);
	Location spawnLocation = new Location(world, target.getLocation().getX(), 255, target.getLocation().getZ());
	final FallingBlock b = world.spawnFallingBlock(spawnLocation, 46, (byte) 0x0);
	b.setVelocity(vec);

	new BukkitRunnable() {

		Location	l	= b.getLocation();

		@Override
		public void run() {
			l = b.getLocation();
			if(b.isDead()) {
				l.getBlock().setType(Material.AIR);
				for(int i = 0; i <= TNTAmount + level; i++) {
					TNTPrimed tnt = world.spawn(l, TNTPrimed.class);
					tnt.setFuseTicks(0);
					if(!Main.createExplosions)
						tnt.setMetadata("ce.explosive", new FixedMetadataValue(getPlugin(), null));
				}
				this.cancel();
			}
			
			EffectManager.playSound(l, "ENTITY_ENDERDRAGON_GROWL", Volume, 2f);
		}
	}.runTaskTimer(getPlugin(), 0l, 5l);
	}
}
 
Example 4
Source File: BlockDropsMatchModule.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * This is not an event handler. It is called explicitly by BlockTransformListener after all event
 * handlers have been called.
 */
@SuppressWarnings("deprecation")
public void doBlockDrops(final BlockTransformEvent event) {
  if (!causesDrops(event.getCause())) {
    return;
  }

  final BlockDrops drops = event.getDrops();
  if (drops != null) {
    event.setCancelled(true);
    final BlockState oldState = event.getOldState();
    final BlockState newState = event.getNewState();
    final Block block = event.getOldState().getBlock();
    final int newTypeId = newState.getTypeId();
    final byte newData = newState.getRawData();

    block.setTypeIdAndData(newTypeId, newData, true);

    if (event.getCause() instanceof EntityExplodeEvent) {
      EntityExplodeEvent explodeEvent = (EntityExplodeEvent) event.getCause();
      final float yield = explodeEvent.getYield();

      if (drops.fallChance != null
          && oldState.getType().isBlock()
          && oldState.getType() != Material.AIR
          && match.getRandom().nextFloat() < drops.fallChance) {

        FallingBlock fallingBlock =
            match
                .getWorld()
                .spawnFallingBlock(
                    block.getLocation(),
                    event.getOldState().getType(),
                    event.getOldState().getRawData());
        fallingBlock.setDropItem(false);

        if (drops.landChance != null && match.getRandom().nextFloat() >= drops.landChance) {
          this.fallingBlocksThatWillNotLand.add(fallingBlock);
        }

        Vector v = fallingBlock.getLocation().subtract(explodeEvent.getLocation()).toVector();
        double distance = v.length();
        v.normalize().multiply(BASE_FALL_SPEED * drops.fallSpeed / Math.max(1d, distance));

        // A very simple deflection model. Check for a solid
        // neighbor block and "bounce" the velocity off of it.
        Block west = block.getRelative(BlockFace.WEST);
        Block east = block.getRelative(BlockFace.EAST);
        Block down = block.getRelative(BlockFace.DOWN);
        Block up = block.getRelative(BlockFace.UP);
        Block north = block.getRelative(BlockFace.NORTH);
        Block south = block.getRelative(BlockFace.SOUTH);

        if ((v.getX() < 0 && west != null && west.getType().isSolid())
            || v.getX() > 0 && east != null && east.getType().isSolid()) {
          v.setX(-v.getX());
        }

        if ((v.getY() < 0 && down != null && down.getType().isSolid())
            || v.getY() > 0 && up != null && up.getType().isSolid()) {
          v.setY(-v.getY());
        }

        if ((v.getZ() < 0 && north != null && north.getType().isSolid())
            || v.getZ() > 0 && south != null && south.getType().isSolid()) {
          v.setZ(-v.getZ());
        }

        fallingBlock.setVelocity(v);
      }

      // Defer item drops so the explosion doesn't destroy them
      match
          .getExecutor(MatchScope.RUNNING)
          .execute(() -> dropItems(drops, newState.getLocation(), yield));
    } else {
      MatchPlayer player = ParticipantBlockTransformEvent.getParticipant(event);
      if (player == null
          || player.getBukkit().getGameMode()
              != GameMode.CREATIVE) { // Don't drop items in creative mode
        dropItems(drops, newState.getLocation(), 1d);
        dropExperience(drops, newState.getLocation());
      }
    }
  }
}
 
Example 5
Source File: BlockDropsMatchModule.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * This is not an event handler. It is called explicitly by BlockTransformListener
 * after all event handlers have been called.
 */
@SuppressWarnings("deprecation")
public void doBlockDrops(final BlockTransformEvent event) {
    if(!causesDrops(event.getCause())) {
        return;
    }

    final BlockDrops drops = event.getDrops();
    if(drops != null) {
        event.setCancelled(true);
        final BlockState oldState = event.getOldState();
        final BlockState newState = event.getNewState();
        final Block block = event.getOldState().getBlock();
        final int newTypeId = newState.getTypeId();
        final byte newData = newState.getRawData();

        block.setTypeIdAndData(newTypeId, newData, true);

        boolean explosion = false;
        MatchPlayer player = ParticipantBlockTransformEvent.getParticipant(event);

        if(event.getCause() instanceof EntityExplodeEvent) {
            EntityExplodeEvent explodeEvent = (EntityExplodeEvent) event.getCause();
            explosion = true;

            if(drops.fallChance != null &&
               oldState.getType().isBlock() &&
               oldState.getType() != Material.AIR &&
               this.getMatch().getRandom().nextFloat() < drops.fallChance) {

                FallingBlock fallingBlock = event.getOldState().spawnFallingBlock();
                fallingBlock.setDropItem(false);

                if(drops.landChance != null && this.getMatch().getRandom().nextFloat() >= drops.landChance) {
                    this.fallingBlocksThatWillNotLand.add(fallingBlock);
                }

                Vector v = fallingBlock.getLocation().subtract(explodeEvent.getLocation()).toVector();
                double distance = v.length();
                v.normalize().multiply(BASE_FALL_SPEED * drops.fallSpeed / Math.max(1d, distance));

                // A very simple deflection model. Check for a solid
                // neighbor block and "bounce" the velocity off of it.
                Block west = block.getRelative(BlockFace.WEST);
                Block east = block.getRelative(BlockFace.EAST);
                Block down = block.getRelative(BlockFace.DOWN);
                Block up = block.getRelative(BlockFace.UP);
                Block north = block.getRelative(BlockFace.NORTH);
                Block south = block.getRelative(BlockFace.SOUTH);

                if((v.getX() < 0 && west != null && Materials.isColliding(west.getType())) ||
                    v.getX() > 0 && east != null && Materials.isColliding(east.getType())) {
                    v.setX(-v.getX());
                }

                if((v.getY() < 0 && down != null && Materials.isColliding(down.getType())) ||
                    v.getY() > 0 && up != null && Materials.isColliding(up.getType())) {
                    v.setY(-v.getY());
                }

                if((v.getZ() < 0 && north != null && Materials.isColliding(north.getType())) ||
                    v.getZ() > 0 && south != null && Materials.isColliding(south.getType())) {
                    v.setZ(-v.getZ());
                }

                fallingBlock.setVelocity(v);
            }
        }

        dropObjects(drops, player, newState.getLocation(), 1d, explosion);

    }
}
 
Example 6
Source File: Flamethrower.java    From ce with GNU Lesser General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
   @Override
public boolean effect(Event event, final Player player) {
	PlayerInteractEvent e = (PlayerInteractEvent) event;
		e.setCancelled(true);

		if(player.getItemInHand().getDurability() >= 64) {
			if(IsReloadable) {
			addLock(player);
			player.getWorld().playEffect(player.getLocation(), Effect.CLICK1, 2);
			player.sendMessage(ChatColor.RED + "Reloading...");
			new BukkitRunnable() {
				@Override
				public void run() {
					if(player.getItemInHand().getDurability() == 0) {
						removeLock(player);
						player.getWorld().playEffect(player.getLocation(), Effect.CLICK2, 2);
						this.cancel();
					} else {
						player.getItemInHand().setDurability((short) (player.getItemInHand().getDurability() - 1));
					}
				}
			}.runTaskTimer(main, 0l, 2l);
		} else { 
			player.getItemInHand().setType(Material.AIR);
		}
		} else {
			final List<Location> list = getLinePlayer(player, FireBlocksPerBurst);
			for(final Location l: list) {
				if(l.getBlock().getType().equals(Material.AIR))
					l.getBlock().setType(Material.FIRE);
				l.getWorld().playEffect(l, Effect.SMOKE, 20);
				final FallingBlock fire = l.getWorld().spawnFallingBlock(l, Material.FIRE.getId(), (byte) 0);
				fire.setDropItem(false);
				fire.setVelocity(player.getLocation().getDirection());
				new BukkitRunnable() {
					@Override
					public void run() {
					if(fire.isDead()) {
						list.add(fire.getLocation());
						this.cancel();
					} else {
						if(!Tools.checkWorldGuard(fire.getLocation(), player, "BUILD", true) || fire.getLocation().getBlock().getType().equals(Material.WATER) || fire.getLocation().getBlock().getType().equals(Material.STATIONARY_WATER)) {
							fire.getWorld().playEffect(fire.getLocation(), Effect.EXTINGUISH, 60);
							fire.remove();
							this.cancel();
						}
						for(Entity ent:fire.getNearbyEntities(0, 0, 0)) {
							if(ent != player) {
								ent.setFireTicks(BurnDuration);
							}
						}
					}
					}
				}.runTaskTimer(main, 0l, 1l);
			}
			new BukkitRunnable() {
				@Override
				public void run() {
					for(Location ls:list) {
						if(ls.getBlock().getType().equals(Material.FIRE)) {
						ls.getWorld().playEffect(ls, Effect.EXTINGUISH, 60);
						ls.getBlock().setType(Material.AIR);
						}
					}
				}
			}.runTaskLater(main, 200l);
			player.getWorld().playEffect(player.getLocation(), Effect.BLAZE_SHOOT, 30);
			if(!player.getGameMode().equals(GameMode.CREATIVE)) {
			player.getItemInHand().setDurability((short) (player.getItemInHand().getDurability() + 1));
			}
		}
	
	return true;
}
 
Example 7
Source File: Shockwave.java    From ce with GNU Lesser General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public void effect(Event e, ItemStack item, final int level) {

    final EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) e;
    Player damager = (Player) event.getDamager();

    new BukkitRunnable() {
        @Override
        public void run() {
            event.getEntity().setVelocity(new Vector(0, 1 + (level / 4), 0));
        }
    }.runTaskLater(getPlugin(), 1l);

    Location loc = damager.getLocation();
    loc.setY(damager.getLocation().getY() - 1);
    List<Location> list = Tools.getCone(loc);
    this.generateCooldown(damager, cooldown);
    damager.getWorld().playEffect(damager.getLocation(), Effect.ZOMBIE_DESTROY_DOOR, 10);
    for (final Location l : list) {
        final org.bukkit.block.Block block = l.getBlock();
        Material blockMat = block.getType();
        if (!ForbiddenMaterials.contains(blockMat) && checkSurrounding(block)) {

            if (!Tools.checkWorldGuard(l, damager, "PVP", false))
                return;
            final Material mat = blockMat;
            final byte matData = block.getData();
            final FallingBlock b = l.getWorld().spawnFallingBlock(l, mat, block.getData());
            b.setDropItem(false);
            b.setVelocity(new Vector(0, (0.5 + 0.1 * (list.indexOf(l))) + (level / 4), 0));
            block.setType(Material.AIR);
            new BukkitRunnable() {
                Location finLoc = l;

                @Override
                public void run() {
                    if (!b.isDead()) {
                        finLoc = b.getLocation();
                    } else {
                        //if(finLoc.getBlock().getLocation() != l) 
                        finLoc.getBlock().setType(Material.AIR);
                        block.setType(mat);
                        block.setData(matData);
                        this.cancel();
                    }
                }
            }.runTaskTimer(main, 0l, 5l);
        }
    }

}
 
Example 8
Source File: GravitationalAxe.java    From NBTEditor with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onBlockBreak(BlockBreakEvent event, PlayerDetails details) {
	Block root = event.getBlock();
	if (isLog(root.getType())) {
		// Initialize some variables:
		World world = event.getPlayer().getWorld();
		Location location = root.getLocation();
		Random random = new Random();
		Vector vel = event.getPlayer().getLocation().toVector().subtract(location.toVector()).normalize().setY(0).multiply(0.2);
		// Find the blocks
		Set<Block> blocks = getTreeBlocks(root);
		if (blocks.size() > 0) {
			world.playSound(location, Sound.ENTITY_ZOMBIE_BREAK_WOODEN_DOOR, 0.5f, 1);
		}
		double durability = blocks.size()*0.25;
		// Bring 'em down.
		for (Block block : blocks) {
			Material mat = block.getType();
			if (random.nextFloat() < 0.1f && isLog(mat)) {
				world.playSound(block.getLocation(), Sound.ENTITY_ZOMBIE_BREAK_WOODEN_DOOR, 0.6f, 1);
				block.breakNaturally();
				durability += 1;
			} else if (random.nextFloat() < 0.4f && isLeaves(mat)) {
				world.playSound(block.getLocation(), Sound.BLOCK_GRASS_BREAK, 0.5f, 1);
				block.breakNaturally();
				durability += 1;
			} else {
				FallingBlock fallingBlock = world.spawnFallingBlock(block.getLocation(), block.getBlockData());
				fallingBlock.setVelocity(vel.multiply(random.nextFloat()*0.2 + 0.9));
				if (isLeaves(mat)) {
					fallingBlock.setDropItem(false);
				}
				block.setType(Material.AIR);
			}
		}
		// Apply durability.
		if (event.getPlayer().getGameMode() != GameMode.CREATIVE) {
			UtilsMc.offsetItemStackDamage(details.getItem(), (int) durability);
		}
	}
}