Alpha 1.0 RELEASE

This commit is contained in:
Paldiu 2022-04-10 18:48:09 -05:00
parent d08b240ff9
commit 057450c585
12 changed files with 325 additions and 199 deletions

View File

@ -6,6 +6,7 @@ import org.bukkit.entity.Player;
import java.io.Serializable;
public interface LuckContainer extends Serializable {
Attribute asAttribute();
double getNumber();

View File

@ -1,6 +1,6 @@
package io.github.simplex.luck;
import io.github.simplex.luck.listener.PlayerListener;
import io.github.simplex.luck.listener.*;
import io.github.simplex.luck.player.PlayerConfig;
import io.github.simplex.luck.player.PlayerHandler;
import org.bukkit.Bukkit;
@ -13,22 +13,27 @@ import java.util.Map;
import java.util.UUID;
public final class FeelingLucky extends JavaPlugin {
private static final Map<UUID, PlayerConfig> configMap = new HashMap<>();
private final Map<UUID, PlayerConfig> configMap = new HashMap<>();
public LuckCMD cmd;
public PlayerHandler handler;
public PlayerListener playerListener;
public static Map<UUID, PlayerConfig> getConfigMap() {
public Map<UUID, PlayerConfig> getConfigMap() {
return configMap;
}
@Override
public void onEnable() {
Bukkit.getLogger().info("Initializing the PlayerHandler...");
getLogger().info("Initializing the PlayerHandler...");
handler = new PlayerHandler(this);
Bukkit.getLogger().info("Initialization complete! Attempting to register the Listeners...");
playerListener = new PlayerListener(this);
Bukkit.getLogger().info("Registration complete! Attempting to load all player configuration files...");
getLogger().info("Initialization complete! Attempting to register the Listeners...");
new PlayerListener(this);
new BlockDrops(this);
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();
if (files != null) {

View File

@ -39,8 +39,13 @@ public class LuckCMD extends Command implements TabCompleter {
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);
PlayerConfig config = FeelingLucky.getConfigMap().get(player.getUniqueId());
PlayerConfig config = plugin.getConfigMap().get(player.getUniqueId());
switch (args[0]) {
case "set" -> {
@ -95,7 +100,7 @@ public class LuckCMD extends Command implements TabCompleter {
}
Luck luck = plugin.handler.getLuckContainer(player);
PlayerConfig config = FeelingLucky.getConfigMap().get(player.getUniqueId());
PlayerConfig config = plugin.getConfigMap().get(player.getUniqueId());
luck.reset();
plugin.handler.updatePlayer(player, luck);
config.setLuck(luck.getValue());

View File

@ -0,0 +1,29 @@
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);
}
}
}

View File

@ -0,0 +1,37 @@
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);
}
}
}

View File

@ -0,0 +1,73 @@
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);
}
}
}

View File

@ -1,177 +1,27 @@
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.SneakyWorker;
import io.github.simplex.luck.player.Luck;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.*;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Witch;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockDropItemEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import java.util.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) {
this.plugin = 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
public void rabbitFoot(PlayerInteractEvent event) {
Action action = event.getAction();

View File

@ -0,0 +1,37 @@
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);
}
}
});
}
}
}

View File

@ -0,0 +1,80 @@
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."));
}
}
}
}
}

View File

@ -16,21 +16,45 @@ import java.util.SplittableRandom;
public class Luck implements LuckContainer {
private final Player player;
private final double multiplier;
private final double BASE_VALUE;
private final PlayerLuckChangeEvent event;
private final FeelingLucky plugin;
private final List<Player> markedPlayers = new ArrayList<>();
private double BASE_VALUE;
public Luck(Player player) {
this(player, 1.0);
public Luck(FeelingLucky plugin, Player player) {
this(plugin, player, 1.0);
}
public Luck(Player player, double multiplier) {
public Luck(FeelingLucky plugin, Player player, double multiplier) {
this.player = player;
this.multiplier = multiplier;
BASE_VALUE = player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue();
this.plugin = plugin;
BASE_VALUE = plugin.getConfigMap().get(player.getUniqueId()).getConfig().getDouble("luck");
event = new PlayerLuckChangeEvent(this);
}
private final List<Player> markedPlayers = new ArrayList<>();
@Contract(pure = true,
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) {
markedPlayers.add(player);
@ -44,25 +68,6 @@ public class Luck implements LuckContainer {
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
public Attribute asAttribute() {
return Attribute.GENERIC_LUCK;
@ -119,14 +124,15 @@ public class Luck implements LuckContainer {
return BASE_VALUE;
}
public double getDefaultValue() {
return player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue();
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);
}
public void setValue(double value) {
player.getAttribute(Attribute.GENERIC_LUCK).setBaseValue(value);
FeelingLucky.getConfigMap().get(associatedPlayer().getUniqueId()).setLuck(value);
Bukkit.getPluginManager().callEvent(event);
public double getDefaultValue() {
return player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue();
}
public void addTo(double value) {

View File

@ -45,11 +45,14 @@ public class PlayerConfig {
String tempUsername = config.getString("username");
if (tempUsername != null && !tempUsername.equalsIgnoreCase(player.getName())) {
if (tempUsername == null) {
config.set("username", player.getName());
config.set("luck", plugin.handler.getLuckContainer(player).getDefaultValue());
config.set("multiplier", "1.0");
save();
} else if (!tempUsername.equalsIgnoreCase(player.getName())) {
config.set("username", player.getName());
save();
}
}

View File

@ -30,11 +30,11 @@ public class PlayerHandler implements Listener {
@EventHandler
public void initializePlayer(PlayerLoginEvent event) {
Player player = event.getPlayer();
PlayerConfig playerConfig = FeelingLucky.getConfigMap().get(player.getUniqueId());
PlayerConfig playerConfig = plugin.getConfigMap().get(player.getUniqueId());
if (playerConfig == null) {
playerConfig = new PlayerConfig(plugin, player);
FeelingLucky.getConfigMap().put(player.getUniqueId(), playerConfig);
plugin.getConfigMap().put(player.getUniqueId(), playerConfig);
}
String username = playerConfig.getConfig().getString("username");
@ -47,7 +47,7 @@ public class PlayerHandler implements Listener {
playerConfig.load();
}
Luck container = new Luck(player, multiplier);
Luck container = new Luck(plugin, player, multiplier);
container.setValue(luck);
playerLuckMap.put(player, container);