Beta 20220422-SNAPSHOT

Changelog:
 - Added a configuration with editable rarity values and rarity types for the different events that can occur, to give the end user more control over how often players receive the effects.
- Modified listeners to use the respective Configuration values.
- Added the ability to reload a specific player configuration with /luck reload -p <player_name>
- Added the ability to reload the main configuration using /luck reload -m
- Added an integrity checker to validate the main configuration on boot.
This commit is contained in:
Paldiu 2022-04-22 19:25:02 -05:00
parent 6119180b0b
commit 330fd278b7
18 changed files with 280 additions and 93 deletions

View File

@ -0,0 +1,91 @@
package io.github.simplex.luck;
import io.github.simplex.lib.MiniComponent;
import io.github.simplex.luck.listener.AbstractListener;
import io.github.simplex.luck.util.SneakyWorker;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Config extends YamlConfiguration {
private final FeelingLucky plugin;
private File configFile;
public Config(FeelingLucky plugin) {
this.plugin = plugin;
File dataFolder = plugin.getDataFolder();
if (!dataFolder.exists()) dataFolder.mkdirs();
File configFile = new File(dataFolder, "config.yml");
if (!configFile.exists()) {
SneakyWorker.sneakyTry(configFile::createNewFile);
plugin.saveResource("config.yml", true);
}
this.configFile = configFile;
load();
if (validateIntegrity()) {
File newFile = new File(plugin.getDataFolder(), "config.yml");
SneakyWorker.sneakyTry(() -> {
Files.delete(Path.of(plugin.getDataFolder().getPath()));
newFile.createNewFile();
plugin.saveResource("config.yml", true);
});
this.configFile = newFile;
load();
}
}
public void save() {
SneakyWorker.sneakyTry(() -> save(configFile));
}
public void load() {
SneakyWorker.sneakyTry(() -> load(configFile));
}
public void reload() {
save();
load();
}
public boolean validateIntegrity() {
for (String key : getKeys(false)) {
if (!configEntries.contains(key)) {
plugin.getLogger().severe("The contents of your configuration file is corrupted! Regenerating a new configuration file...");
return true;
}
}
return false;
}
public AbstractListener.Rarity getRarity(String name) {
return AbstractListener.Rarity.valueOf(getString(name));
}
public double getChance(String path) {
return getDouble(path);
}
private List<String> configEntries = new ArrayList<>() {{
add("high_rarity_chance");
add("medium_rarity_chance");
add("low_rarity_chance");
add("block_drops");
add("bonemeal");
add("cheat_death");
add("enchanting");
add("experience");
add("item_drops");
add("random_effect");
add("restore_hunger");
add("take_damage");
add("unbreakable");
}};
}

View File

