Alpha 1.0.0 RC04

Switched PlayerHandler access modifiers in favor of Dependency Injection.
This commit is contained in:
Paldiu 2022-04-02 21:16:08 -05:00
parent 247a2fafc0
commit cbc57d5971
15 changed files with 77 additions and 63 deletions

Binary file not shown.

View File

@ -34,7 +34,7 @@ public final class FeelingLucky extends JavaPlugin {
if (files != null) { if (files != null) {
Arrays.stream(files).forEach(file -> { Arrays.stream(files).forEach(file -> {
UUID uuid = UUID.fromString(file.getName().split("\\.")[0]); 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()); configMap.forEach((u, pc) -> pc.load());
getLogger().info("Successfully loaded all configurations!"); getLogger().info("Successfully loaded all configurations!");

View File

@ -17,8 +17,11 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class LuckCMD extends Command implements TabCompleter { public class LuckCMD extends Command implements TabCompleter {
private final FeelingLucky plugin;
public LuckCMD(FeelingLucky plugin) { public LuckCMD(FeelingLucky plugin) {
super("luck", "FeelingLucky main command.", "/<command> <info | set | reset | give | take> [player] [amount]", List.of()); super("luck", "FeelingLucky main command.", "/<command> <info | set | reset | give | take> [player] [amount]", List.of());
this.plugin = plugin;
setPermission("luck.default"); setPermission("luck.default");
plugin.getServer().getCommandMap().register("luck", "FeelingLucky", this); plugin.getServer().getCommandMap().register("luck", "FeelingLucky", this);
} }
@ -37,27 +40,27 @@ public class LuckCMD extends Command implements TabCompleter {
return true; return true;
} }
Luck luck = PlayerHandler.getLuckContainer(player); Luck luck = plugin.handler.getLuckContainer(player);
PlayerConfig config = FeelingLucky.getConfigMap().get(player.getUniqueId()); PlayerConfig config = FeelingLucky.getConfigMap().get(player.getUniqueId());
switch (args[0]) { switch (args[0]) {
case "set" -> { case "set" -> {
luck.setValue(amount); luck.setValue(amount);
PlayerHandler.updatePlayer(player, luck); plugin.handler.updatePlayer(player, luck);
config.setLuck(luck.baseValue()); config.setLuck(luck.baseValue());
sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat.")); sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat."));
return true; return true;
} }
case "give" -> { case "give" -> {
luck.addTo(amount); luck.addTo(amount);
PlayerHandler.updatePlayer(player, luck); plugin.handler.updatePlayer(player, luck);
config.setLuck(luck.baseValue()); config.setLuck(luck.baseValue());
sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat.")); sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat."));
return true; return true;
} }
case "take" -> { case "take" -> {
luck.takeFrom(amount); luck.takeFrom(amount);
PlayerHandler.updatePlayer(player, luck); plugin.handler.updatePlayer(player, luck);
config.setLuck(luck.baseValue()); config.setLuck(luck.baseValue());
sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat.")); sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat."));
return true; return true;
@ -65,6 +68,7 @@ public class LuckCMD extends Command implements TabCompleter {
} }
} else { } else {
sender.sendMessage(Messages.NO_PERMISSION.get()); sender.sendMessage(Messages.NO_PERMISSION.get());
return true;
} }
} }
@ -78,7 +82,7 @@ public class LuckCMD extends Command implements TabCompleter {
return true; 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())); sender.sendMessage(Component.empty().content("Luck stat for " + args[1] + ": " + luck.baseValue()));
return true; return true;
} }
@ -91,10 +95,10 @@ public class LuckCMD extends Command implements TabCompleter {
return true; return true;
} }
Luck luck = PlayerHandler.getLuckContainer(player); Luck luck = plugin.handler.getLuckContainer(player);
PlayerConfig config = FeelingLucky.getConfigMap().get(player.getUniqueId()); PlayerConfig config = FeelingLucky.getConfigMap().get(player.getUniqueId());
luck.reset(); luck.reset();
PlayerHandler.updatePlayer(player, luck); plugin.handler.updatePlayer(player, luck);
config.setLuck(luck.baseValue()); config.setLuck(luck.baseValue());
sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat.")); sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat."));
return true; return true;
@ -108,7 +112,7 @@ public class LuckCMD extends Command implements TabCompleter {
if (args.length == 1) { if (args.length == 1) {
if ((sender instanceof Player player) && player.hasPermission("luck.default")) { if ((sender instanceof Player player) && player.hasPermission("luck.default")) {
if (args[0].equalsIgnoreCase("info")) { if (args[0].equalsIgnoreCase("info")) {
Luck luck = PlayerHandler.getLuckContainer(player); Luck luck = plugin.handler.getLuckContainer(player);
TextComponent c = Component.text("Your Luck: " + luck.getPercentage()); TextComponent c = Component.text("Your Luck: " + luck.getPercentage());
player.sendMessage(c.color(TextColor.color(0, 255, 0))); player.sendMessage(c.color(TextColor.color(0, 255, 0)));
return true; return true;

View File

@ -5,7 +5,6 @@ import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.ListBox; import io.github.simplex.luck.ListBox;
import io.github.simplex.luck.SneakyWorker; import io.github.simplex.luck.SneakyWorker;
import io.github.simplex.luck.player.Luck; import io.github.simplex.luck.player.Luck;
import io.github.simplex.luck.player.PlayerHandler;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
@ -30,10 +29,12 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
public record PlayerListener(FeelingLucky plugin) implements Listener { public class PlayerListener implements Listener {
private static final Map<UUID, Player> entityPlayerMap = new HashMap<>(); private final Map<UUID, Player> entityPlayerMap = new HashMap<>();
private final FeelingLucky plugin;
public PlayerListener { public PlayerListener(FeelingLucky plugin) {
this.plugin = plugin;
Bukkit.getServer().getPluginManager().registerEvents(this, plugin); Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
} }
@ -44,7 +45,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
return; return;
} }
Player player = (Player) event.getEntity(); Player player = (Player) event.getEntity();
Luck luck = PlayerHandler.getLuckContainer(player); Luck luck = plugin.handler.getLuckContainer(player);
if (ListBox.acceptedCauses.contains(event.getCause())) { if (ListBox.acceptedCauses.contains(event.getCause())) {
if (luck.notDefault()) { if (luck.notDefault()) {
double percentage = luck.getPercentage(); 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", * If a player's luck stat is a negative number, or they are "marked",
* this will trigger instead of the regular luck spin. * 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); percentage = Math.abs(percentage);
if (luck.quickRNG(percentage)) { if (luck.quickRNG(percentage)) {
event.setCancelled(true); 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", * If a player's luck stat is a negative number, or they are "marked",
* this will trigger instead of the regular luck spin. * 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); percentage = Math.abs(percentage);
if (luck.quickRNG(percentage)) { if (luck.quickRNG(percentage)) {
event.setCancelled(true); event.setCancelled(true);
@ -101,7 +102,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
@EventHandler @EventHandler
public void extraBlockDrops(BlockDropItemEvent event) { public void extraBlockDrops(BlockDropItemEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
Luck luck = PlayerHandler.getLuckContainer(player); Luck luck = plugin.handler.getLuckContainer(player);
List<Item> items = event.getItems(); List<Item> items = event.getItems();
if (luck.quickRNG(luck.getPercentage())) { if (luck.quickRNG(luck.getPercentage())) {
items.forEach(SneakyWorker::move); items.forEach(SneakyWorker::move);
@ -141,7 +142,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
if (entityPlayerMap.get(entity.getUniqueId()) == null) return; if (entityPlayerMap.get(entity.getUniqueId()) == null) return;
Player player = entityPlayerMap.get(entity.getUniqueId()); Player player = entityPlayerMap.get(entity.getUniqueId());
Luck luck = PlayerHandler.getLuckContainer(player); Luck luck = plugin.handler.getLuckContainer(player);
Item item = event.getItemDrop(); Item item = event.getItemDrop();
ItemStack stack = item.getItemStack(); ItemStack stack = item.getItemStack();
int amount = stack.getAmount(); int amount = stack.getAmount();
@ -156,7 +157,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
@EventHandler @EventHandler
public void restoreHunger(PlayerItemConsumeEvent event) { public void restoreHunger(PlayerItemConsumeEvent event) {
ItemStack item = event.getItem(); 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(); PotionEffect effect = PotionEffectBuilder.newEffect().type(PotionEffectType.SATURATION).amplifier(2).duration(10).particles(false).create();
if (luck.notDefault()) { if (luck.notDefault()) {
double percentage = luck.getPercentage(); double percentage = luck.getPercentage();
@ -176,12 +177,12 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
Action action = event.getAction(); Action action = event.getAction();
ItemStack foot = new ItemStack(Material.RABBIT_FOOT); ItemStack foot = new ItemStack(Material.RABBIT_FOOT);
Player player = event.getPlayer(); Player player = event.getPlayer();
Luck luck = PlayerHandler.getLuckContainer(player); Luck luck = plugin.handler.getLuckContainer(player);
if (action.isRightClick() && player.getInventory().getItemInMainHand().isSimilar(foot)) { if (action.isRightClick() && player.getInventory().getItemInMainHand().isSimilar(foot)) {
double rng = Luck.RNG().nextDouble(2.0, 5.0); double rng = Luck.RNG().nextDouble(2.0, 5.0);
player.getInventory().remove(player.getInventory().getItemInMainHand()); player.getInventory().remove(player.getInventory().getItemInMainHand());
luck.addTo(rng); luck.addTo(rng);
plugin.handler.updatePlayer(player, luck);
player.sendMessage(Component.empty().content("Your luck has been increased by " + rng + " points.")); 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; return;
} }
Luck luck = PlayerHandler.getLuckContainer(player); Luck luck = plugin.handler.getLuckContainer(player);
if (cause.equals(EntityDamageEvent.DamageCause.MAGIC) || cause.equals(EntityDamageEvent.DamageCause.POISON)) { if (cause.equals(EntityDamageEvent.DamageCause.MAGIC) || cause.equals(EntityDamageEvent.DamageCause.POISON)) {
if (luck.quickRNG(33.0)) { if (luck.quickRNG(33.0)) {
luck.takeFrom(5.0); luck.takeFrom(5.0);
PlayerHandler.updatePlayer(player, luck); plugin.handler.updatePlayer(player, luck);
} }
} }
} }

View File

@ -8,6 +8,8 @@ import org.bukkit.entity.Player;
import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.SplittableRandom; import java.util.SplittableRandom;
@SuppressWarnings("all") @SuppressWarnings("all")
@ -25,7 +27,21 @@ public class Luck implements LuckContainer {
this.player = player; this.player = player;
this.multiplier = multiplier; this.multiplier = multiplier;
BASE_VALUE = player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue(); BASE_VALUE = player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue();
event = new PlayerLuckChangeEvent(player); event = new PlayerLuckChangeEvent(this);
}
private final List<Player> 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, @Contract(pure = true,
@ -114,13 +130,19 @@ public class Luck implements LuckContainer {
} }
public void addTo(double value) { public void addTo(double value) {
if (value >= 1024.0 || (baseValue() + value) >= 1024.0) {
setValue(1024.0);
return;
}
setValue(baseValue() + value); setValue(baseValue() + value);
Bukkit.getPluginManager().callEvent(event);
} }
public void takeFrom(double value) { public void takeFrom(double value) {
setValue(baseValue() - value); if (value <= -1024.0 || (baseValue() - value) <= -1024.0) {
Bukkit.getPluginManager().callEvent(event); setValue(-1024.0);
return;
}
setValue(baseValue() + value);
} }
public double getPercentage() { public double getPercentage() {

View File

@ -14,10 +14,12 @@ import java.nio.charset.StandardCharsets;
public class PlayerConfig { public class PlayerConfig {
private final File configFile; private final File configFile;
private final FeelingLucky plugin;
private volatile YamlConfiguration config; private volatile YamlConfiguration config;
@SuppressWarnings("ResultOfMethodCallIgnored") @SuppressWarnings("ResultOfMethodCallIgnored")
public PlayerConfig(FeelingLucky plugin, Player player) { public PlayerConfig(FeelingLucky plugin, Player player) {
this.plugin = plugin;
File dataFolder = plugin.getDataFolder(); File dataFolder = plugin.getDataFolder();
if (!dataFolder.exists()) dataFolder.mkdirs(); if (!dataFolder.exists()) dataFolder.mkdirs();
File file = new File(dataFolder, player.getUniqueId() + ".yml"); File file = new File(dataFolder, player.getUniqueId() + ".yml");
@ -45,20 +47,21 @@ public class PlayerConfig {
if (tempUsername != null && !tempUsername.equalsIgnoreCase(player.getName())) { if (tempUsername != null && !tempUsername.equalsIgnoreCase(player.getName())) {
config.set("username", 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"); config.set("multiplier", "1.0");
save(); save();
} }
} }
protected PlayerConfig(File file) { protected PlayerConfig(FeelingLucky plugin, File file) {
this.plugin = plugin;
this.configFile = file; this.configFile = file;
config = YamlConfiguration.loadConfiguration(configFile); config = YamlConfiguration.loadConfiguration(configFile);
} }
@Contract("_ -> new") @Contract("_, _ -> new")
public static PlayerConfig loadFrom(File file) { public static PlayerConfig loadFrom(FeelingLucky plugin, File file) {
return new PlayerConfig(file); return new PlayerConfig(plugin, file);
} }
public void save() { public void save() {
@ -66,7 +69,6 @@ public class PlayerConfig {
} }
public void load() { public void load() {
SneakyWorker.sneakyTry(() -> config = YamlConfiguration.loadConfiguration(configFile)); SneakyWorker.sneakyTry(() -> config = YamlConfiguration.loadConfiguration(configFile));
} }

View File

@ -7,36 +7,23 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLoginEvent; import org.bukkit.event.player.PlayerLoginEvent;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
public record PlayerHandler(FeelingLucky plugin) implements Listener { public class PlayerHandler implements Listener {
public PlayerHandler { public final FeelingLucky plugin;
private final Map<Player, Luck> playerLuckMap = new HashMap<>();
public PlayerHandler(FeelingLucky plugin) {
this.plugin = plugin;
Bukkit.getServer().getPluginManager().registerEvents(this, plugin); Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
} }
private static final Map<Player, Luck> playerLuckMap = new HashMap<>(); public Luck getLuckContainer(Player player) {
private static final List<Player> markedPlayers = new ArrayList<>();
public static Luck getLuckContainer(Player player) {
return playerLuckMap.get(player); return playerLuckMap.get(player);
} }
public static void markPlayer(Player player) { public void updatePlayer(Player player, Luck luck) {
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) {
playerLuckMap.replace(player, luck); playerLuckMap.replace(player, luck);
} }

View File

@ -1,7 +1,5 @@
package io.github.simplex.luck.player; 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.HandlerList;
import org.bukkit.event.player.PlayerEvent; import org.bukkit.event.player.PlayerEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -9,16 +7,16 @@ import org.jetbrains.annotations.NotNull;
public class PlayerLuckChangeEvent extends PlayerEvent { public class PlayerLuckChangeEvent extends PlayerEvent {
public final HandlerList handlerList = new HandlerList(); public final HandlerList handlerList = new HandlerList();
public PlayerLuckChangeEvent(@NotNull Player who) { public PlayerLuckChangeEvent(@NotNull Luck luck) {
super(who); super(luck.associatedPlayer());
Luck luck = PlayerHandler.getLuckContainer(who);
if (luck.lessThan(0.0) && !PlayerHandler.isMarked(who)) { if (luck.lessThan(0.0) && !luck.isMarked(luck.associatedPlayer())) {
PlayerHandler.markPlayer(who); luck.markPlayer(luck.associatedPlayer());
return; return;
} }
if (luck.greaterThan(0.0) && PlayerHandler.isMarked(who)) { if (luck.greaterThan(0.0) && luck.isMarked(luck.associatedPlayer())) {
PlayerHandler.unmarkPlayer(who); luck.unmarkPlayer(luck.associatedPlayer());
} }
} }