mirror of
https://github.com/SimplexDevelopment/FeelingLucky.git
synced 2025-07-04 11:06:43 +00:00
Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
474abcdb0d |
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.
@ -6,7 +6,6 @@ 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();
|
Attribute asAttribute();
|
||||||
|
|
||||||
double getNumber();
|
double getNumber();
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
package io.github.simplex.luck;
|
package io.github.simplex.luck;
|
||||||
|
|
||||||
import io.github.simplex.luck.listener.*;
|
import io.github.simplex.luck.listener.PlayerListener;
|
||||||
|
import io.github.simplex.luck.player.Luck;
|
||||||
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 org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -11,12 +13,16 @@ import java.util.Arrays;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
public final class FeelingLucky extends JavaPlugin {
|
public final class FeelingLucky extends JavaPlugin {
|
||||||
private final Map<UUID, PlayerConfig> configMap = new HashMap<>();
|
private final Map<UUID, PlayerConfig> configMap = new HashMap<>();
|
||||||
|
private final Map<OfflinePlayer, Luck> offlinePlayerLuckMap = new HashMap<>();
|
||||||
public LuckCMD cmd;
|
public LuckCMD cmd;
|
||||||
public PlayerHandler handler;
|
public PlayerHandler handler;
|
||||||
|
public PlayerListener playerListener;
|
||||||
|
|
||||||
|
public static FeelingLucky plugin = getPlugin(FeelingLucky.class);
|
||||||
|
|
||||||
public Map<UUID, PlayerConfig> getConfigMap() {
|
public Map<UUID, PlayerConfig> getConfigMap() {
|
||||||
return configMap;
|
return configMap;
|
||||||
@ -24,16 +30,11 @@ public final class FeelingLucky extends JavaPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
getLogger().info("Initializing the PlayerHandler...");
|
Bukkit.getLogger().info("Initializing the PlayerHandler...");
|
||||||
handler = new PlayerHandler(this);
|
handler = new PlayerHandler(this);
|
||||||
getLogger().info("Initialization complete! Attempting to register the Listeners...");
|
Bukkit.getLogger().info("Initialization complete! Attempting to register the Listeners...");
|
||||||
new PlayerListener(this);
|
playerListener = new PlayerListener(this);
|
||||||
new BlockDrops(this);
|
Bukkit.getLogger().info("Registration complete! Attempting to load all player configuration files...");
|
||||||
new ItemDrops(this);
|
|
||||||
new TakeDamage(this);
|
|
||||||
new RestoreHunger(this);
|
|
||||||
new EnchantmentBoost(this);
|
|
||||||
getLogger().info("Registration complete! Attempting to load all player configuration files...");
|
|
||||||
|
|
||||||
File[] files = getDataFolder().listFiles();
|
File[] files = getDataFolder().listFiles();
|
||||||
if (files != null) {
|
if (files != null) {
|
||||||
|
@ -39,11 +39,6 @@ public class LuckCMD extends Command implements TabCompleter {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (amount > 1024.0 || amount < -1024.0) {
|
|
||||||
sender.sendMessage(Component.empty().content("Number must be between -1024.0 and 1024.0"));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
Luck luck = plugin.handler.getLuckContainer(player);
|
Luck luck = plugin.handler.getLuckContainer(player);
|
||||||
PlayerConfig config = plugin.getConfigMap().get(player.getUniqueId());
|
PlayerConfig config = plugin.getConfigMap().get(player.getUniqueId());
|
||||||
|
|
||||||
@ -114,11 +109,6 @@ public class LuckCMD extends Command implements TabCompleter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
if (args[0].equalsIgnoreCase("reload") && sender.hasPermission("luck.admin")) {
|
|
||||||
plugin.getConfigMap().values().forEach(PlayerConfig::reload);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
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.handler.getLuckContainer(player);
|
Luck luck = plugin.handler.getLuckContainer(player);
|
||||||
@ -132,7 +122,7 @@ public class LuckCMD extends Command implements TabCompleter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -158,7 +148,6 @@ public class LuckCMD extends Command implements TabCompleter {
|
|||||||
switch (args[0]) {
|
switch (args[0]) {
|
||||||
case "info":
|
case "info":
|
||||||
case "reset":
|
case "reset":
|
||||||
case "reload":
|
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
case "give":
|
case "give":
|
||||||
case "take":
|
case "take":
|
||||||
|
@ -1,29 +0,0 @@
|
|||||||
package io.github.simplex.luck.listener;
|
|
||||||
|
|
||||||
import io.github.simplex.luck.FeelingLucky;
|
|
||||||
import io.github.simplex.luck.SneakyWorker;
|
|
||||||
import io.github.simplex.luck.player.Luck;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.entity.Item;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.EventHandler;
|
|
||||||
import org.bukkit.event.Listener;
|
|
||||||
import org.bukkit.event.block.BlockDropItemEvent;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public record BlockDrops(FeelingLucky plugin) implements Listener {
|
|
||||||
public BlockDrops {
|
|
||||||
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
|
||||||
public void extraBlockDrops(BlockDropItemEvent event) {
|
|
||||||
Player player = event.getPlayer();
|
|
||||||
Luck luck = plugin.handler.getLuckContainer(player);
|
|
||||||
List<Item> items = event.getItems();
|
|
||||||
if (luck.quickRNG(luck.getPercentage())) {
|
|
||||||
items.forEach(SneakyWorker::move);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,37 +0,0 @@
|
|||||||
package io.github.simplex.luck.listener;
|
|
||||||
|
|
||||||
import io.github.simplex.luck.FeelingLucky;
|
|
||||||
import io.github.simplex.luck.player.Luck;
|
|
||||||
import org.bukkit.enchantments.Enchantment;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.EventHandler;
|
|
||||||
import org.bukkit.event.Listener;
|
|
||||||
import org.bukkit.event.enchantment.EnchantItemEvent;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public record EnchantmentBoost(FeelingLucky plugin) implements Listener {
|
|
||||||
public EnchantmentBoost(FeelingLucky plugin) {
|
|
||||||
this.plugin = plugin;
|
|
||||||
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
|
||||||
public void enchantItem(EnchantItemEvent event) {
|
|
||||||
Map<Enchantment, Integer> enchMap = event.getEnchantsToAdd();
|
|
||||||
List<Enchantment> enchList = enchMap.keySet().stream().toList();
|
|
||||||
Player player = event.getEnchanter();
|
|
||||||
Luck luck = plugin.handler.getLuckContainer(player);
|
|
||||||
if (luck.quickRNG(luck.getPercentage())) {
|
|
||||||
Enchantment particular = enchList.get(Luck.RNG().nextInt(enchList.size()));
|
|
||||||
int rng = Luck.RNG().nextInt(1, 5);
|
|
||||||
|
|
||||||
if ((enchMap.get(particular) + rng) > particular.getMaxLevel()) {
|
|
||||||
enchMap.replace(particular, particular.getMaxLevel());
|
|
||||||
}
|
|
||||||
|
|
||||||
enchMap.replace(particular, enchMap.get(particular) + rng);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,73 +0,0 @@
|
|||||||
package io.github.simplex.luck.listener;
|
|
||||||
|
|
||||||
import io.github.simplex.luck.FeelingLucky;
|
|
||||||
import io.github.simplex.luck.player.Luck;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.Location;
|
|
||||||
import org.bukkit.Material;
|
|
||||||
import org.bukkit.World;
|
|
||||||
import org.bukkit.entity.*;
|
|
||||||
import org.bukkit.event.EventHandler;
|
|
||||||
import org.bukkit.event.Listener;
|
|
||||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
|
||||||
import org.bukkit.event.entity.EntityDropItemEvent;
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public class ItemDrops implements Listener {
|
|
||||||
private final Map<UUID, Player> entityPlayerMap = new HashMap<>();
|
|
||||||
private final FeelingLucky plugin;
|
|
||||||
|
|
||||||
public ItemDrops(FeelingLucky plugin) {
|
|
||||||
this.plugin = plugin;
|
|
||||||
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
|
||||||
public void checkForPreItemDrop(EntityDamageByEntityEvent event) {
|
|
||||||
if (!(event.getEntity() instanceof LivingEntity entity)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(event.getDamager() instanceof Player player)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(entity.getHealth() <= 0.0)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (entity instanceof Witch witch) {
|
|
||||||
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 itemDrops(EntityDropItemEvent event) {
|
|
||||||
Entity entity = event.getEntity();
|
|
||||||
|
|
||||||
if (entityPlayerMap.get(entity.getUniqueId()) == null) return;
|
|
||||||
|
|
||||||
Player player = entityPlayerMap.get(entity.getUniqueId());
|
|
||||||
Luck luck = plugin.handler.getLuckContainer(player);
|
|
||||||
Item item = event.getItemDrop();
|
|
||||||
ItemStack stack = item.getItemStack();
|
|
||||||
int amount = stack.getAmount();
|
|
||||||
if (luck.quickRNG(luck.getPercentage())) {
|
|
||||||
int rng = Luck.RNG().nextInt(2, 5);
|
|
||||||
amount += rng;
|
|
||||||
stack.setAmount(amount);
|
|
||||||
event.getItemDrop().setItemStack(stack);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,27 +1,177 @@
|
|||||||
package io.github.simplex.luck.listener;
|
package io.github.simplex.luck.listener;
|
||||||
|
|
||||||
|
import io.github.simplex.lib.PotionEffectBuilder;
|
||||||
import io.github.simplex.luck.FeelingLucky;
|
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.Luck;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.Location;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.entity.Entity;
|
import org.bukkit.World;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.*;
|
||||||
import org.bukkit.entity.Witch;
|
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.block.Action;
|
import org.bukkit.event.block.Action;
|
||||||
|
import org.bukkit.event.block.BlockDropItemEvent;
|
||||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||||
import org.bukkit.event.entity.EntityDamageEvent;
|
import org.bukkit.event.entity.EntityDamageEvent;
|
||||||
|
import org.bukkit.event.entity.EntityDropItemEvent;
|
||||||
import org.bukkit.event.player.PlayerInteractEvent;
|
import org.bukkit.event.player.PlayerInteractEvent;
|
||||||
|
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.PotionEffectType;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class PlayerListener implements Listener {
|
||||||
|
private final Map<UUID, Player> entityPlayerMap = new HashMap<>();
|
||||||
|
private final FeelingLucky plugin;
|
||||||
|
|
||||||
public record PlayerListener(FeelingLucky plugin) implements Listener {
|
|
||||||
public PlayerListener(FeelingLucky plugin) {
|
public PlayerListener(FeelingLucky plugin) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
|
Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void takeDamage(EntityDamageEvent event) {
|
||||||
|
Entity entity = event.getEntity();
|
||||||
|
if (!(entity instanceof Player)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Player player = (Player) event.getEntity();
|
||||||
|
Luck luck = plugin.handler.getLuckContainer(player);
|
||||||
|
if (ListBox.acceptedCauses.contains(event.getCause())) {
|
||||||
|
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 || luck.isMarked(player)) {
|
||||||
|
percentage = Math.abs(percentage);
|
||||||
|
if (luck.quickRNG(percentage)) {
|
||||||
|
event.setCancelled(true);
|
||||||
|
player.damage(event.getDamage() * 2);
|
||||||
|
player.sendMessage(Component.empty().content("You were unlucky and took double damage."));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (luck.quickRNG(percentage)) {
|
||||||
|
event.setCancelled(true);
|
||||||
|
player.damage(event.getDamage() / 2);
|
||||||
|
player.sendMessage(Component.empty().content("You got lucky and took less damage."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ListBox.sideCauses.contains(event.getCause())) {
|
||||||
|
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 || luck.isMarked(player)) {
|
||||||
|
percentage = Math.abs(percentage);
|
||||||
|
if (luck.quickRNG(percentage)) {
|
||||||
|
event.setCancelled(true);
|
||||||
|
player.addPotionEffect(PotionEffectBuilder.newEffect().type(ListBox.potionEffects.get(Luck.RNG().nextInt(ListBox.potionEffects.size() - 1))).amplifier(Luck.RNG().nextInt(1, 5)).duration(Luck.RNG().nextInt(1, 120)).create());
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (luck.quickRNG(percentage)) {
|
||||||
|
event.setCancelled(true);
|
||||||
|
player.getActivePotionEffects().removeIf(p -> ListBox.potionEffects.contains(p.getType()));
|
||||||
|
player.setFireTicks(0);
|
||||||
|
player.sendMessage(Component.empty().content("You got lucky and your afflictions were cured."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void extraBlockDrops(BlockDropItemEvent event) {
|
||||||
|
Player player = event.getPlayer();
|
||||||
|
Luck luck = plugin.handler.getLuckContainer(player);
|
||||||
|
List<Item> items = event.getItems();
|
||||||
|
if (luck.quickRNG(luck.getPercentage())) {
|
||||||
|
items.forEach(SneakyWorker::move);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void checkForPreItemDrop(EntityDamageByEntityEvent event) {
|
||||||
|
if (!(event.getEntity() instanceof LivingEntity entity)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(event.getDamager() instanceof Player player)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(entity.getHealth() <= 0.0)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entity instanceof Witch witch) {
|
||||||
|
if (Luck.quickRNG2(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 itemDrops(EntityDropItemEvent event) {
|
||||||
|
Entity entity = event.getEntity();
|
||||||
|
|
||||||
|
if (entityPlayerMap.get(entity.getUniqueId()) == null) return;
|
||||||
|
|
||||||
|
Player player = entityPlayerMap.get(entity.getUniqueId());
|
||||||
|
Luck luck = plugin.handler.getLuckContainer(player);
|
||||||
|
Item item = event.getItemDrop();
|
||||||
|
ItemStack stack = item.getItemStack();
|
||||||
|
int amount = stack.getAmount();
|
||||||
|
if (luck.quickRNG(luck.getPercentage())) {
|
||||||
|
int rng = Luck.RNG().nextInt(2, 5);
|
||||||
|
amount += rng;
|
||||||
|
stack.setAmount(amount);
|
||||||
|
event.getItemDrop().setItemStack(stack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void restoreHunger(PlayerItemConsumeEvent event) {
|
||||||
|
ItemStack item = event.getItem();
|
||||||
|
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();
|
||||||
|
ListBox.foods.forEach(food -> {
|
||||||
|
if (item.isSimilar(food)) {
|
||||||
|
if (luck.quickRNG(percentage)) {
|
||||||
|
event.getPlayer().setExhaustion(event.getPlayer().getExhaustion() + 2);
|
||||||
|
event.getPlayer().addPotionEffect(effect);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void rabbitFoot(PlayerInteractEvent event) {
|
public void rabbitFoot(PlayerInteractEvent event) {
|
||||||
Action action = event.getAction();
|
Action action = event.getAction();
|
||||||
|
@ -1,37 +0,0 @@
|
|||||||
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 org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.event.EventHandler;
|
|
||||||
import org.bukkit.event.Listener;
|
|
||||||
import org.bukkit.event.player.PlayerItemConsumeEvent;
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
|
||||||
import org.bukkit.potion.PotionEffect;
|
|
||||||
import org.bukkit.potion.PotionEffectType;
|
|
||||||
|
|
||||||
public record RestoreHunger(FeelingLucky plugin) implements Listener {
|
|
||||||
public RestoreHunger {
|
|
||||||
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
|
||||||
public void restoreHunger(PlayerItemConsumeEvent event) {
|
|
||||||
ItemStack item = event.getItem();
|
|
||||||
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();
|
|
||||||
ListBox.foods.forEach(food -> {
|
|
||||||
if (item.isSimilar(food)) {
|
|
||||||
if (luck.quickRNG(percentage)) {
|
|
||||||
event.getPlayer().setExhaustion(event.getPlayer().getExhaustion() + 2);
|
|
||||||
event.getPlayer().addPotionEffect(effect);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,80 +0,0 @@
|
|||||||
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 net.kyori.adventure.text.Component;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.entity.Entity;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.EventHandler;
|
|
||||||
import org.bukkit.event.Listener;
|
|
||||||
import org.bukkit.event.entity.EntityDamageEvent;
|
|
||||||
|
|
||||||
public record TakeDamage(FeelingLucky plugin) implements Listener {
|
|
||||||
public TakeDamage {
|
|
||||||
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
|
||||||
public void takeDamage(EntityDamageEvent event) {
|
|
||||||
Entity entity = event.getEntity();
|
|
||||||
if (!(entity instanceof Player)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Player player = (Player) event.getEntity();
|
|
||||||
Luck luck = plugin.handler.getLuckContainer(player);
|
|
||||||
if (ListBox.acceptedCauses.contains(event.getCause())) {
|
|
||||||
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 || luck.isMarked(player)) {
|
|
||||||
percentage = Math.abs(percentage);
|
|
||||||
if (luck.quickRNG(percentage)) {
|
|
||||||
event.setCancelled(true);
|
|
||||||
player.damage(event.getDamage() * 2);
|
|
||||||
player.sendMessage(Component.empty().content("You were unlucky and took double damage."));
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (luck.quickRNG(percentage)) {
|
|
||||||
event.setCancelled(true);
|
|
||||||
player.damage(event.getDamage() / 2);
|
|
||||||
player.sendMessage(Component.empty().content("You got lucky and took less damage."));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ListBox.sideCauses.contains(event.getCause())) {
|
|
||||||
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 || luck.isMarked(player)) {
|
|
||||||
percentage = Math.abs(percentage);
|
|
||||||
if (luck.quickRNG(percentage)) {
|
|
||||||
event.setCancelled(true);
|
|
||||||
player.addPotionEffect(PotionEffectBuilder.newEffect().type(ListBox.potionEffects.get(Luck.RNG().nextInt(ListBox.potionEffects.size() - 1))).amplifier(Luck.RNG().nextInt(1, 5)).duration(Luck.RNG().nextInt(1, 120)).create());
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (luck.quickRNG(percentage)) {
|
|
||||||
event.setCancelled(true);
|
|
||||||
player.getActivePotionEffects().removeIf(p -> ListBox.potionEffects.contains(p.getType()));
|
|
||||||
player.setFireTicks(0);
|
|
||||||
player.sendMessage(Component.empty().content("You got lucky and your afflictions were cured."));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +1,6 @@
|
|||||||
package io.github.simplex.luck.player;
|
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 org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.attribute.Attribute;
|
import org.bukkit.attribute.Attribute;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
@ -12,50 +11,27 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.SplittableRandom;
|
import java.util.SplittableRandom;
|
||||||
|
|
||||||
|
import static io.github.simplex.luck.FeelingLucky.plugin;
|
||||||
|
|
||||||
@SuppressWarnings("all")
|
@SuppressWarnings("all")
|
||||||
public class Luck implements LuckContainer {
|
public class Luck implements LuckContainer {
|
||||||
private final Player player;
|
private final Player player;
|
||||||
private final double multiplier;
|
private final double multiplier;
|
||||||
|
private final double BASE_VALUE;
|
||||||
private final PlayerLuckChangeEvent event;
|
private final PlayerLuckChangeEvent event;
|
||||||
private final FeelingLucky plugin;
|
|
||||||
private final List<Player> markedPlayers = new ArrayList<>();
|
|
||||||
private double BASE_VALUE;
|
|
||||||
|
|
||||||
public Luck(FeelingLucky plugin, Player player) {
|
public Luck(Player player) {
|
||||||
this(plugin, player, 1.0);
|
this(player, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Luck(FeelingLucky plugin, Player player, double multiplier) {
|
public Luck(Player player, double multiplier) {
|
||||||
this.player = player;
|
this.player = player;
|
||||||
this.multiplier = multiplier;
|
this.multiplier = multiplier;
|
||||||
this.plugin = plugin;
|
BASE_VALUE = player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue();
|
||||||
BASE_VALUE = plugin.getConfigMap().get(player.getUniqueId()).getConfig().getDouble("luck");
|
|
||||||
|
|
||||||
event = new PlayerLuckChangeEvent(this);
|
event = new PlayerLuckChangeEvent(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Contract(pure = true,
|
private final List<Player> markedPlayers = new ArrayList<>();
|
||||||
value = "-> new")
|
|
||||||
public static @NotNull SplittableRandom RNG() {
|
|
||||||
return new SplittableRandom();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean quickRNGnoMultiplier(double percentage) {
|
|
||||||
double rng;
|
|
||||||
if (percentage >= 100.0) {
|
|
||||||
rng = 1024.0; // 100% chance to trigger, obviously;
|
|
||||||
} else {
|
|
||||||
rng = RNG().nextDouble(0.0, 1024.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
double actual = Math.round((rng / 1024.0) * 100);
|
|
||||||
|
|
||||||
return (percentage >= actual);
|
|
||||||
}
|
|
||||||
|
|
||||||
public FeelingLucky getPlugin() {
|
|
||||||
return plugin;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void markPlayer(Player player) {
|
public void markPlayer(Player player) {
|
||||||
markedPlayers.add(player);
|
markedPlayers.add(player);
|
||||||
@ -69,6 +45,25 @@ public class Luck implements LuckContainer {
|
|||||||
return markedPlayers.contains(player);
|
return markedPlayers.contains(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Contract(pure = true,
|
||||||
|
value = "-> new")
|
||||||
|
public static @NotNull SplittableRandom RNG() {
|
||||||
|
return new SplittableRandom();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean quickRNG2(double percentage) {
|
||||||
|
double rng;
|
||||||
|
if (percentage >= 100.0) {
|
||||||
|
rng = 1024.0; // 100% chance to trigger, obviously;
|
||||||
|
} else {
|
||||||
|
rng = RNG().nextDouble(0.0, 1024.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
double actual = Math.round((rng / 1024.0) * 100);
|
||||||
|
|
||||||
|
return (percentage >= actual);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Attribute asAttribute() {
|
public Attribute asAttribute() {
|
||||||
return Attribute.GENERIC_LUCK;
|
return Attribute.GENERIC_LUCK;
|
||||||
@ -125,17 +120,16 @@ public class Luck implements LuckContainer {
|
|||||||
return BASE_VALUE;
|
return BASE_VALUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double getDefaultValue() {
|
||||||
|
return player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue();
|
||||||
|
}
|
||||||
|
|
||||||
public void setValue(double value) {
|
public void setValue(double value) {
|
||||||
BASE_VALUE = value;
|
|
||||||
player.getAttribute(Attribute.GENERIC_LUCK).setBaseValue(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);
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getDefaultValue() {
|
|
||||||
return player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addTo(double value) {
|
public void addTo(double value) {
|
||||||
if (value >= 1024.0 || (getValue() + value) >= 1024.0) {
|
if (value >= 1024.0 || (getValue() + value) >= 1024.0) {
|
||||||
setValue(1024.0);
|
setValue(1024.0);
|
||||||
|
@ -45,14 +45,11 @@ public class PlayerConfig {
|
|||||||
|
|
||||||
String tempUsername = config.getString("username");
|
String tempUsername = config.getString("username");
|
||||||
|
|
||||||
if (tempUsername == null) {
|
if (tempUsername != null && !tempUsername.equalsIgnoreCase(player.getName())) {
|
||||||
config.set("username", player.getName());
|
config.set("username", player.getName());
|
||||||
config.set("luck", plugin.handler.getLuckContainer(player).getDefaultValue());
|
config.set("luck", plugin.handler.getLuckContainer(player).getDefaultValue());
|
||||||
config.set("multiplier", "1.0");
|
config.set("multiplier", "1.0");
|
||||||
save();
|
save();
|
||||||
} else if (!tempUsername.equalsIgnoreCase(player.getName())) {
|
|
||||||
config.set("username", player.getName());
|
|
||||||
save();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,11 +72,6 @@ public class PlayerConfig {
|
|||||||
SneakyWorker.sneakyTry(() -> config = YamlConfiguration.loadConfiguration(configFile));
|
SneakyWorker.sneakyTry(() -> config = YamlConfiguration.loadConfiguration(configFile));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reload() {
|
|
||||||
save();
|
|
||||||
load();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLuck(double luck) {
|
public void setLuck(double luck) {
|
||||||
config.set("luck", luck);
|
config.set("luck", luck);
|
||||||
save();
|
save();
|
||||||
|
@ -47,7 +47,7 @@ public class PlayerHandler implements Listener {
|
|||||||
playerConfig.load();
|
playerConfig.load();
|
||||||
}
|
}
|
||||||
|
|
||||||
Luck container = new Luck(plugin, player, multiplier);
|
Luck container = new Luck(player, multiplier);
|
||||||
container.setValue(luck);
|
container.setValue(luck);
|
||||||
|
|
||||||
playerLuckMap.put(player, container);
|
playerLuckMap.put(player, container);
|
||||||
|
Reference in New Issue
Block a user