From 73affcf4c46cf5093d15c6a92f97a9ce2d7959e3 Mon Sep 17 00:00:00 2001 From: Paldiu Date: Sun, 27 Mar 2022 19:21:35 -0500 Subject: [PATCH] Alpha 1.0 --- .../io/github/simplex/luck/FeelingLucky.java | 5 +- .../io/github/simplex/luck/SneakyWorker.java | 15 +++- .../simplex/luck/listener/PlayerListener.java | 85 +++++++++++++++---- .../io/github/simplex/luck/player/Luck.java | 19 ++++- .../simplex/luck/player/PlayerConfig.java | 8 +- src/main/resources/default_player.yml | 3 - 6 files changed, 108 insertions(+), 27 deletions(-) delete mode 100644 src/main/resources/default_player.yml diff --git a/src/main/java/io/github/simplex/luck/FeelingLucky.java b/src/main/java/io/github/simplex/luck/FeelingLucky.java index dcc7a36..92e56e3 100644 --- a/src/main/java/io/github/simplex/luck/FeelingLucky.java +++ b/src/main/java/io/github/simplex/luck/FeelingLucky.java @@ -6,7 +6,10 @@ import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; import java.io.File; -import java.util.*; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; public final class FeelingLucky extends JavaPlugin { private static final Map configMap = new HashMap<>(); diff --git a/src/main/java/io/github/simplex/luck/SneakyWorker.java b/src/main/java/io/github/simplex/luck/SneakyWorker.java index a1c74f8..f2f7aba 100644 --- a/src/main/java/io/github/simplex/luck/SneakyWorker.java +++ b/src/main/java/io/github/simplex/luck/SneakyWorker.java @@ -1,6 +1,9 @@ package io.github.simplex.luck; +import io.github.simplex.luck.player.Luck; import org.bukkit.Bukkit; +import org.bukkit.entity.Item; +import org.bukkit.inventory.ItemStack; public class SneakyWorker { public static void sneakyTry(SneakyTry sneakyTry) { @@ -15,10 +18,18 @@ public class SneakyWorker { } } - public static void silentTry(SneakyTry sneakyTry) { + public static void quietTry(SneakyTry sneakyTry) { try { sneakyTry.tryThis(); - } catch (Exception ignored) {} + } catch (Exception ignored) { + } + } + + public static void move(Item item) { + ItemStack stack = item.getItemStack(); + int rng = (Luck.RNG().nextInt(2, 5)) + stack.getAmount(); + stack.setAmount(rng); + item.setItemStack(stack); } public interface SneakyTry { diff --git a/src/main/java/io/github/simplex/luck/listener/PlayerListener.java b/src/main/java/io/github/simplex/luck/listener/PlayerListener.java index b094445..3386599 100644 --- a/src/main/java/io/github/simplex/luck/listener/PlayerListener.java +++ b/src/main/java/io/github/simplex/luck/listener/PlayerListener.java @@ -3,27 +3,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.SneakyWorker; import io.github.simplex.luck.player.Luck; import io.github.simplex.luck.player.PlayerHandler; import net.kyori.adventure.text.Component; +import org.bukkit.Location; import org.bukkit.Material; -import org.bukkit.entity.Entity; -import org.bukkit.entity.Player; -import org.bukkit.entity.Witch; +import org.bukkit.World; +import org.bukkit.entity.*; 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.EntityDamageEvent.DamageCause; +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 record PlayerListener(FeelingLucky plugin) implements Listener { + private static final Map entityPlayerMap = new HashMap<>(); + @EventHandler public void takeDamage(EntityDamageEvent event) { Entity entity = event.getEntity(); @@ -70,11 +80,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { 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()); + 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; } @@ -89,16 +95,66 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { } } + @EventHandler + public void extraBlockDrops(BlockDropItemEvent event) { + Player player = event.getPlayer(); + Luck luck = PlayerHandler.getLuckContainer(player); + List 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 = PlayerHandler.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 = PlayerHandler.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()) { double percentage = luck.getPercentage(); ListBox.foods.forEach(food -> { @@ -149,5 +205,4 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { } } } - } diff --git a/src/main/java/io/github/simplex/luck/player/Luck.java b/src/main/java/io/github/simplex/luck/player/Luck.java index 6d89513..e181fe1 100644 --- a/src/main/java/io/github/simplex/luck/player/Luck.java +++ b/src/main/java/io/github/simplex/luck/player/Luck.java @@ -33,6 +33,17 @@ public class Luck implements LuckContainer { return new SplittableRandom(); } + public static boolean quickRNG2(double percentage) { + double rng; + if (percentage >= 100.0) { + rng = 100.0; // 100% chance to trigger, obviously; + } else { + rng = RNG().nextDouble(0.0, 99.0); + } + + return (percentage >= rng); + } + @Override public Attribute asAttribute() { return Attribute.GENERIC_LUCK; @@ -65,13 +76,13 @@ public class Luck implements LuckContainer { public boolean quickRNG(double percentage) { double rng; - if (percentage > 99.0) { - rng = RNG().nextDouble(100.0, 199.0); + if (percentage >= 100.0) { + rng = 100.0; // 100% chance to trigger, obviously; } else { rng = RNG().nextDouble(0.0, 99.0); } - if (multiplier() > 0.0) { + if (multiplier() > 1.0) { return ((percentage * multiplier()) >= rng); } @@ -87,7 +98,7 @@ public class Luck implements LuckContainer { return player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue(); } - public void setValue(double value) { + protected void setValue(double value) { player.getAttribute(Attribute.GENERIC_LUCK).setBaseValue(value); Bukkit.getPluginManager().callEvent(event); } diff --git a/src/main/java/io/github/simplex/luck/player/PlayerConfig.java b/src/main/java/io/github/simplex/luck/player/PlayerConfig.java index 18c2def..035525a 100644 --- a/src/main/java/io/github/simplex/luck/player/PlayerConfig.java +++ b/src/main/java/io/github/simplex/luck/player/PlayerConfig.java @@ -6,7 +6,9 @@ import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.jetbrains.annotations.Contract; -import java.io.*; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; import java.nio.charset.StandardCharsets; public class PlayerConfig extends YamlConfiguration { @@ -38,7 +40,9 @@ public class PlayerConfig extends YamlConfiguration { configFile = file; config = loadConfiguration(configFile); - if (config.getString("username").equalsIgnoreCase("replace")) { + String tempUsername = config.getString("username"); + + if (tempUsername != null && tempUsername.equalsIgnoreCase("replace")) { config.set("username", player.getName()); config.set("luck", PlayerHandler.getLuckContainer(player).defaultValue()); config.set("multiplier", "1.0"); diff --git a/src/main/resources/default_player.yml b/src/main/resources/default_player.yml deleted file mode 100644 index 01fc08e..0000000 --- a/src/main/resources/default_player.yml +++ /dev/null @@ -1,3 +0,0 @@ -username: replace -luck: 0.0 -multiplier: 1.0 \ No newline at end of file