[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.
This commit is contained in:
Paldiu 2022-04-25 16:30:38 -05:00
parent 1bf2a818ec
commit bb9bdcdf03
6 changed files with 132 additions and 16 deletions

View File

@ -4,10 +4,13 @@ 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.LuckCMD; import io.github.simplex.luck.util.LuckCMD;
import io.github.simplex.luck.util.SneakyWorker;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -64,18 +67,16 @@ public final class FeelingLucky extends JavaPlugin {
} }
private void registerListeners() { private void registerListeners() {
new BlockDrops(this); try {
new BonemealFullCrop(this); Class<?>[] listeners = SneakyWorker.getClasses("io.github.simplex.luck.listener");
new CheatDeath(this); Arrays.stream(listeners).forEach(l -> {
new EnchantmentBoost(this); if (AbstractListener.class.isAssignableFrom(l)) {
new ExpBoost(this); SneakyWorker.sneakyTry(() -> l.getDeclaredConstructor(FeelingLucky.class).newInstance(this));
new IllOmen(this); }
new ItemDrops(this); });
new PlayerListener(this); } catch (IOException | ClassNotFoundException ex) {
new RestoreHunger(this); getLogger().severe(ex.getMessage());
new TakeDamage(this); }
new UnbreakableTool(this);
new VillagerInventory(this);
} }
public PlayerHandler getHandler() { public PlayerHandler getHandler() {

View File

@ -21,7 +21,8 @@ public final class BlockDrops extends AbstractListener {
Luck luck = getHandler().getLuckContainer(player); Luck luck = getHandler().getLuckContainer(player);
List<Item> items = event.getItems(); List<Item> items = event.getItems();
if (luck.quickRNG(luck.getPercentage()) && doesQualify("block_drops", luck.getPercentage())) { 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());
} }
} }
} }

View File

@ -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<PotionEffect> 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<PotionEffect> 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."));
}
}
}

View File

@ -6,6 +6,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
public class ListBox { public class ListBox {
@ -36,6 +37,25 @@ public class ListBox {
add(PotionEffectType.WEAKNESS); add(PotionEffectType.WEAKNESS);
}}; }};
public static final List<PotionEffectType> 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<ItemStack> foods = new ArrayList<>() {{ public static final List<ItemStack> foods = new ArrayList<>() {{
add(new ItemStack(Material.COOKED_BEEF)); add(new ItemStack(Material.COOKED_BEEF));
add(new ItemStack(Material.COOKED_CHICKEN)); add(new ItemStack(Material.COOKED_CHICKEN));

View File

@ -1,10 +1,18 @@
package io.github.simplex.luck.util; package io.github.simplex.luck.util;
import io.github.simplex.luck.listener.AbstractListener;
import io.github.simplex.luck.player.Luck; import io.github.simplex.luck.player.Luck;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Item; import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack; 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 class SneakyWorker {
public static void sneakyTry(SneakyTry sneakyTry) { public static void sneakyTry(SneakyTry sneakyTry) {
try { 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<URL> resources = classLoader.getResources(path);
List<File> dirs = new ArrayList<>();
while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
dirs.add(new File(resource.getFile()));
}
ArrayList<Class> classes = new ArrayList<>();
for (File directory : dirs) {
classes.addAll(findClasses(directory, packageName));
}
return classes.toArray(new Class[classes.size()]);
}
private static List<Class<?>> findClasses(File directory, String packageName) throws ClassNotFoundException {
List<Class<?>> 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(); ItemStack stack = item.getItemStack();
int rng = (Luck.RNG().nextInt(2, 5)) + stack.getAmount(); int rng = (Luck.RNG().nextInt(2, 5)) + stack.getAmount();
stack.setAmount(rng); stack.setAmount(rng);
item.setItemStack(stack); item.setItemStack(stack);
return item;
} }
public interface SneakyTry { public interface SneakyTry {

View File

@ -15,12 +15,12 @@ low_rarity_chance: 64.0
# These entries are case-sensitive. # These entries are case-sensitive.
block_drops: LOW block_drops: LOW
bonemeal: LOW bonemeal: MED
cheat_death: MED cheat_death: MED
enchanting: HIGH enchanting: HIGH
experience: HIGH experience: HIGH
item_drops: LOW item_drops: LOW
random_effect: HIGH random_effect: HIGH
restore_hunger: MED restore_hunger: NONE
take_damage: MED take_damage: MED
unbreakable: HIGH unbreakable: HIGH