This commit is contained in:
Paldiu 2022-03-24 17:21:12 -05:00
parent 31bbf6622a
commit 6d211490c0
5 changed files with 110 additions and 77 deletions

View File

@ -5,16 +5,15 @@ import io.github.simplex.luck.player.PlayerHandler;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList; import java.io.File;
import java.util.Arrays; import java.util.*;
import java.util.List;
public final class FeelingLucky extends JavaPlugin { public final class FeelingLucky extends JavaPlugin {
private static final List<PlayerConfig> configList = new ArrayList<>(); private static final Map<UUID, PlayerConfig> configMap = new HashMap<>();
public PlayerHandler handler; public PlayerHandler handler;
public static List<PlayerConfig> getConfigList() { public static Map<UUID, PlayerConfig> getConfigMap() {
return configList; return configMap;
} }
@Override @Override
@ -24,21 +23,25 @@ public final class FeelingLucky extends JavaPlugin {
Bukkit.getLogger().info("Initialization complete! Attempting to register the handler..."); Bukkit.getLogger().info("Initialization complete! Attempting to register the handler...");
this.getServer().getPluginManager().registerEvents(handler, this); this.getServer().getPluginManager().registerEvents(handler, this);
Bukkit.getLogger().info("Registration complete! Attempting to load all player configuration files..."); Bukkit.getLogger().info("Registration complete! Attempting to load all player configuration files...");
if (getDataFolder().listFiles() != null) {
Arrays.stream(getDataFolder().listFiles()).forEach(file -> { File[] files = getDataFolder().listFiles();
configList.add(PlayerConfig.loadFrom(file)); if (files != null) {
Arrays.stream(files).forEach(file -> {
UUID uuid = UUID.fromString(file.getName().split("\\.")[0]);
configMap.put(uuid, PlayerConfig.loadFrom(file));
}); });
configList.forEach(PlayerConfig::load); configMap.forEach((u, pc) -> pc.load());
getLogger().info("Successfully loaded all configurations!"); getLogger().info("Successfully loaded all configurations!");
} else { } else {
getLogger().info("There are no player configurations to load."); getLogger().info("There are no player configurations to load.");
} }
Bukkit.getLogger().info("Successfully initialized!"); Bukkit.getLogger().info("Successfully initialized!");
} }
@Override @Override
public void onDisable() { public void onDisable() {
Bukkit.getLogger().info("Saving all player configurations..."); Bukkit.getLogger().info("Saving all player configurations...");
configList.forEach(PlayerConfig::save); configMap.forEach((u, pc) -> pc.save());
} }
} }

View File

@ -0,0 +1,67 @@
package io.github.simplex.luck;
import org.bukkit.Material;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffectType;
import java.util.ArrayList;
import java.util.List;
public class ListBox {
public static final List<EntityDamageEvent.DamageCause> acceptedCauses = new ArrayList<>() {{
add(EntityDamageEvent.DamageCause.ENTITY_ATTACK);
add(EntityDamageEvent.DamageCause.ENTITY_SWEEP_ATTACK);
add(EntityDamageEvent.DamageCause.PROJECTILE);
add(EntityDamageEvent.DamageCause.ENTITY_EXPLOSION);
add(EntityDamageEvent.DamageCause.FLY_INTO_WALL);
add(EntityDamageEvent.DamageCause.LIGHTNING);
add(EntityDamageEvent.DamageCause.MAGIC);
}};
public static final List<EntityDamageEvent.DamageCause> sideCauses = new ArrayList<>() {{
add(EntityDamageEvent.DamageCause.POISON);
add(EntityDamageEvent.DamageCause.WITHER);
add(EntityDamageEvent.DamageCause.FIRE_TICK);
}};
public static final List<PotionEffectType> potionEffects = new ArrayList<>() {{
add(PotionEffectType.POISON);
add(PotionEffectType.WITHER);
add(PotionEffectType.BLINDNESS);
add(PotionEffectType.SLOW);
add(PotionEffectType.SLOW_DIGGING);
add(PotionEffectType.BAD_OMEN);
add(PotionEffectType.CONFUSION);
add(PotionEffectType.WEAKNESS);
}};
public static final List<ItemStack> foods = new ArrayList<>() {{
add(new ItemStack(Material.COOKED_BEEF));
add(new ItemStack(Material.COOKED_CHICKEN));
add(new ItemStack(Material.COOKED_PORKCHOP));
add(new ItemStack(Material.COOKED_COD));
add(new ItemStack(Material.COOKED_MUTTON));
add(new ItemStack(Material.COOKED_RABBIT));
add(new ItemStack(Material.COOKED_SALMON));
add(new ItemStack(Material.BEETROOT_SOUP));
add(new ItemStack(Material.POTATO));
add(new ItemStack(Material.BAKED_POTATO));
add(new ItemStack(Material.CARROT));
add(new ItemStack(Material.GOLDEN_CARROT));
add(new ItemStack(Material.APPLE));
add(new ItemStack(Material.GOLDEN_APPLE));
add(new ItemStack(Material.ENCHANTED_GOLDEN_APPLE));
add(new ItemStack(Material.BEEF));
add(new ItemStack(Material.PORKCHOP));
add(new ItemStack(Material.CHICKEN));
add(new ItemStack(Material.COD));
add(new ItemStack(Material.SALMON));
add(new ItemStack(Material.MUTTON));
add(new ItemStack(Material.RABBIT));
add(new ItemStack(Material.MUSHROOM_STEW));
add(new ItemStack(Material.BREAD));
add(new ItemStack(Material.CAKE));
add(new ItemStack(Material.COOKIE));
}};
}

