From cbc57d5971e248b98e1a691dff157185feda3220 Mon Sep 17 00:00:00 2001 From: Paldiu Date: Sat, 2 Apr 2022 21:16:08 -0500 Subject: [PATCH] Alpha 1.0.0 RC04 Switched PlayerHandler access modifiers in favor of Dependency Injection. --- .../executionHistory/executionHistory.bin | Bin 78800 -> 78800 bytes .../executionHistory/executionHistory.lock | Bin 17 -> 17 bytes .gradle/7.3.3/fileHashes/fileHashes.bin | Bin 22597 -> 22597 bytes .gradle/7.3.3/fileHashes/fileHashes.lock | Bin 17 -> 17 bytes .../7.3.3/fileHashes/resourceHashesCache.bin | Bin 19313 -> 19381 bytes .../buildOutputCleanup.lock | Bin 17 -> 17 bytes .gradle/file-system.probe | Bin 8 -> 8 bytes .../compileJava/previous-compilation-data.bin | Bin 31255 -> 31255 bytes .../io/github/simplex/luck/FeelingLucky.java | 2 +- .../java/io/github/simplex/luck/LuckCMD.java | 20 +++++++----- .../simplex/luck/listener/PlayerListener.java | 29 +++++++++-------- .../io/github/simplex/luck/player/Luck.java | 30 +++++++++++++++--- .../simplex/luck/player/PlayerConfig.java | 14 ++++---- .../simplex/luck/player/PlayerHandler.java | 29 +++++------------ .../luck/player/PlayerLuckChangeEvent.java | 16 ++++------ 15 files changed, 77 insertions(+), 63 deletions(-) diff --git a/.gradle/7.3.3/executionHistory/executionHistory.bin b/.gradle/7.3.3/executionHistory/executionHistory.bin index 3e038622e861f0b091d5a5caa40e0cf6a0aa900d..5e007ee2ad95d6a6f37affecc50a897294cf0ca7 100644 GIT binary patch delta 744 zcmcccoaMrEmJN13N~x7Ox%oy_>E=nPd1Z;Y1=%SDx!IL@Y38L25O8{Pf{!P^K)bTU zMK!5gQx6oM^ZxkxjPc~ceyPdxC-F^YiHMtQA0r@8=hDMsrFuhDa<})kTVFzsLKU!2 zZip!s@Nz19raJpm(UBSUS~`3O-b_9?S#7dr>}P?^^QKSYGxJvRUKyGy^)vs<{={xio1z3I` z>bbl9j2&4&bz^8T$o;9z$h+(9@e*jR{zugtulSS0izmA!x5lEP8u+3bKM4OYExsJ zGua?Pdb*?`qmcAtu-ayBW(EQEt)@+nu1alw^VV_;U&5)wlMnteV0rrw=+P=eMy>RZ zK-NF~%R)ev@mGCpmp@ve*f+IRyYNGo4^XIMdgux>Flf4BL5?z;R`g~>GgzL_?uf2Z3837bRPY&C;jTo0P%6m7jQMX*v5I*Tf Uctlyh&*zCJL4KMJNeU*@0A|)tHvj+t delta 551 zcmcccoaMrEmJN13lcTE?m>BIgC-`{sPcEHgF_|SIZnAxhz+{oh7L(g!awcz_Y&Kal z_OpQe#&^y4KP{~_;Z=U@FYIx8a^gpk%?@$9SOia==6xnpC$YClMC8ds%NfoGCkI9v zZ?;QrVw6|4Rh)JAm3LET#kwm;RThhP?74OMpZ=LIj4X@{3=*riH;OS%;@ezUe7PIs z4i-TxhRu@?o|T<^`oG%d)&FN`PX6%4bb5s*W2(?vsd+v>BKPdS80B~5h2%98#ubwv z9+TV7tHsF7EO6%NXOV^X{3h}rU2^&C&Nlh&3c8Fsj4b_YZ(W`qpvP#)G7HF>^vihq z4n0NzmU}?X_A`2n+>9*CHv?7N)@Rh3-0)j~W$wmXmnTp8t5_(wLejUv zYMZ&483b(lL%uWII}rR``$n;N$j#cx2mctbyahR=%8*ej{WDnQWg(!-gBIP7+gmLb zs~r91z2lPgQ=kyn)%R!bf3Q=QI4pKT=J?J(K%qG6#jBzf&%DlGJ#W3dMKU8$D9r8# zTS9=Z)PiH?K8~%M&jE$dBWXLg5#usOdAXOSE6oKJPR#ifxGVcd#fb;oL2jQ8i6E0{ E09K3pGynhq diff --git a/.gradle/7.3.3/executionHistory/executionHistory.lock b/.gradle/7.3.3/executionHistory/executionHistory.lock index 8d6b8cb8390618d8faf976a79809b540f5ded192..e199d1bb03eaa609134db7b1ed700a09ac5d50fb 100644 GIT binary patch literal 17 UcmZRcB;nt8QAKht0|b}?04QJsYXATM literal 17 UcmZRcB;nt8QAKht0|e*-04P`kV*mgE diff --git a/.gradle/7.3.3/fileHashes/fileHashes.bin b/.gradle/7.3.3/fileHashes/fileHashes.bin index bed1db525fa013d6d37ba07c8c6dd6d938fa2f2f..56f6852c0978eb3e253433e14883201215f98743 100644 GIT binary patch delta 390 zcmX@Qf$``D#tp(jLEiI%7ern>#473Aeq_Dps5r=lY>?6q|G4!nU%T067@( zyp_CHhNepW%)bKFaSNhDsCu$=m>SS!N(_@-!$6Kzj-Ff&VLYD9?GN&YeePsi2%~Uv zzQ2@&ck>!`%QX$*lWv4Zl=b_3hM1@_ak72*BNd6?_xAlfc3#e|jJxK;KBh|$NmUPs KbycAt2Lb@8fQFO+ delta 274 zcmX@Qf$``D#tp(jlXv?YDO`M%vGd2Ktsl8QXqhhDvMh>$fwBI%f$?kxFyNoHIW;JV zk>yp{wEoGvgY`g+S(D#F822{IhiHK0W-g!X9V#_hK0pM>nK5^AEszrk;VhcG9>{5i zaBfYW9Uv{g@m=%%PfKe}c$FXf3wxXfSqL=Z0>nb0$&;nS)PTlJpD@`q3}oL-(aGfy z#_h@6{$P=}lWif4&y(~0r6lBDnyxe#R5&r`Q{b-b9~CDcCeD5^**^S{imI*Rth=wg Zn>s7jT{)_2wDO+ literal 8 PcmZQzV61;tRy79z2_pjc diff --git a/build/tmp/compileJava/previous-compilation-data.bin b/build/tmp/compileJava/previous-compilation-data.bin index 1ab3c7d57aa6e38cb6b3ea31fecd64b37f59f8dd..dcb3c8dda8612d7b62c4d05a6ff37d335c94c6f3 100644 GIT binary patch delta 94 zcmV-k0HObv^Z}Rj0kDbz5VM|_k`6XYB1@@NWg7W!)RUS4AP`DIcIG9U@^ZwOKPoH^ z!0eOC0n#OhBN)*p8rqe>bIwcg^UO0(BsCHcengA}IwjZ@8M{li+VoV#v%vyZcd(Qx A)c^nh delta 94 zcmV-k0HObv^Z}Rj0kDbz5FfGbgWvL{eKZXt<4+Sv%afV`AQ0_2k { UUID uuid = UUID.fromString(file.getName().split("\\.")[0]); - configMap.put(uuid, PlayerConfig.loadFrom(file)); + configMap.put(uuid, PlayerConfig.loadFrom(this, file)); }); configMap.forEach((u, pc) -> pc.load()); getLogger().info("Successfully loaded all configurations!"); diff --git a/src/main/java/io/github/simplex/luck/LuckCMD.java b/src/main/java/io/github/simplex/luck/LuckCMD.java index 3d44d8f..7e75854 100644 --- a/src/main/java/io/github/simplex/luck/LuckCMD.java +++ b/src/main/java/io/github/simplex/luck/LuckCMD.java @@ -17,8 +17,11 @@ import java.util.ArrayList; import java.util.List; public class LuckCMD extends Command implements TabCompleter { + private final FeelingLucky plugin; + public LuckCMD(FeelingLucky plugin) { super("luck", "FeelingLucky main command.", "/ [player] [amount]", List.of()); + this.plugin = plugin; setPermission("luck.default"); plugin.getServer().getCommandMap().register("luck", "FeelingLucky", this); } @@ -37,27 +40,27 @@ public class LuckCMD extends Command implements TabCompleter { return true; } - Luck luck = PlayerHandler.getLuckContainer(player); + Luck luck = plugin.handler.getLuckContainer(player); PlayerConfig config = FeelingLucky.getConfigMap().get(player.getUniqueId()); switch (args[0]) { case "set" -> { luck.setValue(amount); - PlayerHandler.updatePlayer(player, luck); + plugin.handler.updatePlayer(player, luck); config.setLuck(luck.baseValue()); sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat.")); return true; } case "give" -> { luck.addTo(amount); - PlayerHandler.updatePlayer(player, luck); + plugin.handler.updatePlayer(player, luck); config.setLuck(luck.baseValue()); sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat.")); return true; } case "take" -> { luck.takeFrom(amount); - PlayerHandler.updatePlayer(player, luck); + plugin.handler.updatePlayer(player, luck); config.setLuck(luck.baseValue()); sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat.")); return true; @@ -65,6 +68,7 @@ public class LuckCMD extends Command implements TabCompleter { } } else { sender.sendMessage(Messages.NO_PERMISSION.get()); + return true; } } @@ -78,7 +82,7 @@ public class LuckCMD extends Command implements TabCompleter { return true; } - Luck luck = PlayerHandler.getLuckContainer(player); + Luck luck = plugin.handler.getLuckContainer(player); sender.sendMessage(Component.empty().content("Luck stat for " + args[1] + ": " + luck.baseValue())); return true; } @@ -91,10 +95,10 @@ public class LuckCMD extends Command implements TabCompleter { return true; } - Luck luck = PlayerHandler.getLuckContainer(player); + Luck luck = plugin.handler.getLuckContainer(player); PlayerConfig config = FeelingLucky.getConfigMap().get(player.getUniqueId()); luck.reset(); - PlayerHandler.updatePlayer(player, luck); + plugin.handler.updatePlayer(player, luck); config.setLuck(luck.baseValue()); sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat.")); return true; @@ -108,7 +112,7 @@ public class LuckCMD extends Command implements TabCompleter { if (args.length == 1) { if ((sender instanceof Player player) && player.hasPermission("luck.default")) { if (args[0].equalsIgnoreCase("info")) { - Luck luck = PlayerHandler.getLuckContainer(player); + Luck luck = plugin.handler.getLuckContainer(player); TextComponent c = Component.text("Your Luck: " + luck.getPercentage()); player.sendMessage(c.color(TextColor.color(0, 255, 0))); return true; 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 37434e9..49f9c25 100644 --- a/src/main/java/io/github/simplex/luck/listener/PlayerListener.java +++ b/src/main/java/io/github/simplex/luck/listener/PlayerListener.java @@ -5,7 +5,6 @@ import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.ListBox; import io.github.simplex.luck.SneakyWorker; import io.github.simplex.luck.player.Luck; -import io.github.simplex.luck.player.PlayerHandler; import net.kyori.adventure.text.Component; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -30,10 +29,12 @@ import java.util.List; import java.util.Map; import java.util.UUID; -public record PlayerListener(FeelingLucky plugin) implements Listener { - private static final Map entityPlayerMap = new HashMap<>(); +public class PlayerListener implements Listener { + private final Map entityPlayerMap = new HashMap<>(); + private final FeelingLucky plugin; - public PlayerListener { + public PlayerListener(FeelingLucky plugin) { + this.plugin = plugin; Bukkit.getServer().getPluginManager().registerEvents(this, plugin); } @@ -44,7 +45,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { return; } Player player = (Player) event.getEntity(); - Luck luck = PlayerHandler.getLuckContainer(player); + Luck luck = plugin.handler.getLuckContainer(player); if (ListBox.acceptedCauses.contains(event.getCause())) { if (luck.notDefault()) { double percentage = luck.getPercentage(); @@ -53,7 +54,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { * If a player's luck stat is a negative number, or they are "marked", * this will trigger instead of the regular luck spin. */ - if (percentage < 0 || PlayerHandler.isMarked(player)) { + if (percentage < 0 || luck.isMarked(player)) { percentage = Math.abs(percentage); if (luck.quickRNG(percentage)) { event.setCancelled(true); @@ -79,7 +80,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { * If a player's luck stat is a negative number, or they are "marked", * this will trigger instead of the regular luck spin. */ - if (percentage < 0 || PlayerHandler.isMarked(player)) { + if (percentage < 0 || luck.isMarked(player)) { percentage = Math.abs(percentage); if (luck.quickRNG(percentage)) { event.setCancelled(true); @@ -101,7 +102,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { @EventHandler public void extraBlockDrops(BlockDropItemEvent event) { Player player = event.getPlayer(); - Luck luck = PlayerHandler.getLuckContainer(player); + Luck luck = plugin.handler.getLuckContainer(player); List items = event.getItems(); if (luck.quickRNG(luck.getPercentage())) { items.forEach(SneakyWorker::move); @@ -141,7 +142,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { if (entityPlayerMap.get(entity.getUniqueId()) == null) return; Player player = entityPlayerMap.get(entity.getUniqueId()); - Luck luck = PlayerHandler.getLuckContainer(player); + Luck luck = plugin.handler.getLuckContainer(player); Item item = event.getItemDrop(); ItemStack stack = item.getItemStack(); int amount = stack.getAmount(); @@ -156,7 +157,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { @EventHandler public void restoreHunger(PlayerItemConsumeEvent event) { ItemStack item = event.getItem(); - Luck luck = PlayerHandler.getLuckContainer(event.getPlayer()); + Luck luck = plugin.handler.getLuckContainer(event.getPlayer()); PotionEffect effect = PotionEffectBuilder.newEffect().type(PotionEffectType.SATURATION).amplifier(2).duration(10).particles(false).create(); if (luck.notDefault()) { double percentage = luck.getPercentage(); @@ -176,12 +177,12 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { Action action = event.getAction(); ItemStack foot = new ItemStack(Material.RABBIT_FOOT); Player player = event.getPlayer(); - Luck luck = PlayerHandler.getLuckContainer(player); + Luck luck = plugin.handler.getLuckContainer(player); if (action.isRightClick() && player.getInventory().getItemInMainHand().isSimilar(foot)) { double rng = Luck.RNG().nextDouble(2.0, 5.0); player.getInventory().remove(player.getInventory().getItemInMainHand()); luck.addTo(rng); - + plugin.handler.updatePlayer(player, luck); player.sendMessage(Component.empty().content("Your luck has been increased by " + rng + " points.")); } } @@ -200,11 +201,11 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { return; } - Luck luck = PlayerHandler.getLuckContainer(player); + Luck luck = plugin.handler.getLuckContainer(player); if (cause.equals(EntityDamageEvent.DamageCause.MAGIC) || cause.equals(EntityDamageEvent.DamageCause.POISON)) { if (luck.quickRNG(33.0)) { luck.takeFrom(5.0); - PlayerHandler.updatePlayer(player, luck); + plugin.handler.updatePlayer(player, luck); } } } 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 466477e..d344a9b 100644 --- a/src/main/java/io/github/simplex/luck/player/Luck.java +++ b/src/main/java/io/github/simplex/luck/player/Luck.java @@ -8,6 +8,8 @@ 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; @SuppressWarnings("all") @@ -25,7 +27,21 @@ public class Luck implements LuckContainer { this.player = player; this.multiplier = multiplier; BASE_VALUE = player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue(); - event = new PlayerLuckChangeEvent(player); + event = new PlayerLuckChangeEvent(this); + } + + private final List markedPlayers = new ArrayList<>(); + + public void markPlayer(Player player) { + markedPlayers.add(player); + } + + public void unmarkPlayer(Player player) { + markedPlayers.remove(player); + } + + public boolean isMarked(Player player) { + return markedPlayers.contains(player); } @Contract(pure = true, @@ -114,13 +130,19 @@ public class Luck implements LuckContainer { } public void addTo(double value) { + if (value >= 1024.0 || (baseValue() + value) >= 1024.0) { + setValue(1024.0); + return; + } setValue(baseValue() + value); - Bukkit.getPluginManager().callEvent(event); } public void takeFrom(double value) { - setValue(baseValue() - value); - Bukkit.getPluginManager().callEvent(event); + if (value <= -1024.0 || (baseValue() - value) <= -1024.0) { + setValue(-1024.0); + return; + } + setValue(baseValue() + value); } public double getPercentage() { diff --git a/src/main/java/io/github/simplex/luck/player/PlayerConfig.java b/src/main/java/io/github/simplex/luck/player/PlayerConfig.java index 65ecf35..dece37a 100644 --- a/src/main/java/io/github/simplex/luck/player/PlayerConfig.java +++ b/src/main/java/io/github/simplex/luck/player/PlayerConfig.java @@ -14,10 +14,12 @@ import java.nio.charset.StandardCharsets; public class PlayerConfig { private final File configFile; + private final FeelingLucky plugin; private volatile YamlConfiguration config; @SuppressWarnings("ResultOfMethodCallIgnored") public PlayerConfig(FeelingLucky plugin, Player player) { + this.plugin = plugin; File dataFolder = plugin.getDataFolder(); if (!dataFolder.exists()) dataFolder.mkdirs(); File file = new File(dataFolder, player.getUniqueId() + ".yml"); @@ -45,20 +47,21 @@ public class PlayerConfig { if (tempUsername != null && !tempUsername.equalsIgnoreCase(player.getName())) { config.set("username", player.getName()); - config.set("luck", PlayerHandler.getLuckContainer(player).defaultValue()); + config.set("luck", plugin.handler.getLuckContainer(player).defaultValue()); config.set("multiplier", "1.0"); save(); } } - protected PlayerConfig(File file) { + protected PlayerConfig(FeelingLucky plugin, File file) { + this.plugin = plugin; this.configFile = file; config = YamlConfiguration.loadConfiguration(configFile); } - @Contract("_ -> new") - public static PlayerConfig loadFrom(File file) { - return new PlayerConfig(file); + @Contract("_, _ -> new") + public static PlayerConfig loadFrom(FeelingLucky plugin, File file) { + return new PlayerConfig(plugin, file); } public void save() { @@ -66,7 +69,6 @@ public class PlayerConfig { } public void load() { - SneakyWorker.sneakyTry(() -> config = YamlConfiguration.loadConfiguration(configFile)); } diff --git a/src/main/java/io/github/simplex/luck/player/PlayerHandler.java b/src/main/java/io/github/simplex/luck/player/PlayerHandler.java index 4defb6e..a5566eb 100644 --- a/src/main/java/io/github/simplex/luck/player/PlayerHandler.java +++ b/src/main/java/io/github/simplex/luck/player/PlayerHandler.java @@ -7,36 +7,23 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerLoginEvent; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; -public record PlayerHandler(FeelingLucky plugin) implements Listener { - public PlayerHandler { +public class PlayerHandler implements Listener { + public final FeelingLucky plugin; + private final Map playerLuckMap = new HashMap<>(); + + public PlayerHandler(FeelingLucky plugin) { + this.plugin = plugin; Bukkit.getServer().getPluginManager().registerEvents(this, plugin); } - private static final Map playerLuckMap = new HashMap<>(); - private static final List markedPlayers = new ArrayList<>(); - - public static Luck getLuckContainer(Player player) { + public Luck getLuckContainer(Player player) { return playerLuckMap.get(player); } - public static void markPlayer(Player player) { - markedPlayers.add(player); - } - - public static void unmarkPlayer(Player player) { - markedPlayers.remove(player); - } - - public static boolean isMarked(Player player) { - return markedPlayers.contains(player); - } - - public static void updatePlayer(Player player, Luck luck) { + public void updatePlayer(Player player, Luck luck) { playerLuckMap.replace(player, luck); } diff --git a/src/main/java/io/github/simplex/luck/player/PlayerLuckChangeEvent.java b/src/main/java/io/github/simplex/luck/player/PlayerLuckChangeEvent.java index 2889962..1fe56ae 100644 --- a/src/main/java/io/github/simplex/luck/player/PlayerLuckChangeEvent.java +++ b/src/main/java/io/github/simplex/luck/player/PlayerLuckChangeEvent.java @@ -1,7 +1,5 @@ package io.github.simplex.luck.player; -import io.github.simplex.luck.FeelingLucky; -import org.bukkit.entity.Player; import org.bukkit.event.HandlerList; import org.bukkit.event.player.PlayerEvent; import org.jetbrains.annotations.NotNull; @@ -9,16 +7,16 @@ import org.jetbrains.annotations.NotNull; public class PlayerLuckChangeEvent extends PlayerEvent { public final HandlerList handlerList = new HandlerList(); - public PlayerLuckChangeEvent(@NotNull Player who) { - super(who); - Luck luck = PlayerHandler.getLuckContainer(who); - if (luck.lessThan(0.0) && !PlayerHandler.isMarked(who)) { - PlayerHandler.markPlayer(who); + public PlayerLuckChangeEvent(@NotNull Luck luck) { + super(luck.associatedPlayer()); + + if (luck.lessThan(0.0) && !luck.isMarked(luck.associatedPlayer())) { + luck.markPlayer(luck.associatedPlayer()); return; } - if (luck.greaterThan(0.0) && PlayerHandler.isMarked(who)) { - PlayerHandler.unmarkPlayer(who); + if (luck.greaterThan(0.0) && luck.isMarked(luck.associatedPlayer())) { + luck.unmarkPlayer(luck.associatedPlayer()); } }