diff --git a/.gradle/7.3.3/checksums/checksums.lock b/.gradle/7.3.3/checksums/checksums.lock index 7259438..6d35531 100644 Binary files a/.gradle/7.3.3/checksums/checksums.lock and b/.gradle/7.3.3/checksums/checksums.lock differ diff --git a/.gradle/7.3.3/executionHistory/executionHistory.bin b/.gradle/7.3.3/executionHistory/executionHistory.bin index 20822a5..19db05e 100644 Binary files a/.gradle/7.3.3/executionHistory/executionHistory.bin and b/.gradle/7.3.3/executionHistory/executionHistory.bin differ diff --git a/.gradle/7.3.3/executionHistory/executionHistory.lock b/.gradle/7.3.3/executionHistory/executionHistory.lock index 88d784a..8472ed4 100644 Binary files a/.gradle/7.3.3/executionHistory/executionHistory.lock and b/.gradle/7.3.3/executionHistory/executionHistory.lock differ diff --git a/.gradle/7.3.3/fileHashes/fileHashes.bin b/.gradle/7.3.3/fileHashes/fileHashes.bin index 67da483..0a10393 100644 Binary files a/.gradle/7.3.3/fileHashes/fileHashes.bin and b/.gradle/7.3.3/fileHashes/fileHashes.bin differ diff --git a/.gradle/7.3.3/fileHashes/fileHashes.lock b/.gradle/7.3.3/fileHashes/fileHashes.lock index 4d7883e..6298bcd 100644 Binary files a/.gradle/7.3.3/fileHashes/fileHashes.lock and b/.gradle/7.3.3/fileHashes/fileHashes.lock differ diff --git a/.gradle/7.3.3/fileHashes/resourceHashesCache.bin b/.gradle/7.3.3/fileHashes/resourceHashesCache.bin index bc8b179..e222252 100644 Binary files a/.gradle/7.3.3/fileHashes/resourceHashesCache.bin and b/.gradle/7.3.3/fileHashes/resourceHashesCache.bin differ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index 06f8ed1..d1bf79c 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/.gradle/file-system.probe b/.gradle/file-system.probe index 9dc1248..a9dd5bc 100644 Binary files a/.gradle/file-system.probe and b/.gradle/file-system.probe differ diff --git a/build.gradle b/build.gradle index 978c740..34c2519 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ plugins { } group = 'io.github.simplex' -version = '1.0-SNAPSHOT' +version = '1.0-RC02' repositories { mavenCentral() @@ -29,6 +29,9 @@ java { if (JavaVersion.current() < javaVersion) { toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion) } + + withSourcesJar(); + withJavadocJar(); } tasks.withType(JavaCompile).configureEach { diff --git a/build/resources/main/plugin.yml b/build/resources/main/plugin.yml index cb93d23..8a5d590 100644 --- a/build/resources/main/plugin.yml +++ b/build/resources/main/plugin.yml @@ -1,4 +1,5 @@ -name: Crumb +name: FeelingLucky version: '1.0-SNAPSHOT' -main: io.github.simplex.crumb.Crumb +author: SimplexDevelopment +main: io.github.simplex.luck.FeelingLucky api-version: 1.18 diff --git a/build/tmp/compileJava/previous-compilation-data.bin b/build/tmp/compileJava/previous-compilation-data.bin index fcb7d76..1d6cbb5 100644 Binary files a/build/tmp/compileJava/previous-compilation-data.bin and b/build/tmp/compileJava/previous-compilation-data.bin differ diff --git a/src/main/java/io/github/simplex/lib/Messages.java b/src/main/java/io/github/simplex/lib/Messages.java new file mode 100644 index 0000000..35e3825 --- /dev/null +++ b/src/main/java/io/github/simplex/lib/Messages.java @@ -0,0 +1,35 @@ +package io.github.simplex.lib; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.TextColor; +import net.kyori.adventure.text.format.TextDecoration; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public enum Messages { + + NOT_FROM_CONSOLE(builder("This command may only be used in game.", null, null)), + NO_PERMISSION(builder("You do not have permission to use this command.", TextColor.color(255, 0, 0), TextDecoration.ITALIC)); + + private final Component message; + + Messages(Component message) { + this.message = message; + } + + public Component get() { + return message; + } + + private static Component builder(@NotNull String message, @Nullable TextColor color, @Nullable TextDecoration decoration) { + if (color == null) { + if (decoration == null) return Component.empty().content(message); + + return Component.empty().content(message).decoration(decoration, TextDecoration.State.TRUE); + } + + if (decoration == null) return Component.empty().content(message).color(color); + + return Component.empty().content(message).color(color).decoration(decoration, TextDecoration.State.TRUE); + } +} diff --git a/src/main/java/io/github/simplex/luck/FeelingLucky.java b/src/main/java/io/github/simplex/luck/FeelingLucky.java index 92e56e3..7fa6f4a 100644 --- a/src/main/java/io/github/simplex/luck/FeelingLucky.java +++ b/src/main/java/io/github/simplex/luck/FeelingLucky.java @@ -13,6 +13,7 @@ import java.util.UUID; public final class FeelingLucky extends JavaPlugin { private static final Map configMap = new HashMap<>(); + public LuckCMD cmd; public PlayerHandler handler; public static Map getConfigMap() { @@ -39,6 +40,10 @@ public final class FeelingLucky extends JavaPlugin { getLogger().info("There are no player configurations to load."); } + Bukkit.getLogger().info("Attempting to load the Luck command..."); + cmd = new LuckCMD(this); + Bukkit.getLogger().info("Successfully loaded the Luck command!"); + Bukkit.getLogger().info("Successfully initialized!"); } diff --git a/src/main/java/io/github/simplex/luck/LuckCMD.java b/src/main/java/io/github/simplex/luck/LuckCMD.java new file mode 100644 index 0000000..3d44d8f --- /dev/null +++ b/src/main/java/io/github/simplex/luck/LuckCMD.java @@ -0,0 +1,158 @@ +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; +import org.bukkit.Bukkit; +import org.bukkit.command.*; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; + +public class LuckCMD extends Command implements TabCompleter { + public LuckCMD(FeelingLucky plugin) { + super("luck", "FeelingLucky main command.", "/ [player] [amount]", List.of()); + setPermission("luck.default"); + plugin.getServer().getCommandMap().register("luck", "FeelingLucky", this); + } + + @Override + public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) { + if (args.length < 1 || args.length > 3) return false; + + if (args.length == 3) { + if ((sender instanceof ConsoleCommandSender) || sender.hasPermission("luck.admin")) { + Player player = Bukkit.getPlayer(args[1]); + double amount = Double.parseDouble(args[2]); + + if (player == null) { + sender.sendMessage(Component.empty().content("That player cannot be found.")); + return true; + } + + Luck luck = PlayerHandler.getLuckContainer(player); + PlayerConfig config = FeelingLucky.getConfigMap().get(player.getUniqueId()); + + switch (args[0]) { + case "set" -> { + luck.setValue(amount); + PlayerHandler.updatePlayer(player, luck); + config.setLuck(luck.baseValue()); + 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()); + 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()); + sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat.")); + return true; + } + } + } else { + sender.sendMessage(Messages.NO_PERMISSION.get()); + } + } + + if (args.length == 2) { + if ((sender instanceof ConsoleCommandSender) || sender.hasPermission("luck.admin")) { + if (args[0].equalsIgnoreCase("info")) { + Player player = Bukkit.getPlayer(args[1]); + + if (player == null) { + sender.sendMessage("That player cannot be found."); + return true; + } + + Luck luck = PlayerHandler.getLuckContainer(player); + sender.sendMessage(Component.empty().content("Luck stat for " + args[1] + ": " + luck.baseValue())); + return true; + } + + if (args[0].equalsIgnoreCase("reset")) { + Player player = Bukkit.getPlayer(args[1]); + + if (player == null) { + sender.sendMessage(Component.empty().content("That player cannot be found.")); + return true; + } + + Luck luck = PlayerHandler.getLuckContainer(player); + PlayerConfig config = FeelingLucky.getConfigMap().get(player.getUniqueId()); + luck.reset(); + PlayerHandler.updatePlayer(player, luck); + config.setLuck(luck.baseValue()); + sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat.")); + return true; + } + } else { + sender.sendMessage(Messages.NO_PERMISSION.get()); + return true; + } + } + + if (args.length == 1) { + if ((sender instanceof Player player) && player.hasPermission("luck.default")) { + if (args[0].equalsIgnoreCase("info")) { + Luck luck = PlayerHandler.getLuckContainer(player); + TextComponent c = Component.text("Your Luck: " + luck.getPercentage()); + player.sendMessage(c.color(TextColor.color(0, 255, 0))); + return true; + } + } else if (sender instanceof ConsoleCommandSender) { + sender.sendMessage(Messages.NOT_FROM_CONSOLE.get()); + return true; + } + } + + return true; + } + + @Override + public @Nullable List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) { + List completions = new ArrayList<>() {{ + add("info"); + }}; + List playerNames = new ArrayList<>() {{ + Bukkit.getOnlinePlayers().forEach(p -> add(p.getName())); + }}; + List adminCommands = List.of("set", "reset", "give", "take"); + + if ((sender instanceof ConsoleCommandSender) || sender.hasPermission("luck.admin")) { + completions.addAll(adminCommands); + return completions; + } + + if (args[0].equalsIgnoreCase("info") && sender.hasPermission("luck.admin")) { + return playerNames; + } + + if (completions.contains(args[1]) && sender.hasPermission("luck.admin")) { + switch (args[0]) { + case "info": + case "reset": + return new ArrayList<>(); + case "give": + case "take": + case "set": + return List.of("amount"); + } + } + + return completions; + } +} 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 e181fe1..d4598bb 100644 --- a/src/main/java/io/github/simplex/luck/player/Luck.java +++ b/src/main/java/io/github/simplex/luck/player/Luck.java @@ -89,6 +89,10 @@ public class Luck implements LuckContainer { return (percentage >= rng); } + public void reset() { + setValue(defaultValue()); + } + @Override public double baseValue() { return BASE_VALUE; @@ -98,7 +102,7 @@ public class Luck implements LuckContainer { return player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue(); } - protected void setValue(double value) { + public void setValue(double value) { player.getAttribute(Attribute.GENERIC_LUCK).setBaseValue(value); Bukkit.getPluginManager().callEvent(event); } 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 035525a..cbdb8be 100644 --- a/src/main/java/io/github/simplex/luck/player/PlayerConfig.java +++ b/src/main/java/io/github/simplex/luck/player/PlayerConfig.java @@ -11,7 +11,7 @@ import java.io.File; import java.io.FileWriter; import java.nio.charset.StandardCharsets; -public class PlayerConfig extends YamlConfiguration { +public class PlayerConfig { private final File configFile; private volatile YamlConfiguration config; @@ -38,7 +38,7 @@ public class PlayerConfig extends YamlConfiguration { }); } configFile = file; - config = loadConfiguration(configFile); + config = YamlConfiguration.loadConfiguration(configFile); String tempUsername = config.getString("username"); @@ -52,7 +52,7 @@ public class PlayerConfig extends YamlConfiguration { protected PlayerConfig(File file) { this.configFile = file; - config = loadConfiguration(configFile); + config = YamlConfiguration.loadConfiguration(configFile); } @Contract("_ -> new") @@ -65,7 +65,13 @@ public class PlayerConfig extends YamlConfiguration { } public void load() { - SneakyWorker.sneakyTry(() -> config = loadConfiguration(configFile)); + + SneakyWorker.sneakyTry(() -> config = YamlConfiguration.loadConfiguration(configFile)); + } + + public void setLuck(double luck) { + config.set("luck", luck); + save(); } public YamlConfiguration getConfig() { 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 4487b40..6e8f7a2 100644 --- a/src/main/java/io/github/simplex/luck/player/PlayerHandler.java +++ b/src/main/java/io/github/simplex/luck/player/PlayerHandler.java @@ -57,7 +57,7 @@ public record PlayerHandler(FeelingLucky plugin) implements Listener { playerLuckMap.put(player, container); } - public void updatePlayer(Player player, Luck luck) { + public static void updatePlayer(Player player, Luck luck) { playerLuckMap.replace(player, luck); } }