Initial commit
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package com.alttd.playerutils;
|
||||
|
||||
import com.alttd.playerutils.commands.PlayerUtilsCommand;
|
||||
import com.alttd.playerutils.event_listeners.XpBottleEvent;
|
||||
import com.alttd.playerutils.util.Logger;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public final class PlayerUtils extends JavaPlugin {
|
||||
|
||||
private Logger logger;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
this.logger = new Logger(getLogger());
|
||||
registerCommands();
|
||||
registerEvents();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
}
|
||||
|
||||
private void registerCommands() {
|
||||
new PlayerUtilsCommand(this, logger);
|
||||
}
|
||||
|
||||
private void registerEvents() {
|
||||
getServer().getPluginManager().registerEvents(new XpBottleEvent(this, logger), this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.alttd.playerutils.commands;
|
||||
|
||||
import com.alttd.playerutils.PlayerUtils;
|
||||
import com.alttd.playerutils.commands.playerutils_subcommands.Glow;
|
||||
import com.alttd.playerutils.commands.playerutils_subcommands.XPCheque;
|
||||
import com.alttd.playerutils.config.Messages;
|
||||
import com.alttd.playerutils.util.Logger;
|
||||
import org.bukkit.command.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PlayerUtilsCommand implements CommandExecutor, TabExecutor {
|
||||
private final List<SubCommand> subCommands;
|
||||
|
||||
public PlayerUtilsCommand(PlayerUtils playerUtils, Logger logger) {
|
||||
PluginCommand command = playerUtils.getCommand("playerutils");
|
||||
if (command == null) {
|
||||
subCommands = null;
|
||||
logger.severe("Unable to find transfer command.");
|
||||
return;
|
||||
}
|
||||
command.setExecutor(this);
|
||||
command.setTabCompleter(this);
|
||||
command.setAliases(List.of("pu"));
|
||||
|
||||
subCommands = Arrays.asList(
|
||||
new Glow(),
|
||||
new XPCheque(playerUtils)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String cmd, @NotNull String[] args) {
|
||||
if (args.length == 0) {
|
||||
commandSender.sendMiniMessage(Messages.HELP.HELP_MESSAGE_WRAPPER.replaceAll("<commands>", subCommands.stream()
|
||||
.filter(subCommand -> commandSender.hasPermission(subCommand.getPermission()))
|
||||
.map(SubCommand::getHelpMessage)
|
||||
.collect(Collectors.joining("\n"))), null);
|
||||
return true;
|
||||
}
|
||||
|
||||
SubCommand subCommand = getSubCommand(args[0]);
|
||||
if (subCommand == null)
|
||||
return false;
|
||||
|
||||
if (!commandSender.hasPermission(subCommand.getPermission())) {
|
||||
commandSender.sendMiniMessage(Messages.GENERIC.NO_PERMISSION, null);
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean executedCorrectly = subCommand.onCommand(commandSender, args);
|
||||
if (!executedCorrectly) {
|
||||
commandSender.sendMiniMessage(subCommand.getHelpMessage(), null);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String cmd, @NotNull String[] args) {
|
||||
List<String> res = new ArrayList<>();
|
||||
|
||||
if (args.length <= 1) {
|
||||
res.addAll(subCommands.stream()
|
||||
.filter(subCommand -> commandSender.hasPermission(subCommand.getPermission()))
|
||||
.map(SubCommand::getName)
|
||||
.filter(name -> args.length == 0 || name.startsWith(args[0]))
|
||||
.toList()
|
||||
);
|
||||
} else {
|
||||
SubCommand subCommand = getSubCommand(args[0]);
|
||||
if (subCommand != null && commandSender.hasPermission(subCommand.getPermission()))
|
||||
res.addAll(subCommand.getTabComplete(commandSender, args).stream()
|
||||
.filter(str -> str.toLowerCase().startsWith(args[args.length - 1].toLowerCase()))
|
||||
.toList());
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public List<SubCommand> getSubCommands() {
|
||||
return subCommands;
|
||||
}
|
||||
|
||||
private SubCommand getSubCommand(String cmdName) {
|
||||
return subCommands.stream()
|
||||
.filter(subCommand -> subCommand.getName().equals(cmdName))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.alttd.playerutils.commands;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class SubCommand {
|
||||
|
||||
public SubCommand() {}
|
||||
|
||||
public abstract boolean onCommand(CommandSender commandSender, String[] args);
|
||||
|
||||
public abstract String getName();
|
||||
|
||||
public String getPermission() {
|
||||
return "playerutils." + getName();
|
||||
}
|
||||
|
||||
public abstract List<String> getTabComplete(CommandSender commandSender, String[] args);
|
||||
|
||||
public abstract String getHelpMessage();
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.alttd.playerutils.commands.playerutils_subcommands;
|
||||
|
||||
import com.alttd.playerutils.commands.SubCommand;
|
||||
import com.alttd.playerutils.config.Messages;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.DyeColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scoreboard.Scoreboard;
|
||||
import org.bukkit.scoreboard.Team;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Glow extends SubCommand {
|
||||
@Override
|
||||
public boolean onCommand(CommandSender commandSender, String[] args) {
|
||||
if (!(commandSender instanceof Player player)) {
|
||||
commandSender.sendMiniMessage(Messages.GENERIC.PLAYER_ONLY, null);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length != 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (args[1].equalsIgnoreCase("off")) {
|
||||
commandSender.sendMiniMessage(Messages.GLOW.GLOW_OFF, null);
|
||||
player.setGlowing(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
Optional<DyeColor> any = Arrays.stream(DyeColor.values()).filter(chatColor -> chatColor.name().equalsIgnoreCase(args[1])).findAny();
|
||||
if (any.isEmpty()) {
|
||||
commandSender.sendMiniMessage(getHelpMessage(), null);
|
||||
return true;
|
||||
}
|
||||
|
||||
DyeColor dyeColor = any.get();
|
||||
Scoreboard board = Bukkit.getScoreboardManager().getMainScoreboard();
|
||||
|
||||
Team team = board.getTeam("Glow " + dyeColor.name());
|
||||
|
||||
if (team == null) {
|
||||
team = board.registerNewTeam("Glow " + dyeColor.name());
|
||||
}
|
||||
|
||||
if (!team.hasEntry(player.getName())) {
|
||||
team.addEntry(player.getName());
|
||||
}
|
||||
|
||||
NamedTextColor namedTextColor = NamedTextColor.nearestTo(TextColor.color(dyeColor.getColor().asRGB()));
|
||||
team.color(namedTextColor);
|
||||
|
||||
player.setGlowing(true);
|
||||
commandSender.sendMiniMessage(Messages.GLOW.GLOW_ON, Placeholder.parsed("color", dyeColor.name()));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "glow";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTabComplete(CommandSender commandSender, String[] args) {
|
||||
if (args.length == 2) {
|
||||
return Arrays.stream(DyeColor.values()).map(Enum::name).collect(Collectors.toList());
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpMessage() {
|
||||
return Messages.HELP.GLOW;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
package com.alttd.playerutils.commands.playerutils_subcommands;
|
||||
|
||||
import com.alttd.playerutils.PlayerUtils;
|
||||
import com.alttd.playerutils.commands.SubCommand;
|
||||
import com.alttd.playerutils.config.Messages;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class XPCheque extends SubCommand {
|
||||
|
||||
private final PlayerUtils playerUtils;
|
||||
private final MiniMessage miniMessage = MiniMessage.miniMessage();
|
||||
|
||||
public XPCheque(PlayerUtils playerUtils) {
|
||||
this.playerUtils = playerUtils;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender commandSender, String[] args) {
|
||||
if (!(commandSender instanceof Player player)) {
|
||||
commandSender.sendMiniMessage(Messages.GENERIC.PLAYER_ONLY, null);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length != 2)
|
||||
return false;
|
||||
|
||||
int xpValue;
|
||||
try {
|
||||
xpValue = Integer.parseInt(args[1]);
|
||||
} catch (NumberFormatException e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int totalExperience = player.getTotalExperience();
|
||||
if (totalExperience < xpValue) {
|
||||
commandSender.sendMiniMessage(Messages.XP_CHEQUE.NOT_ENOUGH_XP, Placeholder.parsed("xp", String.valueOf(totalExperience)));
|
||||
return true;
|
||||
}
|
||||
|
||||
ItemStack heldItem = player.getInventory().getItemInMainHand();
|
||||
if (!heldItem.getType().equals(Material.GLASS_BOTTLE)) {
|
||||
commandSender.sendMiniMessage(Messages.XP_CHEQUE.NOT_HOLDING_BOTTLE, null);
|
||||
return true;
|
||||
}
|
||||
|
||||
Optional<ItemStack> optionalItemStack = getExpBottleItem(player, xpValue);
|
||||
if (optionalItemStack.isEmpty())
|
||||
return true;
|
||||
|
||||
ItemStack xpBottle = optionalItemStack.get();
|
||||
|
||||
decreaseExperience(player, xpValue);
|
||||
|
||||
if (heldItem.getAmount() == 1) {
|
||||
player.getInventory().setItemInMainHand(xpBottle);
|
||||
player.updateInventory();
|
||||
return true;
|
||||
}
|
||||
|
||||
heldItem.setAmount(heldItem.getAmount() - 1);
|
||||
player.getInventory().addItem(xpBottle).values()
|
||||
.forEach(item -> player.getWorld().dropItemNaturally(player.getLocation(), item));
|
||||
player.updateInventory();
|
||||
return true;
|
||||
}
|
||||
|
||||
private Optional<ItemStack> getExpBottleItem(Player player, int xpValue) {
|
||||
ItemStack expBottle = new ItemStack(Material.EXPERIENCE_BOTTLE);
|
||||
ItemMeta itemMeta = expBottle.getItemMeta();
|
||||
PersistentDataContainer persistentDataContainer = itemMeta.getPersistentDataContainer();
|
||||
NamespacedKey customXp = NamespacedKey.fromString("custom_xp", playerUtils);
|
||||
|
||||
if (customXp == null) {
|
||||
player.sendMiniMessage(Messages.XP_CHEQUE.FAILED_STORAGE, null);
|
||||
return Optional.empty();
|
||||
}
|
||||
persistentDataContainer.set(customXp, PersistentDataType.INTEGER, xpValue);
|
||||
itemMeta.displayName(miniMessage.deserialize(Messages.XP_CHEQUE.DISPLAY_NAME, Placeholder.parsed("xp", String.valueOf(xpValue))));
|
||||
itemMeta.lore(Messages.XP_CHEQUE.LORE.stream()
|
||||
.map(str -> miniMessage.deserialize(str, Placeholder.component("name", player.displayName())))
|
||||
.collect(Collectors.toList()));
|
||||
expBottle.setItemMeta(itemMeta);
|
||||
return Optional.of(expBottle);
|
||||
}
|
||||
|
||||
public void decreaseExperience(Player player, int xpToRemove) {
|
||||
// int oldLevel = player.getLevel();
|
||||
// float oldExp = player.getExp();
|
||||
// int newTotalExp = Math.max(player.getTotalExperience() - xpToRemove, 0);
|
||||
//
|
||||
// player.setTotalExperience(newTotalExp);
|
||||
//
|
||||
// int newLevel = player.getLevel();
|
||||
// float newExp = player.getExp();
|
||||
// float progressDiff = newExp - oldExp;
|
||||
//
|
||||
// if (newLevel < oldLevel) {
|
||||
// progressDiff += 1;
|
||||
// player.sendExperienceChange(-progressDiff, newLevel - oldLevel);
|
||||
// } else
|
||||
// player.sendExperienceChange(progressDiff, newLevel - oldLevel);
|
||||
|
||||
int totalExp = player.getTotalExperience();
|
||||
int newTotalExp = Math.max(totalExp - xpToRemove, 0);
|
||||
|
||||
int level = 0;
|
||||
while (true) {
|
||||
int expToNextLevel = getExpToNext(level);
|
||||
if (newTotalExp < expToNextLevel)
|
||||
break;
|
||||
newTotalExp -= expToNextLevel;
|
||||
level++;
|
||||
}
|
||||
|
||||
float progress = (float) newTotalExp / getExpToNext(level);
|
||||
|
||||
player.setTotalExperience(totalExp - xpToRemove);
|
||||
player.setLevel(level);
|
||||
player.setExp(progress);
|
||||
}
|
||||
|
||||
public int getExpToNext(int level) {
|
||||
if (level <= 15) {
|
||||
return 2 * level + 7;
|
||||
} else if (level <= 30) {
|
||||
return 5 * level - 38;
|
||||
} else {
|
||||
return 9 * level - 158;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "xpcheque";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTabComplete(CommandSender commandSender, String[] args) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpMessage() {
|
||||
return Messages.HELP.XP_CHEQUE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package com.alttd.playerutils.config;
|
||||
|
||||
import com.alttd.playerutils.PlayerUtils;
|
||||
import com.alttd.playerutils.util.Logger;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
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.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@SuppressWarnings({"unused", "SameParameterValue"})
|
||||
abstract class AbstractConfig {
|
||||
File file;
|
||||
YamlConfiguration yaml;
|
||||
private static Logger logger = null;
|
||||
|
||||
AbstractConfig(PlayerUtils playerUtils, String filename, Logger logger) {
|
||||
AbstractConfig.logger = logger;
|
||||
init(new File(playerUtils.getDataFolder(), filename), filename);
|
||||
}
|
||||
|
||||
AbstractConfig(File file, String filename, Logger logger) {
|
||||
AbstractConfig.logger = logger;
|
||||
init(new File(file.getPath() + File.separator + filename), filename);
|
||||
}
|
||||
|
||||
private void init(File file, String filename) {
|
||||
this.file = file;
|
||||
this.yaml = new YamlConfiguration();
|
||||
try {
|
||||
yaml.load(file);
|
||||
} catch (IOException ignore) {
|
||||
} catch (InvalidConfigurationException ex) {
|
||||
if (logger != null)
|
||||
logger.severe(String.format("Could not load %s, please correct your syntax errors", filename));
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
yaml.options().copyDefaults(true);
|
||||
}
|
||||
|
||||
void readConfig(Class<?> clazz, Object instance) {
|
||||
for (Class<?> declaredClass : clazz.getDeclaredClasses()) {
|
||||
for (Method method : declaredClass.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) {
|
||||
throw new RuntimeException(ex.getCause());
|
||||
} catch (Exception ex) {
|
||||
if (logger != null)
|
||||
logger.severe("Error invoking %.", method.toString());
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
save();
|
||||
}
|
||||
|
||||
private void save() {
|
||||
try {
|
||||
yaml.save(file);
|
||||
} catch (IOException ex) {
|
||||
if (logger != null)
|
||||
logger.severe("Could not save %.", file.toString());
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
void set(String prefix, String path, Object val) {
|
||||
path = prefix + path;
|
||||
yaml.addDefault(path, val);
|
||||
yaml.set(path, val);
|
||||
save();
|
||||
}
|
||||
|
||||
String getString(String prefix, String path, String def) {
|
||||
path = prefix + path;
|
||||
yaml.addDefault(path, def);
|
||||
return yaml.getString(path, yaml.getString(path));
|
||||
}
|
||||
|
||||
boolean getBoolean(String prefix, String path, boolean def) {
|
||||
path = prefix + path;
|
||||
yaml.addDefault(path, def);
|
||||
return yaml.getBoolean(path, yaml.getBoolean(path));
|
||||
}
|
||||
|
||||
int getInt(String prefix, String path, int def) {
|
||||
path = prefix + path;
|
||||
yaml.addDefault(path, def);
|
||||
return yaml.getInt(path, yaml.getInt(path));
|
||||
}
|
||||
|
||||
double getDouble(String prefix, String path, double def) {
|
||||
path = prefix + path;
|
||||
yaml.addDefault(path, def);
|
||||
return yaml.getDouble(path, yaml.getDouble(path));
|
||||
}
|
||||
|
||||
<T> List<String> getList(String prefix, String path, T def) {
|
||||
path = prefix + path;
|
||||
yaml.addDefault(path, def);
|
||||
List<?> list = yaml.getList(path, yaml.getList(path));
|
||||
return list == null ? null : list.stream().map(Object::toString).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
List<String> getStringList(String prefix, String path, List<String> def) {
|
||||
path = prefix + path;
|
||||
yaml.addDefault(path, def);
|
||||
return yaml.getStringList(path);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
<T> Map<String, T> getMap(String prefix, @NonNull String path, final @Nullable Map<String, T> def) {
|
||||
path = prefix + path;
|
||||
final ImmutableMap.Builder<String, T> builder = ImmutableMap.builder();
|
||||
if (def != null && yaml.getConfigurationSection(path) == null) {
|
||||
yaml.addDefault(path, def.isEmpty() ? new HashMap<>() : def);
|
||||
return def;
|
||||
}
|
||||
final ConfigurationSection section = yaml.getConfigurationSection(path);
|
||||
if (section != null) {
|
||||
for (String key : section.getKeys(false)) {
|
||||
@SuppressWarnings("unchecked")
|
||||
final T val = (T) section.get(key);
|
||||
if (val != null) {
|
||||
builder.put(key, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
ConfigurationSection getConfigurationSection(String path) {
|
||||
return yaml.getConfigurationSection(path);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.alttd.playerutils.config;
|
||||
|
||||
import com.alttd.playerutils.PlayerUtils;
|
||||
import com.alttd.playerutils.util.Logger;
|
||||
|
||||
public class Config extends AbstractConfig{
|
||||
|
||||
static Config config;
|
||||
private Logger logger;
|
||||
|
||||
Config(PlayerUtils playerUtils, Logger logger) {
|
||||
super(playerUtils, "config.yml", logger);
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public static void reload(PlayerUtils playerUtils, Logger logger) {
|
||||
logger.info("Reloading config");
|
||||
config = new Config(playerUtils, logger);
|
||||
config.readConfig(Config.class, null);
|
||||
}
|
||||
|
||||
public static class SETTINGS {
|
||||
private static final String prefix = "settings.";
|
||||
public static boolean DEBUG = false;
|
||||
public static boolean WARNINGS = true;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static void load() {
|
||||
DEBUG = config.getBoolean(prefix, "debug", DEBUG);
|
||||
WARNINGS = config.getBoolean(prefix, "warnings", WARNINGS);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.alttd.playerutils.config;
|
||||
|
||||
import com.alttd.playerutils.PlayerUtils;
|
||||
import com.alttd.playerutils.util.Logger;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Messages extends AbstractConfig {
|
||||
static Messages config;
|
||||
private final Logger logger;
|
||||
|
||||
Messages(PlayerUtils playerUtils, Logger logger) {
|
||||
super(playerUtils, "config.yml", logger);
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public static void reload(PlayerUtils playerUtils, Logger logger) {
|
||||
config = new Messages(playerUtils, logger);
|
||||
config.readConfig(Messages.class, null);
|
||||
}
|
||||
|
||||
public static class HELP {
|
||||
private static final String prefix = "help.";
|
||||
|
||||
public static String HELP_MESSAGE_WRAPPER = "<gold>PlayerUtils help:\n<commands></gold>";
|
||||
public static String HELP_MESSAGE = "<green>Show this menu: <gold>/pu help</gold></green>";
|
||||
public static String GLOW = "<green>Glow in a specified color: <gold>/pu glow <color></gold></green>";
|
||||
public static String XP_CHEQUE = "<green>Create an xp cheque: <gold>/pu xpcheque <amount></gold></green>";
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static void load() {
|
||||
HELP_MESSAGE_WRAPPER = config.getString(prefix, "help-wrapper", HELP_MESSAGE_WRAPPER);
|
||||
HELP_MESSAGE = config.getString(prefix, "help", HELP_MESSAGE);
|
||||
GLOW = config.getString(prefix, "glow", GLOW);
|
||||
XP_CHEQUE = config.getString(prefix, "xp-cheque", XP_CHEQUE);
|
||||
}
|
||||
}
|
||||
|
||||
public static class GENERIC {
|
||||
private static final String prefix = "generic.";
|
||||
|
||||
public static String NO_PERMISSION = "<red>You don't have permission for this command</red>";
|
||||
public static String PLAYER_ONLY = "<red>This command can only be executed as a player</red>";
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static void load() {
|
||||
NO_PERMISSION = config.getString(prefix, "no-permission", NO_PERMISSION);
|
||||
PLAYER_ONLY = config.getString(prefix, "player-only", PLAYER_ONLY);
|
||||
}
|
||||
}
|
||||
|
||||
public static class GLOW {
|
||||
private static final String prefix = "pu-command.glow";
|
||||
|
||||
public static String GLOW_OFF = "<green>Glow turned off</green>";
|
||||
public static String GLOW_ON = "<green>Glow turned on, you are now glowing <color></green>";
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static void load() {
|
||||
GLOW_OFF = config.getString(prefix, "glow-off",GLOW_OFF);
|
||||
GLOW_ON = config.getString(prefix, "glow-on",GLOW_ON);
|
||||
}
|
||||
}
|
||||
|
||||
public static class XP_CHEQUE {
|
||||
private static final String prefix = "pu-command.xp-cheque.";
|
||||
|
||||
public static String FAILED_STORAGE = "<red>Unable to create custom item for xp cheque</red>";
|
||||
public static String NOT_ENOUGH_XP = "<red>Not enough xp, you have <xp></red>";
|
||||
public static String NOT_HOLDING_BOTTLE = "<red>You need to hold an empty glass bottle while executing this command</red>";
|
||||
public static String DISPLAY_NAME = "<yellow>Xp bottle containing <xp> xp</yellow>";
|
||||
public static List<String> LORE = List.of("Issued by <name>", "Throw to retrieve xp");
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static void load() {
|
||||
FAILED_STORAGE = config.getString(prefix, "failed-storage", FAILED_STORAGE);
|
||||
NOT_ENOUGH_XP = config.getString(prefix, "not-enough-xp", NOT_ENOUGH_XP);
|
||||
NOT_HOLDING_BOTTLE = config.getString(prefix, "not-holding-bottle", NOT_HOLDING_BOTTLE);
|
||||
DISPLAY_NAME = config.getString(prefix, "display-name", DISPLAY_NAME);
|
||||
LORE = config.getStringList(prefix, "lore", LORE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.alttd.playerutils.event_listeners;
|
||||
|
||||
import com.alttd.playerutils.PlayerUtils;
|
||||
import com.alttd.playerutils.util.Logger;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.ExpBottleEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
public class XpBottleEvent implements Listener {
|
||||
|
||||
private final PlayerUtils playerUtils;
|
||||
private final Logger logger;
|
||||
|
||||
public XpBottleEvent(PlayerUtils playerUtils, Logger logger) {
|
||||
this.playerUtils = playerUtils;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onExpBottle(ExpBottleEvent event) {
|
||||
ItemStack item = event.getEntity().getItem();
|
||||
PersistentDataContainer persistentDataContainer = item.getItemMeta().getPersistentDataContainer();
|
||||
NamespacedKey customXp = NamespacedKey.fromString("custom_xp", playerUtils);
|
||||
if (customXp == null) {
|
||||
logger.warning("Unable to retrieve name spaced key.");
|
||||
return;
|
||||
}
|
||||
Integer integer = persistentDataContainer.get(customXp, PersistentDataType.INTEGER);
|
||||
if (integer == null) {
|
||||
return;
|
||||
}
|
||||
event.setExperience(integer);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.alttd.playerutils.util;
|
||||
|
||||
import com.alttd.playerutils.config.Config;
|
||||
|
||||
public class Logger {
|
||||
|
||||
private final java.util.logging.Logger logger;
|
||||
static private final String RESET = "\u001B[0m";
|
||||
static private final String GREEN = "\u001B[32m";
|
||||
static private final String TEAL = "\u001B[36m";
|
||||
|
||||
public Logger(java.util.logging.Logger logger) {
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public void debug(String debug, String... variables) {
|
||||
if (!Config.SETTINGS.DEBUG)
|
||||
return;
|
||||
logger.info(TEAL + replace(debug, variables) + RESET);
|
||||
}
|
||||
|
||||
public void info(String info, String... variables) {
|
||||
logger.info(GREEN + replace(info, variables) + RESET);
|
||||
}
|
||||
|
||||
public void warning(String warning, String... variables) {
|
||||
if (!Config.SETTINGS.WARNINGS)
|
||||
return;
|
||||
logger.warning(replace(warning, variables));
|
||||
}
|
||||
|
||||
public void severe(String severe, String... variables) {
|
||||
logger.severe(replace(severe, variables));
|
||||
}
|
||||
|
||||
private String replace(String text, String... variables) {
|
||||
for (String variable : variables) {
|
||||
text = text.replaceFirst("%", variable);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user