Straighten up the Taming classes in order to stop passing events around quite so much. Also reordered some checks in our EntityDamage listeners for readability & efficiency purposes.

This commit is contained in:
GJ 2013-03-01 15:49:24 -05:00
parent c0b7f8a323
commit cbe9cae4ea
10 changed files with 242 additions and 424 deletions

View File

@ -38,6 +38,7 @@ import com.gmail.nossr50.skills.fishing.Fishing;
import com.gmail.nossr50.skills.herbalism.Herbalism; import com.gmail.nossr50.skills.herbalism.Herbalism;
import com.gmail.nossr50.skills.mining.MiningManager; import com.gmail.nossr50.skills.mining.MiningManager;
import com.gmail.nossr50.skills.taming.Taming; import com.gmail.nossr50.skills.taming.Taming;
import com.gmail.nossr50.skills.taming.TamingManager;
import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.player.UserManager;
@ -59,18 +60,21 @@ public class EntityListener implements Listener {
public void onEntityChangeBlockEvent(EntityChangeBlockEvent event) { public void onEntityChangeBlockEvent(EntityChangeBlockEvent event) {
Entity entity = event.getEntity(); Entity entity = event.getEntity();
if (entity instanceof FallingBlock) { if (!(entity instanceof FallingBlock)) {
Block block = event.getBlock(); return;
}
if (mcMMO.placeStore.isTrue(block) && !entity.hasMetadata(mcMMO.entityMetadataKey)) { Block block = event.getBlock();
boolean isTracked = entity.hasMetadata(mcMMO.entityMetadataKey);
if (mcMMO.placeStore.isTrue(block) && !isTracked) {
mcMMO.placeStore.setFalse(block); mcMMO.placeStore.setFalse(block);
entity.setMetadata(mcMMO.entityMetadataKey, mcMMO.metadataValue); entity.setMetadata(mcMMO.entityMetadataKey, mcMMO.metadataValue);
} }
else if (entity.hasMetadata(mcMMO.entityMetadataKey)) { else if (isTracked) {
mcMMO.placeStore.setTrue(block); mcMMO.placeStore.setTrue(block);
} }
} }
}
/** /**
* Handle EntityDamageByEntity events that involve modifying the event. * Handle EntityDamageByEntity events that involve modifying the event.
@ -85,11 +89,12 @@ public class EntityListener implements Listener {
Entity defender = event.getEntity(); Entity defender = event.getEntity();
if (Misc.isNPCEntity(defender) || defender.isDead()) { if (Misc.isNPCEntity(defender) || !defender.isValid() || !(defender instanceof LivingEntity)) {
return; return;
} }
Entity attacker = event.getDamager(); Entity attacker = event.getDamager();
LivingEntity target = (LivingEntity) defender;
if (attacker instanceof Projectile) { if (attacker instanceof Projectile) {
attacker = ((Projectile) attacker).getShooter(); attacker = ((Projectile) attacker).getShooter();
@ -102,6 +107,10 @@ public class EntityListener implements Listener {
} }
} }
if (CombatUtils.isInvincible(target, event.getDamage())) {
return;
}
if (defender instanceof Player && attacker instanceof Player) { if (defender instanceof Player && attacker instanceof Player) {
Player defendingPlayer = (Player) defender; Player defendingPlayer = (Player) defender;
Player attackingPlayer = (Player) attacker; Player attackingPlayer = (Player) attacker;
@ -110,22 +119,14 @@ public class EntityListener implements Listener {
if (defendingPlayer == attackingPlayer) { if (defendingPlayer == attackingPlayer) {
return; return;
} }
else if (PartyManager.inSameParty(defendingPlayer, attackingPlayer)) {
if (!(Permissions.friendlyFire(attackingPlayer) && Permissions.friendlyFire(defendingPlayer))) { if (PartyManager.inSameParty(defendingPlayer, attackingPlayer) && !(Permissions.friendlyFire(attackingPlayer) && Permissions.friendlyFire(defendingPlayer))) {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
} }
}
/* Check for invincibility */ CombatUtils.combatChecks(event, attacker, target);
if (defender instanceof LivingEntity) {
LivingEntity livingDefender = (LivingEntity) defender;
if (!CombatUtils.isInvincible(livingDefender, event.getDamage())) {
CombatUtils.combatChecks(event, attacker, livingDefender);
}
}
} }
/** /**
@ -210,24 +211,26 @@ public class EntityListener implements Listener {
Player player = (Player) owner; Player player = (Player) owner;
Wolf wolf = (Wolf) pet; Wolf wolf = (Wolf) pet;
TamingManager tamingManager = UserManager.getPlayer(player).getTamingManager();
switch (cause) { switch (cause) {
case CONTACT: case CONTACT:
case FIRE: case FIRE:
case LAVA: case LAVA:
if (Taming.canUseEnvironmentallyAware(player)) { if (tamingManager.canUseEnvironmentallyAware()) {
Taming.processEnvironmentallyAware(player, wolf, event.getDamage()); tamingManager.processEnvironmentallyAware(wolf, event.getDamage());
} }
return; return;
case FALL: case FALL:
if (Taming.canUseEnvironmentallyAware(player)) { if (tamingManager.canUseEnvironmentallyAware()) {
event.setCancelled(true); event.setCancelled(true);
} }
return; return;
case ENTITY_ATTACK: case ENTITY_ATTACK:
case PROJECTILE: case PROJECTILE:
if (Taming.canUseThickFur(player)) { if (tamingManager.canUseThickFur()) {
event.setDamage(Taming.processThickFur(wolf, event.getDamage())); event.setDamage(Taming.processThickFur(wolf, event.getDamage()));
if (event.getDamage() == 0) { if (event.getDamage() == 0) {
@ -237,7 +240,7 @@ public class EntityListener implements Listener {
return; return;
case FIRE_TICK: case FIRE_TICK:
if (Taming.canUseThickFur(player)) { if (tamingManager.canUseThickFur()) {
Taming.processThickFurFire(wolf); Taming.processThickFurFire(wolf);
} }
return; return;
@ -245,7 +248,7 @@ public class EntityListener implements Listener {
case MAGIC: case MAGIC:
case POISON: case POISON:
case WITHER: case WITHER:
if (Taming.canUseHolyHound(player)) { if (tamingManager.canUseHolyHound()) {
Taming.processHolyHound(wolf, event.getDamage()); Taming.processHolyHound(wolf, event.getDamage());
} }
return; return;
@ -253,7 +256,7 @@ public class EntityListener implements Listener {
case BLOCK_EXPLOSION: case BLOCK_EXPLOSION:
case ENTITY_EXPLOSION: case ENTITY_EXPLOSION:
case LIGHTNING: case LIGHTNING:
if (Taming.canUseShockProof(player)) { if (tamingManager.canUseShockProof()) {
event.setDamage(Taming.processShockProof(wolf, event.getDamage())); event.setDamage(Taming.processShockProof(wolf, event.getDamage()));
if (event.getDamage() == 0) { if (event.getDamage() == 0) {
@ -294,7 +297,7 @@ public class EntityListener implements Listener {
*/ */
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onCreatureSpawn(CreatureSpawnEvent event) { public void onCreatureSpawn(CreatureSpawnEvent event) {
if (Misc.isSpawnerXPEnabled || event.getEntity() == null) { if (Misc.isSpawnerXPEnabled) {
return; return;
} }
@ -317,7 +320,9 @@ public class EntityListener implements Listener {
if (entity instanceof TNTPrimed) { if (entity instanceof TNTPrimed) {
int id = entity.getEntityId(); int id = entity.getEntityId();
if (plugin.tntIsTracked(id)) { if (!plugin.tntIsTracked(id)) {
return;
}
MiningManager miningManager = UserManager.getPlayer(plugin.getTNTPlayer(id)).getMiningManager(); MiningManager miningManager = UserManager.getPlayer(plugin.getTNTPlayer(id)).getMiningManager();
@ -326,7 +331,6 @@ public class EntityListener implements Listener {
} }
} }
} }
}
/** /**
* Handle EntityExplode events that involve modifying the event. * Handle EntityExplode events that involve modifying the event.
@ -340,7 +344,10 @@ public class EntityListener implements Listener {
if (entity instanceof TNTPrimed) { if (entity instanceof TNTPrimed) {
int id = entity.getEntityId(); int id = entity.getEntityId();
if (plugin.tntIsTracked(id)) { if (!plugin.tntIsTracked(id)) {
return;
}
MiningManager miningManager = UserManager.getPlayer(plugin.getTNTPlayer(id)).getMiningManager(); MiningManager miningManager = UserManager.getPlayer(plugin.getTNTPlayer(id)).getMiningManager();
if (miningManager.canUseBlastMining()) { if (miningManager.canUseBlastMining()) {
@ -351,7 +358,6 @@ public class EntityListener implements Listener {
plugin.removeFromTNTTracker(id); plugin.removeFromTNTTracker(id);
} }
} }
}
/** /**
* Handle FoodLevelChange events that involve modifying the event. * Handle FoodLevelChange events that involve modifying the event.
@ -373,13 +379,16 @@ public class EntityListener implements Listener {
int newFoodLevel = event.getFoodLevel(); int newFoodLevel = event.getFoodLevel();
int foodChange = newFoodLevel - currentFoodLevel; int foodChange = newFoodLevel - currentFoodLevel;
if (foodChange <= 0) {
return;
}
/* /*
* Some foods have 3 ranks * Some foods have 3 ranks
* Some foods have 5 ranks * Some foods have 5 ranks
* The number of ranks is based on how 'common' the item is * The number of ranks is based on how 'common' the item is
* We can adjust this quite easily if we find something is giving too much of a bonus * We can adjust this quite easily if we find something is giving too much of a bonus
*/ */
if (foodChange > 0) {
switch (player.getItemInHand().getType()) { switch (player.getItemInHand().getType()) {
case BAKED_POTATO: /* RESTORES 3 HUNGER - RESTORES 5 1/2 HUNGER @ 1000 */ case BAKED_POTATO: /* RESTORES 3 HUNGER - RESTORES 5 1/2 HUNGER @ 1000 */
case BREAD: /* RESTORES 2 1/2 HUNGER - RESTORES 5 HUNGER @ 1000 */ case BREAD: /* RESTORES 2 1/2 HUNGER - RESTORES 5 HUNGER @ 1000 */
@ -418,7 +427,6 @@ public class EntityListener implements Listener {
} }
} }
} }
}
/** /**
* Monitor EntityTame events. * Monitor EntityTame events.
@ -428,34 +436,39 @@ public class EntityListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntityTame(EntityTameEvent event) { public void onEntityTame(EntityTameEvent event) {
Player player = (Player) event.getOwner(); Player player = (Player) event.getOwner();
LivingEntity entity = event.getEntity();
if (Misc.isNPCEntity(player)) { if (Misc.isNPCEntity(player) || Misc.isNPCEntity(entity) || entity.hasMetadata(mcMMO.entityMetadataKey)) {
return; return;
} }
LivingEntity entity = event.getEntity();
if (entity != null && !entity.hasMetadata(mcMMO.entityMetadataKey)) {
UserManager.getPlayer(player).getTamingManager().awardTamingXP(entity); UserManager.getPlayer(player).getTamingManager().awardTamingXP(entity);
} }
}
/**
* Handle EntityTarget events.
*
* @param event The event to process
*/
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onEntityTarget(EntityTargetEvent event) { public void onEntityTarget(EntityTargetEvent event) {
if (event.getEntity() instanceof Tameable && event.getTarget() instanceof Player) { Entity entity = event.getEntity();
Player player = (Player) event.getTarget(); Entity target = event.getTarget();
Tameable tameable = (Tameable) event.getEntity();
if (entity instanceof Tameable && target instanceof Player) {
Player player = (Player) target;
Tameable tameable = (Tameable) entity;
if (!CombatUtils.isFriendlyPet(player, tameable)) {
return;
}
if (CombatUtils.isFriendlyPet(player, tameable)) {
// isFriendlyPet ensures that the Tameable is: Tamed, owned by a player, and the owner is in the same party // isFriendlyPet ensures that the Tameable is: Tamed, owned by a player, and the owner is in the same party
// So we can make some assumptions here, about our casting and our check // So we can make some assumptions here, about our casting and our check
Player owner = (Player) tameable.getOwner(); if (!(Permissions.friendlyFire(player) && Permissions.friendlyFire((Player) tameable.getOwner()))) {
if (!(Permissions.friendlyFire(player) && Permissions.friendlyFire(owner))) {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
} }
} }
} }
}

