mirror of
https://github.com/SimplexDevelopment/FeelingLucky.git
synced 2025-07-04 11:06:43 +00:00
Compare commits
4 Commits
Alpha-RC02
...
bleeding
Author | SHA1 | Date | |
---|---|---|---|
474abcdb0d | |||
d08b240ff9 | |||
cbc57d5971 | |||
247a2fafc0 |
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.
Binary file not shown.
@ -3,7 +3,7 @@ plugins {
|
||||
}
|
||||
|
||||
group = 'io.github.simplex'
|
||||
version = '1.0-RC02'
|
||||
version = 'Alpha-1.0-RC03'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
@ -1,5 +1,5 @@
|
||||
name: FeelingLucky
|
||||
version: '1.0-RC02'
|
||||
version: 'Alpha-1.0-RC03'
|
||||
author: SimplexDevelopment
|
||||
main: io.github.simplex.luck.FeelingLucky
|
||||
api-version: 1.18
|
||||
|
Binary file not shown.
@ -18,5 +18,5 @@ public interface LuckContainer extends Serializable {
|
||||
|
||||
Player associatedPlayer();
|
||||
|
||||
double baseValue();
|
||||
double getValue();
|
||||
}
|
||||
|
@ -1,8 +1,11 @@
|
||||
package io.github.simplex.luck;
|
||||
|
||||
import io.github.simplex.luck.listener.PlayerListener;
|
||||
import io.github.simplex.luck.player.Luck;
|
||||
import io.github.simplex.luck.player.PlayerConfig;
|
||||
import io.github.simplex.luck.player.PlayerHandler;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
@ -10,13 +13,18 @@ import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public final class FeelingLucky extends JavaPlugin {
|
||||
private static final Map<UUID, PlayerConfig> configMap = new HashMap<>();
|
||||
private final Map<UUID, PlayerConfig> configMap = new HashMap<>();
|
||||
private final Map<OfflinePlayer, Luck> offlinePlayerLuckMap = new HashMap<>();
|
||||
public LuckCMD cmd;
|
||||
public PlayerHandler handler;
|
||||
public PlayerListener playerListener;
|
||||
|
||||
public static Map<UUID, PlayerConfig> getConfigMap() {
|
||||
public static FeelingLucky plugin = getPlugin(FeelingLucky.class);
|
||||
|
||||
public Map<UUID, PlayerConfig> getConfigMap() {
|
||||
return configMap;
|
||||
}
|
||||
|
||||
@ -24,15 +32,15 @@ public final class FeelingLucky extends JavaPlugin {
|
||||
public void onEnable() {
|
||||
Bukkit.getLogger().info("Initializing the PlayerHandler...");
|
||||
handler = new PlayerHandler(this);
|
||||
Bukkit.getLogger().info("Initialization complete! Attempting to register the handler...");
|
||||
this.getServer().getPluginManager().registerEvents(handler, this);
|
||||
Bukkit.getLogger().info("Initialization complete! Attempting to register the Listeners...");
|
||||
playerListener = new PlayerListener(this);
|
||||
Bukkit.getLogger().info("Registration complete! Attempting to load all player configuration files...");
|
||||
|
||||
File[] files = getDataFolder().listFiles();
|
||||
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!");
|
||||
@ -50,6 +58,6 @@ public final class FeelingLucky extends JavaPlugin {
|
||||
@Override
|
||||
public void onDisable() {
|
||||
Bukkit.getLogger().info("Saving all player configurations...");
|
||||
configMap.forEach((u, pc) -> pc.save());
|
||||
configMap.values().forEach(PlayerConfig::save);
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package io.github.simplex.luck;
|
||||
import io.github.simplex.lib.Messages;
|
||||
import io.github.simplex.luck.player.Luck;
|
||||
import io.github.simplex.luck.player.PlayerConfig;
|
||||
import io.github.simplex.luck.player.PlayerHandler;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
@ -17,8 +16,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,34 +39,35 @@ public class LuckCMD extends Command implements TabCompleter {
|
||||
return true;
|
||||
}
|
||||
|
||||
Luck luck = PlayerHandler.getLuckContainer(player);
|
||||
PlayerConfig config = FeelingLucky.getConfigMap().get(player.getUniqueId());
|
||||
Luck luck = plugin.handler.getLuckContainer(player);
|
||||
PlayerConfig config = plugin.getConfigMap().get(player.getUniqueId());
|
||||
|
||||
switch (args[0]) {
|
||||
case "set" -> {
|
||||
luck.setValue(amount);
|
||||
PlayerHandler.updatePlayer(player, luck);
|
||||
config.setLuck(luck.baseValue());
|
||||
plugin.handler.updatePlayer(player, luck);
|
||||
config.setLuck(luck.getValue());
|
||||
sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat."));
|
||||
return true;
|
||||
}
|
||||
case "give" -> {
|
||||
luck.addTo(amount);
|
||||
PlayerHandler.updatePlayer(player, luck);
|
||||
config.setLuck(luck.baseValue());
|
||||
plugin.handler.updatePlayer(player, luck);
|
||||
config.setLuck(luck.getValue());
|
||||
sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat."));
|
||||
return true;
|
||||
}
|
||||
case "take" -> {
|
||||
luck.takeFrom(amount);
|
||||
PlayerHandler.updatePlayer(player, luck);
|
||||
config.setLuck(luck.baseValue());
|
||||
plugin.handler.updatePlayer(player, luck);
|
||||
config.setLuck(luck.getValue());
|
||||
sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat."));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage(Messages.NO_PERMISSION.get());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -78,8 +81,8 @@ public class LuckCMD extends Command implements TabCompleter {
|
||||
return true;
|
||||
}
|
||||
|
||||
Luck luck = PlayerHandler.getLuckContainer(player);
|
||||
sender.sendMessage(Component.empty().content("Luck stat for " + args[1] + ": " + luck.baseValue()));
|
||||
Luck luck = plugin.handler.getLuckContainer(player);
|
||||
sender.sendMessage(Component.empty().content("Luck stat for " + args[1] + ": " + luck.getValue()));
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -91,11 +94,11 @@ public class LuckCMD extends Command implements TabCompleter {
|
||||
return true;
|
||||
}
|
||||
|
||||
Luck luck = PlayerHandler.getLuckContainer(player);
|
||||
PlayerConfig config = FeelingLucky.getConfigMap().get(player.getUniqueId());
|
||||
Luck luck = plugin.handler.getLuckContainer(player);
|
||||
PlayerConfig config = plugin.getConfigMap().get(player.getUniqueId());
|
||||
luck.reset();
|
||||
PlayerHandler.updatePlayer(player, luck);
|
||||
config.setLuck(luck.baseValue());
|
||||
plugin.handler.updatePlayer(player, luck);
|
||||
config.setLuck(luck.getValue());
|
||||
sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat."));
|
||||
return true;
|
||||
}
|
||||
@ -108,7 +111,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,8 +5,8 @@ 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;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
@ -17,7 +17,6 @@ import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.block.BlockDropItemEvent;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
import org.bukkit.event.entity.EntityDamageEvent;
|
||||
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
|
||||
import org.bukkit.event.entity.EntityDropItemEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.event.player.PlayerItemConsumeEvent;
|
||||
@ -30,9 +29,14 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public record PlayerListener(FeelingLucky plugin) implements Listener {
|
||||
public class PlayerListener implements Listener {
|
||||
private final Map<UUID, Player> entityPlayerMap = new HashMap<>();
|
||||
private final FeelingLucky plugin;
|
||||
|
||||
private static final Map<UUID, Player> entityPlayerMap = new HashMap<>();
|
||||
public PlayerListener(FeelingLucky plugin) {
|
||||
this.plugin = plugin;
|
||||
Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void takeDamage(EntityDamageEvent event) {
|
||||
@ -41,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();
|
||||
@ -50,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);
|
||||
@ -76,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);
|
||||
@ -98,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);
|
||||
@ -138,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();
|
||||
@ -153,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();
|
||||
@ -173,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."));
|
||||
}
|
||||
}
|
||||
@ -187,7 +191,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
|
||||
public void witchesBrew(EntityDamageByEntityEvent event) {
|
||||
Entity eTEMP = event.getDamager();
|
||||
Entity pTEMP = event.getEntity();
|
||||
DamageCause cause = event.getCause();
|
||||
EntityDamageEvent.DamageCause cause = event.getCause();
|
||||
|
||||
if (!(pTEMP instanceof Player player)) {
|
||||
return;
|
||||
@ -197,11 +201,11 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
Luck luck = PlayerHandler.getLuckContainer(player);
|
||||
if (cause.equals(DamageCause.MAGIC) || cause.equals(DamageCause.POISON)) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,8 +7,12 @@ 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 static io.github.simplex.luck.FeelingLucky.plugin;
|
||||
|
||||
@SuppressWarnings("all")
|
||||
public class Luck implements LuckContainer {
|
||||
private final Player player;
|
||||
@ -24,7 +28,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,
|
||||
@ -36,12 +54,14 @@ public class Luck implements LuckContainer {
|
||||
public static boolean quickRNG2(double percentage) {
|
||||
double rng;
|
||||
if (percentage >= 100.0) {
|
||||
rng = 100.0; // 100% chance to trigger, obviously;
|
||||
rng = 1024.0; // 100% chance to trigger, obviously;
|
||||
} else {
|
||||
rng = RNG().nextDouble(0.0, 99.0);
|
||||
rng = RNG().nextDouble(0.0, 1024.0);
|
||||
}
|
||||
|
||||
return (percentage >= rng);
|
||||
double actual = Math.round((rng / 1024.0) * 100);
|
||||
|
||||
return (percentage >= actual);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -77,64 +97,73 @@ public class Luck implements LuckContainer {
|
||||
public boolean quickRNG(double percentage) {
|
||||
double rng;
|
||||
if (percentage >= 100.0) {
|
||||
rng = 100.0; // 100% chance to trigger, obviously;
|
||||
rng = 1024.0; // 100% chance to trigger, obviously;
|
||||
} else {
|
||||
rng = RNG().nextDouble(0.0, 99.0);
|
||||
rng = RNG().nextDouble(0.0, 1024.0);
|
||||
}
|
||||
|
||||
double actual = Math.round((rng / 1024) * 100);
|
||||
|
||||
if (multiplier() > 1.0) {
|
||||
return ((percentage * multiplier()) >= rng);
|
||||
return ((percentage * multiplier()) >= actual);
|
||||
}
|
||||
|
||||
return (percentage >= rng);
|
||||
return (percentage >= actual);
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
setValue(defaultValue());
|
||||
setValue(getDefaultValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public double baseValue() {
|
||||
public double getValue() {
|
||||
return BASE_VALUE;
|
||||
}
|
||||
|
||||
public double defaultValue() {
|
||||
public double getDefaultValue() {
|
||||
return player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue();
|
||||
}
|
||||
|
||||
public void setValue(double value) {
|
||||
player.getAttribute(Attribute.GENERIC_LUCK).setBaseValue(value);
|
||||
plugin.getConfigMap().get(associatedPlayer().getUniqueId()).setLuck(value);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
}
|
||||
|
||||
public void addTo(double value) {
|
||||
setValue(baseValue() + value);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
if (value >= 1024.0 || (getValue() + value) >= 1024.0) {
|
||||
setValue(1024.0);
|
||||
return;
|
||||
}
|
||||
setValue(getValue() + value);
|
||||
}
|
||||
|
||||
public void takeFrom(double value) {
|
||||
setValue(baseValue() - value);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
if (value <= -1024.0 || (getValue() - value) <= -1024.0) {
|
||||
setValue(-1024.0);
|
||||
return;
|
||||
}
|
||||
setValue(getValue() + value);
|
||||
}
|
||||
|
||||
public double getPercentage() {
|
||||
return baseValue() - defaultValue();
|
||||
return getValue() - getDefaultValue();
|
||||
}
|
||||
|
||||
public boolean notDefault() {
|
||||
return baseValue() != defaultValue();
|
||||
return getValue() != getDefaultValue();
|
||||
}
|
||||
|
||||
public boolean lessThan(double value) {
|
||||
return baseValue() < value;
|
||||
return getValue() < value;
|
||||
}
|
||||
|
||||
public boolean greaterThan(double value) {
|
||||
return baseValue() > value;
|
||||
return getValue() > value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "" + baseValue();
|
||||
return "" + getValue();
|
||||
}
|
||||
}
|
||||
|
@ -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");
|
||||
@ -43,22 +45,23 @@ public class PlayerConfig {
|
||||
|
||||
String tempUsername = config.getString("username");
|
||||
|
||||
if (tempUsername != null && tempUsername.equalsIgnoreCase("replace")) {
|
||||
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).getDefaultValue());
|
||||
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));
|
||||
}
|
||||
|
||||
|
@ -1,48 +1,40 @@
|
||||
package io.github.simplex.luck.player;
|
||||
|
||||
import io.github.simplex.luck.FeelingLucky;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
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 {
|
||||
private static final Map<Player, Luck> playerLuckMap = new HashMap<>();
|
||||
private static final List<Player> markedPlayers = new ArrayList<>();
|
||||
public class PlayerHandler implements Listener {
|
||||
public final FeelingLucky plugin;
|
||||
private final Map<Player, Luck> playerLuckMap = new HashMap<>();
|
||||
|
||||
public static Luck getLuckContainer(Player player) {
|
||||
public PlayerHandler(FeelingLucky plugin) {
|
||||
this.plugin = plugin;
|
||||
Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void initializePlayer(PlayerLoginEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
PlayerConfig playerConfig = FeelingLucky.getConfigMap().get(player.getUniqueId());
|
||||
PlayerConfig playerConfig = plugin.getConfigMap().get(player.getUniqueId());
|
||||
|
||||
if (playerConfig == null) {
|
||||
playerConfig = new PlayerConfig(plugin, player);
|
||||
FeelingLucky.getConfigMap().put(player.getUniqueId(), playerConfig);
|
||||
plugin.getConfigMap().put(player.getUniqueId(), playerConfig);
|
||||
}
|
||||
|
||||
String username = playerConfig.getConfig().getString("username");
|
||||
|
@ -1,6 +1,5 @@
|
||||
package io.github.simplex.luck.player;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.event.player.PlayerEvent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -8,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());
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user