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.
This commit is contained in:
Paldiu 2022-05-20 16:54:28 -05:00
parent 17f83bd9f2
commit 010fd76031
17 changed files with 28 additions and 50 deletions

View File

@ -1,16 +1,11 @@
package io.github.simplex.api; package io.github.simplex.api;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.io.Serializable; import java.io.Serializable;
public interface LuckContainer extends Serializable { public interface LuckContainer extends Serializable {
Attribute asAttribute();
double getNumber();
boolean isMatch(double number); boolean isMatch(double number);
boolean isClose(double number, int range); boolean isClose(double number, int range);

View File

@ -20,7 +20,7 @@ public final class BlockDrops extends AbstractListener {
Player player = event.getPlayer(); Player player = event.getPlayer();
Luck luck = getHandler().getLuckContainer(player); Luck luck = getHandler().getLuckContainer(player);
List<Item> items = event.getItems(); List<Item> 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().clear();
event.getItems().addAll(items.stream().map(SneakyWorker::move).toList()); event.getItems().addAll(items.stream().map(SneakyWorker::move).toList());
} }

View File

@ -37,8 +37,8 @@ public final class BonemealFullCrop extends AbstractListener {
if (action.isRightClick() if (action.isRightClick()
&& handItem.isSimilar(bonemeal) && handItem.isSimilar(bonemeal)
&& (data instanceof Ageable crop) && (data instanceof Ageable crop)
&& luck.quickRNG(luck.getPercentage()) && luck.quickRNG(luck.getValue())
&& doesQualify("bonemeal", luck.getPercentage())) { && doesQualify("bonemeal", luck.getValue())) {
crop.setAge(crop.getMaximumAge()); crop.setAge(crop.getMaximumAge());
data.merge(crop); data.merge(crop);
block.setBlockData(data); block.setBlockData(data);

View File

@ -17,7 +17,7 @@ public final class CheatDeath extends AbstractListener {
Player player = event.getPlayer(); Player player = event.getPlayer();
Luck luck = getHandler().getLuckContainer(player); Luck luck = getHandler().getLuckContainer(player);
double absorption = Math.round(Luck.RNG().nextDouble(5.0, 10.0)); 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); event.setCancelled(true);
player.setHealth(1.0); player.setHealth(1.0);
player.setAbsorptionAmount(absorption); player.setAbsorptionAmount(absorption);

View File

@ -21,7 +21,7 @@ public final class EnchantmentBoost extends AbstractListener {
List<Enchantment> enchList = enchMap.keySet().stream().toList(); List<Enchantment> enchList = enchMap.keySet().stream().toList();
Player player = event.getEnchanter(); Player player = event.getEnchanter();
Luck luck = getHandler().getLuckContainer(player); 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())); Enchantment particular = enchList.get(Luck.RNG().nextInt(enchList.size()));
int rng = Luck.RNG().nextInt(1, 5); int rng = Luck.RNG().nextInt(1, 5);

View File

@ -20,7 +20,7 @@ public final class ExpBoost extends AbstractListener {
int rounded = Math.round(math); int rounded = Math.round(math);
Player player = event.getPlayer(); Player player = event.getPlayer();
Luck luck = plugin.getHandler().getLuckContainer(player); 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); orb.setExperience(rounded);
} }
} }

View File

@ -19,7 +19,7 @@ public class GiveDamage extends AbstractListener {
&& (e.getEntity() instanceof LivingEntity)) { && (e.getEntity() instanceof LivingEntity)) {
double nextDmg = e.getDamage() + Luck.RNG().nextDouble(1.0, 5.0); double nextDmg = e.getDamage() + Luck.RNG().nextDouble(1.0, 5.0);
Luck luck = plugin.getHandler().getLuckContainer(player); Luck luck = plugin.getHandler().getLuckContainer(player);
if (luck.quickRNG(luck.getPercentage())) { if (luck.quickRNG(luck.getValue())) {
e.setDamage(nextDmg); e.setDamage(nextDmg);
player.sendMessage(MiniComponent.info("Your luck has increased your damage output!")); player.sendMessage(MiniComponent.info("Your luck has increased your damage output!"));
} }

View File

@ -64,7 +64,7 @@ public class ItemDrops extends AbstractListener {
Item item = event.getItemDrop(); Item item = event.getItemDrop();
ItemStack stack = item.getItemStack(); ItemStack stack = item.getItemStack();
int amount = stack.getAmount(); 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); int rng = Luck.RNG().nextInt(2, 5);
amount += rng; amount += rng;
stack.setAmount(amount); stack.setAmount(amount);

View File

@ -24,7 +24,7 @@ public class OreVein extends AbstractListener {
public void playerMine(BlockBreakEvent event) { public void playerMine(BlockBreakEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
Luck luck = plugin.getHandler().getLuckContainer(player); 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); getOresInArea(event.getBlock()).forEach(Block::breakNaturally);
player.sendMessage(MiniComponent.info("Your luck has let you mine all the blocks with one swing.")); player.sendMessage(MiniComponent.info("Your luck has let you mine all the blocks with one swing."));
} }

View File

@ -26,7 +26,7 @@ public class RandomEffect extends AbstractListener {
int size = effectList.size(); int size = effectList.size();
PotionEffect random = effectList.get(Luck.RNG().nextInt(size - 1)); 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.addPotionEffect(random);
player.sendMessage(MiniComponent.info("Thanks to luck, a random positive potion effect has been applied to you.")); 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(); int size = effectList.size();
PotionEffect random = effectList.get(Luck.RNG().nextInt(size - 1)); 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.addPotionEffect(random);
player.sendMessage(MiniComponent.info("Thanks to luck, a random positive potion effect has been applied to you.")); player.sendMessage(MiniComponent.info("Thanks to luck, a random positive potion effect has been applied to you."));
} }

