Mineplex2018-withcommit/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/kit/perks/PerkBlockToss.java
Chiss aa45f031ac 3 new achievement Kits
SSM Balance patch
Champs balance
tons of bug fixes
2014-10-31 10:35:47 +11:00

217 lines
5.9 KiB
Java

package nautilus.game.arcade.kit.perks;
import java.util.HashMap;
import java.util.HashSet;
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.UtilEvent;
import mineplex.core.common.util.UtilGear;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilServer;
import mineplex.core.common.util.UtilEvent.ActionType;
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;
import nautilus.game.arcade.kit.perks.data.BlockTossData;
import nautilus.game.arcade.kit.perks.event.PerkBlockGrabEvent;
import nautilus.game.arcade.kit.perks.event.PerkBlockThrowEvent;
import org.bukkit.Effect;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.FallingBlock;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.player.PlayerInteractEvent;
public class PerkBlockToss extends Perk implements IThrown
{
private HashMap<Player, BlockTossData> _hold = new HashMap<Player, BlockTossData>();
private HashMap<Player, Long> _charge = new HashMap<Player, Long>();
private HashSet<Player> _charged = new HashSet<Player>();
private HashMap<FallingBlock, Player> _falling = new HashMap<FallingBlock, Player>();
public PerkBlockToss()
{
super("Block Toss", new String[]
{
C.cYellow + "Hold Block" + C.cGray + " to " + C.cGreen + "Grab Block",
C.cYellow + "Release Block" + C.cGray + " to " + C.cGreen + "Throw Block"
});
}
@EventHandler
public void Grab(PlayerInteractEvent event)
{
Player player = event.getPlayer();
if (!UtilEvent.isAction(event, ActionType.R_BLOCK))
return;
if (!Kit.HasKit(player))
return;
if (!UtilGear.isSword(player.getItemInHand()))
return;
if (_hold.containsKey(player))
return;
Block grab = event.getClickedBlock();
if (UtilBlock.usable(grab))
return;
if (!UtilBlock.airFoliage(grab.getRelative(BlockFace.UP)) || Manager.GetBlockRestore().Contains(grab.getRelative(BlockFace.UP)))
{
UtilPlayer.message(player, F.main("Game", "You can only pick up blocks with Air above them."));
return;
}
//Event
PerkBlockGrabEvent blockEvent = new PerkBlockGrabEvent(player, grab.getTypeId(), grab.getData());
UtilServer.getServer().getPluginManager().callEvent(blockEvent);
//Block to Data
int id = grab.getTypeId();
byte data = grab.getData();
//Remove Block
Manager.GetBlockRestore().Add(event.getClickedBlock(), 0, (byte)0, 10000);
_hold.put(player, new BlockTossData(id, data));
_charge.put(player, System.currentTimeMillis());
//Effect
player.getWorld().playEffect(event.getClickedBlock().getLocation(), Effect.STEP_SOUND, id);
}
@EventHandler
public void Throw(UpdateEvent event)
{
if (event.getType() != UpdateType.TICK)
return;
HashSet<Player> throwSet = new HashSet<Player>();
for (Player cur : _hold.keySet())
{
//Throw
if (!cur.isBlocking())
throwSet.add(cur);
//Charged Tick
if (!_charged.contains(cur))
if (System.currentTimeMillis() - _charge.get(cur) > 800)
{
_charged.add(cur);
cur.playEffect(cur.getLocation(), Effect.CLICK1, 0);
}
}
for (Player cur : throwSet)
{
BlockTossData data = _hold.remove(cur);
FallingBlock block = cur.getWorld().spawnFallingBlock(cur.getEyeLocation().add(cur.getLocation().getDirection()), data.Type, data.Data);
_falling.put(block, cur);
_charged.remove(cur);
long charge = System.currentTimeMillis() - _charge.remove(cur);
//Throw
double mult = 1.4;
if (charge < 1000)
mult = mult * (charge/1000);
//Action
UtilAction.velocity(block, cur.getLocation().getDirection(), mult, false, 0.2, 0, 1, true);
Manager.GetProjectile().AddThrow(block, cur, this, -1, true, true, true,
null, 0, 0, null, 0, UpdateType.FASTEST, 2d);
//Event
PerkBlockThrowEvent blockEvent = new PerkBlockThrowEvent(cur);
UtilServer.getServer().getPluginManager().callEvent(blockEvent);
}
}
@Override
public void Collide(LivingEntity target, Block block, ProjectileUser data)
{
if (target == null)
return;
//Damage Event
Manager.GetDamage().NewDamageEvent(target, data.GetThrower(), null,
DamageCause.PROJECTILE, 2 + (data.GetThrown().getVelocity().length() * 8), true, true, false,
UtilEnt.getName(data.GetThrower()), GetName());
//Block to Item
if (data.GetThrown() instanceof FallingBlock)
{
FallingBlock thrown = (FallingBlock) data.GetThrown();
FallingBlock newThrown = data.GetThrown().getWorld().spawnFallingBlock(data.GetThrown().getLocation(), thrown.getMaterial(), (byte)0);
//Remove Old
_falling.remove(thrown);
thrown.remove();
//Add New
if (data.GetThrower() instanceof Player)
_falling.put(newThrown, (Player)data.GetThrower());
}
}
@Override
public void Idle(ProjectileUser data)
{
}
@Override
public void Expire(ProjectileUser data)
{
}
@EventHandler
public void BlockForm(EntityChangeBlockEvent event)
{
if (!(event.getEntity() instanceof FallingBlock))
return;
FallingBlock falling = (FallingBlock)event.getEntity();
falling.getWorld().playEffect(event.getBlock().getLocation(), Effect.STEP_SOUND, falling.getBlockId());
_falling.remove(falling);
falling.remove();
event.setCancelled(true);
}
@EventHandler
public void Knockback(CustomDamageEvent event)
{
if (event.GetReason() == null || !event.GetReason().contains(GetName()))
return;
event.AddKnockback(GetName(), 2.5);
}
}