4 Commits

Author SHA1 Message Date
247a2fafc0 Bug Fix 0001
**Changelog**:
 - Fixed a bug where the PlayerListener was not being initialized
 - Included the registration for the listener inside the constructor
 - Dropped static import in favor of class parent access
 - Added to the Luck method "setValue(double)" to automatically update the configuration file when called.
 - Adjusted Luck to utilize all 1024 units when calculating the boolean for rng percentage.
2022-04-02 09:00:40 -05:00
ffdafda69c Changed Project Settings 2022-04-01 07:51:58 -05:00
0383bfeaf3 Alpha 1.0 RC02 PATCH 01
Fixed an issue where initializing a new PlayerConfig would attempt to read a non existent value from the Luck Container list.
2022-03-31 19:42:18 -05:00
93ee1e2c43 Alpha 1.0 RC02
Changelog:
  - Some visibility changes (Developers)
  - Added the Luck command.
  - Added some extra backend shortcuts
2022-03-31 19:30:10 -05:00
24 changed files with 271 additions and 40 deletions

Binary file not shown.

1
.idea/.name generated Normal file
View File

@ -0,0 +1 @@
FeelingLucky

View File

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

View File

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

View File

@ -1 +1 @@
rootProject.name = 'Crumb'
rootProject.name = 'FeelingLucky'

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

