Created
February 5, 2021 21:21
-
-
Save Cryptite/85803b9bcc3e301c9f8e17ee3225e973 to your computer and use it in GitHub Desktop.
Loka's Buff Handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BuffHandler implements Runnable, Listener { | |
private static final BuffHandler instance = new BuffHandler(); | |
private final TheArtifact plugin; | |
private final Map<BuffType, Buff> buffs = new EnumMap<>(BuffType.class); | |
private final Multimap<LivingEntity, Buff> activeBuffs = Multimaps.synchronizedMultimap(HashMultimap.create()); | |
private BuffHandler() { | |
this.plugin = TheArtifact.getPlugin(); | |
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this, 20, 20); | |
Bukkit.getPluginManager().registerEvents(this, plugin); | |
load(); | |
} | |
public static BuffHandler getInstance() { | |
return instance; | |
} | |
public void load() { | |
ConfigFile config = new ConfigFile(plugin, "/pve/buffs.yml"); | |
buffs.clear(); | |
for (String buff : config.getKeys("buffs")) { | |
try { | |
BuffType buffType = BuffType.valueOf(buff); | |
buffs.put(buffType, new Buff(plugin, buffType, config)); | |
} catch (Exception e) { | |
TheArtifact.log.info("[Buffs] Could not load " + buff); | |
} | |
} | |
TheArtifact.log.info("[Buffs] Loaded " + buffs.size() + " buffs"); | |
} | |
@Override | |
public void run() { | |
Set<LivingEntity> entities = activeBuffs.keySet(); | |
synchronized (activeBuffs) { | |
for (LivingEntity entity : entities) { | |
Iterator<Buff> iter = activeBuffs.get(entity).iterator(); | |
List<String> buffs = new ArrayList<>(); | |
while (iter.hasNext()) { | |
Buff buff = iter.next(); | |
if (buff.isPermanent()) continue; | |
float secondsLeft = buff.getSecondsLeft(); | |
if (secondsLeft <= 0) { | |
buff.remove(entity); | |
iter.remove(); | |
} else if (entity instanceof Player) { | |
buffs.add(buff.getShortName() + RESET + GRAY + " - " + GREEN + TimeUtil.getTimeFromSeconds(secondsLeft)); | |
} | |
} | |
if (!buffs.isEmpty() && entity instanceof Player p) { | |
p.sendActionBar(Strings.join(buffs, "" + RESET + GRAY + " | " + RESET)); | |
} | |
} | |
} | |
} | |
public Buff getBuff(BuffType type) { | |
return new Buff(buffs.get(type)); | |
} | |
void addBuff(LivingEntity e, Buff buff) { | |
synchronized (activeBuffs) { | |
activeBuffs.get(e).removeIf(buff1 -> buff1.getType() == buff.getType()); | |
activeBuffs.get(e).add(buff); | |
} | |
} | |
public void removeBuff(LivingEntity e, BuffType type) { | |
if (e == null) return; | |
Buff buff = buffs.get(type); | |
if (buff != null) { | |
buff.remove(e); | |
} | |
} | |
public boolean hasBuff(LivingEntity e, BuffType buffType) { | |
synchronized (activeBuffs) { | |
return activeBuffs.get(e).stream().anyMatch(activeBuff -> activeBuff.getType() == buffType); | |
} | |
} | |
@EventHandler(priority = EventPriority.MONITOR) | |
public void onEntityDeath(EntityDeathEvent e) { | |
buffs.values().forEach(buff -> buff.remove(e.getEntity())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment