mirror of
https://github.com/SimplexDevelopment/FeelingLucky.git
synced 2025-07-04 19:16:41 +00:00
Compare commits
4 Commits
Beta-20220
...
Beta-1.0-R
Author | SHA1 | Date | |
---|---|---|---|
a4d71b2c0b | |||
bb9bdcdf03 | |||
1bf2a818ec | |||
0488e1d6b1 |
@ -3,7 +3,7 @@ plugins {
|
||||
}
|
||||
|
||||
group = 'io.github.simplex'
|
||||
version = 'Alpha-1.0'
|
||||
version = 'Beta-1.0-RC01'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
@ -1,6 +1,5 @@
|
||||
package io.github.simplex.luck;
|
||||
|
||||
import io.github.simplex.lib.MiniComponent;
|
||||
import io.github.simplex.luck.listener.AbstractListener;
|
||||
import io.github.simplex.luck.util.SneakyWorker;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
@ -9,11 +8,26 @@ import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
public class Config extends YamlConfiguration {
|
||||
private final FeelingLucky plugin;
|
||||
private final List<String> configEntries = new ArrayList<>() {{
|
||||
add("high_rarity_chance");
|
||||
add("medium_rarity_chance");
|
||||
add("low_rarity_chance");
|
||||
add("block_drops");
|
||||
add("bonemeal");
|
||||
add("cheat_death");
|
||||
add("enchanting");
|
||||
add("experience");
|
||||
add("item_drops");
|
||||
add("random_effect");
|
||||
add("restore_hunger");
|
||||
add("take_damage");
|
||||
add("unbreakable");
|
||||
}};
|
||||
private File configFile;
|
||||
|
||||
public Config(FeelingLucky plugin) {
|
||||
@ -33,7 +47,7 @@ public class Config extends YamlConfiguration {
|
||||
if (validateIntegrity()) {
|
||||
File newFile = new File(plugin.getDataFolder(), "config.yml");
|
||||
SneakyWorker.sneakyTry(() -> {
|
||||
Files.delete(Path.of(plugin.getDataFolder().getPath()));
|
||||
Files.delete(Path.of(this.configFile.getPath()));
|
||||
newFile.createNewFile();
|
||||
plugin.saveResource("config.yml", true);
|
||||
});
|
||||
@ -72,20 +86,4 @@ public class Config extends YamlConfiguration {
|
||||
public double getChance(String path) {
|
||||
return getDouble(path);
|
||||
}
|
||||
|
||||
private List<String> configEntries = new ArrayList<>() {{
|
||||
add("high_rarity_chance");
|
||||
add("medium_rarity_chance");
|
||||
add("low_rarity_chance");
|
||||
add("block_drops");
|
||||
add("bonemeal");
|
||||
add("cheat_death");
|
||||
add("enchanting");
|
||||
add("experience");
|
||||
add("item_drops");
|
||||
add("random_effect");
|
||||
add("restore_hunger");
|
||||
add("take_damage");
|
||||
add("unbreakable");
|
||||
}};
|
||||
}
|
||||
|
@ -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,18 @@ 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(AbstractListener.class.getPackage().getName());
|
||||
Arrays.stream(listeners).forEach(l -> {
|
||||
if (AbstractListener.class.isAssignableFrom(l)) {
|
||||
if (l.equals(AbstractListener.class)) return;
|
||||
|
||||
SneakyWorker.sneakyTry(() -> l.getDeclaredConstructor(FeelingLucky.class).newInstance(this));
|
||||
}
|
||||
});
|
||||
} catch (IOException | ClassNotFoundException ex) {
|
||||
getLogger().severe(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public PlayerHandler getHandler() {
|
||||
|
@ -5,12 +5,8 @@ import io.github.simplex.luck.FeelingLucky;
|
||||
import io.github.simplex.luck.player.PlayerHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class AbstractListener implements Listener {
|
||||
protected final FeelingLucky plugin;
|
||||
protected final Map<AbstractListener, Rarity> listenerRarityMap = new HashMap<>();
|
||||
protected final Config config;
|
||||
|
||||
public AbstractListener(FeelingLucky plugin) {
|
||||
@ -23,24 +19,19 @@ public abstract class AbstractListener implements Listener {
|
||||
return plugin.getHandler();
|
||||
}
|
||||
|
||||
public boolean doesQualify(String name, double luck) {
|
||||
return switch (config.getRarity(name)) {
|
||||
case HIGH -> luck > config.getChance("high_rarity_chance");
|
||||
case MED -> luck > config.getChance("medium_rarity_chance");
|
||||
case LOW -> luck > config.getChance("low_rarity_chance");
|
||||
case NONE -> true;
|
||||
};
|
||||
}
|
||||
|
||||
public enum Rarity {
|
||||
HIGH,
|
||||
MED,
|
||||
LOW,
|
||||
NONE
|
||||
}
|
||||
|
||||
public boolean doesQualify(String name, double luck) {
|
||||
switch (config.getRarity(name)) {
|
||||
case HIGH:
|
||||
if (luck < config.getChance("high_rarity_chance")) return false;
|
||||
case MED:
|
||||
if (luck < config.getChance("medium_rarity_chance")) return false;
|
||||
case LOW:
|
||||
if (luck < config.getChance("low_rarity_chance")) return false;
|
||||
case NONE:
|
||||
return true;
|
||||
}
|
||||
throw new IllegalArgumentException("The value for the listener rarity is not a recognized rarity value.");
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +21,8 @@ public final class BlockDrops extends AbstractListener {
|
||||
Luck luck = getHandler().getLuckContainer(player);
|
||||
List<Item> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,10 @@ package io.github.simplex.luck.listener;
|
||||
|
||||
import io.github.simplex.luck.FeelingLucky;
|
||||
import io.github.simplex.luck.player.Luck;
|
||||
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.Item;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
import org.bukkit.event.entity.EntityDeathEvent;
|
||||
|
@ -5,15 +5,12 @@ import io.github.simplex.luck.FeelingLucky;
|
||||
import io.github.simplex.luck.player.Luck;
|
||||
import io.github.simplex.luck.util.CooldownTimer;
|
||||
import io.github.simplex.luck.util.SpecialFootItem;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Guardian;
|
||||
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.entity.EntityDamageByEntityEvent;
|
||||
import org.bukkit.event.entity.EntityDamageEvent;
|
||||
|
@ -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."));
|
||||
}
|
||||
}
|
||||
}
|
@ -7,7 +7,6 @@ import io.github.simplex.luck.util.SpecialFootItem;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Villager;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
|
||||
import org.bukkit.inventory.MerchantRecipe;
|
||||
|
||||
|
@ -1,7 +1,5 @@
|
||||
package io.github.simplex.luck.util;
|
||||
|
||||
import io.github.simplex.lib.MiniComponent;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.HashMap;
|
||||
@ -10,9 +8,8 @@ import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class CooldownTimer {
|
||||
private final Map<UUID, Long> cooldowns = new HashMap<>();
|
||||
|
||||
public static final long DEFAULT_COOLDOWN = 30L;
|
||||
private final Map<UUID, Long> cooldowns = new HashMap<>();
|
||||
|
||||
public void setCooldown(UUID playerUUID, long time) {
|
||||
if (time < 1) {
|
||||
|
@ -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<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<>() {{
|
||||
add(new ItemStack(Material.COOKED_BEEF));
|
||||
add(new ItemStack(Material.COOKED_CHICKEN));
|
||||
|
@ -95,7 +95,9 @@ public class LuckCMD extends Command implements TabCompleter {
|
||||
if (args.length == 2) {
|
||||
if ((sender instanceof ConsoleCommandSender) || sender.hasPermission("luck.admin")) {
|
||||
if (args[0].equalsIgnoreCase("reload") && args[1].equalsIgnoreCase("-m")) {
|
||||
|
||||
plugin.getConfig().reload();
|
||||
sender.sendMessage(MiniComponent.info("Configuration successfully reloaded."));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[0].equalsIgnoreCase("info")) {
|
||||
@ -160,33 +162,42 @@ public class LuckCMD extends Command implements TabCompleter {
|
||||
List<String> completions = new ArrayList<>() {{
|
||||
add("info");
|
||||
}};
|
||||
List<String> playerNames = new ArrayList<>() {{
|
||||
Bukkit.getOnlinePlayers().forEach(p -> add(p.getName()));
|
||||
}};
|
||||
List<String> adminCommands = List.of("set", "reset", "give", "take");
|
||||
List<String> playerNames = Bukkit.getOnlinePlayers().stream().map(Player::getName).toList();
|
||||
List<String> adminCommands = List.of("set", "reset", "give", "take", "reload");
|
||||
|
||||
if ((sender instanceof ConsoleCommandSender) || sender.hasPermission("luck.admin")) {
|
||||
completions.addAll(adminCommands);
|
||||
return completions;
|
||||
return completions.stream().filter(n -> n.startsWith(args[0])).toList();
|
||||
}
|
||||
|
||||
if (args[0].equalsIgnoreCase("info") && sender.hasPermission("luck.admin")) {
|
||||
return playerNames;
|
||||
}
|
||||
if (adminCommands.contains(args[0])
|
||||
&& sender.hasPermission("luck.admin")) {
|
||||
if (args.length == 2) {
|
||||
switch (args[0]) {
|
||||
case "info":
|
||||
case "reset":
|
||||
return playerNames.stream().filter(n -> n.startsWith(args[1])).toList();
|
||||
case "reload":
|
||||
return List.of("-m", "-p");
|
||||
}
|
||||
}
|
||||
|
||||
if (completions.contains(args[1]) && sender.hasPermission("luck.admin")) {
|
||||
switch (args[0]) {
|
||||
case "info":
|
||||
case "reset":
|
||||
case "reload":
|
||||
return new ArrayList<>();
|
||||
case "give":
|
||||
case "take":
|
||||
case "set":
|
||||
return List.of("amount");
|
||||
if (args.length == 3 && playerNames.contains(args[1])) {
|
||||
switch (args[0]) {
|
||||
case "give":
|
||||
case "take":
|
||||
case "set":
|
||||
return List.of("amount");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return completions;
|
||||
if (args[0].equalsIgnoreCase("reload")
|
||||
&& args[1].equalsIgnoreCase("-p")
|
||||
&& sender.hasPermission("luck.admin") && (args.length == 3)) {
|
||||
return playerNames.stream().filter(n -> n.startsWith(args[2])).toList();
|
||||
}
|
||||
|
||||
return completions.stream().filter(n -> n.startsWith(args[0])).toList();
|
||||
}
|
||||
}
|
||||
|
@ -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<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();
|
||||
int rng = (Luck.RNG().nextInt(2, 5)) + stack.getAmount();
|
||||
stack.setAmount(rng);
|
||||
item.setItemStack(stack);
|
||||
return item;
|
||||
}
|
||||
|
||||
public interface SneakyTry {
|
||||
|
@ -4,9 +4,9 @@
|
||||
# These values must be in the form of doubles, as listed below.
|
||||
# The maximum amount of luck that can be attributed is 1024.0, and the minimum is -1024.0
|
||||
|
||||
high_rarity_chance: 512
|
||||
medium_rarity_chance: 128
|
||||
low_rarity_chance: 64
|
||||
high_rarity_chance: 512.0
|
||||
medium_rarity_chance: 128.0
|
||||
low_rarity_chance: 64.0
|
||||
|
||||
# The following entries are for the rarity level of each event trigger.
|
||||
# This will determine which rarity chance to use which ensures players
|
||||
@ -15,12 +15,12 @@ low_rarity_chance: 64
|
||||
# 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
|
Reference in New Issue
Block a user