From 56a616ee76890e0c7d565d2e8fdf8f4d174eb23f Mon Sep 17 00:00:00 2001 From: garbagemule Date: Mon, 7 Oct 2013 02:05:59 +0200 Subject: [PATCH] Implement mounts. --- resources/plugin.yml | 2 +- src/com/garbagemule/MobArena/ArenaImpl.java | 72 ++++++++++++++++--- .../garbagemule/MobArena/ArenaListener.java | 12 ++-- 3 files changed, 67 insertions(+), 19 deletions(-) diff --git a/resources/plugin.yml b/resources/plugin.yml index db2bef9..d260b57 100644 --- a/resources/plugin.yml +++ b/resources/plugin.yml @@ -1,7 +1,7 @@ name: MobArena author: garbagemule main: com.garbagemule.MobArena.MobArena -version: 0.95.5.35 +version: 0.95.5.36 softdepend: [Multiverse-Core,Towny,Heroes,MagicSpells,Vault] commands: ma: diff --git a/src/com/garbagemule/MobArena/ArenaImpl.java b/src/com/garbagemule/MobArena/ArenaImpl.java index 78e332e..8f8d716 100644 --- a/src/com/garbagemule/MobArena/ArenaImpl.java +++ b/src/com/garbagemule/MobArena/ArenaImpl.java @@ -624,8 +624,9 @@ public class ArenaImpl implements Arena return false; } - // Clear inventory if player is an arena player + // Clear inventory if player is an arena player, and unmount if (arenaPlayers.contains(p)) { + unmount(p); clearInv(p); } @@ -667,8 +668,9 @@ public class ArenaImpl implements Arena ArenaPlayerDeathEvent event = new ArenaPlayerDeathEvent(p, this, last); plugin.getServer().getPluginManager().callEvent(event); - // Clear the player's inventory + // Clear the player's inventory, and unmount if (arenaPlayers.remove(p)) { + unmount(p); clearInv(p); } @@ -692,6 +694,15 @@ public class ArenaImpl implements Arena } } + private void unmount(Player p) { + Entity v = p.getVehicle(); + if (v != null) { + monsterManager.removeMount(v); + v.eject(); + v.remove(); + } + } + @Override public void playerRespawn(Player p) { if (settings.getBoolean("auto-respawn", true)) { @@ -774,21 +785,60 @@ public class ArenaImpl implements Arena } private void spawnMounts() { - /* TODO: Uncomment for 1.6 - for (Map.Entry entry : arenaPlayerMap.entrySet()) { - ArenaClass arenaClass = entry.getValue().getArenaClass(); - if (!arenaClass.hasMount()) continue; + for (Player p : arenaPlayers) { + // Skip players who are either null or offline + if (p == null || !p.isOnline()) continue; - // Remove the hay bale - Player p = entry.getKey(); - p.getInventory().removeItem(new ItemStack(Material.HAY_BLOCK, 1)); + // Grab the inventory + PlayerInventory inv = p.getInventory(); + if (inv == null) continue; - // Spawn the horse + // Find the first slot containing a haybale + int hay = inv.first(Material.HAY_BLOCK); + if (hay == -1) continue; + + // Grab the amount and calculate the configuration + int amount = inv.getItem(hay).getAmount(); + + // Variant + Horse.Variant variant = Horse.Variant.HORSE; + switch (amount % 8) { + case 2: variant = Horse.Variant.DONKEY; break; + case 3: variant = Horse.Variant.MULE; break; + case 4: variant = Horse.Variant.SKELETON_HORSE; break; + case 5: variant = Horse.Variant.UNDEAD_HORSE; break; + default: break; + } + + // Barding + Material barding = null; + switch ((amount >> 3) % 4) { + case 1: barding = Material.IRON_BARDING; break; + case 2: barding = Material.GOLD_BARDING; break; + case 3: barding = Material.DIAMOND_BARDING; break; + default: break; + } + + // Spawn the horse, set its variant, tame it, etc. Horse horse = (Horse) world.spawnEntity(p.getLocation(), EntityType.HORSE); + horse.setVariant(variant); + horse.setTamed(true); + horse.setOwner(p); horse.setPassenger(p); horse.setHealth(horse.getMaxHealth()); + + // Give it a saddle and possibly barding + horse.getInventory().setSaddle(new ItemStack(Material.SADDLE)); + if (barding != null) { + horse.getInventory().setArmor(new ItemStack(barding)); + } + + // Add to monster manager monsterManager.addMount(horse); - }*/ + + // Remove the hay + inv.setItem(hay, null); + } } private void removePotionEffects(Player p) { diff --git a/src/com/garbagemule/MobArena/ArenaListener.java b/src/com/garbagemule/MobArena/ArenaListener.java index d246dea..e7dbb97 100644 --- a/src/com/garbagemule/MobArena/ArenaListener.java +++ b/src/com/garbagemule/MobArena/ArenaListener.java @@ -457,7 +457,7 @@ public class ArenaListener } private void onMountDeath(EntityDeathEvent event) { - + // Shouldn't ever happen } private void onMonsterDeath(EntityDeathEvent event) { @@ -566,10 +566,9 @@ public class ArenaListener onPetDamage(event, (Wolf) damagee, damager); } // Mount - /* TODO: Uncomment for 1.6 else if (damagee instanceof Horse && monsters.hasMount(damagee)) { onMountDamage(event, (Horse) damagee, damager); - }*/ + } // Player else if (damagee instanceof Player) { onPlayerDamage(event, (Player) damagee, damager); @@ -613,11 +612,10 @@ public class ArenaListener private void onPetDamage(EntityDamageEvent event, Wolf pet, Entity damager) { event.setCancelled(true); } - - /* TODO: Uncomment for 1.6 - private void onMountDamage(EntityDamageEvent event, Horse mount, Entity damager) { - }*/ + private void onMountDamage(EntityDamageEvent event, Horse mount, Entity damager) { + event.setCancelled(true); + } private void onMonsterDamage(EntityDamageEvent event, Entity monster, Entity damager) { if (damager instanceof Player) {