Use metadata instead of storing the entity and location

This commit is contained in:
libraryaddict 2014-11-29 14:50:38 +13:00
parent b0e62bfedf
commit 6439fc93c2

View File

@ -1,10 +1,6 @@
package mineplex.minecraft.game.classcombat.Skill.Ranger;
import java.util.HashMap;
import java.util.Iterator;
import org.bukkit.*;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.*;
@ -13,92 +9,75 @@ import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import mineplex.minecraft.game.classcombat.Class.IPvpClass.ClassType;
import mineplex.minecraft.game.core.damage.CustomDamageEvent;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.core.updater.UpdateType;
import mineplex.core.common.util.UtilMath;
import mineplex.minecraft.game.classcombat.Skill.Skill;
import mineplex.minecraft.game.classcombat.Skill.SkillFactory;
import org.bukkit.event.player.*;
import org.bukkit.metadata.FixedMetadataValue;
public class Longshot extends Skill
{
private HashMap<Entity, Location> _arrows = new HashMap<Entity, Location>();
public Longshot(SkillFactory skills, String name, ClassType classType, SkillType skillType, int cost, int levels)
{
super(skills, name, classType, skillType, cost, levels);
public Longshot(SkillFactory skills, String name, ClassType classType, SkillType skillType, int cost, int levels)
{
super(skills, name, classType, skillType, cost, levels);
SetDesc(new String[]
{
"Arrows do an additional 1 damage",
"for every #4#-0.5 Blocks they travelled,",
"however, their base damage is",
"reduced by 3.",
"",
"Maximum of #5#5 additional damage."
});
}
SetDesc(new String[]
{
"Arrows do an additional 1 damage", "for every #4#-0.5 Blocks they travelled,",
"however, their base damage is", "reduced by 3.", "", "Maximum of #5#5 additional damage."
});
}
@EventHandler
public void ShootBow(EntityShootBowEvent event)
{
if (!(event.getEntity() instanceof Player))
return;
int level = getLevel((Player)event.getEntity());
if (level == 0) return;
//Save
_arrows.put(event.getProjectile(), event.getProjectile().getLocation());
}
@EventHandler
public void ShootBow(EntityShootBowEvent event)
{
if (!(event.getEntity() instanceof Player))
return;
@EventHandler(priority = EventPriority.HIGH)
public void Damage(CustomDamageEvent event)
{
if (event.IsCancelled())
return;
if (event.GetCause() != DamageCause.PROJECTILE)
return;
int level = getLevel((Player) event.getEntity());
if (level == 0)
return;
Projectile projectile = event.GetProjectile();
if (projectile == null) return;
// Save
event.getProjectile().setMetadata("ShotFrom",
new FixedMetadataValue(this.Factory.GetPlugin(), event.getProjectile().getLocation()));
}
if (!_arrows.containsKey(projectile))
return;
Player damager = event.GetDamagerPlayer(true);
if (damager == null) return;
int level = getLevel(damager);
Location loc = _arrows.remove(projectile);
double length = UtilMath.offset(loc, projectile.getLocation());
//Damage
double damage = Math.min(5 + 5 * level, (length / (4 - 0.5 * level)) - 3);
event.AddMod(damager.getName(), GetName(), damage, damage > 0);
}
@EventHandler
public void Clean(UpdateEvent event)
{
if (event.getType() != UpdateType.SEC)
return;
for (Iterator<Entity> arrowIterator = _arrows.keySet().iterator(); arrowIterator.hasNext();)
{
Entity arrow = arrowIterator.next();
if (arrow.isDead() || !arrow.isValid())
arrowIterator.remove();
}
}
@Override
public void Reset(Player player)
{
}
@EventHandler(priority = EventPriority.HIGH)
public void Damage(CustomDamageEvent event)
{
if (event.IsCancelled())
return;
if (event.GetCause() != DamageCause.PROJECTILE)
return;
Projectile projectile = event.GetProjectile();
if (projectile == null)
return;
if (!projectile.hasMetadata("ShotFrom"))
return;
Player damager = event.GetDamagerPlayer(true);
if (damager == null)
return;
int level = getLevel(damager);
Location loc = (Location) projectile.getMetadata("ShotFrom").get(0).value();
double length = UtilMath.offset(loc, projectile.getLocation());
// Damage
double damage = Math.min(5 + 5 * level, (length / (4 - 0.5 * level)) - 3);
event.AddMod(damager.getName(), GetName(), damage, damage > 0);
}
@Override
public void Reset(Player player)
{
}
}