Easter/src/main/java/com/alttd/easter/config/Config.java

142 lines
4.8 KiB
Java

package com.alttd.easter.config;
import lombok.extern.slf4j.Slf4j;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class Config extends AbstractConfig {
static Config config;
Config() {
super(
new File(File.separator
+ "mnt" + File.separator
+ "configs" + File.separator
+ "Easter"),
"config.yml");
}
public static void reload() {
log.info("Reloading config");
config = new Config();
config.readConfig(Config.class, null);
}
public static class EGGS {
private static final String prefix = "eggs.";
public static double SPAWN_CHANCE = 0.02; // ~2 per night per player
@SuppressWarnings("unused")
private static void load() {
SPAWN_CHANCE = config.getDouble(prefix, "spawn-chance", SPAWN_CHANCE);
}
}
public static class RABBIT {
private static final String prefix = "rabbit.";
public static String WORLD = "world";
public static double X = 0, Y = 64, Z = 0;
public static float YAW = 0, PITCH = 0;
public static double INTERACT_RADIUS = 2.5;
@SuppressWarnings("unused")
private static void load() {
WORLD = config.getString(prefix, "world", WORLD);
X = config.getDouble(prefix, "x", X);
Y = config.getDouble(prefix, "y", Y);
Z = config.getDouble(prefix, "z", Z);
YAW = (float) config.getDouble(prefix, "yaw", YAW);
PITCH = (float) config.getDouble(prefix, "pitch", PITCH);
INTERACT_RADIUS = config.getDouble(prefix, "interact-radius", INTERACT_RADIUS);
}
public static Location getLocation(org.bukkit.Server server) {
World world = server.getWorld(WORLD);
if (world == null) {
return null;
}
return new Location(world, X, Y, Z, YAW, PITCH);
}
}
public static class PRIZES {
private static final String prefix = "prizes.";
public static List<ItemStack> LIST = new ArrayList<>();
private static int NEXT_INDEX = 0;
@SuppressWarnings("unused")
private static void load() {
List<?> raw = config.yaml.getList(prefix + "list");
LIST = new ArrayList<>();
if (raw != null) {
for (Object o : raw) {
if (o instanceof ItemStack item) {
LIST.add(item);
}
}
}
// ensure path exists
config.yaml.addDefault(prefix + "list", LIST);
// load next index and clamp to range
NEXT_INDEX = config.getInt(prefix, "next-index", 0);
if (LIST.isEmpty()) {
NEXT_INDEX = 0;
} else if (NEXT_INDEX < 0 || NEXT_INDEX >= LIST.size()) {
NEXT_INDEX = NEXT_INDEX % LIST.size();
if (NEXT_INDEX < 0) NEXT_INDEX += LIST.size();
}
config.yaml.addDefault(prefix + "next-index", NEXT_INDEX);
}
public static void addPrize(ItemStack item) {
LIST.add(item);
config.set(prefix, "list", LIST);
// keep NEXT_INDEX valid
if (NEXT_INDEX < 0 || NEXT_INDEX >= LIST.size()) {
NEXT_INDEX = 0;
}
config.set(prefix, "next-index", NEXT_INDEX);
}
public static void setPrizeAt(int index, ItemStack item) {
if (index < 0 || index > LIST.size()) {
throw new IndexOutOfBoundsException("Index must be between 0 and " + LIST.size());
}
if (index == LIST.size()) {
LIST.add(item);
} else {
LIST.set(index, item);
}
config.set(prefix, "list", LIST);
// adjust NEXT_INDEX if necessary
if (LIST.isEmpty()) {
NEXT_INDEX = 0;
} else if (NEXT_INDEX >= LIST.size()) {
NEXT_INDEX = NEXT_INDEX % LIST.size();
}
config.set(prefix, "next-index", NEXT_INDEX);
}
public static ItemStack getNextPrize() {
if (LIST.isEmpty()) {
log.warn("[Easter] No prizes configured. Staff should configure prizes using /easter setprize.");
return null;
}
if (NEXT_INDEX < 0 || NEXT_INDEX >= LIST.size()) {
NEXT_INDEX = 0;
}
ItemStack prize = LIST.get(NEXT_INDEX).clone();
NEXT_INDEX = (NEXT_INDEX + 1) % LIST.size();
config.set(prefix, "next-index", NEXT_INDEX);
return prize;
}
}
}