From 330fd278b772a940301860726adc2ce88ff10aa3 Mon Sep 17 00:00:00 2001 From: Paldiu Date: Fri, 22 Apr 2022 19:25:02 -0500 Subject: [PATCH] Beta 20220422-SNAPSHOT Changelog: - Added a configuration with editable rarity values and rarity types for the different events that can occur, to give the end user more control over how often players receive the effects. - Modified listeners to use the respective Configuration values. - Added the ability to reload a specific player configuration with /luck reload -p - Added the ability to reload the main configuration using /luck reload -m - Added an integrity checker to validate the main configuration on boot. --- .../java/io/github/simplex/luck/Config.java | 91 +++++++++++++++++++ .../io/github/simplex/luck/FeelingLucky.java | 27 ++++-- .../luck/listener/AbstractListener.java | 46 ++++++++++ .../simplex/luck/listener/BlockDrops.java | 11 +-- .../luck/listener/BonemealFullCrop.java | 13 ++- .../simplex/luck/listener/CheatDeath.java | 11 +-- .../luck/listener/EnchantmentBoost.java | 14 +-- .../simplex/luck/listener/ExpBoost.java | 8 +- .../github/simplex/luck/listener/IllOmen.java | 9 +- .../simplex/luck/listener/ItemDrops.java | 32 ++++--- .../simplex/luck/listener/PlayerListener.java | 8 +- .../simplex/luck/listener/RestoreHunger.java | 11 +-- .../simplex/luck/listener/TakeDamage.java | 13 ++- .../luck/listener/UnbreakableTool.java | 11 +-- .../luck/listener/VillagerInventory.java | 8 +- .../simplex/luck/player/PlayerConfig.java | 16 +++- .../io/github/simplex/luck/util/LuckCMD.java | 18 ++++ src/main/resources/config.yml | 26 ++++++ 18 files changed, 280 insertions(+), 93 deletions(-) create mode 100644 src/main/java/io/github/simplex/luck/Config.java create mode 100644 src/main/java/io/github/simplex/luck/listener/AbstractListener.java create mode 100644 src/main/resources/config.yml diff --git a/src/main/java/io/github/simplex/luck/Config.java b/src/main/java/io/github/simplex/luck/Config.java new file mode 100644 index 0000000..e2c36a3 --- /dev/null +++ b/src/main/java/io/github/simplex/luck/Config.java @@ -0,0 +1,91 @@ +package io.github.simplex.luck; + +import io.github.simplex.lib.MiniComponent; +import io.github.simplex.luck.listener.AbstractListener; +import io.github.simplex.luck.util.SneakyWorker; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class Config extends YamlConfiguration { + private final FeelingLucky plugin; + private File configFile; + + public Config(FeelingLucky plugin) { + this.plugin = plugin; + + File dataFolder = plugin.getDataFolder(); + if (!dataFolder.exists()) dataFolder.mkdirs(); + File configFile = new File(dataFolder, "config.yml"); + if (!configFile.exists()) { + SneakyWorker.sneakyTry(configFile::createNewFile); + plugin.saveResource("config.yml", true); + } + + this.configFile = configFile; + load(); + + if (validateIntegrity()) { + File newFile = new File(plugin.getDataFolder(), "config.yml"); + SneakyWorker.sneakyTry(() -> { + Files.delete(Path.of(plugin.getDataFolder().getPath())); + newFile.createNewFile(); + plugin.saveResource("config.yml", true); + }); + this.configFile = newFile; + load(); + } + } + + public void save() { + SneakyWorker.sneakyTry(() -> save(configFile)); + } + + public void load() { + SneakyWorker.sneakyTry(() -> load(configFile)); + } + + public void reload() { + save(); + load(); + } + + public boolean validateIntegrity() { + for (String key : getKeys(false)) { + if (!configEntries.contains(key)) { + plugin.getLogger().severe("The contents of your configuration file is corrupted! Regenerating a new configuration file..."); + return true; + } + } + return false; + } + + public AbstractListener.Rarity getRarity(String name) { + return AbstractListener.Rarity.valueOf(getString(name)); + } + + public double getChance(String path) { + return getDouble(path); + } + + private List configEntries = new ArrayList<>() {{ + add("high_rarity_chance"); + add("medium_rarity_chance"); + add("low_rarity_chance"); + add("block_drops"); + add("bonemeal"); + add("cheat_death"); + add("enchanting"); + add("experience"); + add("item_drops"); + add("random_effect"); + add("restore_hunger"); + add("take_damage"); + add("unbreakable"); + }}; +} diff --git a/src/main/java/io/github/simplex/luck/FeelingLucky.java b/src/main/java/io/github/simplex/luck/FeelingLucky.java index 6615b52..48f760e 100644 --- a/src/main/java/io/github/simplex/luck/FeelingLucky.java +++ b/src/main/java/io/github/simplex/luck/FeelingLucky.java @@ -3,10 +3,9 @@ package io.github.simplex.luck; import io.github.simplex.luck.listener.*; import io.github.simplex.luck.player.PlayerConfig; import io.github.simplex.luck.player.PlayerHandler; -import io.github.simplex.luck.util.CooldownTimer; import io.github.simplex.luck.util.LuckCMD; -import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; import java.io.File; import java.util.Arrays; @@ -18,6 +17,7 @@ public final class FeelingLucky extends JavaPlugin { private final Map configMap = new HashMap<>(); private PlayerHandler handler; + private Config config; public Map getConfigMap() { return configMap; @@ -30,21 +30,26 @@ public final class FeelingLucky extends JavaPlugin { getLogger().info("Initialization complete! Attempting to register the Listeners..."); registerListeners(); getLogger().info("Registration complete! Attempting to load all player configuration files..."); - loadConfigurations(); - Bukkit.getLogger().info("Attempting to load the Luck command..."); + loadPlayerConfigurations(); + getLogger().info("Attempting to load the main configuration..."); + config = new Config(this); + getLogger().info("Main Config loaded successfully! Attempting to load the Luck command..."); new LuckCMD(this); - Bukkit.getLogger().info("Successfully loaded the Luck command!"); + getLogger().info("Successfully loaded the Luck command!"); - Bukkit.getLogger().info("Successfully initialized!"); + getLogger().info("Successfully initialized!"); } @Override public void onDisable() { - Bukkit.getLogger().info("Saving all player configurations..."); + getLogger().info("Saving all player configurations..."); configMap.values().forEach(PlayerConfig::save); + getLogger().info("Complete! Saving the main config..."); + config.save(); + getLogger().info("Complete! Goodbye! :)"); } - private void loadConfigurations() { + private void loadPlayerConfigurations() { File[] files = getDataFolder().listFiles(); if (files != null) { Arrays.stream(files).forEach(file -> { @@ -76,4 +81,10 @@ public final class FeelingLucky extends JavaPlugin { public PlayerHandler getHandler() { return handler; } + + @Override + @NotNull + public Config getConfig() { + return config; + } } diff --git a/src/main/java/io/github/simplex/luck/listener/AbstractListener.java b/src/main/java/io/github/simplex/luck/listener/AbstractListener.java new file mode 100644 index 0000000..e8afccc --- /dev/null +++ b/src/main/java/io/github/simplex/luck/listener/AbstractListener.java @@ -0,0 +1,46 @@ +package io.github.simplex.luck.listener; + +import io.github.simplex.luck.Config; +import io.github.simplex.luck.FeelingLucky; +import io.github.simplex.luck.player.PlayerHandler; +import org.bukkit.event.Listener; + +import java.util.HashMap; +import java.util.Map; + +public abstract class AbstractListener implements Listener { + protected final FeelingLucky plugin; + protected final Map listenerRarityMap = new HashMap<>(); + protected final Config config; + + public AbstractListener(FeelingLucky plugin) { + this.plugin = plugin; + this.config = plugin.getConfig(); + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + protected PlayerHandler getHandler() { + return plugin.getHandler(); + } + + public enum Rarity { + HIGH, + MED, + LOW, + NONE + } + + public boolean doesQualify(String name, double luck) { + switch (config.getRarity(name)) { + case HIGH: + if (luck < config.getChance("high_rarity_chance")) return false; + case MED: + if (luck < config.getChance("medium_rarity_chance")) return false; + case LOW: + if (luck < config.getChance("low_rarity_chance")) return false; + case NONE: + return true; + } + throw new IllegalArgumentException("The value for the listener rarity is not a recognized rarity value."); + } +} diff --git a/src/main/java/io/github/simplex/luck/listener/BlockDrops.java b/src/main/java/io/github/simplex/luck/listener/BlockDrops.java index 1267b44..68c7de1 100644 --- a/src/main/java/io/github/simplex/luck/listener/BlockDrops.java +++ b/src/main/java/io/github/simplex/luck/listener/BlockDrops.java @@ -6,22 +6,21 @@ import io.github.simplex.luck.util.SneakyWorker; 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); +public final class BlockDrops extends AbstractListener { + public BlockDrops(FeelingLucky plugin) { + super(plugin); } @EventHandler public void extraBlockDrops(BlockDropItemEvent event) { Player player = event.getPlayer(); - Luck luck = plugin.getHandler().getLuckContainer(player); + Luck luck = getHandler().getLuckContainer(player); List items = event.getItems(); - if (luck.quickRNG(luck.getPercentage())) { + if (luck.quickRNG(luck.getPercentage()) && doesQualify("block_drops", luck.getPercentage())) { items.forEach(SneakyWorker::move); } } diff --git a/src/main/java/io/github/simplex/luck/listener/BonemealFullCrop.java b/src/main/java/io/github/simplex/luck/listener/BonemealFullCrop.java index d62430f..041a2df 100644 --- a/src/main/java/io/github/simplex/luck/listener/BonemealFullCrop.java +++ b/src/main/java/io/github/simplex/luck/listener/BonemealFullCrop.java @@ -10,14 +10,13 @@ import org.bukkit.block.data.Ageable; import org.bukkit.block.data.BlockData; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; -public record BonemealFullCrop(FeelingLucky plugin) implements Listener { - public BonemealFullCrop { - plugin.getServer().getPluginManager().registerEvents(this, plugin); +public final class BonemealFullCrop extends AbstractListener { + public BonemealFullCrop(FeelingLucky plugin) { + super(plugin); } @EventHandler @@ -25,7 +24,7 @@ public record BonemealFullCrop(FeelingLucky plugin) implements Listener { Player player = event.getPlayer(); Action action = event.getAction(); ItemStack bonemeal = ItemBuilder.of(Material.BONE_MEAL).build(); - Luck luck = plugin.getHandler().getLuckContainer(player); + Luck luck = getHandler().getLuckContainer(player); ItemStack handItem = event.getItem(); if (handItem == null) return; @@ -38,12 +37,12 @@ public record BonemealFullCrop(FeelingLucky plugin) implements Listener { if (action.isRightClick() && handItem.isSimilar(bonemeal) && (data instanceof Ageable crop) - && luck.quickRNG(luck.getPercentage())) { + && luck.quickRNG(luck.getPercentage()) + && doesQualify("bonemeal", luck.getPercentage())) { crop.setAge(crop.getMaximumAge()); data.merge(crop); block.setBlockData(data); player.sendMessage(MiniComponent.info("You got lucky and your crops grew to maturity.")); } } - } diff --git a/src/main/java/io/github/simplex/luck/listener/CheatDeath.java b/src/main/java/io/github/simplex/luck/listener/CheatDeath.java index 17b3083..24eb5d7 100644 --- a/src/main/java/io/github/simplex/luck/listener/CheatDeath.java +++ b/src/main/java/io/github/simplex/luck/listener/CheatDeath.java @@ -5,26 +5,23 @@ import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.player.Luck; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; import org.bukkit.event.entity.PlayerDeathEvent; -public record CheatDeath(FeelingLucky plugin) implements Listener { +public final class CheatDeath extends AbstractListener { public CheatDeath(FeelingLucky plugin) { - this.plugin = plugin; - plugin.getServer().getPluginManager().registerEvents(this, plugin); + super(plugin); } @EventHandler public void cheatDeath(PlayerDeathEvent event) { Player player = event.getPlayer(); - Luck luck = plugin.getHandler().getLuckContainer(player); + Luck luck = getHandler().getLuckContainer(player); double absorption = Math.round(Luck.RNG().nextDouble(5.0, 10.0)); - if (luck.quickRNG(luck.getPercentage())) { + if (luck.quickRNG(luck.getPercentage()) && doesQualify("cheat_death", luck.getPercentage())) { event.setCancelled(true); player.setHealth(1.0); player.setAbsorptionAmount(absorption); player.sendMessage(MiniComponent.of("You got lucky and cheated death!").send()); } - } } diff --git a/src/main/java/io/github/simplex/luck/listener/EnchantmentBoost.java b/src/main/java/io/github/simplex/luck/listener/EnchantmentBoost.java index 09c0e77..10c93cc 100644 --- a/src/main/java/io/github/simplex/luck/listener/EnchantmentBoost.java +++ b/src/main/java/io/github/simplex/luck/listener/EnchantmentBoost.java @@ -5,16 +5,14 @@ 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 final class EnchantmentBoost extends AbstractListener { public EnchantmentBoost(FeelingLucky plugin) { - this.plugin = plugin; - plugin.getServer().getPluginManager().registerEvents(this, plugin); + super(plugin); } @EventHandler @@ -22,8 +20,8 @@ public record EnchantmentBoost(FeelingLucky plugin) implements Listener { Map enchMap = event.getEnchantsToAdd(); List enchList = enchMap.keySet().stream().toList(); Player player = event.getEnchanter(); - Luck luck = plugin.getHandler().getLuckContainer(player); - if (luck.quickRNG(luck.getPercentage())) { + Luck luck = getHandler().getLuckContainer(player); + if (luck.quickRNG(luck.getPercentage()) && doesQualify("enchanting", luck.getPercentage())) { Enchantment particular = enchList.get(Luck.RNG().nextInt(enchList.size())); int rng = Luck.RNG().nextInt(1, 5); @@ -34,4 +32,8 @@ public record EnchantmentBoost(FeelingLucky plugin) implements Listener { enchMap.replace(particular, enchMap.get(particular) + rng); } } + + public FeelingLucky plugin() { + return plugin; + } } diff --git a/src/main/java/io/github/simplex/luck/listener/ExpBoost.java b/src/main/java/io/github/simplex/luck/listener/ExpBoost.java index 3679c76..bf0309b 100644 --- a/src/main/java/io/github/simplex/luck/listener/ExpBoost.java +++ b/src/main/java/io/github/simplex/luck/listener/ExpBoost.java @@ -6,12 +6,10 @@ import io.github.simplex.luck.player.Luck; import org.bukkit.entity.ExperienceOrb; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -public record ExpBoost(FeelingLucky plugin) implements Listener { +public final class ExpBoost extends AbstractListener { public ExpBoost(FeelingLucky plugin) { - this.plugin = plugin; - plugin.getServer().getPluginManager().registerEvents(this, plugin); + super(plugin); } @EventHandler @@ -22,7 +20,7 @@ public record ExpBoost(FeelingLucky plugin) implements Listener { int rounded = Math.round(math); Player player = event.getPlayer(); Luck luck = plugin.getHandler().getLuckContainer(player); - if (luck.quickRNG(luck.getPercentage())) { + if (luck.quickRNG(luck.getPercentage()) && doesQualify("experience", luck.getPercentage())) { orb.setExperience(rounded); } } diff --git a/src/main/java/io/github/simplex/luck/listener/IllOmen.java b/src/main/java/io/github/simplex/luck/listener/IllOmen.java index e167afd..ac444f1 100644 --- a/src/main/java/io/github/simplex/luck/listener/IllOmen.java +++ b/src/main/java/io/github/simplex/luck/listener/IllOmen.java @@ -5,23 +5,22 @@ import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.player.Luck; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityPotionEffectEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; -public record IllOmen(FeelingLucky plugin) implements Listener { - public IllOmen { - plugin.getServer().getPluginManager().registerEvents(this, plugin); +public class IllOmen extends AbstractListener { + public IllOmen(FeelingLucky plugin) { + super(plugin); } @EventHandler public void reconnectCheck(PlayerJoinEvent event) { Player player = event.getPlayer(); PotionEffectType type = PotionEffectType.BAD_OMEN; - Luck luck = plugin.getHandler().getLuckContainer(player); + Luck luck = getHandler().getLuckContainer(player); if (player.hasPotionEffect(type)) { luck.cache(); diff --git a/src/main/java/io/github/simplex/luck/listener/ItemDrops.java b/src/main/java/io/github/simplex/luck/listener/ItemDrops.java index f3bf388..5c9a688 100644 --- a/src/main/java/io/github/simplex/luck/listener/ItemDrops.java +++ b/src/main/java/io/github/simplex/luck/listener/ItemDrops.java @@ -7,8 +7,8 @@ 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.EntityDeathEvent; import org.bukkit.event.entity.EntityDropItemEvent; import org.bukkit.inventory.ItemStack; @@ -16,13 +16,12 @@ import java.util.HashMap; import java.util.Map; import java.util.UUID; -public class ItemDrops implements Listener { +public class ItemDrops extends AbstractListener { private final Map entityPlayerMap = new HashMap<>(); - private final FeelingLucky plugin; + private boolean canAffect = false; public ItemDrops(FeelingLucky plugin) { - this.plugin = plugin; - plugin.getServer().getPluginManager().registerEvents(this, plugin); + super(plugin); } @EventHandler @@ -39,16 +38,17 @@ public class ItemDrops implements Listener { 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 checkForDroppedItems(EntityDeathEvent event) { + if (event.getEntity() instanceof Player) { + canAffect = false; + return; } - entityPlayerMap.put(entity.getUniqueId(), player); + canAffect = event.getDrops().isEmpty(); } @EventHandler @@ -57,12 +57,14 @@ public class ItemDrops implements Listener { if (entityPlayerMap.get(entity.getUniqueId()) == null) return; + if (!canAffect) return; + Player player = entityPlayerMap.get(entity.getUniqueId()); - Luck luck = plugin.getHandler().getLuckContainer(player); + Luck luck = getHandler().getLuckContainer(player); Item item = event.getItemDrop(); ItemStack stack = item.getItemStack(); int amount = stack.getAmount(); - if (luck.quickRNG(luck.getPercentage())) { + if (luck.quickRNG(luck.getPercentage()) && doesQualify("item_drops", luck.getPercentage())) { int rng = Luck.RNG().nextInt(2, 5); amount += rng; stack.setAmount(amount); 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 74fcf6b..10add9a 100644 --- a/src/main/java/io/github/simplex/luck/listener/PlayerListener.java +++ b/src/main/java/io/github/simplex/luck/listener/PlayerListener.java @@ -22,14 +22,12 @@ import org.bukkit.inventory.ItemStack; import java.util.Objects; -public final class PlayerListener implements Listener { - private final FeelingLucky plugin; +public final class PlayerListener extends AbstractListener { private final CooldownTimer timer; public PlayerListener(FeelingLucky plugin) { - this.plugin = plugin; + super(plugin); this.timer = new CooldownTimer(); - Bukkit.getServer().getPluginManager().registerEvents(this, plugin); } @EventHandler @@ -38,7 +36,7 @@ public final class PlayerListener implements Listener { ItemStack foot = new ItemStack(Material.RABBIT_FOOT); SpecialFootItem special = new SpecialFootItem(); Player player = event.getPlayer(); - Luck luck = plugin.getHandler().getLuckContainer(player); + Luck luck = getHandler().getLuckContainer(player); if (timer.onCooldown(player)) { player.sendMessage(MiniComponent.err("That feature can only be used once every 30 seconds.")); diff --git a/src/main/java/io/github/simplex/luck/listener/RestoreHunger.java b/src/main/java/io/github/simplex/luck/listener/RestoreHunger.java index 7f03ec6..0f9571d 100644 --- a/src/main/java/io/github/simplex/luck/listener/RestoreHunger.java +++ b/src/main/java/io/github/simplex/luck/listener/RestoreHunger.java @@ -5,27 +5,26 @@ import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.player.Luck; import io.github.simplex.luck.util.ListBox; 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); +public class RestoreHunger extends AbstractListener { + public RestoreHunger(FeelingLucky plugin) { + super(plugin); } @EventHandler public void restoreHunger(PlayerItemConsumeEvent event) { ItemStack item = event.getItem(); - Luck luck = plugin.getHandler().getLuckContainer(event.getPlayer()); + Luck luck = getHandler().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)) { + if (luck.quickRNG(percentage) && doesQualify("restore_hunger", 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 index 12e3c09..7138c6d 100644 --- a/src/main/java/io/github/simplex/luck/listener/TakeDamage.java +++ b/src/main/java/io/github/simplex/luck/listener/TakeDamage.java @@ -8,12 +8,11 @@ import net.kyori.adventure.text.Component; 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); +public class TakeDamage extends AbstractListener { + public TakeDamage(FeelingLucky plugin) { + super(plugin); } @EventHandler @@ -23,7 +22,7 @@ public record TakeDamage(FeelingLucky plugin) implements Listener { return; } Player player = (Player) event.getEntity(); - Luck luck = plugin.getHandler().getLuckContainer(player); + Luck luck = getHandler().getLuckContainer(player); if (ListBox.acceptedCauses.contains(event.getCause())) { if (luck.notDefault()) { double percentage = luck.getPercentage(); @@ -42,7 +41,7 @@ public record TakeDamage(FeelingLucky plugin) implements Listener { return; } - if (luck.quickRNG(percentage)) { + if (luck.quickRNG(percentage) && doesQualify("take_damage", percentage)) { event.setCancelled(true); player.damage(event.getDamage() / 2); player.sendMessage(Component.empty().content("You got lucky and took less damage.")); @@ -67,7 +66,7 @@ public record TakeDamage(FeelingLucky plugin) implements Listener { return; } - if (luck.quickRNG(percentage)) { + if (luck.quickRNG(percentage) && doesQualify("take_damage", percentage)) { event.setCancelled(true); player.getActivePotionEffects().removeIf(p -> ListBox.potionEffects.contains(p.getType())); player.setFireTicks(0); diff --git a/src/main/java/io/github/simplex/luck/listener/UnbreakableTool.java b/src/main/java/io/github/simplex/luck/listener/UnbreakableTool.java index 12ad1a0..430b643 100644 --- a/src/main/java/io/github/simplex/luck/listener/UnbreakableTool.java +++ b/src/main/java/io/github/simplex/luck/listener/UnbreakableTool.java @@ -6,15 +6,14 @@ import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.player.Luck; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; import org.bukkit.event.inventory.CraftItemEvent; import org.bukkit.inventory.CraftingInventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; -public record UnbreakableTool(FeelingLucky plugin) implements Listener { - public UnbreakableTool { - plugin.getServer().getPluginManager().registerEvents(this, plugin); +public class UnbreakableTool extends AbstractListener { + public UnbreakableTool(FeelingLucky plugin) { + super(plugin); } @EventHandler @@ -27,8 +26,8 @@ public record UnbreakableTool(FeelingLucky plugin) implements Listener { if (ItemBuilder.isTool(stack.getType())) { if (event.getWhoClicked() instanceof Player player) { - Luck luck = plugin.getHandler().getLuckContainer(player); - if (luck.quickRNG(luck.getPercentage())) { + Luck luck = getHandler().getLuckContainer(player); + if (luck.quickRNG(luck.getPercentage()) && doesQualify("unbreakable", luck.getPercentage())) { meta.setUnbreakable(true); stack.setItemMeta(meta); inventory.setResult(stack); diff --git a/src/main/java/io/github/simplex/luck/listener/VillagerInventory.java b/src/main/java/io/github/simplex/luck/listener/VillagerInventory.java index 025cbe5..d0752ef 100644 --- a/src/main/java/io/github/simplex/luck/listener/VillagerInventory.java +++ b/src/main/java/io/github/simplex/luck/listener/VillagerInventory.java @@ -15,14 +15,12 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -public class VillagerInventory implements Listener { +public class VillagerInventory extends AbstractListener { private final SpecialFootItem foot = new SpecialFootItem(); private final MerchantRecipe recipe = new MerchantRecipe(foot.get(), 0, 2, true); - private final FeelingLucky plugin; - public VillagerInventory(FeelingLucky plugin) { - this.plugin = plugin; + super(plugin); recipe.setIngredients(Arrays.asList( ItemBuilder.of(Material.EMERALD).build(), @@ -32,8 +30,6 @@ public class VillagerInventory implements Listener { recipe.setPriceMultiplier(1.25F); recipe.setVillagerExperience(25); recipe.setSpecialPrice(4); - - plugin.getServer().getPluginManager().registerEvents(this, plugin); } @EventHandler 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 76d660d..59bc2dd 100644 --- a/src/main/java/io/github/simplex/luck/player/PlayerConfig.java +++ b/src/main/java/io/github/simplex/luck/player/PlayerConfig.java @@ -2,6 +2,8 @@ package io.github.simplex.luck.player; import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.util.SneakyWorker; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; import org.bukkit.attribute.Attribute; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; @@ -11,16 +13,18 @@ import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.nio.charset.StandardCharsets; +import java.util.UUID; public class PlayerConfig { private final File configFile; - private final FeelingLucky plugin; + private final OfflinePlayer player; private volatile YamlConfiguration config; @SuppressWarnings("ResultOfMethodCallIgnored") public PlayerConfig(FeelingLucky plugin, Player player) { - this.plugin = plugin; - File dataFolder = plugin.getDataFolder(); + this.player = player; + if (!plugin.getDataFolder().exists()) plugin.getDataFolder().mkdirs(); + File dataFolder = new File(plugin.getDataFolder(), "players"); if (!dataFolder.exists()) dataFolder.mkdirs(); File file = new File(dataFolder, player.getUniqueId() + ".yml"); if (!file.exists()) { @@ -54,8 +58,8 @@ public class PlayerConfig { } protected PlayerConfig(FeelingLucky plugin, File file) { - this.plugin = plugin; this.configFile = file; + this.player = Bukkit.getOfflinePlayer(UUID.fromString(file.getName().split("\\.")[0])); config = YamlConfiguration.loadConfiguration(configFile); } @@ -77,6 +81,10 @@ public class PlayerConfig { load(); } + public OfflinePlayer getPlayer() { + return player; + } + public void setUsername(String name) { config.set("username", name); save(); diff --git a/src/main/java/io/github/simplex/luck/util/LuckCMD.java b/src/main/java/io/github/simplex/luck/util/LuckCMD.java index e68842f..3f1b7e2 100644 --- a/src/main/java/io/github/simplex/luck/util/LuckCMD.java +++ b/src/main/java/io/github/simplex/luck/util/LuckCMD.java @@ -16,6 +16,7 @@ import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; +import java.util.UUID; public class LuckCMD extends Command implements TabCompleter { private final FeelingLucky plugin; @@ -33,6 +34,19 @@ public class LuckCMD extends Command implements TabCompleter { if (args.length == 3) { if ((sender instanceof ConsoleCommandSender) || sender.hasPermission("luck.admin")) { + if (args[0].equalsIgnoreCase("reload") && args[1].equalsIgnoreCase("-p")) { + Player player = Bukkit.getPlayer(args[2]); + if (player == null) { + sender.sendMessage(Messages.NO_PLAYER.get()); + return true; + } + UUID uuid = player.getUniqueId(); + PlayerConfig config = plugin.getConfigMap().get(uuid); + config.reload(); + sender.sendMessage(MiniComponent.info("Successfully reloaded " + player.getName() + "'s configuration file.")); + return true; + } + Player player = Bukkit.getPlayer(args[1]); double amount = Double.parseDouble(args[2]); @@ -80,6 +94,10 @@ public class LuckCMD extends Command implements TabCompleter { if (args.length == 2) { if ((sender instanceof ConsoleCommandSender) || sender.hasPermission("luck.admin")) { + if (args[0].equalsIgnoreCase("reload") && args[1].equalsIgnoreCase("-m")) { + + } + if (args[0].equalsIgnoreCase("info")) { Player player = Bukkit.getPlayer(args[1]); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..5100bf2 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,26 @@ +# These entries are for the minimum amount of luck required for the event to actually trigger. +# These work in addition to the RNG generator, +# meaning both the RNG and this condition must be met for the event to trigger. +# These values must be in the form of doubles, as listed below. +# The maximum amount of luck that can be attributed is 1024.0, and the minimum is -1024.0 + +high_rarity_chance: 512 +medium_rarity_chance: 128 +low_rarity_chance: 64 + +# The following entries are for the rarity level of each event trigger. +# This will determine which rarity chance to use which ensures players +# The following values are accepted: NONE, LOW, MED, HIGH +# - None implies that there is no rarity chance attributed to that feature. +# These entries are case-sensitive. + +block_drops: LOW +bonemeal: LOW +cheat_death: MED +enchanting: HIGH +experience: HIGH +item_drops: LOW +random_effect: HIGH +restore_hunger: MED +take_damage: MED +unbreakable: HIGH \ No newline at end of file