From 5b1fb352bb82e66a63a0f3200e73cbe6b13bfda6 Mon Sep 17 00:00:00 2001 From: Paldiu Date: Sun, 17 Apr 2022 14:00:22 -0500 Subject: [PATCH] Beta 20220417-SNAPSHOT Changelog: - Reorganized the listener registration method to register the listeners in alphabetical order - Added the IllOmen effect, which will apply a -25% debuff to a user's luck stat when they have the Bad Omen status effect. - Added the ability for Guardian lasers to inflict negative damage to a users luck stat. - Added a cache to the Luck class to store user values in the event that they have the Ill Omen debuff applied. --- .../io/github/simplex/luck/FeelingLucky.java | 13 +-- .../github/simplex/luck/listener/IllOmen.java | 82 +++++++++++++++++++ .../simplex/luck/listener/PlayerListener.java | 7 +- .../io/github/simplex/luck/player/Luck.java | 30 ++++++- 4 files changed, 121 insertions(+), 11 deletions(-) create mode 100644 src/main/java/io/github/simplex/luck/listener/IllOmen.java diff --git a/src/main/java/io/github/simplex/luck/FeelingLucky.java b/src/main/java/io/github/simplex/luck/FeelingLucky.java index c366a5f..257bc1a 100644 --- a/src/main/java/io/github/simplex/luck/FeelingLucky.java +++ b/src/main/java/io/github/simplex/luck/FeelingLucky.java @@ -58,15 +58,16 @@ public final class FeelingLucky extends JavaPlugin { } private void registerListeners() { - new PlayerListener(this); new BlockDrops(this); - new ItemDrops(this); - new TakeDamage(this); - new RestoreHunger(this); + new BonemealFullCrop(this); + new CheatDeath(this); new EnchantmentBoost(this); new ExpBoost(this); - new CheatDeath(this); - new BonemealFullCrop(this); + new IllOmen(this); + new ItemDrops(this); + new PlayerListener(this); + new RestoreHunger(this); + new TakeDamage(this); new UnbreakableTool(this); } diff --git a/src/main/java/io/github/simplex/luck/listener/IllOmen.java b/src/main/java/io/github/simplex/luck/listener/IllOmen.java new file mode 100644 index 0000000..e167afd --- /dev/null +++ b/src/main/java/io/github/simplex/luck/listener/IllOmen.java @@ -0,0 +1,82 @@ +package io.github.simplex.luck.listener; + +import io.github.simplex.lib.MiniComponent; +import io.github.simplex.luck.FeelingLucky; +import io.github.simplex.luck.player.Luck; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityPotionEffectEvent; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; + +public record IllOmen(FeelingLucky plugin) implements Listener { + public IllOmen { + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @EventHandler + public void reconnectCheck(PlayerJoinEvent event) { + Player player = event.getPlayer(); + PotionEffectType type = PotionEffectType.BAD_OMEN; + Luck luck = plugin.getHandler().getLuckContainer(player); + + if (player.hasPotionEffect(type)) { + luck.cache(); + double maths = luck.getValue() - (luck.getValue() * 0.25); + luck.setValue(maths); + player.sendMessage(MiniComponent.info("A -25% debuff has been applied to your luck from the Bad Omen status effect.")); + } else if (luck.cached(player) && !player.hasPotionEffect(type)) { + luck.restore(); + player.sendMessage("The -25% debuff to your luck has been removed."); + } + } + + @EventHandler + public void effectApplyCheck(EntityPotionEffectEvent event) { + EntityPotionEffectEvent.Cause cause = EntityPotionEffectEvent.Cause.PATROL_CAPTAIN; + EntityPotionEffectEvent.Action added = EntityPotionEffectEvent.Action.ADDED; + EntityPotionEffectEvent.Action changed = EntityPotionEffectEvent.Action.CHANGED; + + if (event.getCause().equals(cause) && (event.getAction().equals(added) || event.getAction().equals(changed))) { + if (event.getEntity() instanceof Player player) { + Luck luck = plugin.getHandler().getLuckContainer(player); + luck.cache(); + double maths = luck.getValue() - (luck.getValue() * 0.25); + luck.setValue(maths); + player.sendMessage(MiniComponent.warn("A -25% debuff has been applied to your luck from the Bad Omen status effect.")); + } + } + } + + @EventHandler + public void effectRemoveCheck(EntityPotionEffectEvent event) { + PotionEffect old = event.getOldEffect(); + EntityPotionEffectEvent.Action cleared = EntityPotionEffectEvent.Action.CLEARED; + EntityPotionEffectEvent.Action removed = EntityPotionEffectEvent.Action.REMOVED; + + if (old == null) return; + + if (old.getType().equals(PotionEffectType.BAD_OMEN) && (event.getAction().equals(cleared) || event.getAction().equals(removed))) { + if ((event.getEntity() instanceof Player player)) { + Luck luck = plugin.getHandler().getLuckContainer(player); + if (luck.cached(player)) { + luck.restore(); + player.sendMessage("The -25% debuff to your luck has been removed."); + } + } + } + } + + @EventHandler + public void disconnectCheck(PlayerQuitEvent event) { + if (event.getPlayer().hasPotionEffect(PotionEffectType.BAD_OMEN)) { + Luck luck = plugin.getHandler().getLuckContainer(event.getPlayer()); + if (luck.cached(event.getPlayer())) { + luck.restore(); + } + } + } +} diff --git a/src/main/java/io/github/simplex/luck/listener/PlayerListener.java b/src/main/java/io/github/simplex/luck/listener/PlayerListener.java index 22ae990..6ff8fbe 100644 --- a/src/main/java/io/github/simplex/luck/listener/PlayerListener.java +++ b/src/main/java/io/github/simplex/luck/listener/PlayerListener.java @@ -1,5 +1,6 @@ package io.github.simplex.luck.listener; +import io.github.simplex.lib.MiniComponent; import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.player.Luck; import io.github.simplex.luck.util.SpecialFootItem; @@ -7,6 +8,7 @@ import net.kyori.adventure.text.Component; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Entity; +import org.bukkit.entity.Guardian; import org.bukkit.entity.Player; import org.bukkit.entity.Witch; import org.bukkit.event.EventHandler; @@ -43,7 +45,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { } @EventHandler - public void witchesBrew(EntityDamageByEntityEvent event) { + public void luckDecrease(EntityDamageByEntityEvent event) { Entity eTEMP = event.getDamager(); Entity pTEMP = event.getEntity(); EntityDamageEvent.DamageCause cause = event.getCause(); @@ -52,7 +54,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { return; } - if (!(eTEMP instanceof Witch)) { + if (!(eTEMP instanceof Witch) || !(eTEMP instanceof Guardian)) { return; } @@ -61,6 +63,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { if (luck.quickRNG(33.0)) { luck.takeFrom(5.0); plugin.getHandler().updatePlayer(player, luck); + player.sendMessage(MiniComponent.warn("Your luck has been decreased by 5 points!")); } } } diff --git a/src/main/java/io/github/simplex/luck/player/Luck.java b/src/main/java/io/github/simplex/luck/player/Luck.java index d6ea785..f9ae840 100644 --- a/src/main/java/io/github/simplex/luck/player/Luck.java +++ b/src/main/java/io/github/simplex/luck/player/Luck.java @@ -8,9 +8,7 @@ import org.bukkit.entity.Player; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; -import java.util.ArrayList; -import java.util.List; -import java.util.SplittableRandom; +import java.util.*; @SuppressWarnings("all") public class Luck implements LuckContainer { @@ -18,8 +16,10 @@ public class Luck implements LuckContainer { private final PlayerLuckChangeEvent event; private final FeelingLucky plugin; private final List markedPlayers = new ArrayList<>(); + private final Map cache = new HashMap<>(); private double BASE_VALUE; private double multiplier; + private double tempSave; public Luck(FeelingLucky plugin, Player player) { this(plugin, player, 1.0); @@ -30,6 +30,7 @@ public class Luck implements LuckContainer { this.multiplier = multiplier; this.plugin = plugin; BASE_VALUE = plugin.getConfigMap().get(player.getUniqueId()).getConfig().getDouble("luck"); + tempSave = BASE_VALUE; event = new PlayerLuckChangeEvent(this); } @@ -137,6 +138,29 @@ public class Luck implements LuckContainer { plugin.getConfigMap().get(associatedPlayer().getUniqueId()).setMultiplier(multiplier); } + public void cache() { + if (cache.containsKey(player)) { + cache.replace(player, getValue()); + return; + } + + cache.put(player, getValue()); + } + + public boolean cached(Player player) { + return cache.containsKey(player); + } + + public void restore() { + if (!cache.containsKey(player)) { + plugin.getLogger().info("Nothing to restore!"); + return; + } + + setValue(cache.get(player)); + cache.remove(player); + } + public double getDefaultValue() { return player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue(); }