mirror of
https://github.com/SimplexDevelopment/FeelingLucky.git
synced 2025-07-04 11:06:43 +00:00
Compare commits
6 Commits
Alpha-RC01
...
Alpha-RC04
Author | SHA1 | Date | |
---|---|---|---|
d08b240ff9 | |||
cbc57d5971 | |||
247a2fafc0 | |||
ffdafda69c | |||
0383bfeaf3 | |||
93ee1e2c43 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1
.idea/.name
generated
Normal file
1
.idea/.name
generated
Normal file
@ -0,0 +1 @@
|
||||
FeelingLucky
|
@ -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 {
|
||||
|
@ -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
|
||||
|
Binary file not shown.
@ -1 +1 @@
|
||||
rootProject.name = 'Crumb'
|
||||
rootProject.name = 'FeelingLucky'
|
||||
|
@ -18,5 +18,5 @@ public interface LuckContainer extends Serializable {
|
||||
|
||||
Player associatedPlayer();
|
||||
|
||||
double baseValue();
|
||||
double getValue();
|
||||
}
|
||||
|
35
src/main/java/io/github/simplex/lib/Messages.java
Normal file
35
src/main/java/io/github/simplex/lib/Messages.java
Normal 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);
|
||||
}
|
||||
}
|
@ -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,15 +26,15 @@ 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();
|
||||
if (files != null) {
|
||||
Arrays.stream(files).forEach(file -> {
|
||||
UUID uuid = UUID.fromString(file.getName().split("\\.")[0]);
|
||||
configMap.put(uuid, PlayerConfig.loadFrom(file));
|
||||
configMap.put(uuid, PlayerConfig.loadFrom(this, file));
|
||||
});
|
||||
configMap.forEach((u, pc) -> pc.load());
|
||||
getLogger().info("Successfully loaded all configurations!");
|
||||
@ -39,12 +42,16 @@ 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!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
Bukkit.getLogger().info("Saving all player configurations...");
|
||||
configMap.forEach((u, pc) -> pc.save());
|
||||
configMap.values().forEach(PlayerConfig::save);
|
||||
}
|
||||
}
|
||||
|
161
src/main/java/io/github/simplex/luck/LuckCMD.java
Normal file
161
src/main/java/io/github/simplex/luck/LuckCMD.java
Normal file
@ -0,0 +1,161 @@
|
||||
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 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 {
|
||||
private final FeelingLucky plugin;
|
||||
|
||||
public LuckCMD(FeelingLucky plugin) {
|
||||
super("luck", "FeelingLucky main command.", "/<command> <info | set | reset | give | take> [player] [amount]", List.of());
|
||||
this.plugin = plugin;
|
||||
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 = plugin.handler.getLuckContainer(player);
|
||||
PlayerConfig config = FeelingLucky.getConfigMap().get(player.getUniqueId());
|
||||
|
||||
switch (args[0]) {
|
||||
case "set" -> {
|
||||
luck.setValue(amount);
|
||||
plugin.handler.updatePlayer(player, luck);
|
||||
config.setLuck(luck.getValue());
|
||||
sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat."));
|
||||
return true;
|
||||
}
|
||||
case "give" -> {
|
||||
luck.addTo(amount);
|
||||
plugin.handler.updatePlayer(player, luck);
|
||||
config.setLuck(luck.getValue());
|
||||
sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat."));
|
||||
return true;
|
||||
}
|
||||
case "take" -> {
|
||||
luck.takeFrom(amount);
|
||||
plugin.handler.updatePlayer(player, luck);
|
||||
config.setLuck(luck.getValue());
|
||||
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 == 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 = plugin.handler.getLuckContainer(player);
|
||||
sender.sendMessage(Component.empty().content("Luck stat for " + args[1] + ": " + luck.getValue()));
|
||||
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 = plugin.handler.getLuckContainer(player);
|
||||
PlayerConfig config = FeelingLucky.getConfigMap().get(player.getUniqueId());
|
||||
luck.reset();
|
||||
plugin.handler.updatePlayer(player, luck);
|
||||
config.setLuck(luck.getValue());
|
||||
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 = plugin.handler.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;
|
||||
}
|
||||
}
|
@ -5,8 +5,8 @@ import io.github.simplex.luck.FeelingLucky;
|
||||
import io.github.simplex.luck.ListBox;
|
||||
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 +17,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;
|
||||
@ -30,9 +29,14 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public record PlayerListener(FeelingLucky plugin) implements Listener {
|
||||
public class PlayerListener implements Listener {
|
||||
private final Map<UUID, Player> entityPlayerMap = new HashMap<>();
|
||||
private final FeelingLucky plugin;
|
||||
|
||||
private static final Map<UUID, Player> entityPlayerMap = new HashMap<>();
|
||||
public PlayerListener(FeelingLucky plugin) {
|
||||
this.plugin = plugin;
|
||||
Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void takeDamage(EntityDamageEvent event) {
|
||||
@ -41,7 +45,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
|
||||
return;
|
||||
}
|
||||
Player player = (Player) event.getEntity();
|
||||
Luck luck = PlayerHandler.getLuckContainer(player);
|
||||
Luck luck = plugin.handler.getLuckContainer(player);
|
||||
if (ListBox.acceptedCauses.contains(event.getCause())) {
|
||||
if (luck.notDefault()) {
|
||||
double percentage = luck.getPercentage();
|
||||
@ -50,7 +54,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
|
||||
* If a player's luck stat is a negative number, or they are "marked",
|
||||
* this will trigger instead of the regular luck spin.
|
||||
*/
|
||||
if (percentage < 0 || PlayerHandler.isMarked(player)) {
|
||||
if (percentage < 0 || luck.isMarked(player)) {
|
||||
percentage = Math.abs(percentage);
|
||||
if (luck.quickRNG(percentage)) {
|
||||
event.setCancelled(true);
|
||||
@ -76,7 +80,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
|
||||
* If a player's luck stat is a negative number, or they are "marked",
|
||||
* this will trigger instead of the regular luck spin.
|
||||
*/
|
||||
if (percentage < 0 || PlayerHandler.isMarked(player)) {
|
||||
if (percentage < 0 || luck.isMarked(player)) {
|
||||
percentage = Math.abs(percentage);
|
||||
if (luck.quickRNG(percentage)) {
|
||||
event.setCancelled(true);
|
||||
@ -98,7 +102,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
|
||||
@EventHandler
|
||||
public void extraBlockDrops(BlockDropItemEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
Luck luck = PlayerHandler.getLuckContainer(player);
|
||||
Luck luck = plugin.handler.getLuckContainer(player);
|
||||
List<Item> items = event.getItems();
|
||||
if (luck.quickRNG(luck.getPercentage())) {
|
||||
items.forEach(SneakyWorker::move);
|
||||
@ -138,7 +142,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
|
||||
if (entityPlayerMap.get(entity.getUniqueId()) == null) return;
|
||||
|
||||
Player player = entityPlayerMap.get(entity.getUniqueId());
|
||||
Luck luck = PlayerHandler.getLuckContainer(player);
|
||||
Luck luck = plugin.handler.getLuckContainer(player);
|
||||
Item item = event.getItemDrop();
|
||||
ItemStack stack = item.getItemStack();
|
||||
int amount = stack.getAmount();
|
||||
@ -153,7 +157,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
|
||||
@EventHandler
|
||||
public void restoreHunger(PlayerItemConsumeEvent event) {
|
||||
ItemStack item = event.getItem();
|
||||
Luck luck = PlayerHandler.getLuckContainer(event.getPlayer());
|
||||
Luck luck = plugin.handler.getLuckContainer(event.getPlayer());
|
||||
PotionEffect effect = PotionEffectBuilder.newEffect().type(PotionEffectType.SATURATION).amplifier(2).duration(10).particles(false).create();
|
||||
if (luck.notDefault()) {
|
||||
double percentage = luck.getPercentage();
|
||||
@ -173,12 +177,12 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
|
||||
Action action = event.getAction();
|
||||
ItemStack foot = new ItemStack(Material.RABBIT_FOOT);
|
||||
Player player = event.getPlayer();
|
||||
Luck luck = PlayerHandler.getLuckContainer(player);
|
||||
Luck luck = plugin.handler.getLuckContainer(player);
|
||||
if (action.isRightClick() && player.getInventory().getItemInMainHand().isSimilar(foot)) {
|
||||
double rng = Luck.RNG().nextDouble(2.0, 5.0);
|
||||
player.getInventory().remove(player.getInventory().getItemInMainHand());
|
||||
luck.addTo(rng);
|
||||
|
||||
plugin.handler.updatePlayer(player, luck);
|
||||
player.sendMessage(Component.empty().content("Your luck has been increased by " + rng + " points."));
|
||||
}
|
||||
}
|
||||
@ -187,7 +191,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;
|
||||
@ -197,8 +201,8 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
Luck luck = PlayerHandler.getLuckContainer(player);
|
||||
if (cause.equals(DamageCause.MAGIC) || cause.equals(DamageCause.POISON)) {
|
||||
Luck luck = plugin.handler.getLuckContainer(player);
|
||||
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);
|
||||
|
@ -1,12 +1,15 @@
|
||||
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;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.SplittableRandom;
|
||||
|
||||
@SuppressWarnings("all")
|
||||
@ -24,7 +27,21 @@ public class Luck implements LuckContainer {
|
||||
this.player = player;
|
||||
this.multiplier = multiplier;
|
||||
BASE_VALUE = player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue();
|
||||
event = new PlayerLuckChangeEvent(player);
|
||||
event = new PlayerLuckChangeEvent(this);
|
||||
}
|
||||
|
||||
private final List<Player> markedPlayers = new ArrayList<>();
|
||||
|
||||
public void markPlayer(Player player) {
|
||||
markedPlayers.add(player);
|
||||
}
|
||||
|
||||
public void unmarkPlayer(Player player) {
|
||||
markedPlayers.remove(player);
|
||||
}
|
||||
|
||||
public boolean isMarked(Player player) {
|
||||
return markedPlayers.contains(player);
|
||||
}
|
||||
|
||||
@Contract(pure = true,
|
||||
@ -36,12 +53,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 = Math.round((rng / 1024.0) * 100);
|
||||
|
||||
return (percentage >= actual);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -77,60 +96,73 @@ 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 = Math.round((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(getDefaultValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public double baseValue() {
|
||||
public double getValue() {
|
||||
return BASE_VALUE;
|
||||
}
|
||||
|
||||
public double defaultValue() {
|
||||
public double 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);
|
||||
FeelingLucky.getConfigMap().get(associatedPlayer().getUniqueId()).setLuck(value);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
}
|
||||
|
||||
public void addTo(double value) {
|
||||
setValue(baseValue() + value);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
if (value >= 1024.0 || (getValue() + value) >= 1024.0) {
|
||||
setValue(1024.0);
|
||||
return;
|
||||
}
|
||||
setValue(getValue() + value);
|
||||
}
|
||||
|
||||
public void takeFrom(double value) {
|
||||
setValue(baseValue() - value);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
if (value <= -1024.0 || (getValue() - value) <= -1024.0) {
|
||||
setValue(-1024.0);
|
||||
return;
|
||||
}
|
||||
setValue(getValue() + value);
|
||||
}
|
||||
|
||||
public double getPercentage() {
|
||||
return baseValue() - defaultValue();
|
||||
return getValue() - getDefaultValue();
|
||||
}
|
||||
|
||||
public boolean notDefault() {
|
||||
return baseValue() != defaultValue();
|
||||
return getValue() != getDefaultValue();
|
||||
}
|
||||
|
||||
public boolean lessThan(double value) {
|
||||
return baseValue() < value;
|
||||
return getValue() < value;
|
||||
}
|
||||
|
||||
public boolean greaterThan(double value) {
|
||||
return baseValue() > value;
|
||||
return getValue() > value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "" + baseValue();
|
||||
return "" + getValue();
|
||||
}
|
||||
}
|
||||
|
@ -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,18 +12,20 @@ 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 final FeelingLucky plugin;
|
||||
private volatile YamlConfiguration config;
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
public PlayerConfig(FeelingLucky plugin, Player player) {
|
||||
this.plugin = plugin;
|
||||
File dataFolder = plugin.getDataFolder();
|
||||
if (!dataFolder.exists()) dataFolder.mkdirs();
|
||||
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,26 +41,27 @@ 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("luck", plugin.handler.getLuckContainer(player).getDefaultValue());
|
||||
config.set("multiplier", "1.0");
|
||||
save();
|
||||
}
|
||||
}
|
||||
|
||||
protected PlayerConfig(File file) {
|
||||
protected PlayerConfig(FeelingLucky plugin, File file) {
|
||||
this.plugin = plugin;
|
||||
this.configFile = file;
|
||||
config = loadConfiguration(configFile);
|
||||
config = YamlConfiguration.loadConfiguration(configFile);
|
||||
}
|
||||
|
||||
@Contract("_ -> new")
|
||||
public static PlayerConfig loadFrom(File file) {
|
||||
return new PlayerConfig(file);
|
||||
@Contract("_, _ -> new")
|
||||
public static PlayerConfig loadFrom(FeelingLucky plugin, File file) {
|
||||
return new PlayerConfig(plugin, file);
|
||||
}
|
||||
|
||||
public void save() {
|
||||
@ -65,7 +69,12 @@ 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() {
|
||||
|
@ -1,54 +1,50 @@
|
||||
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;
|
||||
import org.bukkit.event.player.PlayerLoginEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public record PlayerHandler(FeelingLucky plugin) implements Listener {
|
||||
private static final Map<Player, Luck> playerLuckMap = new HashMap<>();
|
||||
private static final List<Player> markedPlayers = new ArrayList<>();
|
||||
public class PlayerHandler implements Listener {
|
||||
public final FeelingLucky plugin;
|
||||
private final Map<Player, Luck> playerLuckMap = new HashMap<>();
|
||||
|
||||
public static Luck getLuckContainer(Player player) {
|
||||
public PlayerHandler(FeelingLucky plugin) {
|
||||
this.plugin = plugin;
|
||||
Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
|
||||
}
|
||||
|
||||
public Luck getLuckContainer(Player player) {
|
||||
return playerLuckMap.get(player);
|
||||
}
|
||||
|
||||
public static void markPlayer(Player player) {
|
||||
markedPlayers.add(player);
|
||||
}
|
||||
|
||||
public static void unmarkPlayer(Player player) {
|
||||
markedPlayers.remove(player);
|
||||
}
|
||||
|
||||
public static boolean isMarked(Player player) {
|
||||
return markedPlayers.contains(player);
|
||||
public 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 +52,4 @@ public record PlayerHandler(FeelingLucky plugin) implements Listener {
|
||||
|
||||
playerLuckMap.put(player, container);
|
||||
}
|
||||
|
||||
public void updatePlayer(Player player, Luck luck) {
|
||||
playerLuckMap.replace(player, luck);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
package io.github.simplex.luck.player;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.event.player.PlayerEvent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -8,16 +7,16 @@ import org.jetbrains.annotations.NotNull;
|
||||
public class PlayerLuckChangeEvent extends PlayerEvent {
|
||||
public final HandlerList handlerList = new HandlerList();
|
||||
|
||||
public PlayerLuckChangeEvent(@NotNull Player who) {
|
||||
super(who);
|
||||
Luck luck = PlayerHandler.getLuckContainer(who);
|
||||
if (luck.lessThan(0.0) && !PlayerHandler.isMarked(who)) {
|
||||
PlayerHandler.markPlayer(who);
|
||||
public PlayerLuckChangeEvent(@NotNull Luck luck) {
|
||||
super(luck.associatedPlayer());
|
||||
|
||||
if (luck.lessThan(0.0) && !luck.isMarked(luck.associatedPlayer())) {
|
||||
luck.markPlayer(luck.associatedPlayer());
|
||||
return;
|
||||
}
|
||||
|
||||
if (luck.greaterThan(0.0) && PlayerHandler.isMarked(who)) {
|
||||
PlayerHandler.unmarkPlayer(who);
|
||||
if (luck.greaterThan(0.0) && luck.isMarked(luck.associatedPlayer())) {
|
||||
luck.unmarkPlayer(luck.associatedPlayer());
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user