/** * When a creeper explodes, reinforcements are launched from him with random velocities by this * method. * * <p>Reinforcements are always launched at a 45 degree angle, a configurable range from the * exploding creeper. * * @param creeper the exploding creeper. */ protected void launchReinforcements(Entity creeper) { final int numReinforcements = random(CONFIG.MIN_REINFORCEMENTS, CONFIG.MAX_REINFORCEMENTS); for (int i = 0; i < numReinforcements; ++i) { // Compute unit velocity vector components, given 45 degree pitch. double yaw = 2.0 * Math.PI * Math.random(); double y = INV_ROOT_2; double x = INV_ROOT_2 * Math.cos(yaw); double z = INV_ROOT_2 * Math.sin(yaw); // Spawn one reinforcement. Location origin = creeper.getLocation(); World world = origin.getWorld(); Location loc = origin.clone().add(CONFIG.REINFORCEMENT_RANGE * x, 0.5, CONFIG.REINFORCEMENT_RANGE * z); Creeper reinforcement = (Creeper) world.spawnEntity(loc, EntityType.CREEPER); if (reinforcement != null) { reinforcement.setMetadata(SPECIAL_KEY, SPECIAL_META); double speed = random(CONFIG.MIN_REINFORCEMENT_SPEED, CONFIG.MAX_REINFORCEMENT_SPEED); Vector velocity = new Vector(speed * x, speed * y, speed * z); reinforcement.setVelocity(velocity); // Randomly charge a fraction of creepers. if (Math.random() < CONFIG.CHARGED_CHANCE) { reinforcement.setPowered(true); } } } } // launchReinforcements
@EventHandler public void onInteract(PlayerInteractEvent event) { if (event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.LEFT_CLICK_AIR) { // (2) final Player player = event.getPlayer(); if (player.getItemInHand().getType() == Material.LEATHER) { Location loc = player.getLocation(); Vector vec = calculateVector(loc); final Creeper creeper = player.getWorld().spawn(loc, Creeper.class); // (3) creeper.setVelocity(vec); creeper.setFireTicks(20); BukkitRunnable runnable = new CowTask(player.getWorld(), creeper); runnable.runTaskTimer(this, 0L, 0L); } } }