Mineplex2018-withcommit/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/kit/perks/PerkHammerThrow.java

171 lines
4.6 KiB
Java
Raw Normal View History

package nautilus.game.arcade.kit.perks;
import java.util.HashMap;
import java.util.Iterator;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilAction;
import mineplex.core.common.util.UtilBlock;
import mineplex.core.common.util.UtilEnt;
import mineplex.core.common.util.UtilMath;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.itemstack.ItemStackFactory;
import mineplex.core.projectile.IThrown;
import mineplex.core.projectile.ProjectileUser;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.minecraft.game.core.damage.CustomDamageEvent;
import nautilus.game.arcade.kit.Perk;
public class PerkHammerThrow extends Perk implements IThrown
{
private HashMap<Item, Player> _thrown = new HashMap<Item, Player>();
public PerkHammerThrow()
{
super("Hammer Throw", new String[]
{
C.cYellow + "Right-Click" + C.cGray + " with Diamond Axe to " + C.cGreen + "Hammer Throw"
});
}
@EventHandler
public void Skill(PlayerInteractEvent event)
{
if (event.isCancelled())
return;
if (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK)
return;
if (UtilBlock.usable(event.getClickedBlock()))
return;
if (event.getPlayer().getItemInHand() == null)
return;
if (!event.getPlayer().getItemInHand().getType().toString().contains("DIAMOND_AXE"))
return;
Player player = event.getPlayer();
if (!Kit.HasKit(player))
return;
player.setItemInHand(null);
//Inform
UtilPlayer.message(player, F.main("Game", "You used " + F.skill(GetName()) + "."));
//Throw
Item item = player.getWorld().dropItem(player.getEyeLocation(), new ItemStack(Material.DIAMOND_AXE));
UtilAction.velocity(item, player.getLocation().getDirection(), 1.2, false, 0, 0.2, 10, true);
//Projectile
Manager.GetProjectile().AddThrow(item, player, this, -1, true, true, true, false, 2.5d);
//Store
_thrown.put(item, player);
}
@EventHandler
public void Pickup(PlayerPickupItemEvent event)
{
if (!_thrown.containsKey(event.getItem()))
return;
event.setCancelled(true);
event.getItem().remove();
Player player = _thrown.remove(event.getItem());
player.getInventory().addItem(ItemStackFactory.Instance.CreateStack(Material.DIAMOND_AXE, (byte)0, 1, "Thor Hammer"));
}
@EventHandler
public void Timeout(UpdateEvent event)
{
if (event.getType() != UpdateType.FAST)
return;
Iterator<Item> itemIterator = _thrown.keySet().iterator();
while (itemIterator.hasNext())
{
Item item = itemIterator.next();
if (item.getTicksLived() > 200)
{
_thrown.get(item).getInventory().addItem(ItemStackFactory.Instance.CreateStack(Material.DIAMOND_AXE, (byte)0, 1, "Thor Hammer"));
item.remove();
itemIterator.remove();
}
}
}
@Override
public void Collide(LivingEntity target, Block block, ProjectileUser data)
{
Rebound(data.GetThrower(), data.GetThrown());
if (target == null)
return;
//Damage Event
Manager.GetDamage().NewDamageEvent(target, data.GetThrower(), null,
DamageCause.LIGHTNING, 12, true, true, false,
UtilEnt.getName(data.GetThrower()), GetName());
}
@Override
public void Idle(ProjectileUser data)
{
Rebound(data.GetThrower(), data.GetThrown());
}
@Override
public void Expire(ProjectileUser data)
{
Rebound(data.GetThrower(), data.GetThrown());
}
public void Rebound(LivingEntity player, Entity ent)
{
ent.getWorld().playSound(ent.getLocation(), Sound.ZOMBIE_METAL, 0.6f, 0.5f);
double mult = 0.5 + (0.6 * (UtilMath.offset(player.getLocation(), ent.getLocation())/16d));
//Velocity
ent.setVelocity(player.getLocation().toVector().subtract(ent.getLocation().toVector()).normalize().add(new Vector(0, 0.4, 0)).multiply(mult));
//Ticks
if (ent instanceof Item)
((Item)ent).setPickupDelay(5);
}
@EventHandler
public void Knockback(CustomDamageEvent event)
{
if (event.GetReason() == null || !event.GetReason().contains(GetName()))
return;
event.AddKnockback(GetName(), 2);
}
}