@ -1,5 +1,6 @@
package io.github.simplex.luck;
import io.github.simplex.luck.listener.PlayerListener;
import io.github.simplex.luck.player.PlayerConfig;
import io.github.simplex.luck.player.PlayerHandler;
import org.bukkit.Bukkit;
@ -13,7 +14,9 @@ import java.util.UUID;
public final class FeelingLucky extends JavaPlugin {
private static final Map<UUID, PlayerConfig> configMap = new HashMap<>();
public LuckCMD cmd;
public PlayerHandler handler;
public PlayerListener playerListener;
public static Map<UUID, PlayerConfig> getConfigMap() {
return configMap;
@ -23,8 +26,8 @@ public final class FeelingLucky extends JavaPlugin {
public void onEnable() {
Bukkit.getLogger().info("Initializing the PlayerHandler...");
handler = new PlayerHandler(this);
Bukkit.getLogger().info("Initialization complete! Attempting to register the handler...");
this.getServer().getPluginManager().registerEvents(handler, 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...");
File[] files = getDataFolder().listFiles();
@ -39,6 +42,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!");
}

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

@ -7,6 +7,7 @@ import io.github.simplex.luck.SneakyWorker;
import io.github.simplex.luck.player.Luck;
import io.github.simplex.luck.player.PlayerHandler;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
@ -17,7 +18,6 @@ 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.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.EntityDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
@ -31,9 +31,12 @@ import java.util.Map;
import java.util.UUID;
public record PlayerListener(FeelingLucky plugin) implements Listener {
private static final Map<UUID, Player> entityPlayerMap = new HashMap<>();
public PlayerListener {
Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
}
@EventHandler
public void takeDamage(EntityDamageEvent event) {
Entity entity = event.getEntity();
@ -187,7 +190,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
public void witchesBrew(EntityDamageByEntityEvent event) {
Entity eTEMP = event.getDamager();
Entity pTEMP = event.getEntity();
DamageCause cause = event.getCause();
EntityDamageEvent.DamageCause cause = event.getCause();
if (!(pTEMP instanceof Player player)) {
return;
@ -198,10 +201,10 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
}
Luck luck = PlayerHandler.getLuckContainer(player);
if (cause.equals(DamageCause.MAGIC) || cause.equals(DamageCause.POISON)) {
if (cause.equals(EntityDamageEvent.DamageCause.MAGIC) || cause.equals(EntityDamageEvent.DamageCause.POISON)) {
if (luck.quickRNG(33.0)) {
luck.takeFrom(5.0);
plugin.handler.updatePlayer(player, luck);
PlayerHandler.updatePlayer(player, luck);
}
}
}

View File

@ -1,6 +1,7 @@
package io.github.simplex.luck.player;
import io.github.simplex.api.LuckContainer;
import io.github.simplex.luck.FeelingLucky;
import org.bukkit.Bukkit;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.Player;
@ -36,12 +37,14 @@ public class Luck implements LuckContainer {
public static boolean quickRNG2(double percentage) {
double rng;
if (percentage >= 100.0) {
rng = 100.0; // 100% chance to trigger, obviously;
rng = 1024.0; // 100% chance to trigger, obviously;
} else {
rng = RNG().nextDouble(0.0, 99.0);
rng = RNG().nextDouble(0.0, 1024.0);
}
return (percentage >= rng);
double actual = (rng / 1024.0) * 100;
return (percentage >= actual);
}
@Override
@ -77,16 +80,22 @@ public class Luck implements LuckContainer {
public boolean quickRNG(double percentage) {
double rng;
if (percentage >= 100.0) {
rng = 100.0; // 100% chance to trigger, obviously;
rng = 1024.0; // 100% chance to trigger, obviously;
} else {
rng = RNG().nextDouble(0.0, 99.0);
rng = RNG().nextDouble(0.0, 1024.0);
}
double actual = (rng / 1024) * 100;
if (multiplier() > 1.0) {
return ((percentage * multiplier()) >= rng);
return ((percentage * multiplier()) >= actual);
}
return (percentage >= rng);
return (percentage >= actual);
}
public void reset() {
setValue(defaultValue());
}
@Override
@ -98,8 +107,9 @@ 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);
FeelingLucky.getConfigMap().get(associatedPlayer().getUniqueId()).setLuck(value);
Bukkit.getPluginManager().callEvent(event);
}

View File

@ -2,6 +2,7 @@ package io.github.simplex.luck.player;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.SneakyWorker;
import org.bukkit.attribute.Attribute;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Contract;
@ -11,7 +12,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;
@ -22,7 +23,7 @@ public class PlayerConfig extends YamlConfiguration {
File file = new File(dataFolder, player.getUniqueId() + ".yml");
if (!file.exists()) {
String name = "username: " + player.getName();
String luck = "luck: " + PlayerHandler.getLuckContainer(player).defaultValue();
String luck = "luck: " + player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue();
String multiplier = "multiplier: " + 1.0;
SneakyWorker.sneakyTry(() -> {
@ -38,11 +39,11 @@ public class PlayerConfig extends YamlConfiguration {
});
}
configFile = file;
config = loadConfiguration(configFile);
config = YamlConfiguration.loadConfiguration(configFile);
String tempUsername = config.getString("username");
if (tempUsername != null && tempUsername.equalsIgnoreCase("replace")) {
if (tempUsername != null && !tempUsername.equalsIgnoreCase(player.getName())) {
config.set("username", player.getName());
config.set("luck", PlayerHandler.getLuckContainer(player).defaultValue());
config.set("multiplier", "1.0");
@ -52,7 +53,7 @@ public class PlayerConfig extends YamlConfiguration {
protected PlayerConfig(File file) {
this.configFile = file;
config = loadConfiguration(configFile);
config = YamlConfiguration.loadConfiguration(configFile);
}
@Contract("_ -> new")
@ -65,7 +66,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() {

View File

@ -1,6 +1,7 @@
package io.github.simplex.luck.player;
import io.github.simplex.luck.FeelingLucky;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -12,6 +13,10 @@ import java.util.List;
import java.util.Map;
public record PlayerHandler(FeelingLucky plugin) implements Listener {
public PlayerHandler {
Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
}
private static final Map<Player, Luck> playerLuckMap = new HashMap<>();
private static final List<Player> markedPlayers = new ArrayList<>();
@ -31,24 +36,28 @@ public record PlayerHandler(FeelingLucky plugin) implements Listener {
return markedPlayers.contains(player);
}
public static void updatePlayer(Player player, Luck luck) {
playerLuckMap.replace(player, luck);
}
@EventHandler
public void initializePlayer(PlayerLoginEvent event) {
Player player = event.getPlayer();
PlayerConfig config = FeelingLucky.getConfigMap().get(player.getUniqueId());
PlayerConfig playerConfig = FeelingLucky.getConfigMap().get(player.getUniqueId());
if (config == null) {
config = new PlayerConfig(plugin, player);
FeelingLucky.getConfigMap().put(player.getUniqueId(), config);
if (playerConfig == null) {
playerConfig = new PlayerConfig(plugin, player);
FeelingLucky.getConfigMap().put(player.getUniqueId(), playerConfig);
}
String username = config.getString("username");
double luck = config.getDouble("luck");
double multiplier = config.getDouble("multiplier");
String username = playerConfig.getConfig().getString("username");
double luck = playerConfig.getConfig().getDouble("luck");
double multiplier = playerConfig.getConfig().getDouble("multiplier");
if (!player.getName().equalsIgnoreCase(username)) {
config.set("username", player.getName());
config.save();
config.load();
playerConfig.getConfig().set("username", player.getName());
playerConfig.save();
playerConfig.load();
}
Luck container = new Luck(player, multiplier);
@ -56,8 +65,4 @@ public record PlayerHandler(FeelingLucky plugin) implements Listener {
playerLuckMap.put(player, container);
}
public void updatePlayer(Player player, Luck luck) {
playerLuckMap.replace(player, luck);
}
}

View File

@ -1,5 +1,6 @@
package io.github.simplex.luck.player;
import io.github.simplex.luck.FeelingLucky;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;