From 057450c585dc07c928a882de7da6df382086e336 Mon Sep 17 00:00:00 2001 From: Paldiu Date: Sun, 10 Apr 2022 18:48:09 -0500 Subject: [PATCH] Alpha 1.0 RELEASE --- .../io/github/simplex/api/LuckContainer.java | 1 + .../io/github/simplex/luck/FeelingLucky.java | 21 ++- .../java/io/github/simplex/luck/LuckCMD.java | 9 +- .../simplex/luck/listener/BlockDrops.java | 29 ++++ .../luck/listener/EnchantmentBoost.java | 37 ++++ .../simplex/luck/listener/ItemDrops.java | 73 ++++++++ .../simplex/luck/listener/PlayerListener.java | 158 +----------------- .../simplex/luck/listener/RestoreHunger.java | 37 ++++ .../simplex/luck/listener/TakeDamage.java | 80 +++++++++ .../io/github/simplex/luck/player/Luck.java | 68 ++++---- .../simplex/luck/player/PlayerConfig.java | 5 +- .../simplex/luck/player/PlayerHandler.java | 6 +- 12 files changed, 325 insertions(+), 199 deletions(-) create mode 100644 src/main/java/io/github/simplex/luck/listener/BlockDrops.java create mode 100644 src/main/java/io/github/simplex/luck/listener/EnchantmentBoost.java create mode 100644 src/main/java/io/github/simplex/luck/listener/ItemDrops.java create mode 100644 src/main/java/io/github/simplex/luck/listener/RestoreHunger.java create mode 100644 src/main/java/io/github/simplex/luck/listener/TakeDamage.java diff --git a/src/main/java/io/github/simplex/api/LuckContainer.java b/src/main/java/io/github/simplex/api/LuckContainer.java index dee2cc1..28d8454 100644 --- a/src/main/java/io/github/simplex/api/LuckContainer.java +++ b/src/main/java/io/github/simplex/api/LuckContainer.java @@ -6,6 +6,7 @@ import org.bukkit.entity.Player; import java.io.Serializable; public interface LuckContainer extends Serializable { + Attribute asAttribute(); double getNumber(); diff --git a/src/main/java/io/github/simplex/luck/FeelingLucky.java b/src/main/java/io/github/simplex/luck/FeelingLucky.java index 70c188d..fdca6a2 100644 --- a/src/main/java/io/github/simplex/luck/FeelingLucky.java +++ b/src/main/java/io/github/simplex/luck/FeelingLucky.java @@ -1,6 +1,6 @@ package io.github.simplex.luck; -import io.github.simplex.luck.listener.PlayerListener; +import io.github.simplex.luck.listener.*; import io.github.simplex.luck.player.PlayerConfig; import io.github.simplex.luck.player.PlayerHandler; import org.bukkit.Bukkit; @@ -13,22 +13,27 @@ import java.util.Map; import java.util.UUID; public final class FeelingLucky extends JavaPlugin { - private static final Map configMap = new HashMap<>(); + private final Map configMap = new HashMap<>(); + public LuckCMD cmd; public PlayerHandler handler; - public PlayerListener playerListener; - public static Map getConfigMap() { + public Map getConfigMap() { return configMap; } @Override public void onEnable() { - Bukkit.getLogger().info("Initializing the PlayerHandler..."); + getLogger().info("Initializing the PlayerHandler..."); handler = new PlayerHandler(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..."); + getLogger().info("Initialization complete! Attempting to register the Listeners..."); + new PlayerListener(this); + new BlockDrops(this); + new ItemDrops(this); + new TakeDamage(this); + new RestoreHunger(this); + new EnchantmentBoost(this); + getLogger().info("Registration complete! Attempting to load all player configuration files..."); File[] files = getDataFolder().listFiles(); if (files != null) { diff --git a/src/main/java/io/github/simplex/luck/LuckCMD.java b/src/main/java/io/github/simplex/luck/LuckCMD.java index 9bafe1f..c67a0b6 100644 --- a/src/main/java/io/github/simplex/luck/LuckCMD.java +++ b/src/main/java/io/github/simplex/luck/LuckCMD.java @@ -39,8 +39,13 @@ public class LuckCMD extends Command implements TabCompleter { return true; } + if (amount > 1024.0 || amount < -1024.0) { + sender.sendMessage(Component.empty().content("Number must be between -1024.0 and 1024.0")); + return true; + } + Luck luck = plugin.handler.getLuckContainer(player); - PlayerConfig config = FeelingLucky.getConfigMap().get(player.getUniqueId()); + PlayerConfig config = plugin.getConfigMap().get(player.getUniqueId()); switch (args[0]) { case "set" -> { @@ -95,7 +100,7 @@ public class LuckCMD extends Command implements TabCompleter { } Luck luck = plugin.handler.getLuckContainer(player); - PlayerConfig config = FeelingLucky.getConfigMap().get(player.getUniqueId()); + PlayerConfig config = plugin.getConfigMap().get(player.getUniqueId()); luck.reset(); plugin.handler.updatePlayer(player, luck); config.setLuck(luck.getValue()); diff --git a/src/main/java/io/github/simplex/luck/listener/BlockDrops.java b/src/main/java/io/github/simplex/luck/listener/BlockDrops.java new file mode 100644 index 0000000..73e236b --- /dev/null +++ b/src/main/java/io/github/simplex/luck/listener/BlockDrops.java @@ -0,0 +1,29 @@ +package io.github.simplex.luck.listener; + +import io.github.simplex.luck.FeelingLucky; +import io.github.simplex.luck.SneakyWorker; +import io.github.simplex.luck.player.Luck; +import org.bukkit.Bukkit; +import org.bukkit.entity.Item; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockDropItemEvent; + +import java.util.List; + +public record BlockDrops(FeelingLucky plugin) implements Listener { + public BlockDrops { + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @EventHandler + public void extraBlockDrops(BlockDropItemEvent event) { + Player player = event.getPlayer(); + Luck luck = plugin.handler.getLuckContainer(player); + List items = event.getItems(); + if (luck.quickRNG(luck.getPercentage())) { + items.forEach(SneakyWorker::move); + } + } +} diff --git a/src/main/java/io/github/simplex/luck/listener/EnchantmentBoost.java b/src/main/java/io/github/simplex/luck/listener/EnchantmentBoost.java new file mode 100644 index 0000000..c5fd936 --- /dev/null +++ b/src/main/java/io/github/simplex/luck/listener/EnchantmentBoost.java @@ -0,0 +1,37 @@ +package io.github.simplex.luck.listener; + +import io.github.simplex.luck.FeelingLucky; +import io.github.simplex.luck.player.Luck; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.enchantment.EnchantItemEvent; + +import java.util.List; +import java.util.Map; + +public record EnchantmentBoost(FeelingLucky plugin) implements Listener { + public EnchantmentBoost(FeelingLucky plugin) { + this.plugin = plugin; + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @EventHandler + public void enchantItem(EnchantItemEvent event) { + Map enchMap = event.getEnchantsToAdd(); + List enchList = enchMap.keySet().stream().toList(); + Player player = event.getEnchanter(); + Luck luck = plugin.handler.getLuckContainer(player); + if (luck.quickRNG(luck.getPercentage())) { + Enchantment particular = enchList.get(Luck.RNG().nextInt(enchList.size())); + int rng = Luck.RNG().nextInt(1, 5); + + if ((enchMap.get(particular) + rng) > particular.getMaxLevel()) { + enchMap.replace(particular, particular.getMaxLevel()); + } + + enchMap.replace(particular, enchMap.get(particular) + rng); + } + } +} diff --git a/src/main/java/io/github/simplex/luck/listener/ItemDrops.java b/src/main/java/io/github/simplex/luck/listener/ItemDrops.java new file mode 100644 index 0000000..3c7924d --- /dev/null +++ b/src/main/java/io/github/simplex/luck/listener/ItemDrops.java @@ -0,0 +1,73 @@ +package io.github.simplex.luck.listener; + +import io.github.simplex.luck.FeelingLucky; +import io.github.simplex.luck.player.Luck; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.entity.*; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.entity.EntityDropItemEvent; +import org.bukkit.inventory.ItemStack; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +public class ItemDrops implements Listener { + private final Map entityPlayerMap = new HashMap<>(); + private final FeelingLucky plugin; + + public ItemDrops(FeelingLucky plugin) { + this.plugin = plugin; + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @EventHandler + public void checkForPreItemDrop(EntityDamageByEntityEvent event) { + if (!(event.getEntity() instanceof LivingEntity entity)) { + return; + } + + if (!(event.getDamager() instanceof Player player)) { + return; + } + + if (!(entity.getHealth() <= 0.0)) { + return; + } + + if (entity instanceof Witch witch) { + if (Luck.quickRNGnoMultiplier(33.0)) { + Location location = witch.getLocation(); + World world = location.getWorld(); + Item item = world.dropItemNaturally(location, new ItemStack(Material.RABBIT_FOOT, 1)); + new EntityDropItemEvent(witch, item).callEvent(); + } + } + + entityPlayerMap.put(entity.getUniqueId(), player); + } + + @EventHandler + public void itemDrops(EntityDropItemEvent event) { + Entity entity = event.getEntity(); + + if (entityPlayerMap.get(entity.getUniqueId()) == null) return; + + Player player = entityPlayerMap.get(entity.getUniqueId()); + Luck luck = plugin.handler.getLuckContainer(player); + Item item = event.getItemDrop(); + ItemStack stack = item.getItemStack(); + int amount = stack.getAmount(); + if (luck.quickRNG(luck.getPercentage())) { + int rng = Luck.RNG().nextInt(2, 5); + amount += rng; + stack.setAmount(amount); + event.getItemDrop().setItemStack(stack); + } + } +} 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 49f9c25..30c31eb 100644 --- a/src/main/java/io/github/simplex/luck/listener/PlayerListener.java +++ b/src/main/java/io/github/simplex/luck/listener/PlayerListener.java @@ -1,177 +1,27 @@ package io.github.simplex.luck.listener; -import io.github.simplex.lib.PotionEffectBuilder; 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 net.kyori.adventure.text.Component; import org.bukkit.Bukkit; -import org.bukkit.Location; import org.bukkit.Material; -import org.bukkit.World; -import org.bukkit.entity.*; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.entity.Witch; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; 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.EntityDropItemEvent; import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerItemConsumeEvent; import org.bukkit.inventory.ItemStack; -import org.bukkit.potion.PotionEffect; -import org.bukkit.potion.PotionEffectType; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; - -public class PlayerListener implements Listener { - private final Map entityPlayerMap = new HashMap<>(); - private final FeelingLucky plugin; +public record PlayerListener(FeelingLucky plugin) implements Listener { public PlayerListener(FeelingLucky plugin) { this.plugin = plugin; Bukkit.getServer().getPluginManager().registerEvents(this, plugin); } - @EventHandler - public void takeDamage(EntityDamageEvent event) { - Entity entity = event.getEntity(); - if (!(entity instanceof Player)) { - return; - } - Player player = (Player) event.getEntity(); - Luck luck = plugin.handler.getLuckContainer(player); - if (ListBox.acceptedCauses.contains(event.getCause())) { - if (luck.notDefault()) { - double percentage = luck.getPercentage(); - - /* - * 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 || luck.isMarked(player)) { - percentage = Math.abs(percentage); - if (luck.quickRNG(percentage)) { - event.setCancelled(true); - player.damage(event.getDamage() * 2); - player.sendMessage(Component.empty().content("You were unlucky and took double damage.")); - } - return; - } - - if (luck.quickRNG(percentage)) { - event.setCancelled(true); - player.damage(event.getDamage() / 2); - player.sendMessage(Component.empty().content("You got lucky and took less damage.")); - } - } - } - - if (ListBox.sideCauses.contains(event.getCause())) { - if (luck.notDefault()) { - double percentage = luck.getPercentage(); - - /* - * 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 || luck.isMarked(player)) { - percentage = Math.abs(percentage); - if (luck.quickRNG(percentage)) { - event.setCancelled(true); - player.addPotionEffect(PotionEffectBuilder.newEffect().type(ListBox.potionEffects.get(Luck.RNG().nextInt(ListBox.potionEffects.size() - 1))).amplifier(Luck.RNG().nextInt(1, 5)).duration(Luck.RNG().nextInt(1, 120)).create()); - } - return; - } - - if (luck.quickRNG(percentage)) { - event.setCancelled(true); - player.getActivePotionEffects().removeIf(p -> ListBox.potionEffects.contains(p.getType())); - player.setFireTicks(0); - player.sendMessage(Component.empty().content("You got lucky and your afflictions were cured.")); - } - } - } - } - - @EventHandler - public void extraBlockDrops(BlockDropItemEvent event) { - Player player = event.getPlayer(); - Luck luck = plugin.handler.getLuckContainer(player); - List items = event.getItems(); - if (luck.quickRNG(luck.getPercentage())) { - items.forEach(SneakyWorker::move); - } - } - - @EventHandler - public void checkForPreItemDrop(EntityDamageByEntityEvent event) { - if (!(event.getEntity() instanceof LivingEntity entity)) { - return; - } - - if (!(event.getDamager() instanceof Player player)) { - return; - } - - if (!(entity.getHealth() <= 0.0)) { - return; - } - - if (entity instanceof Witch witch) { - if (Luck.quickRNG2(33.0)) { - Location location = witch.getLocation(); - World world = location.getWorld(); - Item item = world.dropItemNaturally(location, new ItemStack(Material.RABBIT_FOOT, 1)); - new EntityDropItemEvent(witch, item).callEvent(); - } - } - - entityPlayerMap.put(entity.getUniqueId(), player); - } - - @EventHandler - public void itemDrops(EntityDropItemEvent event) { - Entity entity = event.getEntity(); - - if (entityPlayerMap.get(entity.getUniqueId()) == null) return; - - Player player = entityPlayerMap.get(entity.getUniqueId()); - Luck luck = plugin.handler.getLuckContainer(player); - Item item = event.getItemDrop(); - ItemStack stack = item.getItemStack(); - int amount = stack.getAmount(); - if (luck.quickRNG(luck.getPercentage())) { - int rng = Luck.RNG().nextInt(2, 5); - amount += rng; - stack.setAmount(amount); - event.getItemDrop().setItemStack(stack); - } - } - - @EventHandler - public void restoreHunger(PlayerItemConsumeEvent event) { - ItemStack item = event.getItem(); - 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(); - ListBox.foods.forEach(food -> { - if (item.isSimilar(food)) { - if (luck.quickRNG(percentage)) { - event.getPlayer().setExhaustion(event.getPlayer().getExhaustion() + 2); - event.getPlayer().addPotionEffect(effect); - } - } - }); - } - } - @EventHandler public void rabbitFoot(PlayerInteractEvent event) { Action action = event.getAction(); diff --git a/src/main/java/io/github/simplex/luck/listener/RestoreHunger.java b/src/main/java/io/github/simplex/luck/listener/RestoreHunger.java new file mode 100644 index 0000000..f8cd802 --- /dev/null +++ b/src/main/java/io/github/simplex/luck/listener/RestoreHunger.java @@ -0,0 +1,37 @@ +package io.github.simplex.luck.listener; + +import io.github.simplex.lib.PotionEffectBuilder; +import io.github.simplex.luck.FeelingLucky; +import io.github.simplex.luck.ListBox; +import io.github.simplex.luck.player.Luck; +import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerItemConsumeEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; + +public record RestoreHunger(FeelingLucky plugin) implements Listener { + public RestoreHunger { + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @EventHandler + public void restoreHunger(PlayerItemConsumeEvent event) { + ItemStack item = event.getItem(); + 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(); + ListBox.foods.forEach(food -> { + if (item.isSimilar(food)) { + if (luck.quickRNG(percentage)) { + event.getPlayer().setExhaustion(event.getPlayer().getExhaustion() + 2); + event.getPlayer().addPotionEffect(effect); + } + } + }); + } + } +} diff --git a/src/main/java/io/github/simplex/luck/listener/TakeDamage.java b/src/main/java/io/github/simplex/luck/listener/TakeDamage.java new file mode 100644 index 0000000..5474faa --- /dev/null +++ b/src/main/java/io/github/simplex/luck/listener/TakeDamage.java @@ -0,0 +1,80 @@ +package io.github.simplex.luck.listener; + +import io.github.simplex.lib.PotionEffectBuilder; +import io.github.simplex.luck.FeelingLucky; +import io.github.simplex.luck.ListBox; +import io.github.simplex.luck.player.Luck; +import net.kyori.adventure.text.Component; +import org.bukkit.Bukkit; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityDamageEvent; + +public record TakeDamage(FeelingLucky plugin) implements Listener { + public TakeDamage { + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @EventHandler + public void takeDamage(EntityDamageEvent event) { + Entity entity = event.getEntity(); + if (!(entity instanceof Player)) { + return; + } + Player player = (Player) event.getEntity(); + Luck luck = plugin.handler.getLuckContainer(player); + if (ListBox.acceptedCauses.contains(event.getCause())) { + if (luck.notDefault()) { + double percentage = luck.getPercentage(); + + /* + * 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 || luck.isMarked(player)) { + percentage = Math.abs(percentage); + if (luck.quickRNG(percentage)) { + event.setCancelled(true); + player.damage(event.getDamage() * 2); + player.sendMessage(Component.empty().content("You were unlucky and took double damage.")); + } + return; + } + + if (luck.quickRNG(percentage)) { + event.setCancelled(true); + player.damage(event.getDamage() / 2); + player.sendMessage(Component.empty().content("You got lucky and took less damage.")); + } + } + } + + if (ListBox.sideCauses.contains(event.getCause())) { + if (luck.notDefault()) { + double percentage = luck.getPercentage(); + + /* + * 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 || luck.isMarked(player)) { + percentage = Math.abs(percentage); + if (luck.quickRNG(percentage)) { + event.setCancelled(true); + player.addPotionEffect(PotionEffectBuilder.newEffect().type(ListBox.potionEffects.get(Luck.RNG().nextInt(ListBox.potionEffects.size() - 1))).amplifier(Luck.RNG().nextInt(1, 5)).duration(Luck.RNG().nextInt(1, 120)).create()); + } + return; + } + + if (luck.quickRNG(percentage)) { + event.setCancelled(true); + player.getActivePotionEffects().removeIf(p -> ListBox.potionEffects.contains(p.getType())); + player.setFireTicks(0); + player.sendMessage(Component.empty().content("You got lucky and your afflictions were cured.")); + } + } + } + } +} 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 3efdb82..bc1fb58 100644 --- a/src/main/java/io/github/simplex/luck/player/Luck.java +++ b/src/main/java/io/github/simplex/luck/player/Luck.java @@ -16,21 +16,45 @@ import java.util.SplittableRandom; public class Luck implements LuckContainer { private final Player player; private final double multiplier; - private final double BASE_VALUE; private final PlayerLuckChangeEvent event; + private final FeelingLucky plugin; + private final List markedPlayers = new ArrayList<>(); + private double BASE_VALUE; - public Luck(Player player) { - this(player, 1.0); + public Luck(FeelingLucky plugin, Player player) { + this(plugin, player, 1.0); } - public Luck(Player player, double multiplier) { + public Luck(FeelingLucky plugin, Player player, double multiplier) { this.player = player; this.multiplier = multiplier; - BASE_VALUE = player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue(); + this.plugin = plugin; + BASE_VALUE = plugin.getConfigMap().get(player.getUniqueId()).getConfig().getDouble("luck"); event = new PlayerLuckChangeEvent(this); } - private final List markedPlayers = new ArrayList<>(); + @Contract(pure = true, + value = "-> new") + public static @NotNull SplittableRandom RNG() { + return new SplittableRandom(); + } + + public static boolean quickRNGnoMultiplier(double percentage) { + double rng; + if (percentage >= 100.0) { + rng = 1024.0; // 100% chance to trigger, obviously; + } else { + rng = RNG().nextDouble(0.0, 1024.0); + } + + double actual = Math.round((rng / 1024.0) * 100); + + return (percentage >= actual); + } + + public FeelingLucky getPlugin() { + return plugin; + } public void markPlayer(Player player) { markedPlayers.add(player); @@ -44,25 +68,6 @@ public class Luck implements LuckContainer { return markedPlayers.contains(player); } - @Contract(pure = true, - value = "-> new") - public static @NotNull SplittableRandom RNG() { - return new SplittableRandom(); - } - - public static boolean quickRNG2(double percentage) { - double rng; - if (percentage >= 100.0) { - rng = 1024.0; // 100% chance to trigger, obviously; - } else { - rng = RNG().nextDouble(0.0, 1024.0); - } - - double actual = Math.round((rng / 1024.0) * 100); - - return (percentage >= actual); - } - @Override public Attribute asAttribute() { return Attribute.GENERIC_LUCK; @@ -119,14 +124,15 @@ public class Luck implements LuckContainer { return BASE_VALUE; } - public double getDefaultValue() { - return player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue(); + public void setValue(double value) { + BASE_VALUE = value; + player.getAttribute(Attribute.GENERIC_LUCK).setBaseValue(value); + plugin.getConfigMap().get(associatedPlayer().getUniqueId()).setLuck(value); + Bukkit.getPluginManager().callEvent(event); } - public void setValue(double value) { - player.getAttribute(Attribute.GENERIC_LUCK).setBaseValue(value); - FeelingLucky.getConfigMap().get(associatedPlayer().getUniqueId()).setLuck(value); - Bukkit.getPluginManager().callEvent(event); + public double getDefaultValue() { + return player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue(); } public void addTo(double value) { 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 63c9f69..95d5682 100644 --- a/src/main/java/io/github/simplex/luck/player/PlayerConfig.java +++ b/src/main/java/io/github/simplex/luck/player/PlayerConfig.java @@ -45,11 +45,14 @@ public class PlayerConfig { String tempUsername = config.getString("username"); - if (tempUsername != null && !tempUsername.equalsIgnoreCase(player.getName())) { + if (tempUsername == null) { config.set("username", player.getName()); config.set("luck", plugin.handler.getLuckContainer(player).getDefaultValue()); config.set("multiplier", "1.0"); save(); + } else if (!tempUsername.equalsIgnoreCase(player.getName())) { + config.set("username", player.getName()); + save(); } } 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 a5566eb..61ddd60 100644 --- a/src/main/java/io/github/simplex/luck/player/PlayerHandler.java +++ b/src/main/java/io/github/simplex/luck/player/PlayerHandler.java @@ -30,11 +30,11 @@ public class PlayerHandler implements Listener { @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"); @@ -47,7 +47,7 @@ public class PlayerHandler implements Listener { playerConfig.load(); } - Luck container = new Luck(player, multiplier); + Luck container = new Luck(plugin, player, multiplier); container.setValue(luck); playerLuckMap.put(player, container);