View File

@ -401,7 +401,7 @@ public class PlayerListener implements Listener {
case LEFT_CLICK_BLOCK: case LEFT_CLICK_BLOCK:
/* CALL OF THE WILD CHECKS */ /* CALL OF THE WILD CHECKS */
if (player.isSneaking()) { if (player.isSneaking() && Permissions.callOfTheWild(player)) {
Material type = heldItem.getType(); Material type = heldItem.getType();
TamingManager tamingManager = mcMMOPlayer.getTamingManager(); TamingManager tamingManager = mcMMOPlayer.getTamingManager();

View File

@ -1,54 +0,0 @@
package com.gmail.nossr50.skills.taming;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.AnimalTamer;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Tameable;
import com.gmail.nossr50.locale.LocaleLoader;
public class BeastLoreEventHandler {
private Player player;
private LivingEntity livingEntity;
private Tameable beast;
protected BeastLoreEventHandler(Player player, LivingEntity livingEntity) {
this.player = player;
this.livingEntity = livingEntity;
this.beast = (Tameable) livingEntity;
}
protected void sendInspectMessage() {
if (player == null) {
return;
}
String message = LocaleLoader.getString("Combat.BeastLore") + " ";
if (beast.isTamed()) {
message = message.concat(LocaleLoader.getString("Combat.BeastLoreOwner", getOwnerName()) + " ");
}
message = message.concat(LocaleLoader.getString("Combat.BeastLoreHealth", livingEntity.getHealth(), livingEntity.getMaxHealth()));
player.sendMessage(message);
}
/**
* Get the name of a tameable animal's owner.
*
* @return the name of the animal's owner
*/
private String getOwnerName() {
AnimalTamer tamer = beast.getOwner();
if (tamer instanceof Player) {
return ((Player) tamer).getName();
}
else if (tamer instanceof OfflinePlayer) {
return ((OfflinePlayer) tamer).getName();
}
return "Unknown Master";
}
}

View File

@ -1,108 +0,0 @@
package com.gmail.nossr50.skills.taming;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Ocelot;
import org.bukkit.entity.Player;
import org.bukkit.entity.Tameable;
import org.bukkit.inventory.ItemStack;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.StringUtils;
public class CallOfTheWildEventHandler {
protected Player player;
protected ItemStack inHand;
protected EntityType type;
protected int summonAmount;
protected CallOfTheWildEventHandler(Player player, EntityType type, int summonAmount) {
this.player = player;
this.inHand = player.getItemInHand();
this.type = type;
this.summonAmount = summonAmount;
}
protected void sendInsufficientAmountMessage() {
if (player == null) {
return;
}
player.sendMessage(LocaleLoader.getString("Skills.NeedMore", StringUtils.getPrettyItemString(inHand.getTypeId())));
}
protected boolean nearbyEntityExists() {
if (player == null) {
return false;
}
boolean entityExists = false;
for (Entity entity : player.getNearbyEntities(40, 40, 40)) {
if (entity.getType() == type) {
entityExists = true;
break;
}
}
return entityExists;
}
protected void sendFailureMessage() {
if (player == null) {
return;
}
if (type == EntityType.OCELOT) {
player.sendMessage(LocaleLoader.getString("Taming.Summon.Fail.Ocelot"));
}
else {
player.sendMessage(LocaleLoader.getString("Taming.Summon.Fail.Wolf"));
}
}
protected void spawnCreature() {
if (player == null) {
return;
}
LivingEntity entity = (LivingEntity) player.getWorld().spawnEntity(player.getLocation(), type);
entity.setMetadata(mcMMO.entityMetadataKey, mcMMO.metadataValue);
((Tameable) entity).setOwner(player);
if (type == EntityType.OCELOT) {
((Ocelot) entity).setCatType(Ocelot.Type.getType(1 + Misc.getRandom().nextInt(3)));
}
else {
entity.setHealth(entity.getMaxHealth());
}
}
protected void processResourceCost() {
if (player == null) {
return;
}
int newAmount = inHand.getAmount() - summonAmount;
if (newAmount == 0) {
player.setItemInHand(new ItemStack(Material.AIR));
}
else {
player.getItemInHand().setAmount(inHand.getAmount() - summonAmount);
}
}
protected void sendSuccessMessage() {
if (player == null) {
return;
}
player.sendMessage(LocaleLoader.getString("Taming.Summon.Complete"));
}
}

View File

@ -1,27 +0,0 @@
package com.gmail.nossr50.skills.taming;
import org.bukkit.entity.Wolf;
public class FastFoodServiceEventHandler {
private Wolf wolf;
public FastFoodServiceEventHandler(Wolf wolf) {
this.wolf = wolf;
}
protected void modifyHealth(int damage) {
int health = wolf.getHealth();
int maxHealth = wolf.getMaxHealth();
if (health < maxHealth) {
int newHealth = health + damage;
if (newHealth <= maxHealth) {
wolf.setHealth(newHealth);
}
else {
wolf.setHealth(maxHealth);
}
}
}
}

View File

@ -1,44 +0,0 @@
package com.gmail.nossr50.skills.taming;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageEvent;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.runnables.skills.BleedTimerTask;
import com.gmail.nossr50.util.skills.SkillUtils;
public class GoreEventHandler {
private TamingManager manager;
private EntityDamageEvent event;
private Entity entity;
protected int skillModifier;
protected GoreEventHandler(TamingManager manager, EntityDamageEvent event) {
this.manager = manager;
this.event = event;
this.entity = event.getEntity();
calculateSkillModifier();
}
protected void calculateSkillModifier() {
this.skillModifier = SkillUtils.skillCheck(manager.getSkillLevel(), Taming.goreMaxBonusLevel);
}
protected void modifyEventDamage() {
event.setDamage(event.getDamage() * Taming.goreModifier);
}
protected void sendAbilityMessage() {
if (entity instanceof Player) {
((Player) entity).sendMessage(LocaleLoader.getString("Combat.StruckByGore"));
}
manager.getMcMMOPlayer().getPlayer().sendMessage(LocaleLoader.getString("Combat.Gore"));
}
protected void applyBleed() {
BleedTimerTask.add((LivingEntity) entity, Taming.goreBleedTicks);
}
}

View File

@ -1,15 +0,0 @@
package com.gmail.nossr50.skills.taming;
import org.bukkit.event.entity.EntityDamageEvent;
public class SharpenedClawsEventHandler {
private EntityDamageEvent event;
public SharpenedClawsEventHandler(EntityDamageEvent event) {
this.event = event;
}
protected void modifyEventDamage() {
event.setDamage(event.getDamage() + Taming.sharpenedClawsBonusDamage);
}
}

View File

@ -1,17 +1,16 @@
package com.gmail.nossr50.skills.taming; package com.gmail.nossr50.skills.taming;
import org.bukkit.EntityEffect; import org.bukkit.EntityEffect;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.AnimalTamer; import org.bukkit.entity.AnimalTamer;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.Tameable; import org.bukkit.entity.Tameable;
import org.bukkit.entity.Wolf; import org.bukkit.entity.Wolf;
import com.gmail.nossr50.config.AdvancedConfig; import com.gmail.nossr50.config.AdvancedConfig;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.skills.SkillUtils;
public class Taming { public class Taming {
public static int environmentallyAwareUnlockLevel = AdvancedConfig.getInstance().getEnviromentallyAwareUnlock(); public static int environmentallyAwareUnlockLevel = AdvancedConfig.getInstance().getEnviromentallyAwareUnlock();
@ -41,22 +40,6 @@ public class Taming {
return pet.isTamed() && owner instanceof Player && pet instanceof Wolf; return pet.isTamed() && owner instanceof Player && pet instanceof Wolf;
} }
public static boolean canUseThickFur(Player player) {
return SkillUtils.unlockLevelReached(player, SkillType.TAMING, thickFurUnlockLevel) && Permissions.thickFur(player);
}
public static boolean canUseEnvironmentallyAware(Player player) {
return SkillUtils.unlockLevelReached(player, SkillType.TAMING, environmentallyAwareUnlockLevel) && Permissions.environmentallyAware(player);
}
public static boolean canUseShockProof(Player player) {
return SkillUtils.unlockLevelReached(player, SkillType.TAMING, shockProofUnlockLevel) && Permissions.shockProof(player);
}
public static boolean canUseHolyHound(Player player) {
return SkillUtils.unlockLevelReached(player, SkillType.TAMING, holyHoundUnlockLevel) && Permissions.holyHound(player);
}
public static int processThickFur(Wolf wolf, int damage) { public static int processThickFur(Wolf wolf, int damage) {
wolf.playEffect(EntityEffect.WOLF_SHAKE); wolf.playEffect(EntityEffect.WOLF_SHAKE);
return damage / thickFurModifier; return damage / thickFurModifier;
@ -67,24 +50,55 @@ public class Taming {
wolf.setFireTicks(0); wolf.setFireTicks(0);
} }
public static void processEnvironmentallyAware(Player player, Wolf wolf, int damage) {
if (damage > wolf.getHealth()) {
return;
}
wolf.teleport(player);
player.sendMessage(LocaleLoader.getString("Taming.Listener.Wolf"));
}
public static int processShockProof(Wolf wolf, int damage) { public static int processShockProof(Wolf wolf, int damage) {
wolf.playEffect(EntityEffect.WOLF_SHAKE); wolf.playEffect(EntityEffect.WOLF_SHAKE);
return damage / shockProofModifier; return damage / shockProofModifier;
} }
/**
* Apply the Sharpened Claws ability.
*
* @param event The event to modify
*/
public static int sharpenedClaws(int damage) {
return damage + Taming.sharpenedClawsBonusDamage;
}
public static void processHolyHound(Wolf wolf, int damage) { public static void processHolyHound(Wolf wolf, int damage) {
int modifiedHealth = Math.min(wolf.getHealth() + damage, wolf.getMaxHealth()); int modifiedHealth = Math.min(wolf.getHealth() + damage, wolf.getMaxHealth());
wolf.setHealth(modifiedHealth); wolf.setHealth(modifiedHealth);
wolf.playEffect(EntityEffect.WOLF_HEARTS); wolf.playEffect(EntityEffect.WOLF_HEARTS);
} }
/**
* Get the name of a tameable animal's owner.
*
* @return the name of the animal's owner
*/
protected static String getOwnerName(Tameable beast) {
AnimalTamer tamer = beast.getOwner();
if (tamer instanceof Player) {
return ((Player) tamer).getName();
}
else if (tamer instanceof OfflinePlayer) {
return ((OfflinePlayer) tamer).getName();
}
return "Unknown Master";
}
protected static String getCallOfTheWildFailureMessage(EntityType type) {
switch (type) {
case OCELOT:
return LocaleLoader.getString("Taming.Summon.Fail.Ocelot");
case WOLF:
return LocaleLoader.getString("Taming.Summon.Fail.Wolf");
default:
return "";
}
}
} }

View File

@ -1,17 +1,24 @@
package com.gmail.nossr50.skills.taming; package com.gmail.nossr50.skills.taming;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Ocelot;
import org.bukkit.entity.Player;
import org.bukkit.entity.Tameable;
import org.bukkit.entity.Wolf; import org.bukkit.entity.Wolf;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.runnables.skills.BleedTimerTask;
import com.gmail.nossr50.skills.SkillManager; import com.gmail.nossr50.skills.SkillManager;
import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.StringUtils;
import com.gmail.nossr50.util.skills.SkillUtils; import com.gmail.nossr50.util.skills.SkillUtils;
public class TamingManager extends SkillManager { public class TamingManager extends SkillManager {
@ -19,6 +26,22 @@ public class TamingManager extends SkillManager {
super(mcMMOPlayer, SkillType.TAMING); super(mcMMOPlayer, SkillType.TAMING);
} }
public boolean canUseThickFur() {
return getSkillLevel() > Taming.thickFurUnlockLevel && Permissions.thickFur(getPlayer());
}
public boolean canUseEnvironmentallyAware() {
return getSkillLevel() > Taming.environmentallyAwareUnlockLevel && Permissions.environmentallyAware(getPlayer());
}
public boolean canUseShockProof() {
return getSkillLevel() > Taming.shockProofUnlockLevel && Permissions.shockProof(getPlayer());
}
public boolean canUseHolyHound() {
return getSkillLevel() > Taming.holyHoundUnlockLevel && Permissions.holyHound(getPlayer());
}
/** /**
* Award XP for taming. * Award XP for taming.
* *
@ -58,34 +81,26 @@ public class TamingManager extends SkillManager {
} }
} }
/**
* Apply the Sharpened Claws ability.
*
* @param event The event to modify
*/
public void sharpenedClaws(EntityDamageEvent event) {
SharpenedClawsEventHandler eventHandler = new SharpenedClawsEventHandler(event);
eventHandler.modifyEventDamage();
}
/** /**
* Apply the Gore ability. * Apply the Gore ability.
* *
* @param event The event to modify * @param event The event to modify
*/ */
public void gore(EntityDamageEvent event) { public int gore(LivingEntity target, int damage) {
GoreEventHandler eventHandler = new GoreEventHandler(this, event); Player owner = getPlayer();
float chance = (float) ((Taming.goreMaxChance / Taming.goreMaxBonusLevel) * getSkillLevel()); if (SkillUtils.activationSuccessful(owner, skill, Taming.goreMaxChance, Taming.goreMaxBonusLevel)) {
if (chance > Taming.goreMaxChance) { BleedTimerTask.add(target, Taming.goreBleedTicks);
chance = (float) Taming.goreMaxChance;
if (target instanceof Player) {
((Player) target).sendMessage(LocaleLoader.getString("Combat.StruckByGore"));
} }
if (chance > Misc.getRandom().nextInt(activationChance)) { owner.sendMessage(LocaleLoader.getString("Combat.Gore"));
eventHandler.modifyEventDamage(); return damage * Taming.goreModifier;
eventHandler.applyBleed();
eventHandler.sendAbilityMessage();
} }
return damage;
} }
/** /**
@ -105,11 +120,31 @@ public class TamingManager extends SkillManager {
/** /**
* Handle the Beast Lore ability. * Handle the Beast Lore ability.
* *
* @param livingEntity The entity to examine * @param target The entity to examine
*/ */
public void beastLore(LivingEntity livingEntity) { public void beastLore(LivingEntity target) {
BeastLoreEventHandler eventHandler = new BeastLoreEventHandler(getPlayer(), livingEntity); Player player = getPlayer();
eventHandler.sendInspectMessage(); Tameable beast = (Tameable) target;
String message = LocaleLoader.getString("Combat.BeastLore") + " ";
if (beast.isTamed()) {
message = message.concat(LocaleLoader.getString("Combat.BeastLoreOwner", Taming.getOwnerName(beast)) + " ");
}
message = message.concat(LocaleLoader.getString("Combat.BeastLoreHealth", target.getHealth(), target.getMaxHealth()));
player.sendMessage(message);
}
public void processEnvironmentallyAware(Wolf wolf, int damage) {
if (damage > wolf.getHealth()) {
return;
}
Player owner = getPlayer();
wolf.teleport(owner);
owner.sendMessage(LocaleLoader.getString("Taming.Listener.Wolf"));
} }
/** /**
@ -119,27 +154,36 @@ public class TamingManager extends SkillManager {
* @param summonAmount The amount of material needed to summon the entity * @param summonAmount The amount of material needed to summon the entity
*/ */
private void callOfTheWild(EntityType type, int summonAmount) { private void callOfTheWild(EntityType type, int summonAmount) {
if (!Permissions.callOfTheWild(getPlayer())) { Player player = getPlayer();
ItemStack heldItem = player.getItemInHand();
int heldItemAmount = heldItem.getAmount();
if (heldItemAmount < summonAmount) {
player.sendMessage(LocaleLoader.getString("Skills.NeedMore", StringUtils.getPrettyItemString(heldItem.getTypeId())));
return; return;
} }
CallOfTheWildEventHandler eventHandler = new CallOfTheWildEventHandler(getPlayer(), type, summonAmount); for (Entity entity : player.getNearbyEntities(40, 40, 40)) {
if (entity.getType() == type) {
ItemStack inHand = eventHandler.inHand; player.sendMessage(Taming.getCallOfTheWildFailureMessage(type));
int inHandAmount = inHand.getAmount();
if (inHandAmount < summonAmount) {
eventHandler.sendInsufficientAmountMessage();
return; return;
} }
}
if (eventHandler.nearbyEntityExists()) { LivingEntity entity = (LivingEntity) player.getWorld().spawnEntity(player.getLocation(), type);
eventHandler.sendFailureMessage();
entity.setMetadata(mcMMO.entityMetadataKey, mcMMO.metadataValue);
((Tameable) entity).setOwner(player);
if (type == EntityType.OCELOT) {
((Ocelot) entity).setCatType(Ocelot.Type.getType(1 + Misc.getRandom().nextInt(3)));
} }
else { else {
eventHandler.spawnCreature(); entity.setHealth(entity.getMaxHealth());
eventHandler.processResourceCost(); }
eventHandler.sendSuccessMessage();
} player.setItemInHand(new ItemStack(heldItem.getType(), heldItemAmount - summonAmount));
player.sendMessage(LocaleLoader.getString("Taming.Summon.Complete"));
} }
} }

View File

@ -79,12 +79,7 @@ public final class CombatUtils {
} }
if (ItemUtils.isSword(heldItem)) { if (ItemUtils.isSword(heldItem)) {
if (targetIsPlayer || targetIsTamedPet) { if (((targetIsPlayer || targetIsTamedPet) && !SkillType.SWORDS.getPVPEnabled()) || (!targetIsPlayer && !targetIsTamedPet && !SkillType.SWORDS.getPVEEnabled())) {
if (!SkillType.SWORDS.getPVPEnabled()) {
return;
}
}
else if (!SkillType.SWORDS.getPVEEnabled()) {
return; return;
} }
@ -212,11 +207,11 @@ public final class CombatUtils {
} }
if (skillLevel >= Taming.sharpenedClawsUnlockLevel && Permissions.sharpenedClaws(master)) { if (skillLevel >= Taming.sharpenedClawsUnlockLevel && Permissions.sharpenedClaws(master)) {
tamingManager.sharpenedClaws(event); event.setDamage(Taming.sharpenedClaws(event.getDamage()));
} }
if (Permissions.gore(master)) { if (Permissions.gore(master)) {
tamingManager.gore(event); event.setDamage(tamingManager.gore(target, event.getDamage()));
} }
startGainXp(mcMMOPlayer, target, SkillType.TAMING); startGainXp(mcMMOPlayer, target, SkillType.TAMING);