Alpha 1.0 RC02

Changelog:
  - Some visibility changes (Developers)
  - Added the Luck command.
  - Added some extra backend shortcuts
This commit is contained in:
Paldiu 2022-03-31 19:30:10 -05:00
parent 73affcf4c4
commit 93ee1e2c43
17 changed files with 221 additions and 9 deletions

Binary file not shown.

View File

@ -3,7 +3,7 @@ plugins {
} }
group = 'io.github.simplex' group = 'io.github.simplex'
version = '1.0-SNAPSHOT' version = '1.0-RC02'
repositories { repositories {
mavenCentral() mavenCentral()
@ -29,6 +29,9 @@ java {
if (JavaVersion.current() < javaVersion) { if (JavaVersion.current() < javaVersion) {
toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion) toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion)
} }
withSourcesJar();
withJavadocJar();
} }
tasks.withType(JavaCompile).configureEach { tasks.withType(JavaCompile).configureEach {

View File

@ -1,4 +1,5 @@
name: Crumb name: FeelingLucky
version: '1.0-SNAPSHOT' version: '1.0-SNAPSHOT'
main: io.github.simplex.crumb.Crumb author: SimplexDevelopment
main: io.github.simplex.luck.FeelingLucky
api-version: 1.18 api-version: 1.18

View File

@ -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);
}
}

View File

@ -13,6 +13,7 @@ import java.util.UUID;
public final class FeelingLucky extends JavaPlugin { public final class FeelingLucky extends JavaPlugin {
private static final Map<UUID, PlayerConfig> configMap = new HashMap<>(); private static final Map<UUID, PlayerConfig> configMap = new HashMap<>();
public LuckCMD cmd;
public PlayerHandler handler; public PlayerHandler handler;
public static Map<UUID, PlayerConfig> getConfigMap() { public static Map<UUID, PlayerConfig> getConfigMap() {
@ -39,6 +40,10 @@ public final class FeelingLucky extends JavaPlugin {
getLogger().info("There are no player configurations to load."); 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!"); Bukkit.getLogger().info("Successfully initialized!");
} }

View File

@ -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.", "/<command> <info | set | reset | give | take> [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<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
List<String> completions = new ArrayList<>() {{
add("info");
}};
List<String> playerNames = new ArrayList<>() {{
Bukkit.getOnlinePlayers().forEach(p -> add(p.getName()));
}};
List<String> 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;
}
}

View File

@ -89,6 +89,10 @@ public class Luck implements LuckContainer {
return (percentage >= rng); return (percentage >= rng);
} }
public void reset() {
setValue(defaultValue());
}
@Override @Override
public double baseValue() { public double baseValue() {
return BASE_VALUE; return BASE_VALUE;
@ -98,7 +102,7 @@ public class Luck implements LuckContainer {
return player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue(); return player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue();
} }
protected void setValue(double value) { public void setValue(double value) {
player.getAttribute(Attribute.GENERIC_LUCK).setBaseValue(value); player.getAttribute(Attribute.GENERIC_LUCK).setBaseValue(value);
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
} }

View File

@ -11,7 +11,7 @@ import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
public class PlayerConfig extends YamlConfiguration { public class PlayerConfig {
private final File configFile; private final File configFile;
private volatile YamlConfiguration config; private volatile YamlConfiguration config;
@ -38,7 +38,7 @@ public class PlayerConfig extends YamlConfiguration {
}); });
} }
configFile = file; configFile = file;
config = loadConfiguration(configFile); config = YamlConfiguration.loadConfiguration(configFile);
String tempUsername = config.getString("username"); String tempUsername = config.getString("username");
@ -52,7 +52,7 @@ public class PlayerConfig extends YamlConfiguration {
protected PlayerConfig(File file) { protected PlayerConfig(File file) {
this.configFile = file; this.configFile = file;
config = loadConfiguration(configFile); config = YamlConfiguration.loadConfiguration(configFile);
} }
@Contract("_ -> new") @Contract("_ -> new")
@ -65,7 +65,13 @@ public class PlayerConfig extends YamlConfiguration {
} }
public void load() { 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() { public YamlConfiguration getConfig() {

View File

@ -57,7 +57,7 @@ public record PlayerHandler(FeelingLucky plugin) implements Listener {
playerLuckMap.put(player, container); playerLuckMap.put(player, container);
} }
public void updatePlayer(Player player, Luck luck) { public static void updatePlayer(Player player, Luck luck) {
playerLuckMap.replace(player, luck); playerLuckMap.replace(player, luck);
} }
} }