parent
d617adb3a3
commit
f6cda190eb
7 changed files with 148 additions and 18 deletions
@ -0,0 +1,83 @@ |
||||
package com.benjocraeft.sharehealth; |
||||
|
||||
import org.bukkit.Bukkit; |
||||
import org.bukkit.entity.HumanEntity; |
||||
import org.bukkit.entity.Player; |
||||
import org.bukkit.potion.PotionEffect; |
||||
import org.bukkit.potion.PotionEffectType; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collection; |
||||
import java.util.List; |
||||
|
||||
public class Absorption { |
||||
|
||||
double amount; |
||||
int duration; |
||||
|
||||
int task; |
||||
|
||||
public Absorption(){ |
||||
|
||||
} |
||||
|
||||
void create(int duration, double newAmount){ |
||||
if (duration > 0 && amount > newAmount){ |
||||
return; |
||||
} |
||||
|
||||
Bukkit.getScheduler().cancelTask(task); |
||||
task = Bukkit.getScheduler().scheduleSyncRepeatingTask(Sharehealth.Instance, this::onSecond, 0, 20); |
||||
this.duration = duration; |
||||
setAmount(newAmount); |
||||
} |
||||
|
||||
private void onSecond(){ |
||||
if (Bukkit.getOnlinePlayers().size() == 0) |
||||
return; |
||||
|
||||
duration -= 20; |
||||
if (duration <= 0) |
||||
expire(); |
||||
} |
||||
|
||||
void onPlayerGotDamage(Player player, double absorptionDamage){ |
||||
if (!isActive()) |
||||
return; |
||||
|
||||
setAmount(player, amount - absorptionDamage); |
||||
} |
||||
|
||||
private void expire(){ |
||||
Bukkit.getScheduler().cancelTask(task); |
||||
duration = 0; |
||||
amount = 0; |
||||
} |
||||
|
||||
private void setAmount(Player player, double amount){ |
||||
if (amount <= 0){ |
||||
expire(); |
||||
amount = 0; |
||||
} |
||||
this.amount = amount; |
||||
List<Player> players = new ArrayList<>(Bukkit.getOnlinePlayers()); |
||||
players.remove(player); |
||||
players.forEach(this::setAbsorption); |
||||
} |
||||
|
||||
private void setAmount(double amount){ |
||||
setAmount(null, amount); |
||||
} |
||||
|
||||
void setAbsorption(Player player){ |
||||
if (!isActive()) |
||||
return; |
||||
|
||||
player.setAbsorptionAmount(amount); |
||||
} |
||||
|
||||
private boolean isActive(){ |
||||
return Bukkit.getScheduler().isQueued(task); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue