From 0488e1d6b1fb2b0c1a169c38866fc9a415854fdd Mon Sep 17 00:00:00 2001 From: Paldiu Date: Sun, 24 Apr 2022 19:54:40 -0500 Subject: [PATCH] [Beta] SNAPSHOT {Bug Fix} - Patch 0001 Changelog: - Fixed an issue where the integrity checker for the main config would delete the entire data folder if the config was corrupted. - Fixed an issue where the Rarity check was not working as intended; it would either return false if any Rarity other than NONE was set, and throw a new IllegalArgumentException if the switch clause exited with no return value. - Fixed an issue where the command /luck reload -m did absolutely nothing. - Fixed an issue where the proper command arguments were not being Tab Completed. --- build.gradle | 2 +- .../java/io/github/simplex/luck/Config.java | 36 +++++++++---------- .../io/github/simplex/luck/FeelingLucky.java | 2 +- .../luck/listener/AbstractListener.java | 27 +++++--------- .../simplex/luck/listener/ItemDrops.java | 8 ++--- .../simplex/luck/listener/PlayerListener.java | 3 -- .../luck/listener/VillagerInventory.java | 1 - .../simplex/luck/util/CooldownTimer.java | 5 +-- .../io/github/simplex/luck/util/LuckCMD.java | 27 ++++++++------ src/main/resources/config.yml | 6 ++-- 10 files changed, 53 insertions(+), 64 deletions(-) diff --git a/build.gradle b/build.gradle index 3f24ddc..9408900 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ plugins { } group = 'io.github.simplex' -version = 'Alpha-1.0' +version = 'Beta-20220422-SNAPSHOT' repositories { mavenCentral() diff --git a/src/main/java/io/github/simplex/luck/Config.java b/src/main/java/io/github/simplex/luck/Config.java index e2c36a3..0619ddf 100644 --- a/src/main/java/io/github/simplex/luck/Config.java +++ b/src/main/java/io/github/simplex/luck/Config.java @@ -1,6 +1,5 @@ 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; @@ -9,11 +8,26 @@ 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; +@SuppressWarnings("ResultOfMethodCallIgnored") public class Config extends YamlConfiguration { private final FeelingLucky plugin; + private final 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"); + }}; private File configFile; public Config(FeelingLucky plugin) { @@ -33,7 +47,7 @@ public class Config extends YamlConfiguration { if (validateIntegrity()) { File newFile = new File(plugin.getDataFolder(), "config.yml"); SneakyWorker.sneakyTry(() -> { - Files.delete(Path.of(plugin.getDataFolder().getPath())); + Files.delete(Path.of(this.configFile.getPath())); newFile.createNewFile(); plugin.saveResource("config.yml", true); }); @@ -72,20 +86,4 @@ public class Config extends YamlConfiguration { 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 48f760e..e667d4b 100644 --- a/src/main/java/io/github/simplex/luck/FeelingLucky.java +++ b/src/main/java/io/github/simplex/luck/FeelingLucky.java @@ -81,7 +81,7 @@ public final class FeelingLucky extends JavaPlugin { public PlayerHandler getHandler() { return handler; } - + @Override @NotNull public Config getConfig() { diff --git a/src/main/java/io/github/simplex/luck/listener/AbstractListener.java b/src/main/java/io/github/simplex/luck/listener/AbstractListener.java index e8afccc..7195a05 100644 --- a/src/main/java/io/github/simplex/luck/listener/AbstractListener.java +++ b/src/main/java/io/github/simplex/luck/listener/AbstractListener.java @@ -5,12 +5,8 @@ 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) { @@ -23,24 +19,19 @@ public abstract class AbstractListener implements Listener { return plugin.getHandler(); } + public boolean doesQualify(String name, double luck) { + return switch (config.getRarity(name)) { + case HIGH -> luck > config.getChance("high_rarity_chance"); + case MED -> luck > config.getChance("medium_rarity_chance"); + case LOW -> luck > config.getChance("low_rarity_chance"); + case NONE -> true; + }; + } + 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/ItemDrops.java b/src/main/java/io/github/simplex/luck/listener/ItemDrops.java index 5c9a688..154eda6 100644 --- a/src/main/java/io/github/simplex/luck/listener/ItemDrops.java +++ b/src/main/java/io/github/simplex/luck/listener/ItemDrops.java @@ -2,10 +2,10 @@ package io.github.simplex.luck.listener; import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.player.Luck; -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.Item; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDeathEvent; 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 10add9a..fd9b4e1 100644 --- a/src/main/java/io/github/simplex/luck/listener/PlayerListener.java +++ b/src/main/java/io/github/simplex/luck/listener/PlayerListener.java @@ -5,15 +5,12 @@ import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.player.Luck; import io.github.simplex.luck.util.CooldownTimer; import io.github.simplex.luck.util.SpecialFootItem; -import net.kyori.adventure.text.Component; -import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Entity; import org.bukkit.entity.Guardian; 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.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageEvent; 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 d0752ef..b2dc5ae 100644 --- a/src/main/java/io/github/simplex/luck/listener/VillagerInventory.java +++ b/src/main/java/io/github/simplex/luck/listener/VillagerInventory.java @@ -7,7 +7,6 @@ import io.github.simplex.luck.util.SpecialFootItem; import org.bukkit.Material; import org.bukkit.entity.Villager; import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerInteractAtEntityEvent; import org.bukkit.inventory.MerchantRecipe; diff --git a/src/main/java/io/github/simplex/luck/util/CooldownTimer.java b/src/main/java/io/github/simplex/luck/util/CooldownTimer.java index 3f938a2..86379bb 100644 --- a/src/main/java/io/github/simplex/luck/util/CooldownTimer.java +++ b/src/main/java/io/github/simplex/luck/util/CooldownTimer.java @@ -1,7 +1,5 @@ package io.github.simplex.luck.util; -import io.github.simplex.lib.MiniComponent; -import net.kyori.adventure.text.Component; import org.bukkit.entity.Player; import java.util.HashMap; @@ -10,9 +8,8 @@ import java.util.UUID; import java.util.concurrent.TimeUnit; public class CooldownTimer { - private final Map cooldowns = new HashMap<>(); - public static final long DEFAULT_COOLDOWN = 30L; + private final Map cooldowns = new HashMap<>(); public void setCooldown(UUID playerUUID, long time) { if (time < 1) { 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 3f1b7e2..3e183d8 100644 --- a/src/main/java/io/github/simplex/luck/util/LuckCMD.java +++ b/src/main/java/io/github/simplex/luck/util/LuckCMD.java @@ -95,7 +95,9 @@ 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")) { - + plugin.getConfig().reload(); + sender.sendMessage(MiniComponent.info("Configuration successfully reloaded.")); + return true; } if (args[0].equalsIgnoreCase("info")) { @@ -163,23 +165,22 @@ public class LuckCMD extends Command implements TabCompleter { List playerNames = new ArrayList<>() {{ Bukkit.getOnlinePlayers().forEach(p -> add(p.getName())); }}; - List adminCommands = List.of("set", "reset", "give", "take"); + List adminCommands = List.of("set", "reset", "give", "take", "reload"); if ((sender instanceof ConsoleCommandSender) || sender.hasPermission("luck.admin")) { completions.addAll(adminCommands); - return completions; + return completions.stream().filter(n -> n.startsWith(args[0])).toList(); } - if (args[0].equalsIgnoreCase("info") && sender.hasPermission("luck.admin")) { - return playerNames; - } - - if (completions.contains(args[1]) && sender.hasPermission("luck.admin")) { + if (adminCommands.contains(args[0]) + && sender.hasPermission("luck.admin") + && (args.length == 2)) { switch (args[0]) { case "info": case "reset": + return playerNames.stream().filter(n -> n.startsWith(args[1])).toList(); case "reload": - return new ArrayList<>(); + return List.of("-m", "-p"); case "give": case "take": case "set": @@ -187,6 +188,12 @@ public class LuckCMD extends Command implements TabCompleter { } } - return completions; + if (args[0].equalsIgnoreCase("reload") + && args[1].equalsIgnoreCase("-p") + && sender.hasPermission("luck.admin") && (args.length == 3)) { + return playerNames.stream().filter(n -> n.startsWith(args[2])).toList(); + } + + return completions.stream().filter(n -> n.startsWith(args[0])).toList(); } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 5100bf2..b3924f2 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -4,9 +4,9 @@ # 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 +high_rarity_chance: 512.0 +medium_rarity_chance: 128.0 +low_rarity_chance: 64.0 # The following entries are for the rarity level of each event trigger. # This will determine which rarity chance to use which ensures players