View File

@ -21,7 +21,7 @@ public class RestoreHunger extends AbstractListener {
Luck luck = getHandler().getLuckContainer(event.getPlayer()); Luck luck = getHandler().getLuckContainer(event.getPlayer());
PotionEffect effect = PotionEffectBuilder.newEffect().type(PotionEffectType.SATURATION).amplifier(2).duration(10).particles(false).create(); PotionEffect effect = PotionEffectBuilder.newEffect().type(PotionEffectType.SATURATION).amplifier(2).duration(10).particles(false).create();
if (luck.notDefault()) { if (luck.notDefault()) {
double percentage = luck.getPercentage(); double percentage = luck.getValue();
ListBox.foods.forEach(food -> { ListBox.foods.forEach(food -> {
if (item.isSimilar(food)) { if (item.isSimilar(food)) {
if (luck.quickRNG(percentage) && doesQualify("restore_hunger", percentage)) { if (luck.quickRNG(percentage) && doesQualify("restore_hunger", percentage)) {

View File

@ -25,7 +25,7 @@ public class TakeDamage extends AbstractListener {
Luck luck = getHandler().getLuckContainer(player); Luck luck = getHandler().getLuckContainer(player);
if (ListBox.acceptedCauses.contains(event.getCause())) { if (ListBox.acceptedCauses.contains(event.getCause())) {
if (luck.notDefault()) { 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", * 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 (ListBox.sideCauses.contains(event.getCause())) {
if (luck.notDefault()) { 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", * If a player's luck stat is a negative number, or they are "marked",

View File

@ -27,7 +27,7 @@ public class UnbreakableTool extends AbstractListener {
if (ItemBuilder.isTool(stack.getType())) { if (ItemBuilder.isTool(stack.getType())) {
if (event.getWhoClicked() instanceof Player player) { if (event.getWhoClicked() instanceof Player player) {
Luck luck = getHandler().getLuckContainer(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); meta.setUnbreakable(true);
stack.setItemMeta(meta); stack.setItemMeta(meta);
inventory.setResult(stack); inventory.setResult(stack);

View File

@ -44,7 +44,7 @@ public class VillagerInventory extends AbstractListener {
Luck luck = plugin.getHandler().getLuckContainer(event.getPlayer()); Luck luck = plugin.getHandler().getLuckContainer(event.getPlayer());
if (luck == null) return; if (luck == null) return;
if (luck.quickRNG(luck.getPercentage())) { if (luck.quickRNG(luck.getValue())) {
recipeList.add(recipe); recipeList.add(recipe);
vil.setRecipes(recipeList); vil.setRecipes(recipeList);
} }

View File

@ -3,7 +3,6 @@ package io.github.simplex.luck.player;
import io.github.simplex.api.LuckContainer; import io.github.simplex.api.LuckContainer;
import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.FeelingLucky;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -41,9 +40,9 @@ public class Luck implements LuckContainer {
return new SplittableRandom(); return new SplittableRandom();
} }
public static boolean quickRNGnoMultiplier(double percentage) { public static boolean quickRNGnoMultiplier(double value) {
double rng; double rng;
if (percentage >= 100.0) { if (value >= 1024.0) {
rng = 1024.0; // 100% chance to trigger, obviously; rng = 1024.0; // 100% chance to trigger, obviously;
} else { } else {
rng = RNG().nextDouble(0.0, 1024.0); rng = RNG().nextDouble(0.0, 1024.0);
@ -51,7 +50,7 @@ public class Luck implements LuckContainer {
double actual = Math.round((rng / 1024.0) * 100); double actual = Math.round((rng / 1024.0) * 100);
return (percentage >= actual); return (value >= actual);
} }
public FeelingLucky getPlugin() { public FeelingLucky getPlugin() {
@ -70,24 +69,14 @@ public class Luck implements LuckContainer {
return markedPlayers.contains(player); return markedPlayers.contains(player);
} }
@Override
public Attribute asAttribute() {
return Attribute.GENERIC_LUCK;
}
@Override
public double getNumber() {
return associatedPlayer().getAttribute(asAttribute()).getValue();
}
@Override @Override
public boolean isMatch(double number) { public boolean isMatch(double number) {
return getNumber() == number; return getValue() == number;
} }
@Override @Override
public boolean isClose(double number, int range) { public boolean isClose(double number, int range) {
return ((getNumber() - range <= number) && (number <= getNumber() + range)); return ((getValue() - range <= number) && (number <= getValue() + range));
} }
@Override @Override
@ -100,9 +89,9 @@ public class Luck implements LuckContainer {
return player; return player;
} }
public boolean quickRNG(double percentage) { public boolean quickRNG(double value) {
double rng; double rng;
if (percentage >= 100.0) { if (value >= 1024.0) {
rng = 1024.0; // 100% chance to trigger, obviously; rng = 1024.0; // 100% chance to trigger, obviously;
} else { } else {
rng = RNG().nextDouble(0.0, 1024.0); rng = RNG().nextDouble(0.0, 1024.0);
@ -111,10 +100,10 @@ public class Luck implements LuckContainer {
double actual = Math.round((rng / 1024) * 100); double actual = Math.round((rng / 1024) * 100);
if (multiplier() > 1.0) { if (multiplier() > 1.0) {
return ((percentage * multiplier()) >= actual); return ((value * multiplier()) >= actual);
} }
return (percentage >= actual); return (value >= actual);
} }
public void reset() { public void reset() {
@ -128,7 +117,6 @@ public class Luck implements LuckContainer {
public void setValue(double value) { public void setValue(double value) {
BASE_VALUE = value; BASE_VALUE = value;
player.getAttribute(Attribute.GENERIC_LUCK).setBaseValue(value);
plugin.getConfigMap().get(associatedPlayer().getUniqueId()).setLuck(value); plugin.getConfigMap().get(associatedPlayer().getUniqueId()).setLuck(value);
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
} }
@ -162,7 +150,7 @@ public class Luck implements LuckContainer {
} }
public double getDefaultValue() { public double getDefaultValue() {
return player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue(); return 0;
} }
public void addTo(double value) { public void addTo(double value) {
@ -181,10 +169,6 @@ public class Luck implements LuckContainer {
setValue(getValue() + value); setValue(getValue() + value);
} }
public double getPercentage() {
return getValue() - getDefaultValue();
}
public boolean notDefault() { public boolean notDefault() {
return getValue() != getDefaultValue(); return getValue() != getDefaultValue();
} }

View File

@ -29,7 +29,7 @@ public class PlayerConfig {
File file = new File(dataFolder, player.getUniqueId() + ".yml"); File file = new File(dataFolder, player.getUniqueId() + ".yml");
if (!file.exists()) { if (!file.exists()) {
String name = "username: " + player.getName(); String name = "username: " + player.getName();
String luck = "luck: " + player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue(); String luck = "luck: " + 0;
String multiplier = "multiplier: " + 1.0; String multiplier = "multiplier: " + 1.0;
SneakyWorker.sneakyTry(() -> { SneakyWorker.sneakyTry(() -> {

View File

@ -8,7 +8,6 @@ import io.github.simplex.luck.player.PlayerConfig;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.*; import org.bukkit.command.*;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; 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 ((sender instanceof Player player) && player.hasPermission("luck.default")) {
if (args[0].equalsIgnoreCase("info")) { if (args[0].equalsIgnoreCase("info")) {
Luck luck = plugin.getHandler().getLuckContainer(player); Luck luck = plugin.getHandler().getLuckContainer(player);
player.sendMessage(MiniComponent.info("Your Luck: " + luck.getPercentage())); player.sendMessage(MiniComponent.info("Your Luck: " + luck.getValue()));
return true; return true;
} }
} else if (sender instanceof ConsoleCommandSender) { } else if (sender instanceof ConsoleCommandSender) {