@ -3,10 +3,9 @@ package io.github.simplex.luck;
import io.github.simplex.luck.listener.*; import io.github.simplex.luck.listener.*;
import io.github.simplex.luck.player.PlayerConfig; import io.github.simplex.luck.player.PlayerConfig;
import io.github.simplex.luck.player.PlayerHandler; import io.github.simplex.luck.player.PlayerHandler;
import io.github.simplex.luck.util.CooldownTimer;
import io.github.simplex.luck.util.LuckCMD; import io.github.simplex.luck.util.LuckCMD;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import java.io.File; import java.io.File;
import java.util.Arrays; import java.util.Arrays;
@ -18,6 +17,7 @@ public final class FeelingLucky extends JavaPlugin {
private final Map<UUID, PlayerConfig> configMap = new HashMap<>(); private final Map<UUID, PlayerConfig> configMap = new HashMap<>();
private PlayerHandler handler; private PlayerHandler handler;
private Config config;
public Map<UUID, PlayerConfig> getConfigMap() { public Map<UUID, PlayerConfig> getConfigMap() {
return configMap; return configMap;
@ -30,21 +30,26 @@ public final class FeelingLucky extends JavaPlugin {
getLogger().info("Initialization complete! Attempting to register the Listeners..."); getLogger().info("Initialization complete! Attempting to register the Listeners...");
registerListeners(); registerListeners();
getLogger().info("Registration complete! Attempting to load all player configuration files..."); getLogger().info("Registration complete! Attempting to load all player configuration files...");
loadConfigurations(); loadPlayerConfigurations();
Bukkit.getLogger().info("Attempting to load the Luck command..."); getLogger().info("Attempting to load the main configuration...");
config = new Config(this);
getLogger().info("Main Config loaded successfully! Attempting to load the Luck command...");
new LuckCMD(this); new LuckCMD(this);
Bukkit.getLogger().info("Successfully loaded the Luck command!"); getLogger().info("Successfully loaded the Luck command!");
Bukkit.getLogger().info("Successfully initialized!"); getLogger().info("Successfully initialized!");
} }
@Override @Override
public void onDisable() { public void onDisable() {
Bukkit.getLogger().info("Saving all player configurations..."); getLogger().info("Saving all player configurations...");
configMap.values().forEach(PlayerConfig::save); configMap.values().forEach(PlayerConfig::save);
getLogger().info("Complete! Saving the main config...");
config.save();
getLogger().info("Complete! Goodbye! :)");
} }
private void loadConfigurations() { private void loadPlayerConfigurations() {
File[] files = getDataFolder().listFiles(); File[] files = getDataFolder().listFiles();
if (files != null) { if (files != null) {
Arrays.stream(files).forEach(file -> { Arrays.stream(files).forEach(file -> {
@ -76,4 +81,10 @@ public final class FeelingLucky extends JavaPlugin {
public PlayerHandler getHandler() { public PlayerHandler getHandler() {
return handler; return handler;
} }
@Override
@NotNull
public Config getConfig() {
return config;
}
} }

View File

@ -0,0 +1,46 @@
package io.github.simplex.luck.listener;
import io.github.simplex.luck.Config;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.PlayerHandler;
import org.bukkit.event.Listener;
import java.util.HashMap;
import java.util.Map;
public abstract class AbstractListener implements Listener {
protected final FeelingLucky plugin;
protected final Map<AbstractListener, Rarity> listenerRarityMap = new HashMap<>();
protected final Config config;
public AbstractListener(FeelingLucky plugin) {
this.plugin = plugin;
this.config = plugin.getConfig();
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
protected PlayerHandler getHandler() {
return plugin.getHandler();
}
public enum Rarity {
HIGH,
MED,
LOW,
NONE
}
public boolean doesQualify(String name, double luck) {
switch (config.getRarity(name)) {
case HIGH:
if (luck < config.getChance("high_rarity_chance")) return false;
case MED:
if (luck < config.getChance("medium_rarity_chance")) return false;
case LOW:
if (luck < config.getChance("low_rarity_chance")) return false;
case NONE:
return true;
}
throw new IllegalArgumentException("The value for the listener rarity is not a recognized rarity value.");
}
}

View File

@ -6,22 +6,21 @@ import io.github.simplex.luck.util.SneakyWorker;
import org.bukkit.entity.Item; import org.bukkit.entity.Item;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockDropItemEvent; import org.bukkit.event.block.BlockDropItemEvent;
import java.util.List; import java.util.List;
public record BlockDrops(FeelingLucky plugin) implements Listener { public final class BlockDrops extends AbstractListener {
public BlockDrops { public BlockDrops(FeelingLucky plugin) {
plugin.getServer().getPluginManager().registerEvents(this, plugin); super(plugin);
} }
@EventHandler @EventHandler
public void extraBlockDrops(BlockDropItemEvent event) { public void extraBlockDrops(BlockDropItemEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
Luck luck = plugin.getHandler().getLuckContainer(player); Luck luck = getHandler().getLuckContainer(player);
List<Item> items = event.getItems(); List<Item> items = event.getItems();
if (luck.quickRNG(luck.getPercentage())) { if (luck.quickRNG(luck.getPercentage()) && doesQualify("block_drops", luck.getPercentage())) {
items.forEach(SneakyWorker::move); items.forEach(SneakyWorker::move);
} }
} }

View File

@ -10,14 +10,13 @@ import org.bukkit.block.data.Ageable;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
public record BonemealFullCrop(FeelingLucky plugin) implements Listener { public final class BonemealFullCrop extends AbstractListener {
public BonemealFullCrop { public BonemealFullCrop(FeelingLucky plugin) {
plugin.getServer().getPluginManager().registerEvents(this, plugin); super(plugin);
} }
@EventHandler @EventHandler
@ -25,7 +24,7 @@ public record BonemealFullCrop(FeelingLucky plugin) implements Listener {
Player player = event.getPlayer(); Player player = event.getPlayer();
Action action = event.getAction(); Action action = event.getAction();
ItemStack bonemeal = ItemBuilder.of(Material.BONE_MEAL).build(); ItemStack bonemeal = ItemBuilder.of(Material.BONE_MEAL).build();
Luck luck = plugin.getHandler().getLuckContainer(player); Luck luck = getHandler().getLuckContainer(player);
ItemStack handItem = event.getItem(); ItemStack handItem = event.getItem();
if (handItem == null) return; if (handItem == null) return;
@ -38,12 +37,12 @@ public record BonemealFullCrop(FeelingLucky plugin) implements Listener {
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.getPercentage())
&& doesQualify("bonemeal", luck.getPercentage())) {
crop.setAge(crop.getMaximumAge()); crop.setAge(crop.getMaximumAge());
data.merge(crop); data.merge(crop);
block.setBlockData(data); block.setBlockData(data);
player.sendMessage(MiniComponent.info("You got lucky and your crops grew to maturity.")); player.sendMessage(MiniComponent.info("You got lucky and your crops grew to maturity."));
} }
} }
} }

View File

@ -5,26 +5,23 @@ import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck; import io.github.simplex.luck.player.Luck;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.entity.PlayerDeathEvent;
public record CheatDeath(FeelingLucky plugin) implements Listener { public final class CheatDeath extends AbstractListener {
public CheatDeath(FeelingLucky plugin) { public CheatDeath(FeelingLucky plugin) {
this.plugin = plugin; super(plugin);
plugin.getServer().getPluginManager().registerEvents(this, plugin);
} }
@EventHandler @EventHandler
public void cheatDeath(PlayerDeathEvent event) { public void cheatDeath(PlayerDeathEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
Luck luck = plugin.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())) { if (luck.quickRNG(luck.getPercentage()) && doesQualify("cheat_death", luck.getPercentage())) {
event.setCancelled(true); event.setCancelled(true);
player.setHealth(1.0); player.setHealth(1.0);
player.setAbsorptionAmount(absorption); player.setAbsorptionAmount(absorption);
player.sendMessage(MiniComponent.of("You got lucky and cheated death!").send()); player.sendMessage(MiniComponent.of("You got lucky and cheated death!").send());
} }
} }
} }

View File

@ -5,16 +5,14 @@ import io.github.simplex.luck.player.Luck;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.enchantment.EnchantItemEvent; import org.bukkit.event.enchantment.EnchantItemEvent;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public record EnchantmentBoost(FeelingLucky plugin) implements Listener { public final class EnchantmentBoost extends AbstractListener {
public EnchantmentBoost(FeelingLucky plugin) { public EnchantmentBoost(FeelingLucky plugin) {
this.plugin = plugin; super(plugin);
plugin.getServer().getPluginManager().registerEvents(this, plugin);
} }
@EventHandler @EventHandler
@ -22,8 +20,8 @@ public record EnchantmentBoost(FeelingLucky plugin) implements Listener {
Map<Enchantment, Integer> enchMap = event.getEnchantsToAdd(); Map<Enchantment, Integer> enchMap = event.getEnchantsToAdd();
List<Enchantment> enchList = enchMap.keySet().stream().toList(); List<Enchantment> enchList = enchMap.keySet().stream().toList();
Player player = event.getEnchanter(); Player player = event.getEnchanter();
Luck luck = plugin.getHandler().getLuckContainer(player); Luck luck = getHandler().getLuckContainer(player);
if (luck.quickRNG(luck.getPercentage())) { if (luck.quickRNG(luck.getPercentage()) && doesQualify("enchanting", luck.getPercentage())) {
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);
@ -34,4 +32,8 @@ public record EnchantmentBoost(FeelingLucky plugin) implements Listener {
enchMap.replace(particular, enchMap.get(particular) + rng); enchMap.replace(particular, enchMap.get(particular) + rng);
} }
} }
public FeelingLucky plugin() {
return plugin;
}
} }

View File

@ -6,12 +6,10 @@ import io.github.simplex.luck.player.Luck;
import org.bukkit.entity.ExperienceOrb; import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
public record ExpBoost(FeelingLucky plugin) implements Listener { public final class ExpBoost extends AbstractListener {
public ExpBoost(FeelingLucky plugin) { public ExpBoost(FeelingLucky plugin) {
this.plugin = plugin; super(plugin);
plugin.getServer().getPluginManager().registerEvents(this, plugin);
} }
@EventHandler @EventHandler
@ -22,7 +20,7 @@ public record ExpBoost(FeelingLucky plugin) implements Listener {
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())) { if (luck.quickRNG(luck.getPercentage()) && doesQualify("experience", luck.getPercentage())) {
orb.setExperience(rounded); orb.setExperience(rounded);
} }
} }

View File

@ -5,23 +5,22 @@ import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck; import io.github.simplex.luck.player.Luck;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPotionEffectEvent; import org.bukkit.event.entity.EntityPotionEffectEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
public record IllOmen(FeelingLucky plugin) implements Listener { public class IllOmen extends AbstractListener {
public IllOmen { public IllOmen(FeelingLucky plugin) {
plugin.getServer().getPluginManager().registerEvents(this, plugin); super(plugin);
} }
@EventHandler @EventHandler
public void reconnectCheck(PlayerJoinEvent event) { public void reconnectCheck(PlayerJoinEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
PotionEffectType type = PotionEffectType.BAD_OMEN; PotionEffectType type = PotionEffectType.BAD_OMEN;
Luck luck = plugin.getHandler().getLuckContainer(player); Luck luck = getHandler().getLuckContainer(player);
if (player.hasPotionEffect(type)) { if (player.hasPotionEffect(type)) {
luck.cache(); luck.cache();

View File

@ -7,8 +7,8 @@ import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.*; import org.bukkit.entity.*;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityDropItemEvent; import org.bukkit.event.entity.EntityDropItemEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -16,13 +16,12 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
public class ItemDrops implements Listener { public class ItemDrops extends AbstractListener {
private final Map<UUID, Player> entityPlayerMap = new HashMap<>(); private final Map<UUID, Player> entityPlayerMap = new HashMap<>();
private final FeelingLucky plugin; private boolean canAffect = false;
public ItemDrops(FeelingLucky plugin) { public ItemDrops(FeelingLucky plugin) {
this.plugin = plugin; super(plugin);
plugin.getServer().getPluginManager().registerEvents(this, plugin);
} }
@EventHandler @EventHandler
@ -39,16 +38,17 @@ public class ItemDrops implements Listener {
return; return;
} }
if (entity instanceof Witch witch) { entityPlayerMap.put(entity.getUniqueId(), player);
if (Luck.quickRNGnoMultiplier(33.0)) {
Location location = witch.getLocation();
World world = location.getWorld();
Item item = world.dropItemNaturally(location, new ItemStack(Material.RABBIT_FOOT, 1));
new EntityDropItemEvent(witch, item).callEvent();
}
} }
entityPlayerMap.put(entity.getUniqueId(), player); @EventHandler
public void checkForDroppedItems(EntityDeathEvent event) {
if (event.getEntity() instanceof Player) {
canAffect = false;
return;
}
canAffect = event.getDrops().isEmpty();
} }
@EventHandler @EventHandler
@ -57,12 +57,14 @@ public class ItemDrops implements Listener {
if (entityPlayerMap.get(entity.getUniqueId()) == null) return; if (entityPlayerMap.get(entity.getUniqueId()) == null) return;
if (!canAffect) return;
Player player = entityPlayerMap.get(entity.getUniqueId()); Player player = entityPlayerMap.get(entity.getUniqueId());
Luck luck = plugin.getHandler().getLuckContainer(player); Luck luck = getHandler().getLuckContainer(player);
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())) { if (luck.quickRNG(luck.getPercentage()) && doesQualify("item_drops", luck.getPercentage())) {
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

@ -22,14 +22,12 @@ import org.bukkit.inventory.ItemStack;
import java.util.Objects; import java.util.Objects;
public final class PlayerListener implements Listener { public final class PlayerListener extends AbstractListener {
private final FeelingLucky plugin;
private final CooldownTimer timer; private final CooldownTimer timer;
public PlayerListener(FeelingLucky plugin) { public PlayerListener(FeelingLucky plugin) {
this.plugin = plugin; super(plugin);
this.timer = new CooldownTimer(); this.timer = new CooldownTimer();
Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
} }
@EventHandler @EventHandler
@ -38,7 +36,7 @@ public final class PlayerListener implements Listener {
ItemStack foot = new ItemStack(Material.RABBIT_FOOT); ItemStack foot = new ItemStack(Material.RABBIT_FOOT);
SpecialFootItem special = new SpecialFootItem(); SpecialFootItem special = new SpecialFootItem();
Player player = event.getPlayer(); Player player = event.getPlayer();
Luck luck = plugin.getHandler().getLuckContainer(player); Luck luck = getHandler().getLuckContainer(player);
if (timer.onCooldown(player)) { if (timer.onCooldown(player)) {
player.sendMessage(MiniComponent.err("That feature can only be used once every 30 seconds.")); player.sendMessage(MiniComponent.err("That feature can only be used once every 30 seconds."));

View File

@ -5,27 +5,26 @@ import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck; import io.github.simplex.luck.player.Luck;
import io.github.simplex.luck.util.ListBox; import io.github.simplex.luck.util.ListBox;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerItemConsumeEvent; import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
public record RestoreHunger(FeelingLucky plugin) implements Listener { public class RestoreHunger extends AbstractListener {
public RestoreHunger { public RestoreHunger(FeelingLucky plugin) {
plugin.getServer().getPluginManager().registerEvents(this, plugin); super(plugin);
} }
@EventHandler @EventHandler
public void restoreHunger(PlayerItemConsumeEvent event) { public void restoreHunger(PlayerItemConsumeEvent event) {
ItemStack item = event.getItem(); ItemStack item = event.getItem();
Luck luck = plugin.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.getPercentage();
ListBox.foods.forEach(food -> { ListBox.foods.forEach(food -> {
if (item.isSimilar(food)) { if (item.isSimilar(food)) {
if (luck.quickRNG(percentage)) { if (luck.quickRNG(percentage) && doesQualify("restore_hunger", percentage)) {
event.getPlayer().setExhaustion(event.getPlayer().getExhaustion() + 2); event.getPlayer().setExhaustion(event.getPlayer().getExhaustion() + 2);
event.getPlayer().addPotionEffect(effect); event.getPlayer().addPotionEffect(effect);
} }

View File

@ -8,12 +8,11 @@ import net.kyori.adventure.text.Component;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent;
public record TakeDamage(FeelingLucky plugin) implements Listener { public class TakeDamage extends AbstractListener {
public TakeDamage { public TakeDamage(FeelingLucky plugin) {
plugin.getServer().getPluginManager().registerEvents(this, plugin); super(plugin);
} }
@EventHandler @EventHandler
@ -23,7 +22,7 @@ public record TakeDamage(FeelingLucky plugin) implements Listener {
return; return;
} }
Player player = (Player) event.getEntity(); Player player = (Player) event.getEntity();
Luck luck = plugin.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.getPercentage();
@ -42,7 +41,7 @@ public record TakeDamage(FeelingLucky plugin) implements Listener {
return; return;
} }
if (luck.quickRNG(percentage)) { if (luck.quickRNG(percentage) && doesQualify("take_damage", percentage)) {
event.setCancelled(true); event.setCancelled(true);
player.damage(event.getDamage() / 2); player.damage(event.getDamage() / 2);
player.sendMessage(Component.empty().content("You got lucky and took less damage.")); player.sendMessage(Component.empty().content("You got lucky and took less damage."));
@ -67,7 +66,7 @@ public record TakeDamage(FeelingLucky plugin) implements Listener {
return; return;
} }
if (luck.quickRNG(percentage)) { if (luck.quickRNG(percentage) && doesQualify("take_damage", percentage)) {
event.setCancelled(true); event.setCancelled(true);
player.getActivePotionEffects().removeIf(p -> ListBox.potionEffects.contains(p.getType())); player.getActivePotionEffects().removeIf(p -> ListBox.potionEffects.contains(p.getType()));
player.setFireTicks(0); player.setFireTicks(0);

View File

@ -6,15 +6,14 @@ import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck; import io.github.simplex.luck.player.Luck;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.CraftItemEvent; import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.inventory.CraftingInventory; import org.bukkit.inventory.CraftingInventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
public record UnbreakableTool(FeelingLucky plugin) implements Listener { public class UnbreakableTool extends AbstractListener {
public UnbreakableTool { public UnbreakableTool(FeelingLucky plugin) {
plugin.getServer().getPluginManager().registerEvents(this, plugin); super(plugin);
} }
@EventHandler @EventHandler
@ -27,8 +26,8 @@ public record UnbreakableTool(FeelingLucky plugin) implements Listener {
if (ItemBuilder.isTool(stack.getType())) { if (ItemBuilder.isTool(stack.getType())) {
if (event.getWhoClicked() instanceof Player player) { if (event.getWhoClicked() instanceof Player player) {
Luck luck = plugin.getHandler().getLuckContainer(player); Luck luck = getHandler().getLuckContainer(player);
if (luck.quickRNG(luck.getPercentage())) { if (luck.quickRNG(luck.getPercentage()) && doesQualify("unbreakable", luck.getPercentage())) {
meta.setUnbreakable(true); meta.setUnbreakable(true);
stack.setItemMeta(meta); stack.setItemMeta(meta);
inventory.setResult(stack); inventory.setResult(stack);

View File

@ -15,14 +15,12 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
public class VillagerInventory implements Listener { public class VillagerInventory extends AbstractListener {
private final SpecialFootItem foot = new SpecialFootItem(); private final SpecialFootItem foot = new SpecialFootItem();
private final MerchantRecipe recipe = new MerchantRecipe(foot.get(), 0, 2, true); private final MerchantRecipe recipe = new MerchantRecipe(foot.get(), 0, 2, true);
private final FeelingLucky plugin;
public VillagerInventory(FeelingLucky plugin) { public VillagerInventory(FeelingLucky plugin) {
this.plugin = plugin; super(plugin);
recipe.setIngredients(Arrays.asList( recipe.setIngredients(Arrays.asList(
ItemBuilder.of(Material.EMERALD).build(), ItemBuilder.of(Material.EMERALD).build(),
@ -32,8 +30,6 @@ public class VillagerInventory implements Listener {
recipe.setPriceMultiplier(1.25F); recipe.setPriceMultiplier(1.25F);
recipe.setVillagerExperience(25); recipe.setVillagerExperience(25);
recipe.setSpecialPrice(4); recipe.setSpecialPrice(4);
plugin.getServer().getPluginManager().registerEvents(this, plugin);
} }
@EventHandler @EventHandler

View File

@ -2,6 +2,8 @@ package io.github.simplex.luck.player;
import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.util.SneakyWorker; import io.github.simplex.luck.util.SneakyWorker;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.attribute.Attribute; import org.bukkit.attribute.Attribute;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -11,16 +13,18 @@ import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.UUID;
public class PlayerConfig { public class PlayerConfig {
private final File configFile; private final File configFile;
private final FeelingLucky plugin; private final OfflinePlayer player;
private volatile YamlConfiguration config; private volatile YamlConfiguration config;
@SuppressWarnings("ResultOfMethodCallIgnored") @SuppressWarnings("ResultOfMethodCallIgnored")
public PlayerConfig(FeelingLucky plugin, Player player) { public PlayerConfig(FeelingLucky plugin, Player player) {
this.plugin = plugin; this.player = player;
File dataFolder = plugin.getDataFolder(); if (!plugin.getDataFolder().exists()) plugin.getDataFolder().mkdirs();
File dataFolder = new File(plugin.getDataFolder(), "players");
if (!dataFolder.exists()) dataFolder.mkdirs(); if (!dataFolder.exists()) dataFolder.mkdirs();
File file = new File(dataFolder, player.getUniqueId() + ".yml"); File file = new File(dataFolder, player.getUniqueId() + ".yml");
if (!file.exists()) { if (!file.exists()) {
@ -54,8 +58,8 @@ public class PlayerConfig {
} }
protected PlayerConfig(FeelingLucky plugin, File file) { protected PlayerConfig(FeelingLucky plugin, File file) {
this.plugin = plugin;
this.configFile = file; this.configFile = file;
this.player = Bukkit.getOfflinePlayer(UUID.fromString(file.getName().split("\\.")[0]));
config = YamlConfiguration.loadConfiguration(configFile); config = YamlConfiguration.loadConfiguration(configFile);
} }
@ -77,6 +81,10 @@ public class PlayerConfig {
load(); load();
} }
public OfflinePlayer getPlayer() {
return player;
}
public void setUsername(String name) { public void setUsername(String name) {
config.set("username", name); config.set("username", name);
save(); save();

View File

@ -16,6 +16,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID;
public class LuckCMD extends Command implements TabCompleter { public class LuckCMD extends Command implements TabCompleter {
private final FeelingLucky plugin; private final FeelingLucky plugin;
@ -33,6 +34,19 @@ public class LuckCMD extends Command implements TabCompleter {
if (args.length == 3) { if (args.length == 3) {
if ((sender instanceof ConsoleCommandSender) || sender.hasPermission("luck.admin")) { if ((sender instanceof ConsoleCommandSender) || sender.hasPermission("luck.admin")) {
if (args[0].equalsIgnoreCase("reload") && args[1].equalsIgnoreCase("-p")) {
Player player = Bukkit.getPlayer(args[2]);
if (player == null) {
sender.sendMessage(Messages.NO_PLAYER.get());
return true;
}
UUID uuid = player.getUniqueId();
PlayerConfig config = plugin.getConfigMap().get(uuid);
config.reload();
sender.sendMessage(MiniComponent.info("Successfully reloaded " + player.getName() + "'s configuration file."));
return true;
}
Player player = Bukkit.getPlayer(args[1]); Player player = Bukkit.getPlayer(args[1]);
double amount = Double.parseDouble(args[2]); double amount = Double.parseDouble(args[2]);
@ -80,6 +94,10 @@ public class LuckCMD extends Command implements TabCompleter {
if (args.length == 2) { if (args.length == 2) {
if ((sender instanceof ConsoleCommandSender) || sender.hasPermission("luck.admin")) { if ((sender instanceof ConsoleCommandSender) || sender.hasPermission("luck.admin")) {
if (args[0].equalsIgnoreCase("reload") && args[1].equalsIgnoreCase("-m")) {
}
if (args[0].equalsIgnoreCase("info")) { if (args[0].equalsIgnoreCase("info")) {
Player player = Bukkit.getPlayer(args[1]); Player player = Bukkit.getPlayer(args[1]);

View File

@ -0,0 +1,26 @@
# These entries are for the minimum amount of luck required for the event to actually trigger.
# These work in addition to the RNG generator,
# meaning both the RNG and this condition must be met for the event to trigger.
# These values must be in the form of doubles, as listed below.
# The maximum amount of luck that can be attributed is 1024.0, and the minimum is -1024.0
high_rarity_chance: 512
medium_rarity_chance: 128
low_rarity_chance: 64
# The following entries are for the rarity level of each event trigger.
# This will determine which rarity chance to use which ensures players
# The following values are accepted: NONE, LOW, MED, HIGH
# - None implies that there is no rarity chance attributed to that feature.
# These entries are case-sensitive.
block_drops: LOW
bonemeal: LOW
cheat_death: MED
enchanting: HIGH
experience: HIGH
item_drops: LOW
random_effect: HIGH
restore_hunger: MED
take_damage: MED
unbreakable: HIGH