From 831913c6686627c9f78dc00d4b5e20e0aea687db Mon Sep 17 00:00:00 2001 From: Paldiu Date: Sun, 20 Mar 2022 13:09:24 -0500 Subject: [PATCH] Updates --- .idea/vcs.xml | 6 ++ .../java/io/github/simplex/crumb/Config.java | 41 ------------- .../java/io/github/simplex/crumb/Crumb.java | 21 ------- .../github/simplex/crumb/PlayerHandler.java | 58 ------------------ .../io/github/simplex/crumb/SneakyWorker.java | 17 ------ .../io/github/simplex/luck/FeelingLucky.java | 29 +++++++++ .../io/github/simplex/luck/SneakyWorker.java | 21 +++++++ .../listener/PlayerListener.java | 26 ++++---- .../simplex/{crumb => luck/player}/Luck.java | 26 +++++--- .../simplex/luck/player/PlayerConfig.java | 51 ++++++++++++++++ .../simplex/luck/player/PlayerHandler.java | 59 +++++++++++++++++++ .../luck/player/PlayerLuckChangeEvent.java | 28 +++++++++ src/main/resources/default_player.yml | 3 + src/main/resources/plugin.yml | 5 +- 14 files changed, 232 insertions(+), 159 deletions(-) create mode 100644 .idea/vcs.xml delete mode 100644 src/main/java/io/github/simplex/crumb/Config.java delete mode 100644 src/main/java/io/github/simplex/crumb/Crumb.java delete mode 100644 src/main/java/io/github/simplex/crumb/PlayerHandler.java delete mode 100644 src/main/java/io/github/simplex/crumb/SneakyWorker.java create mode 100644 src/main/java/io/github/simplex/luck/FeelingLucky.java create mode 100644 src/main/java/io/github/simplex/luck/SneakyWorker.java rename src/main/java/io/github/simplex/{crumb => luck}/listener/PlayerListener.java (90%) rename src/main/java/io/github/simplex/{crumb => luck/player}/Luck.java (77%) create mode 100644 src/main/java/io/github/simplex/luck/player/PlayerConfig.java create mode 100644 src/main/java/io/github/simplex/luck/player/PlayerHandler.java create mode 100644 src/main/java/io/github/simplex/luck/player/PlayerLuckChangeEvent.java create mode 100644 src/main/resources/default_player.yml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/io/github/simplex/crumb/Config.java b/src/main/java/io/github/simplex/crumb/Config.java deleted file mode 100644 index 2aca13b..0000000 --- a/src/main/java/io/github/simplex/crumb/Config.java +++ /dev/null @@ -1,41 +0,0 @@ -package io.github.simplex.crumb; - -import org.bukkit.configuration.file.YamlConfiguration; - -import java.io.File; -import java.io.IOException; - -public class Config extends YamlConfiguration { - private final String CONFIG_NAME = "config.yml"; - - private final File dataFolder; - private final Crumb plugin; - private final File configFile; - - @SuppressWarnings("ResultOfMethodCallIgnored") - public Config(Crumb plugin, boolean copyResource) { - this.plugin = plugin; - this.dataFolder = plugin.getDataFolder(); - if (!dataFolder.exists()) dataFolder.mkdirs(); - File file = new File(dataFolder, CONFIG_NAME); - if (!file.exists()) { - SneakyWorker.sneakyTry(() -> { - file.createNewFile(); - plugin.saveResource(CONFIG_NAME, true); - }); - } - if (copyResource) plugin.saveResource(CONFIG_NAME, true); - configFile = file; - loadConfiguration(configFile); - } - - public void save() { - SneakyWorker.sneakyTry(() -> super.save(configFile)); - } - - public void load() { - SneakyWorker.sneakyTry(() -> { - super.load(configFile); - }); - } -} diff --git a/src/main/java/io/github/simplex/crumb/Crumb.java b/src/main/java/io/github/simplex/crumb/Crumb.java deleted file mode 100644 index 74c1344..0000000 --- a/src/main/java/io/github/simplex/crumb/Crumb.java +++ /dev/null @@ -1,21 +0,0 @@ -package io.github.simplex.crumb; - -import org.bukkit.plugin.java.JavaPlugin; - -public final class Crumb extends JavaPlugin { - public Config config; - public PlayerHandler handler; - - @Override - public void onEnable() { - config = new Config(this, false); - handler = new PlayerHandler(this); - this.getServer().getPluginManager().registerEvents(handler, this); - } - - @Override - public void onDisable() { - config.save(); - // Plugin shutdown logic - } -} diff --git a/src/main/java/io/github/simplex/crumb/PlayerHandler.java b/src/main/java/io/github/simplex/crumb/PlayerHandler.java deleted file mode 100644 index 62faf8c..0000000 --- a/src/main/java/io/github/simplex/crumb/PlayerHandler.java +++ /dev/null @@ -1,58 +0,0 @@ -package io.github.simplex.crumb; - -import org.bukkit.attribute.Attribute; -import org.bukkit.configuration.ConfigurationSection; -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 class PlayerHandler implements Listener { - private final Map playerLuckMap = new HashMap<>(); - private final List markedPlayers = new ArrayList<>(); - private final Crumb plugin; - - public PlayerHandler(Crumb plugin) { - this.plugin = plugin; - } - - public Luck getLuckContainer(Player player) { - return playerLuckMap.get(player); - } - - @EventHandler - public void initializePlayer(PlayerLoginEvent event) { - Player player = event.getPlayer(); - String uuid = player.getUniqueId().toString(); - ConfigurationSection identifier = plugin.config.getConfigurationSection(uuid); - Luck luck = new Luck(player); - if (identifier == null) { - identifier = plugin.config.createSection(player.getUniqueId().toString()); - identifier.set("luck", luck.defaultValue()); - plugin.config.save(); - } - luck.setValue(identifier.getDouble("luck")); - playerLuckMap.put(player, luck); - } - - public void updatePlayer(Player player, Luck luck) { - playerLuckMap.replace(player, luck); - } - - 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); - } -} diff --git a/src/main/java/io/github/simplex/crumb/SneakyWorker.java b/src/main/java/io/github/simplex/crumb/SneakyWorker.java deleted file mode 100644 index fe7c4e8..0000000 --- a/src/main/java/io/github/simplex/crumb/SneakyWorker.java +++ /dev/null @@ -1,17 +0,0 @@ -package io.github.simplex.crumb; - -import org.bukkit.Bukkit; - -public class SneakyWorker { - public static void sneakyTry(SneakyTry sneakyTry) { - try { - sneakyTry.tryThis(); - } catch (Exception ex) { - Bukkit.getLogger().severe(ex.getMessage()); - } - } - - interface SneakyTry { - void tryThis() throws Exception; - } -} diff --git a/src/main/java/io/github/simplex/luck/FeelingLucky.java b/src/main/java/io/github/simplex/luck/FeelingLucky.java new file mode 100644 index 0000000..ec6b5fe --- /dev/null +++ b/src/main/java/io/github/simplex/luck/FeelingLucky.java @@ -0,0 +1,29 @@ +package io.github.simplex.luck; + +import io.github.simplex.luck.player.PlayerConfig; +import io.github.simplex.luck.player.PlayerHandler; +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.ArrayList; +import java.util.List; + +public final class FeelingLucky extends JavaPlugin { + public PlayerHandler handler; + private static final List configList = new ArrayList<>(); + + @Override + public void onEnable() { + handler = new PlayerHandler(this); + configList.forEach(PlayerConfig::load); + this.getServer().getPluginManager().registerEvents(handler, this); + } + + @Override + public void onDisable() { + configList.forEach(PlayerConfig::save); + } + + public static List getConfigList() { + return configList; + } +} diff --git a/src/main/java/io/github/simplex/luck/SneakyWorker.java b/src/main/java/io/github/simplex/luck/SneakyWorker.java new file mode 100644 index 0000000..c1f417c --- /dev/null +++ b/src/main/java/io/github/simplex/luck/SneakyWorker.java @@ -0,0 +1,21 @@ +package io.github.simplex.luck; + +import org.bukkit.Bukkit; + +public class SneakyWorker { + public static void sneakyTry(SneakyTry sneakyTry) { + try { + sneakyTry.tryThis(); + } catch (Exception ex) { + String sb = "An error of type: " + + ex.getClass().getSimpleName() + + " has occurred. A cause will be printed. \n\n" + + ex.getCause(); + Bukkit.getLogger().severe(sb); + } + } + + public interface SneakyTry { + void tryThis() throws Exception; + } +} diff --git a/src/main/java/io/github/simplex/crumb/listener/PlayerListener.java b/src/main/java/io/github/simplex/luck/listener/PlayerListener.java similarity index 90% rename from src/main/java/io/github/simplex/crumb/listener/PlayerListener.java rename to src/main/java/io/github/simplex/luck/listener/PlayerListener.java index 983ab52..820eea2 100644 --- a/src/main/java/io/github/simplex/crumb/listener/PlayerListener.java +++ b/src/main/java/io/github/simplex/luck/listener/PlayerListener.java @@ -1,10 +1,10 @@ -package io.github.simplex.crumb.listener; +package io.github.simplex.luck.listener; -import io.github.simplex.crumb.Crumb; -import io.github.simplex.crumb.Luck; +import io.github.simplex.luck.FeelingLucky; +import io.github.simplex.luck.player.Luck; +import io.github.simplex.luck.player.PlayerHandler; import net.kyori.adventure.text.Component; import org.bukkit.Material; -import org.bukkit.block.Block; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.entity.Witch; @@ -23,9 +23,9 @@ import java.util.ArrayList; import java.util.List; public class PlayerListener implements Listener { - private final Crumb plugin; + private final FeelingLucky plugin; - public PlayerListener(Crumb plugin) { + public PlayerListener(FeelingLucky plugin) { this.plugin = plugin; } @@ -36,9 +36,9 @@ public class PlayerListener implements Listener { return; } Player player = (Player) event.getEntity(); - Luck luck = plugin.handler.getLuckContainer(player); + Luck luck = PlayerHandler.getLuckContainer(player); if (ListBox.acceptedCauses.contains(event.getCause())) { - if (!luck.isDefault()) { + if (luck.notDefault()) { double percentage = luck.getPercentage(); if (luck.quickRNG(percentage)) { event.setCancelled(true); @@ -49,7 +49,7 @@ public class PlayerListener implements Listener { } if (ListBox.sideCauses.contains(event.getCause())) { - if (!luck.isDefault()) { + if (luck.notDefault()) { double percentage = luck.getPercentage(); if (luck.quickRNG(percentage)) { event.setCancelled(true); @@ -64,8 +64,8 @@ public class PlayerListener implements Listener { @EventHandler public void restoreHunger(PlayerItemConsumeEvent event) { ItemStack item = event.getItem(); - Luck luck = plugin.handler.getLuckContainer(event.getPlayer()); - if (!luck.isDefault()) { + Luck luck = PlayerHandler.getLuckContainer(event.getPlayer()); + if (luck.notDefault()) { double percentage = luck.getPercentage(); ListBox.foods.forEach(food -> { if (item.isSimilar(food)) { @@ -82,7 +82,7 @@ public class PlayerListener implements Listener { Action action = event.getAction(); ItemStack foot = new ItemStack(Material.RABBIT_FOOT); Player player = event.getPlayer(); - Luck luck = plugin.handler.getLuckContainer(player); + Luck luck = PlayerHandler.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()); @@ -106,7 +106,7 @@ public class PlayerListener implements Listener { return; } - Luck luck = plugin.handler.getLuckContainer(player); + Luck luck = PlayerHandler.getLuckContainer(player); if (cause.equals(DamageCause.MAGIC) || cause.equals(DamageCause.POISON)) { if (luck.quickRNG(33.0)) { luck.takeFrom(5.0); diff --git a/src/main/java/io/github/simplex/crumb/Luck.java b/src/main/java/io/github/simplex/luck/player/Luck.java similarity index 77% rename from src/main/java/io/github/simplex/crumb/Luck.java rename to src/main/java/io/github/simplex/luck/player/Luck.java index 50b5f1f..66b4c2a 100644 --- a/src/main/java/io/github/simplex/crumb/Luck.java +++ b/src/main/java/io/github/simplex/luck/player/Luck.java @@ -1,6 +1,7 @@ -package io.github.simplex.crumb; +package io.github.simplex.luck.player; import io.github.simplex.api.LuckContainer; +import org.bukkit.Bukkit; import org.bukkit.attribute.Attribute; import org.bukkit.entity.Player; @@ -10,17 +11,17 @@ public class Luck implements LuckContainer { private final Player player; private final double multiplier; private final double BASE_VALUE; + private final PlayerLuckChangeEvent event; public Luck(Player player) { - this.player = player; - multiplier = 1.0; - BASE_VALUE = player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue(); + this(player, 1.0); } public Luck(Player player, double multiplier) { this.player = player; this.multiplier = multiplier; BASE_VALUE = player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue(); + event = new PlayerLuckChangeEvent(player); } @Override @@ -66,7 +67,7 @@ public class Luck implements LuckContainer { } if (multiplier() > 0.0) { - return (percentage >= (rng * multiplier())); + return ((percentage * multiplier()) >= rng); } return (percentage >= rng); @@ -83,22 +84,33 @@ public class Luck implements LuckContainer { public void setValue(double value) { player.getAttribute(Attribute.GENERIC_LUCK).setBaseValue(value); + Bukkit.getPluginManager().callEvent(event); } public void addTo(double value) { setValue(baseValue() + value); + Bukkit.getPluginManager().callEvent(event); } public void takeFrom(double value) { setValue(baseValue() - value); + Bukkit.getPluginManager().callEvent(event); } public double getPercentage() { return baseValue() - defaultValue(); } - public boolean isDefault() { - return baseValue() == defaultValue(); + public boolean notDefault() { + return baseValue() != defaultValue(); + } + + public boolean lessThan(double value) { + return baseValue() < value; + } + + public boolean greaterThan (double value) { + return baseValue() > value; } @Override diff --git a/src/main/java/io/github/simplex/luck/player/PlayerConfig.java b/src/main/java/io/github/simplex/luck/player/PlayerConfig.java new file mode 100644 index 0000000..37db03d --- /dev/null +++ b/src/main/java/io/github/simplex/luck/player/PlayerConfig.java @@ -0,0 +1,51 @@ +package io.github.simplex.luck.player; + +import io.github.simplex.luck.FeelingLucky; +import io.github.simplex.luck.SneakyWorker; +import org.bukkit.attribute.Attribute; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; + +import java.io.File; +import java.io.InputStream; +import java.io.InputStreamReader; + +public class PlayerConfig extends YamlConfiguration { + private final File configFile; + private volatile YamlConfiguration config; + + @SuppressWarnings("ResultOfMethodCallIgnored") + public PlayerConfig(FeelingLucky plugin, Player player) { + File dataFolder = new File(plugin.getDataFolder(), "players"); + if (!dataFolder.exists()) dataFolder.mkdirs(); + File file = new File(dataFolder, player.getUniqueId() + ".yml"); + if (!file.exists()) { + SneakyWorker.sneakyTry(() -> { + file.createNewFile(); + InputStreamReader reader = new InputStreamReader(plugin.getResource("default_player.yml")); + loadConfiguration(reader).save(file); + }); + } + configFile = file; + config = loadConfiguration(configFile); + + if (config.getString("username").equalsIgnoreCase("replace")) { + config.set("username", player.getName()); + config.set("luck", new Luck(player).defaultValue()); + config.set("multiplier", "1.0"); + save(); + } + } + + public void save() { + SneakyWorker.sneakyTry(() -> config.save(configFile)); + } + + public void load() { + SneakyWorker.sneakyTry(() -> config = loadConfiguration(configFile)); + } + + public YamlConfiguration getConfig() { + return config; + } +} diff --git a/src/main/java/io/github/simplex/luck/player/PlayerHandler.java b/src/main/java/io/github/simplex/luck/player/PlayerHandler.java new file mode 100644 index 0000000..40d10dc --- /dev/null +++ b/src/main/java/io/github/simplex/luck/player/PlayerHandler.java @@ -0,0 +1,59 @@ +package io.github.simplex.luck.player; + +import io.github.simplex.luck.FeelingLucky; +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 playerLuckMap = new HashMap<>(); + private static final List markedPlayers = new ArrayList<>(); + + public static Luck getLuckContainer(Player player) { + return playerLuckMap.get(player); + } + + @EventHandler + public void initializePlayer(PlayerLoginEvent event) { + Player player = event.getPlayer(); + PlayerConfig config = new PlayerConfig(plugin, player); + + String username = config.getString("username"); + double luckstat = config.getDouble("luck"); + double multiplier = config.getDouble("multiplier"); + + if (!player.getName().equalsIgnoreCase(username)) { + config.set("username", player.getName()); + config.save(); + config.load(); + } + + Luck luck = new Luck(player, multiplier); + luck.setValue(luckstat); + + FeelingLucky.getConfigList().add(config); + playerLuckMap.put(player, luck); + } + + public void updatePlayer(Player player, Luck luck) { + playerLuckMap.replace(player, luck); + } + + 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); + } +} diff --git a/src/main/java/io/github/simplex/luck/player/PlayerLuckChangeEvent.java b/src/main/java/io/github/simplex/luck/player/PlayerLuckChangeEvent.java new file mode 100644 index 0000000..77a4fee --- /dev/null +++ b/src/main/java/io/github/simplex/luck/player/PlayerLuckChangeEvent.java @@ -0,0 +1,28 @@ +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; + +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); + return; + } + + if (luck.greaterThan(0.0) && PlayerHandler.isMarked(who)) { + PlayerHandler.unmarkPlayer(who); + } + } + + @Override + public @NotNull HandlerList getHandlers() { + return handlerList; + } +} diff --git a/src/main/resources/default_player.yml b/src/main/resources/default_player.yml new file mode 100644 index 0000000..01fc08e --- /dev/null +++ b/src/main/resources/default_player.yml @@ -0,0 +1,3 @@ +username: replace +luck: 0.0 +multiplier: 1.0 \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index a024527..cb03450 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,4 +1,5 @@ -name: Crumb +name: FeelingLucky version: '${version}' -main: io.github.simplex.crumb.Crumb +author: SimplexDevelopment +main: io.github.simplex.luck.FeelingLucky api-version: 1.18