From 010fd760313473fe09e8fdeec4bced77867aeb63 Mon Sep 17 00:00:00 2001 From: Paldiu Date: Fri, 20 May 2022 16:54:28 -0500 Subject: [PATCH] FeelingLucky v1.0.0 Release Clean Up Cleaned up a bunch of stuff and made the luck stat unique to the plugin rather than using the values provided by minecraft. --- .../io/github/simplex/api/LuckContainer.java | 5 --- .../simplex/luck/listener/BlockDrops.java | 2 +- .../luck/listener/BonemealFullCrop.java | 4 +-- .../simplex/luck/listener/CheatDeath.java | 2 +- .../luck/listener/EnchantmentBoost.java | 2 +- .../simplex/luck/listener/ExpBoost.java | 2 +- .../simplex/luck/listener/GiveDamage.java | 2 +- .../simplex/luck/listener/ItemDrops.java | 2 +- .../github/simplex/luck/listener/OreVein.java | 2 +- .../simplex/luck/listener/RandomEffect.java | 4 +-- .../simplex/luck/listener/RestoreHunger.java | 2 +- .../simplex/luck/listener/TakeDamage.java | 4 +-- .../luck/listener/UnbreakableTool.java | 2 +- .../luck/listener/VillagerInventory.java | 2 +- .../io/github/simplex/luck/player/Luck.java | 36 ++++++------------- .../simplex/luck/player/PlayerConfig.java | 2 +- .../io/github/simplex/luck/util/LuckCMD.java | 3 +- 17 files changed, 28 insertions(+), 50 deletions(-) diff --git a/src/main/java/io/github/simplex/api/LuckContainer.java b/src/main/java/io/github/simplex/api/LuckContainer.java index 28d8454..7ab933c 100644 --- a/src/main/java/io/github/simplex/api/LuckContainer.java +++ b/src/main/java/io/github/simplex/api/LuckContainer.java @@ -1,16 +1,11 @@ package io.github.simplex.api; -import org.bukkit.attribute.Attribute; import org.bukkit.entity.Player; import java.io.Serializable; public interface LuckContainer extends Serializable { - Attribute asAttribute(); - - double getNumber(); - boolean isMatch(double number); boolean isClose(double number, int range); diff --git a/src/main/java/io/github/simplex/luck/listener/BlockDrops.java b/src/main/java/io/github/simplex/luck/listener/BlockDrops.java index 6efa412..239e64b 100644 --- a/src/main/java/io/github/simplex/luck/listener/BlockDrops.java +++ b/src/main/java/io/github/simplex/luck/listener/BlockDrops.java @@ -20,7 +20,7 @@ public final class BlockDrops extends AbstractListener { Player player = event.getPlayer(); Luck luck = getHandler().getLuckContainer(player); List items = event.getItems(); - if (luck.quickRNG(luck.getPercentage()) && doesQualify("block_drops", luck.getPercentage())) { + if (luck.quickRNG(luck.getValue()) && doesQualify("block_drops", luck.getValue())) { event.getItems().clear(); event.getItems().addAll(items.stream().map(SneakyWorker::move).toList()); } diff --git a/src/main/java/io/github/simplex/luck/listener/BonemealFullCrop.java b/src/main/java/io/github/simplex/luck/listener/BonemealFullCrop.java index 041a2df..3c30215 100644 --- a/src/main/java/io/github/simplex/luck/listener/BonemealFullCrop.java +++ b/src/main/java/io/github/simplex/luck/listener/BonemealFullCrop.java @@ -37,8 +37,8 @@ public final class BonemealFullCrop extends AbstractListener { if (action.isRightClick() && handItem.isSimilar(bonemeal) && (data instanceof Ageable crop) - && luck.quickRNG(luck.getPercentage()) - && doesQualify("bonemeal", luck.getPercentage())) { + && luck.quickRNG(luck.getValue()) + && doesQualify("bonemeal", luck.getValue())) { crop.setAge(crop.getMaximumAge()); data.merge(crop); block.setBlockData(data); diff --git a/src/main/java/io/github/simplex/luck/listener/CheatDeath.java b/src/main/java/io/github/simplex/luck/listener/CheatDeath.java index 24eb5d7..ad26919 100644 --- a/src/main/java/io/github/simplex/luck/listener/CheatDeath.java +++ b/src/main/java/io/github/simplex/luck/listener/CheatDeath.java @@ -17,7 +17,7 @@ public final class CheatDeath extends AbstractListener { Player player = event.getPlayer(); Luck luck = getHandler().getLuckContainer(player); double absorption = Math.round(Luck.RNG().nextDouble(5.0, 10.0)); - if (luck.quickRNG(luck.getPercentage()) && doesQualify("cheat_death", luck.getPercentage())) { + if (luck.quickRNG(luck.getValue()) && doesQualify("cheat_death", luck.getValue())) { event.setCancelled(true); player.setHealth(1.0); player.setAbsorptionAmount(absorption); diff --git a/src/main/java/io/github/simplex/luck/listener/EnchantmentBoost.java b/src/main/java/io/github/simplex/luck/listener/EnchantmentBoost.java index 10c93cc..eb6e13a 100644 --- a/src/main/java/io/github/simplex/luck/listener/EnchantmentBoost.java +++ b/src/main/java/io/github/simplex/luck/listener/EnchantmentBoost.java @@ -21,7 +21,7 @@ public final class EnchantmentBoost extends AbstractListener { List enchList = enchMap.keySet().stream().toList(); Player player = event.getEnchanter(); Luck luck = getHandler().getLuckContainer(player); - if (luck.quickRNG(luck.getPercentage()) && doesQualify("enchanting", luck.getPercentage())) { + if (luck.quickRNG(luck.getValue()) && doesQualify("enchanting", luck.getValue())) { Enchantment particular = enchList.get(Luck.RNG().nextInt(enchList.size())); int rng = Luck.RNG().nextInt(1, 5); diff --git a/src/main/java/io/github/simplex/luck/listener/ExpBoost.java b/src/main/java/io/github/simplex/luck/listener/ExpBoost.java index bf0309b..69ec712 100644 --- a/src/main/java/io/github/simplex/luck/listener/ExpBoost.java +++ b/src/main/java/io/github/simplex/luck/listener/ExpBoost.java @@ -20,7 +20,7 @@ public final class ExpBoost extends AbstractListener { int rounded = Math.round(math); Player player = event.getPlayer(); Luck luck = plugin.getHandler().getLuckContainer(player); - if (luck.quickRNG(luck.getPercentage()) && doesQualify("experience", luck.getPercentage())) { + if (luck.quickRNG(luck.getValue()) && doesQualify("experience", luck.getValue())) { orb.setExperience(rounded); } } diff --git a/src/main/java/io/github/simplex/luck/listener/GiveDamage.java b/src/main/java/io/github/simplex/luck/listener/GiveDamage.java index d85b42c..d8f7aef 100644 --- a/src/main/java/io/github/simplex/luck/listener/GiveDamage.java +++ b/src/main/java/io/github/simplex/luck/listener/GiveDamage.java @@ -19,7 +19,7 @@ public class GiveDamage extends AbstractListener { && (e.getEntity() instanceof LivingEntity)) { double nextDmg = e.getDamage() + Luck.RNG().nextDouble(1.0, 5.0); Luck luck = plugin.getHandler().getLuckContainer(player); - if (luck.quickRNG(luck.getPercentage())) { + if (luck.quickRNG(luck.getValue())) { e.setDamage(nextDmg); player.sendMessage(MiniComponent.info("Your luck has increased your damage output!")); } diff --git a/src/main/java/io/github/simplex/luck/listener/ItemDrops.java b/src/main/java/io/github/simplex/luck/listener/ItemDrops.java index 154eda6..4d1204a 100644 --- a/src/main/java/io/github/simplex/luck/listener/ItemDrops.java +++ b/src/main/java/io/github/simplex/luck/listener/ItemDrops.java @@ -64,7 +64,7 @@ public class ItemDrops extends AbstractListener { Item item = event.getItemDrop(); ItemStack stack = item.getItemStack(); int amount = stack.getAmount(); - if (luck.quickRNG(luck.getPercentage()) && doesQualify("item_drops", luck.getPercentage())) { + if (luck.quickRNG(luck.getValue()) && doesQualify("item_drops", luck.getValue())) { int rng = Luck.RNG().nextInt(2, 5); amount += rng; stack.setAmount(amount); diff --git a/src/main/java/io/github/simplex/luck/listener/OreVein.java b/src/main/java/io/github/simplex/luck/listener/OreVein.java index 63fdee8..d6f6fbd 100644 --- a/src/main/java/io/github/simplex/luck/listener/OreVein.java +++ b/src/main/java/io/github/simplex/luck/listener/OreVein.java @@ -24,7 +24,7 @@ public class OreVein extends AbstractListener { public void playerMine(BlockBreakEvent event) { Player player = event.getPlayer(); Luck luck = plugin.getHandler().getLuckContainer(player); - if (luck.quickRNG(luck.getPercentage()) && event.getBlock().isValidTool(player.getInventory().getItemInMainHand())) { + if (luck.quickRNG(luck.getValue()) && event.getBlock().isValidTool(player.getInventory().getItemInMainHand())) { getOresInArea(event.getBlock()).forEach(Block::breakNaturally); player.sendMessage(MiniComponent.info("Your luck has let you mine all the blocks with one swing.")); } diff --git a/src/main/java/io/github/simplex/luck/listener/RandomEffect.java b/src/main/java/io/github/simplex/luck/listener/RandomEffect.java index f80b81d..b6a790a 100644 --- a/src/main/java/io/github/simplex/luck/listener/RandomEffect.java +++ b/src/main/java/io/github/simplex/luck/listener/RandomEffect.java @@ -26,7 +26,7 @@ public class RandomEffect extends AbstractListener { int size = effectList.size(); PotionEffect random = effectList.get(Luck.RNG().nextInt(size - 1)); - if (luck.quickRNG(luck.getPercentage()) && doesQualify("random_effect", luck.getValue())) { + if (luck.quickRNG(luck.getValue()) && doesQualify("random_effect", luck.getValue())) { player.addPotionEffect(random); player.sendMessage(MiniComponent.info("Thanks to luck, a random positive potion effect has been applied to you.")); } @@ -41,7 +41,7 @@ public class RandomEffect extends AbstractListener { int size = effectList.size(); PotionEffect random = effectList.get(Luck.RNG().nextInt(size - 1)); - if (luck.quickRNG(luck.getPercentage()) && doesQualify("random_effect", luck.getValue())) { + if (luck.quickRNG(luck.getValue()) && doesQualify("random_effect", luck.getValue())) { player.addPotionEffect(random); player.sendMessage(MiniComponent.info("Thanks to luck, a random positive potion effect has been applied to you.")); } diff --git a/src/main/java/io/github/simplex/luck/listener/RestoreHunger.java b/src/main/java/io/github/simplex/luck/listener/RestoreHunger.java index 0f9571d..a15b2e8 100644 --- a/src/main/java/io/github/simplex/luck/listener/RestoreHunger.java +++ b/src/main/java/io/github/simplex/luck/listener/RestoreHunger.java @@ -21,7 +21,7 @@ public class RestoreHunger extends AbstractListener { Luck luck = getHandler().getLuckContainer(event.getPlayer()); PotionEffect effect = PotionEffectBuilder.newEffect().type(PotionEffectType.SATURATION).amplifier(2).duration(10).particles(false).create(); if (luck.notDefault()) { - double percentage = luck.getPercentage(); + double percentage = luck.getValue(); ListBox.foods.forEach(food -> { if (item.isSimilar(food)) { if (luck.quickRNG(percentage) && doesQualify("restore_hunger", percentage)) { diff --git a/src/main/java/io/github/simplex/luck/listener/TakeDamage.java b/src/main/java/io/github/simplex/luck/listener/TakeDamage.java index 7138c6d..5c97b25 100644 --- a/src/main/java/io/github/simplex/luck/listener/TakeDamage.java +++ b/src/main/java/io/github/simplex/luck/listener/TakeDamage.java @@ -25,7 +25,7 @@ public class TakeDamage extends AbstractListener { Luck luck = getHandler().getLuckContainer(player); if (ListBox.acceptedCauses.contains(event.getCause())) { if (luck.notDefault()) { - double percentage = luck.getPercentage(); + double percentage = luck.getValue(); /* * If a player's luck stat is a negative number, or they are "marked", @@ -51,7 +51,7 @@ public class TakeDamage extends AbstractListener { if (ListBox.sideCauses.contains(event.getCause())) { if (luck.notDefault()) { - double percentage = luck.getPercentage(); + double percentage = luck.getValue(); /* * If a player's luck stat is a negative number, or they are "marked", diff --git a/src/main/java/io/github/simplex/luck/listener/UnbreakableTool.java b/src/main/java/io/github/simplex/luck/listener/UnbreakableTool.java index 430b643..5356b15 100644 --- a/src/main/java/io/github/simplex/luck/listener/UnbreakableTool.java +++ b/src/main/java/io/github/simplex/luck/listener/UnbreakableTool.java @@ -27,7 +27,7 @@ public class UnbreakableTool extends AbstractListener { if (ItemBuilder.isTool(stack.getType())) { if (event.getWhoClicked() instanceof Player player) { Luck luck = getHandler().getLuckContainer(player); - if (luck.quickRNG(luck.getPercentage()) && doesQualify("unbreakable", luck.getPercentage())) { + if (luck.quickRNG(luck.getValue()) && doesQualify("unbreakable", luck.getValue())) { meta.setUnbreakable(true); stack.setItemMeta(meta); inventory.setResult(stack); diff --git a/src/main/java/io/github/simplex/luck/listener/VillagerInventory.java b/src/main/java/io/github/simplex/luck/listener/VillagerInventory.java index eb3a12d..c55a5fc 100644 --- a/src/main/java/io/github/simplex/luck/listener/VillagerInventory.java +++ b/src/main/java/io/github/simplex/luck/listener/VillagerInventory.java @@ -44,7 +44,7 @@ public class VillagerInventory extends AbstractListener { Luck luck = plugin.getHandler().getLuckContainer(event.getPlayer()); if (luck == null) return; - if (luck.quickRNG(luck.getPercentage())) { + if (luck.quickRNG(luck.getValue())) { recipeList.add(recipe); vil.setRecipes(recipeList); } 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 f9ae840..44988d1 100644 --- a/src/main/java/io/github/simplex/luck/player/Luck.java +++ b/src/main/java/io/github/simplex/luck/player/Luck.java @@ -3,7 +3,6 @@ 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; @@ -41,9 +40,9 @@ public class Luck implements LuckContainer { return new SplittableRandom(); } - public static boolean quickRNGnoMultiplier(double percentage) { + public static boolean quickRNGnoMultiplier(double value) { double rng; - if (percentage >= 100.0) { + if (value >= 1024.0) { rng = 1024.0; // 100% chance to trigger, obviously; } else { rng = RNG().nextDouble(0.0, 1024.0); @@ -51,7 +50,7 @@ public class Luck implements LuckContainer { double actual = Math.round((rng / 1024.0) * 100); - return (percentage >= actual); + return (value >= actual); } public FeelingLucky getPlugin() { @@ -70,24 +69,14 @@ public class Luck implements LuckContainer { return markedPlayers.contains(player); } - @Override - public Attribute asAttribute() { - return Attribute.GENERIC_LUCK; - } - - @Override - public double getNumber() { - return associatedPlayer().getAttribute(asAttribute()).getValue(); - } - @Override public boolean isMatch(double number) { - return getNumber() == number; + return getValue() == number; } @Override public boolean isClose(double number, int range) { - return ((getNumber() - range <= number) && (number <= getNumber() + range)); + return ((getValue() - range <= number) && (number <= getValue() + range)); } @Override @@ -100,9 +89,9 @@ public class Luck implements LuckContainer { return player; } - public boolean quickRNG(double percentage) { + public boolean quickRNG(double value) { double rng; - if (percentage >= 100.0) { + if (value >= 1024.0) { rng = 1024.0; // 100% chance to trigger, obviously; } else { rng = RNG().nextDouble(0.0, 1024.0); @@ -111,10 +100,10 @@ public class Luck implements LuckContainer { double actual = Math.round((rng / 1024) * 100); if (multiplier() > 1.0) { - return ((percentage * multiplier()) >= actual); + return ((value * multiplier()) >= actual); } - return (percentage >= actual); + return (value >= actual); } public void reset() { @@ -128,7 +117,6 @@ public class Luck implements LuckContainer { public void setValue(double value) { BASE_VALUE = value; - player.getAttribute(Attribute.GENERIC_LUCK).setBaseValue(value); plugin.getConfigMap().get(associatedPlayer().getUniqueId()).setLuck(value); Bukkit.getPluginManager().callEvent(event); } @@ -162,7 +150,7 @@ public class Luck implements LuckContainer { } public double getDefaultValue() { - return player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue(); + return 0; } public void addTo(double value) { @@ -181,10 +169,6 @@ public class Luck implements LuckContainer { setValue(getValue() + value); } - public double getPercentage() { - return getValue() - getDefaultValue(); - } - public boolean notDefault() { return getValue() != getDefaultValue(); } 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 c044104..ae5f1cd 100644 --- a/src/main/java/io/github/simplex/luck/player/PlayerConfig.java +++ b/src/main/java/io/github/simplex/luck/player/PlayerConfig.java @@ -29,7 +29,7 @@ public class PlayerConfig { File file = new File(dataFolder, player.getUniqueId() + ".yml"); if (!file.exists()) { String name = "username: " + player.getName(); - String luck = "luck: " + player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue(); + String luck = "luck: " + 0; String multiplier = "multiplier: " + 1.0; SneakyWorker.sneakyTry(() -> { diff --git a/src/main/java/io/github/simplex/luck/util/LuckCMD.java b/src/main/java/io/github/simplex/luck/util/LuckCMD.java index 1a9f21c..18a964e 100644 --- a/src/main/java/io/github/simplex/luck/util/LuckCMD.java +++ b/src/main/java/io/github/simplex/luck/util/LuckCMD.java @@ -8,7 +8,6 @@ import io.github.simplex.luck.player.PlayerConfig; import org.bukkit.Bukkit; import org.bukkit.command.*; import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -143,7 +142,7 @@ public class LuckCMD extends Command implements TabCompleter, PluginIdentifiable if ((sender instanceof Player player) && player.hasPermission("luck.default")) { if (args[0].equalsIgnoreCase("info")) { Luck luck = plugin.getHandler().getLuckContainer(player); - player.sendMessage(MiniComponent.info("Your Luck: " + luck.getPercentage())); + player.sendMessage(MiniComponent.info("Your Luck: " + luck.getValue())); return true; } } else if (sender instanceof ConsoleCommandSender) {