View File

@ -15,6 +15,12 @@ public class SneakyWorker {
} }
} }
public static void silentTry(SneakyTry sneakyTry) {
try {
sneakyTry.tryThis();
} catch (Exception ignored) {}
}
public interface SneakyTry { public interface SneakyTry {
void tryThis() throws Exception; void tryThis() throws Exception;
} }

View File

@ -2,6 +2,7 @@ package io.github.simplex.luck.listener;
import io.github.simplex.lib.PotionEffectBuilder; import io.github.simplex.lib.PotionEffectBuilder;
import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.ListBox;
import io.github.simplex.luck.player.Luck; import io.github.simplex.luck.player.Luck;
import io.github.simplex.luck.player.PlayerHandler; import io.github.simplex.luck.player.PlayerHandler;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -18,11 +19,9 @@ import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent; import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import java.util.ArrayList;
import java.util.List;
public record PlayerListener(FeelingLucky plugin) implements Listener { public record PlayerListener(FeelingLucky plugin) implements Listener {
@EventHandler @EventHandler
@ -37,6 +36,10 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
if (luck.notDefault()) { if (luck.notDefault()) {
double percentage = luck.getPercentage(); 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 || PlayerHandler.isMarked(player)) { if (percentage < 0 || PlayerHandler.isMarked(player)) {
percentage = Math.abs(percentage); percentage = Math.abs(percentage);
if (luck.quickRNG(percentage)) { if (luck.quickRNG(percentage)) {
@ -90,12 +93,19 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
public void restoreHunger(PlayerItemConsumeEvent event) { public void restoreHunger(PlayerItemConsumeEvent event) {
ItemStack item = event.getItem(); ItemStack item = event.getItem();
Luck luck = PlayerHandler.getLuckContainer(event.getPlayer()); Luck luck = PlayerHandler.getLuckContainer(event.getPlayer());
PotionEffect effect = PotionEffectBuilder.newEffect()
.type(PotionEffectType.SATURATION)
.amplifier(2)
.duration(10)
.particles(false)
.create();
if (luck.notDefault()) { if (luck.notDefault()) {
double percentage = luck.getPercentage(); double percentage = luck.getPercentage();
ListBox.foods.forEach(food -> { ListBox.foods.forEach(food -> {
if (item.isSimilar(food)) { if (item.isSimilar(food)) {
if (luck.quickRNG(percentage)) { if (luck.quickRNG(percentage)) {
event.getPlayer().setExhaustion(event.getPlayer().getExhaustion() + 2); event.getPlayer().setExhaustion(event.getPlayer().getExhaustion() + 2);
event.getPlayer().addPotionEffect(effect);
} }
} }
}); });
@ -140,61 +150,4 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
} }
} }
private static class ListBox {
public static final List<DamageCause> acceptedCauses = new ArrayList<>() {{
add(DamageCause.ENTITY_ATTACK);
add(DamageCause.ENTITY_SWEEP_ATTACK);
add(DamageCause.PROJECTILE);
add(DamageCause.ENTITY_EXPLOSION);
add(DamageCause.FLY_INTO_WALL);
add(DamageCause.LIGHTNING);
add(DamageCause.MAGIC);
}};
public static final List<DamageCause> sideCauses = new ArrayList<>() {{
add(DamageCause.POISON);
add(DamageCause.WITHER);
add(DamageCause.FIRE_TICK);
}};
public static final List<PotionEffectType> potionEffects = new ArrayList<>() {{
add(PotionEffectType.POISON);
add(PotionEffectType.WITHER);
add(PotionEffectType.BLINDNESS);
add(PotionEffectType.SLOW);
add(PotionEffectType.SLOW_DIGGING);
add(PotionEffectType.BAD_OMEN);
add(PotionEffectType.CONFUSION);
add(PotionEffectType.WEAKNESS);
}};
public static final List<ItemStack> foods = new ArrayList<>() {{
add(new ItemStack(Material.COOKED_BEEF));
add(new ItemStack(Material.COOKED_CHICKEN));
add(new ItemStack(Material.COOKED_PORKCHOP));
add(new ItemStack(Material.COOKED_COD));
add(new ItemStack(Material.COOKED_MUTTON));
add(new ItemStack(Material.COOKED_RABBIT));
add(new ItemStack(Material.COOKED_SALMON));
add(new ItemStack(Material.BEETROOT_SOUP));
add(new ItemStack(Material.POTATO));
add(new ItemStack(Material.BAKED_POTATO));
add(new ItemStack(Material.CARROT));
add(new ItemStack(Material.GOLDEN_CARROT));
add(new ItemStack(Material.APPLE));
add(new ItemStack(Material.GOLDEN_APPLE));
add(new ItemStack(Material.ENCHANTED_GOLDEN_APPLE));
add(new ItemStack(Material.BEEF));
add(new ItemStack(Material.PORKCHOP));
add(new ItemStack(Material.CHICKEN));
add(new ItemStack(Material.COD));
add(new ItemStack(Material.SALMON));
add(new ItemStack(Material.MUTTON));
add(new ItemStack(Material.RABBIT));
add(new ItemStack(Material.MUSHROOM_STEW));
add(new ItemStack(Material.BREAD));
add(new ItemStack(Material.CAKE));
add(new ItemStack(Material.COOKIE));
}};
}
} }

View File

@ -34,10 +34,15 @@ public record PlayerHandler(FeelingLucky plugin) implements Listener {
@EventHandler @EventHandler
public void initializePlayer(PlayerLoginEvent event) { public void initializePlayer(PlayerLoginEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
PlayerConfig config = new PlayerConfig(plugin, player); PlayerConfig config = FeelingLucky.getConfigMap().get(player.getUniqueId());
if (config == null) {
config = new PlayerConfig(plugin, player);
FeelingLucky.getConfigMap().put(player.getUniqueId(), config);
}
String username = config.getString("username"); String username = config.getString("username");
double luckstat = config.getDouble("luck"); double luck = config.getDouble("luck");
double multiplier = config.getDouble("multiplier"); double multiplier = config.getDouble("multiplier");
if (!player.getName().equalsIgnoreCase(username)) { if (!player.getName().equalsIgnoreCase(username)) {
@ -46,11 +51,10 @@ public record PlayerHandler(FeelingLucky plugin) implements Listener {
config.load(); config.load();
} }
Luck luck = new Luck(player, multiplier); Luck container = new Luck(player, multiplier);
luck.setValue(luckstat); container.setValue(luck);
FeelingLucky.getConfigList().add(config); playerLuckMap.put(player, container);
playerLuckMap.put(player, luck);
} }
public void updatePlayer(Player player, Luck luck) { public void updatePlayer(Player player, Luck luck) {