Essentia plugin

Basic plugin with some essential utilities and commands.
This commit is contained in:
Len
2024-02-03 21:58:02 +01:00
commit 1669a114bc
44 changed files with 2359 additions and 0 deletions
@@ -0,0 +1,239 @@
package com.alttd.essentia.configuration;
import com.alttd.essentia.EssentiaPlugin;
import com.google.common.base.Throwables;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.logging.Level;
public class Config {
private static final String HEADER = """
Essentia main configuration file
""";
private static File CONFIG_FILE;
public static File CONFIG_PATH;
public static YamlConfiguration config;
static int version;
public static void init() {
CONFIG_PATH = EssentiaPlugin.instance().getDataFolder();
CONFIG_FILE = new File(CONFIG_PATH, "config.yml");
config = new YamlConfiguration();
try {
config.load(CONFIG_FILE);
} catch (IOException ignore) {
} catch (InvalidConfigurationException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Could not load config.yml, please correct your syntax errors", ex);
Throwables.throwIfUnchecked(ex);
}
config.options().header(HEADER);
config.options().copyDefaults(true);
version = getInt("config-version", 1);
set("config-version", 1);
readConfig(Config.class, null);
}
static void readConfig(Class<?> clazz, Object instance) {
for (Method method : clazz.getDeclaredMethods()) {
if (Modifier.isPrivate(method.getModifiers())) {
if (method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE) {
try {
method.setAccessible(true);
method.invoke(instance);
} catch (InvocationTargetException ex) {
Throwables.throwIfUnchecked(ex);
} catch (Exception ex) {
Bukkit.getLogger().log(Level.SEVERE, "Error invoking " + method, ex);
}
}
}
}
saveConfig();
}
static void saveConfig() {
try {
config.save(CONFIG_FILE);
} catch (IOException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Could not save " + CONFIG_FILE, ex);
}
}
private static void set(String path, Object val) {
config.addDefault(path, val);
config.set(path, val);
}
private static boolean getBoolean(String path, boolean def) {
config.addDefault(path, def);
return config.getBoolean(path, config.getBoolean(path));
}
private static double getDouble(String path, double def) {
config.addDefault(path, def);
return config.getDouble(path, config.getDouble(path));
}
private static int getInt(String path, int def) {
config.addDefault(path, def);
return config.getInt(path, config.getInt(path));
}
private static <T> List getList(String path, T def) {
config.addDefault(path, def);
return config.getList(path, config.getList(path));
}
private static String getString(String path, String def) {
config.addDefault(path, def);
return config.getString(path, config.getString(path));
}
protected static void log(Level level, String s) {
Bukkit.getLogger().log(level, s);
}
public static int TELEPORT_REQUEST_TIMEOUT = 30;
public static boolean TELEPORT_REQUEST_TIMEOUT_MESSAGES = true;
public static boolean BACK_ON_DEATH = false;
public static boolean TELEPORT_SOUNDS = true;
public static Sound SOUND_TO;
public static Sound SOUND_FROM;
public static int BACK_COOLDOWN = 60;
public static boolean UNSAFE_ENCHANTMENTS = false;
private static void settings() {
TELEPORT_REQUEST_TIMEOUT = getInt("teleport-request-timeout", TELEPORT_REQUEST_TIMEOUT);
TELEPORT_REQUEST_TIMEOUT_MESSAGES = getBoolean("teleport-request-timeout-message", TELEPORT_REQUEST_TIMEOUT_MESSAGES);
BACK_ON_DEATH = getBoolean("back-on-death", BACK_ON_DEATH);
TELEPORT_SOUNDS = getBoolean("use-teleport-sounds", TELEPORT_SOUNDS);
try {
SOUND_TO = Sound.valueOf(config.getString("sound-to", "ENTITY_ENDERMAN_TELEPORT"));
} catch (IllegalArgumentException e) {
SOUND_TO = Sound.ENTITY_ENDERMAN_TELEPORT;
}
try {
SOUND_FROM = Sound.valueOf(config.getString("sound-from", "ENTITY_ENDERMAN_TELEPORT"));
} catch (IllegalArgumentException e) {
SOUND_FROM = Sound.ENTITY_ENDERMAN_TELEPORT;
}
UNSAFE_ENCHANTMENTS = getBoolean("unsafe-enchantments", UNSAFE_ENCHANTMENTS);
BACK_COOLDOWN = getInt("back-cooldown", BACK_COOLDOWN);
}
public static String REQUEST_TIMED_OUT = "Your teleport request has timed out!";
public static String TELEPORT_ACCEPT_REQUESTER = "<target> has accepted your teleport request.";
public static String TELEPORT_ACCEPT_TARGET = "You have accepted the teleport request from <requester>.";
public static String TELEPORT_DENIED_REQUESTER = "<target> has denied your teleport request.";
public static String TELEPORT_DENIED_TARGET = "You have denied the teleport request from <requester>.";
public static String TELEPORT_REQUEST_REQUESTER = "Teleport request sent to <target>.";
public static String TELEPORT_REQUEST_TARGET = "<requester> has requested to teleport to you. Type /tpaccept or /tpdeny.";
public static String TELEPORT_REQUESTHERE_REQUESTER = "Teleport here request sent to <target>.";
public static String TELEPORT_REQUESTHERE_TARGET = "<requester> has requested you to teleport to them. Type <gold>/tpaccept</gold> or <gold>/tpdeny</gold>.";
public static String TELEPORT_TOGGLE_SET = "Teleport requests toggled <gold><toggle></gold>.";
public static String NO_PENDING_REQUESTS = "You do not have any pending teleport requests.";
public static String TELEPORT_TOGGLED_OFF = "<target> has teleports toggled off!";
public static String TARGET_HAS_PENDING_REQUEST = "<target> has pending request!";
public static String REQUEST_TO_SELF = "You can not teleport to yourself!";
public static String PLAYER_ONLY_COMMAND = "<red>This command is only available to players.";
public static String NO_PLAYER_SPECIFIED = "You must specify a player name!";
public static String PLAYER_NOT_FOUND = "That player does not exist!";
public static String PLAYER_NOT_ONLINE = "That player is not online right now!";
public static String COMMAND_NO_PERMISSION = "You do not have permission for that command!";
public static String PLAYER_INVENTORY_CLEARED = "You have cleared the inventory of <target>.";
public static String INVENTORY_CLEARED_BY_OTHER = "Your inventory has been cleared by <requester>";
public static String INVENTORY_CLEARED = "You have cleared your inventory.";
public static String SPECIFY_HOME = "Please specify a home!<newline><homelist>";
public static String HOME_NOT_SET = "You have not set a home!";
public static String HOME_DOES_NOT_EXIST = "Home <home> does not exist!";
public static String HOME_TELEPORT = "Teleporting to home <home>.";
public static String HOME_LIST = "Homes: <homelist>";
public static String HOME_SET = "Home <home> set.";
public static String HOME_SET_MAX = "You have reached the maximum of <limit> homes.";
public static String INVALID_HOME_NAME = "Invalid home name!";
public static String HOME_DELETED = "The home <home> has been deleted.";
public static String TOGGLED_FLIGHT_BY_OTHER = "Toggled flight <status> on <target>.";
public static String TOGGLED_FLIGHT_PLAYER = "<player> toggled flight <status>.";
public static String TOGGLED_FLIGHT = "Toggled fly <status>.";
public static String NO_BACK_LOCATION = "No back location found!";
public static String TELEPORTING_BACK = "Teleporting back to previous location.";
public static String NO_DEATH_LOCATION = "No death location found!";
public static String TELEPORTING_BACK_DEATH = "Teleporting back to previous death location.";
public static String BACK_DEATH_HINT = "Type /dback to go back to where you died.";
public static String GAMEMODE_SET = "Gamemode set to <gamemode>.";
public static String GAMEMODE_SET_OTHER = "Gamemode for <target> set to <gamemode>.";
public static String GAMEMODE_SET_BY_OTHER = "Gamemode set to <gamemode> by <requester>.";
public static String HEAL_SELF = "Your health has been restored.";
public static String HEAL_OTHER = "<target>'s health has been restored.";
public static String HEAL_BY_OTHER = "<requester> has restored your health.";
public static String FEED_SELF = "You just fed yourself.";
public static String FEED_OTHER = "You have fed <target>.";
public static String FEED_BY_OTHER = "<requester> has fed you.";
private static void messages() {
REQUEST_TIMED_OUT = getString("messages.request.time-out", REQUEST_TIMED_OUT);
TELEPORT_ACCEPT_TARGET = getString("messages.request.teleport-accept-target", TELEPORT_ACCEPT_TARGET);
TELEPORT_ACCEPT_REQUESTER = getString("messages.request.teleport-accept-requester", TELEPORT_ACCEPT_REQUESTER);
TELEPORT_DENIED_TARGET = getString("messages.request.teleport-denied-target", TELEPORT_DENIED_TARGET);
TELEPORT_DENIED_REQUESTER = getString("messages.request.teleport-denied-requester", TELEPORT_DENIED_REQUESTER);
TELEPORT_REQUESTHERE_TARGET = getString("messages.request.teleport-requesthere-target", TELEPORT_REQUESTHERE_TARGET);
TELEPORT_REQUEST_TARGET = getString("messages.request.teleport-request-target", TELEPORT_REQUEST_TARGET);
TELEPORT_TOGGLE_SET = getString("messages.request.teleport-toggle-set", TELEPORT_TOGGLE_SET);
NO_PENDING_REQUESTS = getString("messages.request.no-pending-requests", NO_PENDING_REQUESTS);
TELEPORT_TOGGLED_OFF = getString("messages.request.target-toggled-off", TELEPORT_TOGGLED_OFF);
TARGET_HAS_PENDING_REQUEST = getString("messages.request.target-has-pending-request", TARGET_HAS_PENDING_REQUEST);
REQUEST_TO_SELF = getString("messages.request.request-to-self", REQUEST_TO_SELF);
PLAYER_ONLY_COMMAND = getString("messages.command.player-only-command", PLAYER_ONLY_COMMAND);
NO_PLAYER_SPECIFIED = getString("messages.command.no-player-specified", NO_PLAYER_SPECIFIED);
PLAYER_NOT_FOUND = config.getString("messages.command.player-not-found", PLAYER_NOT_FOUND);
PLAYER_NOT_ONLINE = config.getString("messages.command.player-not-online", PLAYER_NOT_ONLINE);
COMMAND_NO_PERMISSION = config.getString("messages.command.no-permission", COMMAND_NO_PERMISSION);
PLAYER_INVENTORY_CLEARED = config.getString("messages.command.clear-inventory.player-inventory-cleared", PLAYER_INVENTORY_CLEARED);
INVENTORY_CLEARED_BY_OTHER = config.getString("messages.command.clear-inventory.inventory-clear-by-other", INVENTORY_CLEARED_BY_OTHER);
INVENTORY_CLEARED = config.getString("messages.command.clear-inventory.inventory-cleared", INVENTORY_CLEARED);
SPECIFY_HOME = config.getString("messages.command.home.specify-home", SPECIFY_HOME);
HOME_NOT_SET = config.getString("messages.command.home.home-not-set", HOME_NOT_SET);
HOME_DOES_NOT_EXIST = config.getString("messages.command.home.home-does-not-exist", HOME_DOES_NOT_EXIST);
HOME_TELEPORT = config.getString("messages.command.home.home-teleport", HOME_TELEPORT);
HOME_LIST = config.getString("messages.command.home.home-list", HOME_LIST);
HOME_SET = config.getString("messages.command.home.home-set", HOME_SET);
HOME_SET_MAX = config.getString("messages.command.home.home-set-max", HOME_SET_MAX);
INVALID_HOME_NAME = config.getString("messages.command.home.invalid-home-name", INVALID_HOME_NAME);
HOME_DELETED = config.getString("messages.command.home.invalid-home-name", HOME_DELETED);
TOGGLED_FLIGHT_BY_OTHER = config.getString("messages.command.fly.toggled-by-other", TOGGLED_FLIGHT_BY_OTHER);
TOGGLED_FLIGHT_PLAYER = config.getString("messages.command.fly.toggled-flight-other", TOGGLED_FLIGHT_PLAYER);
TOGGLED_FLIGHT = config.getString("messages.command.fly.toggled-flight", TOGGLED_FLIGHT);
NO_BACK_LOCATION = config.getString("messages.command.back.no-back-location", NO_BACK_LOCATION);
TELEPORTING_BACK = config.getString("messages.command.back.teleporting-back", TELEPORTING_BACK);
NO_DEATH_LOCATION = config.getString("messages.command.back.no-death-location", NO_DEATH_LOCATION);
TELEPORTING_BACK_DEATH = config.getString("messages.command.back.teleporting-back-death", TELEPORTING_BACK_DEATH);
BACK_DEATH_HINT = config.getString("messages.command.back.dback-hint", BACK_DEATH_HINT);
GAMEMODE_SET = config.getString("messages.command.gamemode.gamemode-set", GAMEMODE_SET);
GAMEMODE_SET_OTHER = config.getString("messages.command.gamemode.gamemode-set-other", GAMEMODE_SET_OTHER);
GAMEMODE_SET_BY_OTHER = config.getString("messages.command.gamemode.gamemode-set-by-other", GAMEMODE_SET_BY_OTHER);
HEAL_SELF = config.getString("messages.command.heal.heal-self", HEAL_SELF);
HEAL_OTHER = config.getString("messages.command.heal.heal-other", HEAL_OTHER);
HEAL_BY_OTHER = config.getString("messages.command.heal.heal-by-other", HEAL_BY_OTHER);
FEED_SELF = config.getString("messages.command.feed.feed-self", FEED_SELF);
FEED_OTHER = config.getString("messages.command.feed.feed-other", FEED_OTHER);
FEED_BY_OTHER = config.getString("messages.command.feed.feed-by-other", FEED_BY_OTHER);
}
}
@@ -0,0 +1,181 @@
package com.alttd.essentia.configuration;
import com.alttd.essentia.EssentiaPlugin;
import com.alttd.essentia.request.Request;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class PlayerConfig extends YamlConfiguration {
private static final Map<Player, PlayerConfig> configs = new HashMap<>();
public static PlayerConfig getConfig(Player player) {
synchronized (configs) {
return configs.computeIfAbsent(player, k -> new PlayerConfig(player));
}
}
public static void remove(Player player) {
synchronized (configs) {
configs.remove(player);
}
}
public static void removeAll() {
synchronized (configs) {
configs.clear();
}
}
private final File file;
private final Object saveLock = new Object();
private final OfflinePlayer player;
@Getter @Setter private Request request;
private PlayerConfig(Player player) {
super();
this.player = player;
this.file = new File(EssentiaPlugin.instance().getDataFolder(), "PlayerData" + File.separator + player.getUniqueId() + ".yml");
reload();
}
private void reload() {
synchronized (saveLock) {
try {
load(file);
} catch (Exception ignore) {
}
}
}
private void save() {
synchronized (saveLock) {
try {
save(file);
} catch (Exception ignore) {
}
}
}
Location getStoredLocation(String path) {
if (get(path) == null) {
return null;
}
World world = Bukkit.getWorld(getString(path + ".world", ""));
if (world == null) {
return null;
}
double x = getDouble(path + ".x");
double y = getDouble(path + ".y");
double z = getDouble(path + ".z");
float pitch = (float) getDouble(path + ".pitch");
float yaw = (float) getDouble(path + ".yaw");
return new Location(world, x, y, z, yaw, pitch);
}
void setStoredLocation(String path, Location location) {
if (location == null) {
set(path, null);
save();
return;
}
set(path + ".world", location.getWorld().getName());
set(path + ".x", location.getX());
set(path + ".y", location.getY());
set(path + ".z", location.getZ());
set(path + ".pitch", location.getPitch());
set(path + ".yaw", location.getYaw());
save();
}
public Location getBackLocation(boolean death) {
return getStoredLocation(death ? "teleports.death" : "teleports.back");
}
public void setBackLocation(boolean death, Location location) {
setStoredLocation(death ? "teleports.death" : "teleports.back", location);
}
public Location getHome(String name) {
return getStoredLocation("home." + name);
}
public void setHome(String name, Location location) {
setStoredLocation("home." + name, location);
}
public int getHomeCount() {
ConfigurationSection section = getConfigurationSection("home");
if (section == null) {
return 0;
}
return section.getKeys(false).size();
}
public List<String> getMatchingHomeNames(String name) {
ConfigurationSection section = getConfigurationSection("home");
if (section == null) {
return null;
}
List<String> list = section.getValues(false).keySet().stream()
.filter(home -> home.toLowerCase().startsWith(name.toLowerCase()))
.collect(Collectors.toList());
if (player.getBedSpawnLocation() != null && "bed".startsWith(name.toLowerCase()))
list.add("bed");
return list;
}
public Map<String, Location> getHomeData() {
ConfigurationSection section = getConfigurationSection("home");
if (section == null) {
return null;
}
Map<String, Location> map = new HashMap<>();
for (String key : section.getValues(false).keySet()) {
map.put(key, getHome(key));
}
if (player.getBedSpawnLocation() != null)
map.put("bed", player.getBedSpawnLocation());
return map;
}
public List<String> getHomeList() {
ConfigurationSection section = getConfigurationSection("home");
if (section == null) {
return null;
}
List<String> list = new ArrayList<>(section.getValues(false).keySet());
if (player.getBedSpawnLocation() != null)
list.add("bed");
return list;
}
public boolean allowTeleports() {
return getBoolean("allow-teleports", true);
}
public void setAllowTeleports(boolean allowTeleports) {
set("allow-teleports", allowTeleports);
save();
}
}