mirror of
https://github.com/SimplexDevelopment/FeelingLucky.git
synced 2024-11-09 20:26:07 +00:00
Alpha 1.0.0 RC04
Switched PlayerHandler access modifiers in favor of Dependency Injection.
This commit is contained in:
parent
247a2fafc0
commit
cbc57d5971
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -34,7 +34,7 @@ public final class FeelingLucky extends JavaPlugin {
|
||||
if (files != null) {
|
||||
Arrays.stream(files).forEach(file -> {
|
||||
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!");
|
||||
|
@ -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.", "/<command> <info | set | reset | give | take> [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;
|
||||
|
@ -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<UUID, Player> entityPlayerMap = new HashMap<>();
|
||||
public class PlayerListener implements Listener {
|
||||
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);
|
||||
}
|
||||
|
||||
@ -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<Item> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<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,
|
||||
@ -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() {
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
|
@ -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<Player, Luck> playerLuckMap = new HashMap<>();
|
||||
|
||||
public PlayerHandler(FeelingLucky plugin) {
|
||||
this.plugin = plugin;
|
||||
Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
|
||||
}
|
||||
|
||||
private static final Map<Player, Luck> playerLuckMap = new HashMap<>();
|
||||
private static final List<Player> 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);
|
||||
}
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user