From 6d211490c09df13ccef8e8bd44c4d3f013689093 Mon Sep 17 00:00:00 2001 From: Paldiu Date: Thu, 24 Mar 2022 17:21:12 -0500 Subject: [PATCH] more qol --- .../io/github/simplex/luck/FeelingLucky.java | 25 ++++--- .../java/io/github/simplex/luck/ListBox.java | 67 +++++++++++++++++ .../io/github/simplex/luck/SneakyWorker.java | 6 ++ .../simplex/luck/listener/PlayerListener.java | 73 ++++--------------- .../simplex/luck/player/PlayerHandler.java | 16 ++-- 5 files changed, 110 insertions(+), 77 deletions(-) create mode 100644 src/main/java/io/github/simplex/luck/ListBox.java diff --git a/src/main/java/io/github/simplex/luck/FeelingLucky.java b/src/main/java/io/github/simplex/luck/FeelingLucky.java index 0048836..dcc7a36 100644 --- a/src/main/java/io/github/simplex/luck/FeelingLucky.java +++ b/src/main/java/io/github/simplex/luck/FeelingLucky.java @@ -5,16 +5,15 @@ import io.github.simplex.luck.player.PlayerHandler; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.io.File; +import java.util.*; public final class FeelingLucky extends JavaPlugin { - private static final List configList = new ArrayList<>(); + private static final Map configMap = new HashMap<>(); public PlayerHandler handler; - public static List getConfigList() { - return configList; + public static Map getConfigMap() { + return configMap; } @Override @@ -24,21 +23,25 @@ public final class FeelingLucky extends JavaPlugin { Bukkit.getLogger().info("Initialization complete! Attempting to register the handler..."); this.getServer().getPluginManager().registerEvents(handler, this); Bukkit.getLogger().info("Registration complete! Attempting to load all player configuration files..."); - if (getDataFolder().listFiles() != null) { - Arrays.stream(getDataFolder().listFiles()).forEach(file -> { - configList.add(PlayerConfig.loadFrom(file)); + + 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)); }); - configList.forEach(PlayerConfig::load); + configMap.forEach((u, pc) -> pc.load()); getLogger().info("Successfully loaded all configurations!"); } else { getLogger().info("There are no player configurations to load."); } + Bukkit.getLogger().info("Successfully initialized!"); } @Override public void onDisable() { Bukkit.getLogger().info("Saving all player configurations..."); - configList.forEach(PlayerConfig::save); + configMap.forEach((u, pc) -> pc.save()); } } diff --git a/src/main/java/io/github/simplex/luck/ListBox.java b/src/main/java/io/github/simplex/luck/ListBox.java new file mode 100644 index 0000000..45833fd --- /dev/null +++ b/src/main/java/io/github/simplex/luck/ListBox.java @@ -0,0 +1,67 @@ +package io.github.simplex.luck; + +import org.bukkit.Material; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.PotionEffectType; + +import java.util.ArrayList; +import java.util.List; + +public class ListBox { + public static final List acceptedCauses = new ArrayList<>() {{ + add(EntityDamageEvent.DamageCause.ENTITY_ATTACK); + add(EntityDamageEvent.DamageCause.ENTITY_SWEEP_ATTACK); + add(EntityDamageEvent.DamageCause.PROJECTILE); + add(EntityDamageEvent.DamageCause.ENTITY_EXPLOSION); + add(EntityDamageEvent.DamageCause.FLY_INTO_WALL); + add(EntityDamageEvent.DamageCause.LIGHTNING); + add(EntityDamageEvent.DamageCause.MAGIC); + }}; + + public static final List sideCauses = new ArrayList<>() {{ + add(EntityDamageEvent.DamageCause.POISON); + add(EntityDamageEvent.DamageCause.WITHER); + add(EntityDamageEvent.DamageCause.FIRE_TICK); + }}; + + public static final List potionEffects = new ArrayList<>() {{ + add(PotionEffectType.POISON); + add(PotionEffectType.WITHER); + add(PotionEffectType.BLINDNESS); + add(PotionEffectType.SLOW); + add(PotionEffectType.SLOW_DIGGING); + add(PotionEffectType.BAD_OMEN); + add(PotionEffectType.CONFUSION); + add(PotionEffectType.WEAKNESS); + }}; + + public static final List foods = new ArrayList<>() {{ + add(new ItemStack(Material.COOKED_BEEF)); + add(new ItemStack(Material.COOKED_CHICKEN)); + add(new ItemStack(Material.COOKED_PORKCHOP)); + add(new ItemStack(Material.COOKED_COD)); + add(new ItemStack(Material.COOKED_MUTTON)); + add(new ItemStack(Material.COOKED_RABBIT)); + add(new ItemStack(Material.COOKED_SALMON)); + add(new ItemStack(Material.BEETROOT_SOUP)); + add(new ItemStack(Material.POTATO)); + add(new ItemStack(Material.BAKED_POTATO)); + add(new ItemStack(Material.CARROT)); + add(new ItemStack(Material.GOLDEN_CARROT)); + add(new ItemStack(Material.APPLE)); + add(new ItemStack(Material.GOLDEN_APPLE)); + add(new ItemStack(Material.ENCHANTED_GOLDEN_APPLE)); + add(new ItemStack(Material.BEEF)); + add(new ItemStack(Material.PORKCHOP)); + add(new ItemStack(Material.CHICKEN)); + add(new ItemStack(Material.COD)); + add(new ItemStack(Material.SALMON)); + add(new ItemStack(Material.MUTTON)); + add(new ItemStack(Material.RABBIT)); + add(new ItemStack(Material.MUSHROOM_STEW)); + add(new ItemStack(Material.BREAD)); + add(new ItemStack(Material.CAKE)); + add(new ItemStack(Material.COOKIE)); + }}; +} diff --git a/src/main/java/io/github/simplex/luck/SneakyWorker.java b/src/main/java/io/github/simplex/luck/SneakyWorker.java index c1f417c..a1c74f8 100644 --- a/src/main/java/io/github/simplex/luck/SneakyWorker.java +++ b/src/main/java/io/github/simplex/luck/SneakyWorker.java @@ -15,6 +15,12 @@ public class SneakyWorker { } } + public static void silentTry(SneakyTry sneakyTry) { + try { + sneakyTry.tryThis(); + } catch (Exception ignored) {} + } + public interface SneakyTry { void tryThis() throws Exception; } 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 3461902..b094445 100644 --- a/src/main/java/io/github/simplex/luck/listener/PlayerListener.java +++ b/src/main/java/io/github/simplex/luck/listener/PlayerListener.java @@ -2,6 +2,7 @@ package io.github.simplex.luck.listener; import io.github.simplex.lib.PotionEffectBuilder; import io.github.simplex.luck.FeelingLucky; +import io.github.simplex.luck.ListBox; import io.github.simplex.luck.player.Luck; import io.github.simplex.luck.player.PlayerHandler; import net.kyori.adventure.text.Component; @@ -18,11 +19,9 @@ import org.bukkit.event.entity.EntityDamageEvent.DamageCause; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerItemConsumeEvent; import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; -import java.util.ArrayList; -import java.util.List; - public record PlayerListener(FeelingLucky plugin) implements Listener { @EventHandler @@ -37,6 +36,10 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { if (luck.notDefault()) { double percentage = luck.getPercentage(); + /* + * 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)) { percentage = Math.abs(percentage); if (luck.quickRNG(percentage)) { @@ -90,12 +93,19 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { public void restoreHunger(PlayerItemConsumeEvent event) { ItemStack item = event.getItem(); Luck luck = PlayerHandler.getLuckContainer(event.getPlayer()); + PotionEffect effect = PotionEffectBuilder.newEffect() + .type(PotionEffectType.SATURATION) + .amplifier(2) + .duration(10) + .particles(false) + .create(); if (luck.notDefault()) { double percentage = luck.getPercentage(); ListBox.foods.forEach(food -> { if (item.isSimilar(food)) { if (luck.quickRNG(percentage)) { event.getPlayer().setExhaustion(event.getPlayer().getExhaustion() + 2); + event.getPlayer().addPotionEffect(effect); } } }); @@ -140,61 +150,4 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { } } - private static class ListBox { - public static final List acceptedCauses = new ArrayList<>() {{ - add(DamageCause.ENTITY_ATTACK); - add(DamageCause.ENTITY_SWEEP_ATTACK); - add(DamageCause.PROJECTILE); - add(DamageCause.ENTITY_EXPLOSION); - add(DamageCause.FLY_INTO_WALL); - add(DamageCause.LIGHTNING); - add(DamageCause.MAGIC); - }}; - - public static final List sideCauses = new ArrayList<>() {{ - add(DamageCause.POISON); - add(DamageCause.WITHER); - add(DamageCause.FIRE_TICK); - }}; - - public static final List potionEffects = new ArrayList<>() {{ - add(PotionEffectType.POISON); - add(PotionEffectType.WITHER); - add(PotionEffectType.BLINDNESS); - add(PotionEffectType.SLOW); - add(PotionEffectType.SLOW_DIGGING); - add(PotionEffectType.BAD_OMEN); - add(PotionEffectType.CONFUSION); - add(PotionEffectType.WEAKNESS); - }}; - - public static final List foods = new ArrayList<>() {{ - add(new ItemStack(Material.COOKED_BEEF)); - add(new ItemStack(Material.COOKED_CHICKEN)); - add(new ItemStack(Material.COOKED_PORKCHOP)); - add(new ItemStack(Material.COOKED_COD)); - add(new ItemStack(Material.COOKED_MUTTON)); - add(new ItemStack(Material.COOKED_RABBIT)); - add(new ItemStack(Material.COOKED_SALMON)); - add(new ItemStack(Material.BEETROOT_SOUP)); - add(new ItemStack(Material.POTATO)); - add(new ItemStack(Material.BAKED_POTATO)); - add(new ItemStack(Material.CARROT)); - add(new ItemStack(Material.GOLDEN_CARROT)); - add(new ItemStack(Material.APPLE)); - add(new ItemStack(Material.GOLDEN_APPLE)); - add(new ItemStack(Material.ENCHANTED_GOLDEN_APPLE)); - add(new ItemStack(Material.BEEF)); - add(new ItemStack(Material.PORKCHOP)); - add(new ItemStack(Material.CHICKEN)); - add(new ItemStack(Material.COD)); - add(new ItemStack(Material.SALMON)); - add(new ItemStack(Material.MUTTON)); - add(new ItemStack(Material.RABBIT)); - add(new ItemStack(Material.MUSHROOM_STEW)); - add(new ItemStack(Material.BREAD)); - add(new ItemStack(Material.CAKE)); - add(new ItemStack(Material.COOKIE)); - }}; - } } 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 9763c46..4487b40 100644 --- a/src/main/java/io/github/simplex/luck/player/PlayerHandler.java +++ b/src/main/java/io/github/simplex/luck/player/PlayerHandler.java @@ -34,10 +34,15 @@ public record PlayerHandler(FeelingLucky plugin) implements Listener { @EventHandler public void initializePlayer(PlayerLoginEvent event) { Player player = event.getPlayer(); - PlayerConfig config = new PlayerConfig(plugin, player); + PlayerConfig config = FeelingLucky.getConfigMap().get(player.getUniqueId()); + + if (config == null) { + config = new PlayerConfig(plugin, player); + FeelingLucky.getConfigMap().put(player.getUniqueId(), config); + } String username = config.getString("username"); - double luckstat = config.getDouble("luck"); + double luck = config.getDouble("luck"); double multiplier = config.getDouble("multiplier"); if (!player.getName().equalsIgnoreCase(username)) { @@ -46,11 +51,10 @@ public record PlayerHandler(FeelingLucky plugin) implements Listener { config.load(); } - Luck luck = new Luck(player, multiplier); - luck.setValue(luckstat); + Luck container = new Luck(player, multiplier); + container.setValue(luck); - FeelingLucky.getConfigList().add(config); - playerLuckMap.put(player, luck); + playerLuckMap.put(player, container); } public void updatePlayer(Player player, Luck luck) {