|
| 1 | +package com.nisovin.magicspells.spells.buff; |
| 2 | + |
| 3 | +import java.util.Set; |
| 4 | +import java.util.UUID; |
| 5 | +import java.util.HashSet; |
| 6 | + |
| 7 | +import org.bukkit.Bukkit; |
| 8 | +import org.bukkit.Location; |
| 9 | +import org.bukkit.util.Vector; |
| 10 | +import org.bukkit.entity.Entity; |
| 11 | +import org.bukkit.entity.Player; |
| 12 | +import org.bukkit.event.EventHandler; |
| 13 | +import org.bukkit.event.entity.EntityDamageEvent; |
| 14 | +import org.bukkit.event.entity.EntityToggleGlideEvent; |
| 15 | + |
| 16 | +import com.nisovin.magicspells.Subspell; |
| 17 | +import com.nisovin.magicspells.MagicSpells; |
| 18 | +import com.nisovin.magicspells.spells.BuffSpell; |
| 19 | +import com.nisovin.magicspells.util.MagicConfig; |
| 20 | +import com.nisovin.magicspells.spelleffects.EffectPosition; |
| 21 | + |
| 22 | +public class WindglideSpell extends BuffSpell { |
| 23 | + |
| 24 | + Set<UUID> gliders; |
| 25 | + |
| 26 | + Subspell glideSpell; |
| 27 | + Subspell collisionSpell; |
| 28 | + String glideSpellName; |
| 29 | + String collisionSpellName; |
| 30 | + |
| 31 | + boolean cancelOnCollision; |
| 32 | + boolean blockCollisionDmg; |
| 33 | + |
| 34 | + float velocity; |
| 35 | + float height; |
| 36 | + int interval; |
| 37 | + |
| 38 | + private GlideMonitor monitor; |
| 39 | + |
| 40 | + public WindglideSpell(MagicConfig config, String spellName) { |
| 41 | + super(config, spellName); |
| 42 | + |
| 43 | + glideSpellName = getConfigString("spell", ""); |
| 44 | + collisionSpellName = getConfigString("collision-spell", ""); |
| 45 | + |
| 46 | + blockCollisionDmg = getConfigBoolean("block-collision-dmg", true); |
| 47 | + cancelOnCollision = getConfigBoolean("cancel-on-collision", false); |
| 48 | + |
| 49 | + velocity = getConfigFloat("velocity", 20F); |
| 50 | + height = getConfigFloat("height", 0F); |
| 51 | + interval = getConfigInt("interval", 4); |
| 52 | + if (interval <= 0) interval = 4; |
| 53 | + |
| 54 | + gliders = new HashSet<>(); |
| 55 | + monitor = new GlideMonitor(); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void initialize() { |
| 60 | + super.initialize(); |
| 61 | + |
| 62 | + Subspell s = new Subspell(glideSpellName); |
| 63 | + |
| 64 | + if (s.process()) { |
| 65 | + glideSpell = s; |
| 66 | + } else { |
| 67 | + if (!glideSpellName.equals("")) MagicSpells.error("WindglideSpell " + internalName + " has an invalid spell defined"); |
| 68 | + } |
| 69 | + |
| 70 | + Subspell s2 = new Subspell(collisionSpellName); |
| 71 | + |
| 72 | + if (s2.process()) { |
| 73 | + collisionSpell = s2; |
| 74 | + } else { |
| 75 | + if (!collisionSpellName.equals("")) MagicSpells.error("WindglideSpell " + internalName + " has an invalid collision-spell defined"); |
| 76 | + } |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public boolean castBuff(Player player, float power, String[] args) { |
| 82 | + gliders.add(player.getUniqueId()); |
| 83 | + player.setGliding(true); |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public boolean isActive(Player player) { |
| 89 | + return gliders.contains(player.getUniqueId()); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void turnOffBuff(Player player) { |
| 94 | + gliders.remove(player.getUniqueId()); |
| 95 | + player.setGliding(false); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + protected void turnOff() { |
| 100 | + for (EffectPosition pos: EffectPosition.values()) { |
| 101 | + cancelEffectForAllPlayers(pos); |
| 102 | + } |
| 103 | + |
| 104 | + for (UUID id : gliders) { |
| 105 | + Player pl = Bukkit.getPlayer(id); |
| 106 | + if (pl != null && pl.isValid()) { |
| 107 | + pl.setGliding(false); |
| 108 | + turnOffBuff(pl); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + gliders.clear(); |
| 113 | + } |
| 114 | + |
| 115 | + @EventHandler |
| 116 | + public void onPlayerGlide(EntityToggleGlideEvent e) { |
| 117 | + Entity entity = e.getEntity(); |
| 118 | + if (!(entity instanceof Player)) return; |
| 119 | + Player pl = (Player)entity; |
| 120 | + if (!gliders.contains(pl.getUniqueId())) return; |
| 121 | + if (pl.isGliding()) { |
| 122 | + e.setCancelled(true); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @EventHandler |
| 127 | + public void onPlayerCollision(EntityDamageEvent e) { |
| 128 | + if (e.getCause() != EntityDamageEvent.DamageCause.FLY_INTO_WALL) return; |
| 129 | + if (!(e.getEntity() instanceof Player)) return; |
| 130 | + Player pl = (Player)e.getEntity(); |
| 131 | + if (!gliders.contains(pl.getUniqueId())) return; |
| 132 | + |
| 133 | + if (blockCollisionDmg) e.setCancelled(true); |
| 134 | + if (cancelOnCollision) turnOffBuff(pl); |
| 135 | + if (collisionSpell != null && collisionSpell.isTargetedLocationSpell()) collisionSpell.castAtLocation(pl, pl.getLocation(), 1); |
| 136 | + } |
| 137 | + |
| 138 | + public class GlideMonitor implements Runnable { |
| 139 | + |
| 140 | + int taskId; |
| 141 | + |
| 142 | + public GlideMonitor() { |
| 143 | + this.taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(MagicSpells.plugin,this, interval, interval); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public void run() { |
| 148 | + for (UUID id : gliders) { |
| 149 | + Player pl = Bukkit.getPlayer(id); |
| 150 | + if (pl == null || !pl.isValid()) continue; |
| 151 | + |
| 152 | + Location pLoc = pl.getLocation(); |
| 153 | + Vector v = pLoc.getDirection().normalize().multiply(velocity).add(new Vector(0,height,0)); |
| 154 | + pl.setVelocity(v); |
| 155 | + |
| 156 | + if (glideSpell != null && glideSpell.isTargetedLocationSpell()) glideSpell.castAtLocation(pl, pLoc, 1); |
| 157 | + playSpellEffects(EffectPosition.SPECIAL, pLoc); |
| 158 | + addUseAndChargeCost(pl); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | +} |
0 commit comments