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

209 lines
5.2 KiB
Java
Raw Normal View History

2013-08-27 17:14:08 +02:00
package nautilus.game.arcade.kit.perks;
import java.util.HashSet;
import java.util.Iterator;
2014-08-22 00:55:40 +02:00
import org.bukkit.Bukkit;
2013-08-27 17:14:08 +02:00
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
2014-08-22 00:55:40 +02:00
import org.bukkit.FireworkEffect.Type;
2013-08-27 17:14:08 +02:00
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
2014-08-22 00:55:40 +02:00
import org.bukkit.event.HandlerList;
2013-08-27 17:14:08 +02:00
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
2014-08-22 00:55:40 +02:00
import org.bukkit.event.player.PlayerEvent;
2013-08-27 17:14:08 +02:00
import org.bukkit.event.player.PlayerInteractEvent;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilAction;
import mineplex.core.common.util.UtilFirework;
2013-08-27 17:14:08 +02:00
import mineplex.core.common.util.UtilInv;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilServer;
import mineplex.core.itemstack.ItemStackFactory;
import mineplex.core.recharge.Recharge;
import mineplex.core.updater.UpdateType;
2014-08-22 00:55:40 +02:00
import mineplex.core.updater.event.UpdateEvent;
2013-08-27 17:14:08 +02:00
import nautilus.game.arcade.kit.Perk;
public class PerkSparkler extends Perk
{
2014-08-22 00:55:40 +02:00
public static class ThrowSparklerEvent extends PlayerEvent
{
private static final HandlerList handlers = new HandlerList();
public static HandlerList getHandlerList()
{
return handlers;
}
@Override
public HandlerList getHandlers()
{
return getHandlerList();
}
private final Item _sparkler;
public ThrowSparklerEvent(Player who, Item sparkler)
{
super(who);
_sparkler = sparkler;
}
public Item getSparkler()
{
return _sparkler;
}
}
2013-08-27 17:14:08 +02:00
private HashSet<Item> _items = new HashSet<Item>();
2014-08-22 00:55:40 +02:00
2013-08-27 17:14:08 +02:00
private int _spawnRate;
private int _max;
2014-08-22 00:55:40 +02:00
public PerkSparkler(int spawnRate, int max)
2013-08-27 17:14:08 +02:00
{
2014-08-22 00:55:40 +02:00
super("", new String[]
2013-08-27 17:14:08 +02:00
{
2014-08-22 00:55:40 +02:00
C.cGray + "Receive 1 Sparkler every " + spawnRate + " seconds. Maximum of " + max + ".",
C.cYellow + "Click" + C.cGray + " with Sparkler to " + C.cGreen + "Throw Sparkler"
2013-08-27 17:14:08 +02:00
});
2014-08-22 00:55:40 +02:00
2013-08-27 17:14:08 +02:00
_spawnRate = spawnRate;
_max = max;
}
2014-08-22 00:55:40 +02:00
public void Apply(Player player)
2013-08-27 17:14:08 +02:00
{
2014-08-22 00:55:40 +02:00
Recharge.Instance.use(player, GetName(), _spawnRate * 1000, false, false);
2013-08-27 17:14:08 +02:00
}
2014-08-22 00:55:40 +02:00
2013-08-27 17:14:08 +02:00
@EventHandler
public void SparklerSpawn(UpdateEvent event)
{
if (event.getType() != UpdateType.FAST)
return;
for (Player cur : UtilServer.getPlayers())
{
if (!Kit.HasKit(cur))
continue;
2014-08-22 00:55:40 +02:00
2013-08-27 17:14:08 +02:00
if (!Manager.GetGame().IsAlive(cur))
continue;
2014-08-22 00:55:40 +02:00
if (!Recharge.Instance.use(cur, GetName(), _spawnRate * 1000, false, false))
2013-08-27 17:14:08 +02:00
continue;
2014-08-22 00:55:40 +02:00
if (UtilInv.contains(cur, Material.EMERALD, (byte) 0, _max))
2013-08-27 17:14:08 +02:00
continue;
//Add
2014-08-22 00:55:40 +02:00
cur.getInventory().addItem(ItemStackFactory.Instance.CreateStack(Material.EMERALD, (byte) 0, 1, F.item("Throwing Sparkler")));
2013-08-27 17:14:08 +02:00
cur.playSound(cur.getLocation(), Sound.ITEM_PICKUP, 2f, 1f);
}
}
@EventHandler
public void SparklerDrop(PlayerDropItemEvent event)
{
2014-08-22 00:55:40 +02:00
if (!UtilInv.IsItem(event.getItemDrop().getItemStack(), Material.EMERALD, (byte) 0))
2013-08-27 17:14:08 +02:00
return;
//Cancel
event.setCancelled(true);
//Inform
UtilPlayer.message(event.getPlayer(), F.main(GetName(), "You cannot drop " + F.item("Throwing Sparkler") + "."));
}
@EventHandler
public void SparklerDeathRemove(PlayerDeathEvent event)
2014-08-22 00:55:40 +02:00
{
2013-08-27 17:14:08 +02:00
HashSet<org.bukkit.inventory.ItemStack> remove = new HashSet<org.bukkit.inventory.ItemStack>();
for (org.bukkit.inventory.ItemStack item : event.getDrops())
2014-08-22 00:55:40 +02:00
if (UtilInv.IsItem(item, Material.EMERALD, (byte) 0))
2013-08-27 17:14:08 +02:00
remove.add(item);
for (org.bukkit.inventory.ItemStack item : remove)
event.getDrops().remove(item);
}
@EventHandler
public void SparklerInvClick(InventoryClickEvent event)
{
2014-08-22 00:55:40 +02:00
UtilInv.DisallowMovementOf(event, "Throwing Sparkler", Material.EMERALD, (byte) 0, true);
2013-08-27 17:14:08 +02:00
}
@EventHandler
public void SparklerThrow(PlayerInteractEvent event)
{
if (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK &&
2014-08-22 00:55:40 +02:00
event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_AIR)
2013-08-27 17:14:08 +02:00
return;
2014-08-22 00:55:40 +02:00
2013-08-27 17:14:08 +02:00
Player player = event.getPlayer();
2014-08-22 00:55:40 +02:00
if (!UtilInv.IsItem(player.getItemInHand(), Material.EMERALD, (byte) 0))
2013-08-27 17:14:08 +02:00
return;
2014-08-22 00:55:40 +02:00
2013-08-27 17:14:08 +02:00
if (!Kit.HasKit(player))
return;
2014-08-22 00:55:40 +02:00
2013-08-27 17:14:08 +02:00
event.setCancelled(true);
2014-08-22 00:55:40 +02:00
UtilInv.remove(player, Material.EMERALD, (byte) 0, 1);
2013-08-27 17:14:08 +02:00
UtilInv.Update(player);
2014-08-22 00:55:40 +02:00
Item item = player.getWorld().dropItem(player.getEyeLocation().add(player.getLocation().getDirection()),
ItemStackFactory.Instance.CreateStack(Material.EMERALD, (byte) 0, 1, F.item("Throwing Sparkler")));
2013-08-27 17:14:08 +02:00
item.setPickupDelay(2000);
2014-08-22 00:55:40 +02:00
2013-08-27 17:14:08 +02:00
_items.add(item);
UtilAction.velocity(item, player.getLocation().getDirection(), 0.8, false, 0, 0.1, 10, false);
2014-08-22 00:55:40 +02:00
Bukkit.getPluginManager().callEvent(new ThrowSparklerEvent(player, item));
2013-08-27 17:14:08 +02:00
}
2014-08-22 00:55:40 +02:00
2013-08-27 17:14:08 +02:00
@EventHandler
public void Sparkle(UpdateEvent event)
{
if (event.getType() != UpdateType.FASTER)
return;
2014-08-22 00:55:40 +02:00
2013-08-27 17:14:08 +02:00
Iterator<Item> itemIterator = _items.iterator();
2014-08-22 00:55:40 +02:00
2013-08-27 17:14:08 +02:00
while (itemIterator.hasNext())
{
Item item = itemIterator.next();
2014-08-22 00:55:40 +02:00
2013-08-27 17:14:08 +02:00
if (!item.isValid() || item.getTicksLived() > 100)
{
item.remove();
itemIterator.remove();
continue;
}
2014-08-22 00:55:40 +02:00
UtilFirework.playFirework(item.getLocation(), Type.BURST, Color.GREEN, false, false);
2013-08-27 17:14:08 +02:00
}
}
2014-08-22 00:55:40 +02:00
2013-08-27 17:14:08 +02:00
public HashSet<Item> GetItems()
{
return _items;
}
}