From bb9bdcdf0328b9486d6b22fc02b0050b63258570 Mon Sep 17 00:00:00 2001 From: Paldiu Date: Mon, 25 Apr 2022 16:30:38 -0500 Subject: [PATCH] [Beta] Snapshot v20220425 Changelog: - Added RandomEffect, which gives a user a random positive potion effect on respawn or teleport. - Implemented a class loading system with reflections to dynamically load all listeners. - Adjusted rarity values in the config.yml - Added a positiveEffects list to ListBox for RandomEffect event - Adjusted the behavior of the BlockDrops event. --- .../io/github/simplex/luck/FeelingLucky.java | 25 +++++----- .../simplex/luck/listener/BlockDrops.java | 3 +- .../simplex/luck/listener/RandomEffect.java | 49 +++++++++++++++++++ .../io/github/simplex/luck/util/ListBox.java | 20 ++++++++ .../simplex/luck/util/SneakyWorker.java | 47 +++++++++++++++++- src/main/resources/config.yml | 4 +- 6 files changed, 132 insertions(+), 16 deletions(-) create mode 100644 src/main/java/io/github/simplex/luck/listener/RandomEffect.java diff --git a/src/main/java/io/github/simplex/luck/FeelingLucky.java b/src/main/java/io/github/simplex/luck/FeelingLucky.java index e667d4b..95e878e 100644 --- a/src/main/java/io/github/simplex/luck/FeelingLucky.java +++ b/src/main/java/io/github/simplex/luck/FeelingLucky.java @@ -4,10 +4,13 @@ import io.github.simplex.luck.listener.*; import io.github.simplex.luck.player.PlayerConfig; import io.github.simplex.luck.player.PlayerHandler; import io.github.simplex.luck.util.LuckCMD; +import io.github.simplex.luck.util.SneakyWorker; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -64,18 +67,16 @@ public final class FeelingLucky extends JavaPlugin { } private void registerListeners() { - new BlockDrops(this); - new BonemealFullCrop(this); - new CheatDeath(this); - new EnchantmentBoost(this); - new ExpBoost(this); - new IllOmen(this); - new ItemDrops(this); - new PlayerListener(this); - new RestoreHunger(this); - new TakeDamage(this); - new UnbreakableTool(this); - new VillagerInventory(this); + try { + Class[] listeners = SneakyWorker.getClasses("io.github.simplex.luck.listener"); + Arrays.stream(listeners).forEach(l -> { + if (AbstractListener.class.isAssignableFrom(l)) { + SneakyWorker.sneakyTry(() -> l.getDeclaredConstructor(FeelingLucky.class).newInstance(this)); + } + }); + } catch (IOException | ClassNotFoundException ex) { + getLogger().severe(ex.getMessage()); + } } public PlayerHandler getHandler() { diff --git a/src/main/java/io/github/simplex/luck/listener/BlockDrops.java b/src/main/java/io/github/simplex/luck/listener/BlockDrops.java index 68c7de1..6efa412 100644 --- a/src/main/java/io/github/simplex/luck/listener/BlockDrops.java +++ b/src/main/java/io/github/simplex/luck/listener/BlockDrops.java @@ -21,7 +21,8 @@ public final class BlockDrops extends AbstractListener { Luck luck = getHandler().getLuckContainer(player); List items = event.getItems(); if (luck.quickRNG(luck.getPercentage()) && doesQualify("block_drops", luck.getPercentage())) { - items.forEach(SneakyWorker::move); + event.getItems().clear(); + event.getItems().addAll(items.stream().map(SneakyWorker::move).toList()); } } } diff --git a/src/main/java/io/github/simplex/luck/listener/RandomEffect.java b/src/main/java/io/github/simplex/luck/listener/RandomEffect.java new file mode 100644 index 0000000..f80b81d --- /dev/null +++ b/src/main/java/io/github/simplex/luck/listener/RandomEffect.java @@ -0,0 +1,49 @@ +package io.github.simplex.luck.listener; + +import io.github.simplex.lib.MiniComponent; +import io.github.simplex.luck.FeelingLucky; +import io.github.simplex.luck.player.Luck; +import io.github.simplex.luck.util.ListBox; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.player.PlayerRespawnEvent; +import org.bukkit.event.player.PlayerTeleportEvent; +import org.bukkit.potion.PotionEffect; + +import java.util.List; + +public class RandomEffect extends AbstractListener { + public RandomEffect(FeelingLucky plugin) { + super(plugin); + } + + @EventHandler + public void giveRandomEffect(PlayerRespawnEvent respawn) { + Player player = respawn.getPlayer(); + Luck luck = plugin.getHandler().getLuckContainer(player); + + List effectList = ListBox.positiveEffects.stream().map(m -> m.createEffect(120, 5)).toList(); + int size = effectList.size(); + PotionEffect random = effectList.get(Luck.RNG().nextInt(size - 1)); + + if (luck.quickRNG(luck.getPercentage()) && doesQualify("random_effect", luck.getValue())) { + player.addPotionEffect(random); + player.sendMessage(MiniComponent.info("Thanks to luck, a random positive potion effect has been applied to you.")); + } + } + + @EventHandler + public void giveRandomEffect(PlayerTeleportEvent tp) { + Player player = tp.getPlayer(); + Luck luck = plugin.getHandler().getLuckContainer(player); + + List effectList = ListBox.positiveEffects.stream().map(m -> m.createEffect(120, 5)).toList(); + int size = effectList.size(); + PotionEffect random = effectList.get(Luck.RNG().nextInt(size - 1)); + + if (luck.quickRNG(luck.getPercentage()) && doesQualify("random_effect", luck.getValue())) { + player.addPotionEffect(random); + player.sendMessage(MiniComponent.info("Thanks to luck, a random positive potion effect has been applied to you.")); + } + } +} diff --git a/src/main/java/io/github/simplex/luck/util/ListBox.java b/src/main/java/io/github/simplex/luck/util/ListBox.java index 3b78105..b1670a8 100644 --- a/src/main/java/io/github/simplex/luck/util/ListBox.java +++ b/src/main/java/io/github/simplex/luck/util/ListBox.java @@ -6,6 +6,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffectType; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class ListBox { @@ -36,6 +37,25 @@ public class ListBox { add(PotionEffectType.WEAKNESS); }}; + public static final List positiveEffects = new ArrayList<>() {{ + add(PotionEffectType.DAMAGE_RESISTANCE); + add(PotionEffectType.DOLPHINS_GRACE); + add(PotionEffectType.INCREASE_DAMAGE); + add(PotionEffectType.ABSORPTION); + add(PotionEffectType.SATURATION); + add(PotionEffectType.FIRE_RESISTANCE); + add(PotionEffectType.WATER_BREATHING); + add(PotionEffectType.SPEED); + add(PotionEffectType.SLOW_FALLING); + add(PotionEffectType.REGENERATION); + add(PotionEffectType.NIGHT_VISION); + add(PotionEffectType.LUCK); + add(PotionEffectType.JUMP); + add(PotionEffectType.INVISIBILITY); + add(PotionEffectType.HEALTH_BOOST); + add(PotionEffectType.FAST_DIGGING); + }}; + public static final List foods = new ArrayList<>() {{ add(new ItemStack(Material.COOKED_BEEF)); add(new ItemStack(Material.COOKED_CHICKEN)); diff --git a/src/main/java/io/github/simplex/luck/util/SneakyWorker.java b/src/main/java/io/github/simplex/luck/util/SneakyWorker.java index 4fcf643..c04110f 100644 --- a/src/main/java/io/github/simplex/luck/util/SneakyWorker.java +++ b/src/main/java/io/github/simplex/luck/util/SneakyWorker.java @@ -1,10 +1,18 @@ package io.github.simplex.luck.util; +import io.github.simplex.luck.listener.AbstractListener; import io.github.simplex.luck.player.Luck; import org.bukkit.Bukkit; import org.bukkit.entity.Item; import org.bukkit.inventory.ItemStack; +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; + public class SneakyWorker { public static void sneakyTry(SneakyTry sneakyTry) { try { @@ -25,11 +33,48 @@ public class SneakyWorker { } } - public static void move(Item item) { + public static Class[] getClasses(String packageName) throws ClassNotFoundException, IOException { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + assert classLoader != null; + String path = packageName.replace(".", "/"); + Enumeration resources = classLoader.getResources(path); + List dirs = new ArrayList<>(); + while (resources.hasMoreElements()) { + URL resource = resources.nextElement(); + dirs.add(new File(resource.getFile())); + } + ArrayList classes = new ArrayList<>(); + for (File directory : dirs) { + classes.addAll(findClasses(directory, packageName)); + } + return classes.toArray(new Class[classes.size()]); + } + + private static List> findClasses(File directory, String packageName) throws ClassNotFoundException { + List> classes = new ArrayList<>(); + if (!directory.exists()) { + return classes; + } + + File[] files = directory.listFiles(); + assert files != null; + for (File file : files) { + if (file.isDirectory()) { + assert !file.getName().contains("."); + classes.addAll(findClasses(file, packageName + "." + file.getName().substring(0, file.getName().length() - 6))); + } else if (file.getName().endsWith(".class")) { + classes.add(Class.forName(packageName + "." + file.getName().substring(0, file.getName().length() - 6))); + } + } + return classes; + } + + public static Item move(Item item) { ItemStack stack = item.getItemStack(); int rng = (Luck.RNG().nextInt(2, 5)) + stack.getAmount(); stack.setAmount(rng); item.setItemStack(stack); + return item; } public interface SneakyTry { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index b3924f2..dee3084 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -15,12 +15,12 @@ low_rarity_chance: 64.0 # These entries are case-sensitive. block_drops: LOW -bonemeal: LOW +bonemeal: MED cheat_death: MED enchanting: HIGH experience: HIGH item_drops: LOW random_effect: HIGH -restore_hunger: MED +restore_hunger: NONE take_damage: MED unbreakable: HIGH \ No newline at end of file