Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Represents a dragon mount */
- public final class Dragon extends EntityEnderDragon {
- /** The player that's riding the dragon */
- final Player rider;
- /** The dragon's current location and orientation around the Z axis */
- final Location loc;
- /** The dragon's speed */
- private float speed = .7f;
- /** The dragon's health */
- private int health;
- /** The list of all the dragons */
- static final List<Dragon> dragons = new ArrayList<Dragon>();
- /** The amount of time until the dragon can be hurt by melee damage again */
- private int damageCooldown = 0;
- /** The default constructor
- * @param rider - the player that summoned this dragon
- * @param maxHealth - the amount of hitpoints this dragon has
- * @param world - the NMS world we're spawning the dragon in
- */
- Dragon(Player rider, int maxHealth, WorldServer world) {
- super(world);
- this.rider = rider;
- setHealth(health = maxHealth);
- loc = rider.getLocation();
- loc.setYaw(yaw = loc.getYaw() - 180);
- loc.setPitch(0);
- setPosition(loc, false);
- getBukkitEntity().setPassenger(rider);
- ((LivingEntity) getBukkitEntity()).setMaxHealth(maxHealth);
- dragons.add(this);
- }
- @Override
- /** The main thread of this dragon, called every tick */
- public void c() {
- // Checks if the player dismounted. Kills the dragon and returns if yes
- if (bukkitEntity.getPassenger() != rider) {
- kill();
- return;
- }
- // Handles the damage cooldown
- if (damageCooldown > 0) damageCooldown--;
- // Handles the controls
- EntityLiving passenger = (EntityLiving) this.passenger;
- loc.setYaw(yaw = passenger.yaw - 180);
- boolean left = passenger.be < 0, right = passenger.be > 0, forwards = passenger.bf > 0, backwards = passenger.bf < 0;
- double y = rider.getLocation().getDirection().getY();
- Vector add = new Vector();
- if (left) addTurnedDir(add, 90, .5, 0);
- if (right) addTurnedDir(add, -90, .5, 0);
- if (forwards) addTurnedDir(add, 0, 1, y);
- else if (backwards) addTurnedDir(add, 0, -.5, y);
- try {
- Field jump = EntityLiving.class.getDeclaredField("bd");
- jump.setAccessible(true);
- if (jump.getBoolean(passenger)) add.setY(.3f);
- } catch (Exception e) {
- e.printStackTrace();
- }
- loc.add(add.multiply(speed));
- setPosition(loc, true);
- }
- /** Adds an angled version of the dragon's direction to the vector
- * @param add - the vector that we're adding to
- * @param angle - the angle under which we're adding to the vector. If 0, this will also set add a vertical thrust
- * @param mod - how much should we add to the vector
- * @param y - the vertical thrust. Get from the player's direction
- */
- void addTurnedDir(Vector add, float angle, double mod, double y) {
- Location loc = this.loc.clone();
- loc.setYaw(yaw + angle);
- Vector dir = loc.getDirection();
- if (angle == 0) dir.setY(-y);
- add.add(dir.normalize().multiply(-mod));
- }
- /** Kills this dragon */
- void kill() {
- getBukkitEntity().remove();
- }
- /** Sets the dragon's position and does the collision checks
- * @param loc - the location to move the dragon to
- * @param colide - if true, the dragon will collide with the terrain
- */
- void setPosition(Location loc, boolean colide) {
- if (colide) if (loc.getBlock().getType().isSolid())
- if (!colAndRev(true, false, false) && !colAndRev(false, true, false) && !colAndRev(false, false, true) && !colAndRev(true, true, false) && !colAndRev(false, true, true)) {
- loc.setX(locX);
- loc.setY(locY);
- loc.setZ(locZ);
- }
- setPosition(loc.getX(), loc.getY(), loc.getZ());
- }
- /** Checks if the selected axes are the one the dragon got stuck on. If yes, it rolls them back
- * @param revX - if it should check the X axis
- * @param revY - if it should check the Y axis
- * @param revZ - if it should check the Z axis
- * @return if it succesfuly rolled back. If false, continue looking for the problematic axes
- */
- private boolean colAndRev(boolean revX, boolean revY, boolean revZ) {
- double x = loc.getX(), y = loc.getY(), z = loc.getZ();
- if (revX) x = locX;
- if (revY) y = locY;
- if (revZ) z = locZ;
- if (!loc.getWorld().getBlockAt((int) x, (int) y, (int) z).getType().isSolid()) {
- loc.setX(x);
- loc.setY(y);
- loc.setZ(z);
- return true;
- }
- return false;
- }
- /** Deals damage to the dragon. Kills him if his health drops to 0
- * @param damage - the amount of damage to be dealt
- */
- void damage(int damage) {
- health -= damage;
- if (health <= 0) kill();
- else setHealth(health);
- }
- /** Deals melee damage to the dragon
- * @param material - the item that the dragon was punched with
- */
- void damage(Material material) {
- if (damageCooldown > 0) return;
- damageCooldown = 10;
- int damage = 1;
- if (material == Material.DIAMOND_SWORD) damage += 7;
- else if (material == Material.IRON_SWORD) damage += 6;
- else if (material == Material.STONE_SWORD) damage += 5;
- else if (material == Material.WOOD_SWORD || material == Material.GOLD_SWORD) damage += 4;
- damage(damage);
- }
- }
- /** Used to listen to events */
- public final class MMListener implements Listener {
- /** The list of all the arrows */
- private List<Arrow> arrows;
- /** The default constructor */
- MMListener() {
- arrows = new ArrayList<Arrow>();
- MM plugin = MM.getPlugin();
- plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
- public void run() {
- for (Arrow arrow : arrows) dragonSearch: for (Dragon dragon : Dragon.dragons) if (arrow.getLocation().distanceSquared(dragon.loc) <= 9) {
- dragon.damage(6);
- arrow.remove();
- arrows.remove(arrow);
- break dragonSearch;
- }
- }
- }, 1, 1);
- }
- /** Handles mouse clicks done by players */
- @EventHandler(priority = EventPriority.HIGH)
- public void onPlayerInteract(PlayerInteractEvent event) {
- Player player = event.getPlayer();
- Action action = event.getAction();
- // Checks if the player attacked a dragon. If so, deals melee damage to that dragon
- dragonSearch: for (Dragon dragon : Dragon.dragons) if (player != dragon.rider) for (int i = 0; i < 5; i++)
- if (player.getTargetBlock(null, i).getLocation().distanceSquared(dragon.loc) <= 9) {
- dragon.damage(player.getItemInHand().getType());
- break dragonSearch;
- }
- }
- @EventHandler(priority = EventPriority.HIGH)
- /** Handles shot projectiles */
- public void onProjectileLaunch(ProjectileLaunchEvent event) {
- Projectile projectile = event.getEntity();
- if (projectile instanceof Arrow) arrows.add((Arrow) projectile);
- }
- @EventHandler(priority = EventPriority.HIGH)
- /** Handles projectiles landing */
- public void onProjectileHit(ProjectileHitEvent event) {
- Projectile projectile = event.getEntity();
- if (arrows.contains(projectile)) arrows.remove(projectile);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment