Initial commit

This commit is contained in:
Teriuihi 2022-01-03 22:25:23 +01:00
parent de11502f37
commit be656b4606
12 changed files with 85 additions and 303 deletions

View File

@ -7,7 +7,7 @@ plugins {
group = "com.alttd"
version = "1.0.0-SNAPSHOT"
description = "Altitude Villager Shop plugin."
description = "Altitude Particles plugin."
apply<JavaLibraryPlugin>()
@ -45,8 +45,4 @@ tasks {
dependencies {
compileOnly("com.alttd:Galaxy-API:1.18.1-R0.1-SNAPSHOT")
compileOnly("com.github.milkbowl:VaultAPI:1.7") {
exclude("org.bukkit","bukkit")
}
shadow("org.apache.commons:commons-math3:3.2")
}

View File

@ -1,10 +1,10 @@
rootProject.name = "VillagerShopUI"
rootProject.name = "AltitudeParticles"
dependencyResolutionManagement {
repositories {
mavenLocal()
mavenCentral()
maven("https://repo.destro.xyz/snapshots") // Altitude - Galaxy
maven("https://papermc.io/repo/repository/maven-public/") // Paper
maven("https://jitpack.io") { // Vault
content { includeGroup("com.github.milkbowl") }
}

View File

@ -0,0 +1,23 @@
package com.alttd;
import org.bukkit.plugin.java.JavaPlugin;
public class AltitudeParticles extends JavaPlugin {
public static AltitudeParticles instance;
public static AltitudeParticles getInstance() {
return instance;
}
@Override
public void onLoad() {
instance = this;
}
@Override
public void onEnable() {
}
}

View File

@ -1,97 +0,0 @@
package com.alttd;
import com.alttd.GUI.GUIListener;
import com.alttd.commands.CommandManager;
import com.alttd.database.Database;
import com.alttd.config.Config;
import com.alttd.config.VillagerConfig;
import com.alttd.config.WorthConfig;
import com.alttd.events.LoginEvent;
import com.alttd.events.LogoutEvent;
import com.alttd.events.VillagerEvents;
import com.alttd.objects.EconUser;
import com.alttd.util.Logger;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
public class VillagerUI extends JavaPlugin {
public static VillagerUI instance;
private Economy economy = null;
public static VillagerUI getInstance() {
return instance;
}
@Override
public void onLoad() {
instance = this;
}
@Override
public void onEnable() {
registerEvents();
new CommandManager();
Config.reload();
VillagerConfig.reload();
WorthConfig.reload();
if (!setupEconomy())
return;
Database.getDatabase().init();
scheduleTasks();
Logger.info("--------------------------------------------------");
Logger.info("Villager UI started");
Logger.info("--------------------------------------------------");
}
private void scheduleTasks() {
new BukkitRunnable() {
@Override
public void run() {
if (Config.DEBUG)
Logger.info("Syncing users.");
EconUser.getEconUsers().forEach(econUser -> {
if (Config.DEBUG)
Logger.info("Syncing %", econUser.getUuid().toString());
econUser.removePoints();
econUser.syncPoints();
});
}
}.runTaskTimerAsynchronously(getInstance(), 0L, 10 * 60 * 20L);
}
private void registerEvents() {
getServer().getPluginManager().registerEvents(new GUIListener(), this);
getServer().getPluginManager().registerEvents(new VillagerEvents(), this);
getServer().getPluginManager().registerEvents(new LogoutEvent(), this);
getServer().getPluginManager().registerEvents(new LoginEvent(), this);
}
public Economy getEconomy() {
if(economy == null)
setupEconomy();
return economy;
}
private boolean setupEconomy() {
if (Bukkit.getPluginManager().getPlugin("Vault") == null) {
this.getLogger().severe("Vault was not found. Please download vault.");
Bukkit.getPluginManager().disablePlugin(this);
return false;
} else {
RegisteredServiceProvider rsp = this.getServer().getServicesManager().getRegistration(Economy.class);
if (rsp == null) {
this.getLogger().severe("Can't find economy! Disabling plugin.");
Bukkit.getPluginManager().disablePlugin(this);
return false;
} else {
this.economy = (Economy)rsp.getProvider();
return this.economy != null;
}
}
}
}

View File

@ -1,14 +1,10 @@
package com.alttd.commands;
import com.alttd.VillagerUI;
import com.alttd.commands.subcommands.CommandCreateVillager;
import com.alttd.AltitudeParticles;
import com.alttd.commands.subcommands.CommandHelp;
import com.alttd.commands.subcommands.CommandReload;
import com.alttd.commands.subcommands.CommandRemoveVillager;
import com.alttd.config.Config;
import com.alttd.util.Logger;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.Template;
import org.bukkit.command.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -20,16 +16,14 @@ import java.util.stream.Collectors;
public class CommandManager implements CommandExecutor, TabExecutor {
private final List<SubCommand> subCommands;
private final MiniMessage miniMessage;
public CommandManager() {
VillagerUI villagerUI = VillagerUI.getInstance();
AltitudeParticles aPart = AltitudeParticles.getInstance();
PluginCommand command = villagerUI.getCommand("villagerui");
PluginCommand command = aPart.getCommand("apart");
if (command == null) {
subCommands = null;
miniMessage = null;
Logger.severe("Unable to find villager ui command.");
Logger.severe("Unable to find AltitudeParticles command.");
return;
}
command.setExecutor(this);
@ -37,26 +31,23 @@ public class CommandManager implements CommandExecutor, TabExecutor {
subCommands = Arrays.asList(
new CommandHelp(this),
new CommandCreateVillager(),
new CommandReload(),
new CommandRemoveVillager());
miniMessage = MiniMessage.get();
new CommandReload());
}
@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String cmd, @NotNull String[] args) {
if (args.length == 0) {
commandSender.sendMessage(miniMessage.parse(Config.HELP_MESSAGE_WRAPPER, Template.of("commands", subCommands.stream()
commandSender.sendMiniMessage(Config.HELP_MESSAGE_WRAPPER.replaceAll("<config>", subCommands.stream()
.filter(subCommand -> commandSender.hasPermission(subCommand.getPermission()))
.map(SubCommand::getHelpMessage)
.collect(Collectors.joining("\n")))));
.collect(Collectors.joining("\n"))), null);
return true;
}
SubCommand subCommand = getSubCommand(args[0]);
if (!commandSender.hasPermission(subCommand.getPermission())) {
commandSender.sendMessage(miniMessage.parse(Config.NO_PERMISSION));
commandSender.sendMiniMessage(Config.NO_PERMISSION, null);
return true;
}

View File

@ -10,7 +10,7 @@ public abstract class SubCommand {
private final MiniMessage miniMessage;
public SubCommand() {
miniMessage = MiniMessage.get();
miniMessage = MiniMessage.miniMessage();
}
public abstract boolean onCommand(CommandSender commandSender, String[] args);

View File

@ -3,7 +3,6 @@ package com.alttd.commands.subcommands;
import com.alttd.commands.CommandManager;
import com.alttd.commands.SubCommand;
import com.alttd.config.Config;
import net.kyori.adventure.text.minimessage.Template;
import org.bukkit.command.CommandSender;
import java.util.ArrayList;
@ -21,10 +20,11 @@ public class CommandHelp extends SubCommand {
@Override
public boolean onCommand(CommandSender commandSender, String[] args) {
commandSender.sendMessage(getMiniMessage().parse(Config.HELP_MESSAGE_WRAPPER, Template.of("commands", commandManager.getSubCommands().stream()
.filter(subCommand -> commandSender.hasPermission(subCommand.getPermission()))
.map(SubCommand::getHelpMessage)
.collect(Collectors.joining("\n")))));
commandSender.sendMiniMessage(Config.HELP_MESSAGE_WRAPPER.replaceAll("<commands>", commandManager
.getSubCommands().stream()
.filter(subCommand -> commandSender.hasPermission(subCommand.getPermission()))
.map(SubCommand::getHelpMessage)
.collect(Collectors.joining("\n"))), null);
return true;
}

View File

@ -2,8 +2,7 @@ package com.alttd.commands.subcommands;
import com.alttd.commands.SubCommand;
import com.alttd.config.Config;
import com.alttd.config.VillagerConfig;
import com.alttd.config.WorthConfig;
import com.alttd.config.DatabaseConfig;
import org.bukkit.command.CommandSender;
import java.util.ArrayList;
@ -14,9 +13,8 @@ public class CommandReload extends SubCommand {
@Override
public boolean onCommand(CommandSender commandSender, String[] args) {
Config.reload();
VillagerConfig.reload();
WorthConfig.reload();
commandSender.sendMessage(getMiniMessage().parse("<green>Reloaded VillagerShopUI config.</green>"));
DatabaseConfig.reload();
commandSender.sendMiniMessage("<green>Reloaded VillagerShopUI config.</green>", null);
return true;
}

View File

@ -1,6 +1,6 @@
package com.alttd.config;
import com.alttd.VillagerUI;
import com.alttd.AltitudeParticles;
import com.alttd.util.Logger;
import com.google.common.collect.ImmutableMap;
import org.bukkit.configuration.ConfigurationSection;
@ -24,7 +24,7 @@ abstract class AbstractConfig {
YamlConfiguration yaml;
AbstractConfig(String filename) {
init(new File(VillagerUI.getInstance().getDataFolder(), filename), filename);
init(new File(AltitudeParticles.getInstance().getDataFolder(), filename), filename);
}
AbstractConfig(File file, String filename) {

View File

@ -1,19 +1,6 @@
package com.alttd.config;
import com.alttd.VillagerUI;
import com.alttd.objects.VillagerType;
import com.alttd.util.Logger;
import com.google.common.collect.Range;
import it.unimi.dsi.fastutil.ints.Int2ObjectAVLTreeMap;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class Config extends AbstractConfig {
@ -32,44 +19,14 @@ public final class Config extends AbstractConfig {
config.readConfig(Config.class, null);
}
public static String DRIVER = "mysql";
public static String IP = "localhost";
public static String PORT = "3306";
public static String DATABASE_NAME = "VillagerShopUI";
public static String USERNAME = "";
public static String PASSWORD = "";
private static void loadDatabase() {
DRIVER = config.getString("database.driver", DRIVER);
IP = config.getString("database.ip", IP);
PORT = config.getString("database.port", PORT);
DATABASE_NAME = config.getString("database.name", DATABASE_NAME);
USERNAME = config.getString("database.username", USERNAME);
PASSWORD = config.getString("database.password", PASSWORD);
}
public static String INITIAL_VILLAGER_WINDOW = "<trader> points: <points>";
public static String BUY_WINDOW = "<trader> points: <points>";
public static String SELL_WINDOW = "<trader> points: <points>";
private static void loadUI() {
INITIAL_VILLAGER_WINDOW = config.getString("ui.initial-window-name", INITIAL_VILLAGER_WINDOW);
BUY_WINDOW = config.getString("ui.buy-window-name", BUY_WINDOW);
SELL_WINDOW = config.getString("ui.sell-window-name", SELL_WINDOW);
}
public static String HELP_MESSAGE_WRAPPER = "<gold>VillagerShopUI help:\n<commands></gold>";
public static String HELP_MESSAGE = "<green>Show this menu: <gold>/villagerui help</gold></green>";
public static String RELOAD_MESSAGE = "<green>Reload configs: <gold>/villagerui reload</gold></green>";
public static String CREATE_VILLAGER_MESSAGE = "<green>Create a new trading villager: <gold>/villagerui createvillager <type> <biome> <x> <y> <z> <yaw> <pitch> <world></gold></green>";
public static String REMOVE_VILLAGER_MESSAGE = "<green>Removes all existing trading villagers in a 2 block radius: <gold>/villagerui removevillager</gold></green>";
public static String HELP_MESSAGE_WRAPPER = "<gold>AltitudeParticles help:\n<commands></gold>";
public static String HELP_MESSAGE = "<green>Show this menu: <gold>/apart help</gold></green>";
public static String RELOAD_MESSAGE = "<green>Reload configs: <gold>/apart reload</gold></green>";
private static void loadHelp() {
HELP_MESSAGE_WRAPPER = config.getString("help.help-wrapper", HELP_MESSAGE_WRAPPER);
HELP_MESSAGE = config.getString("help.help", HELP_MESSAGE);
RELOAD_MESSAGE = config.getString("help.reload", RELOAD_MESSAGE);
CREATE_VILLAGER_MESSAGE = config.getString("help.create-villager", CREATE_VILLAGER_MESSAGE);
REMOVE_VILLAGER_MESSAGE = config.getString("help.remove-villager", REMOVE_VILLAGER_MESSAGE);
}
public static String NO_PERMISSION = "<red>You do not have permission to do that.</red>";
@ -80,24 +37,7 @@ public final class Config extends AbstractConfig {
NO_CONSOLE = config.getString("generic.no-console", NO_CONSOLE);
}
public static String VILLAGER_NAME = "<green><name></green>";
private static void loadIDKYET() {//TODO rename
VILLAGER_NAME = config.getString("idkyet.villager-name", VILLAGER_NAME); //TODO change path
}
public static String NOT_ENOUGH_MONEY = "<red>You only have $<money>, you need at least $<price> for this purchase.</red>";
public static String NOT_ENOUGH_ITEMS = "<red>You only have don't have enough <type> you need at least <amount>.</red>";
public static String PURCHASED_ITEM = "<green>You bought <amount> <item> for <price>!</green>";
public static String SOLD_ITEM = "<green>You sold <amount> <item> for <price>!</green>";
public static String REMOVED_VILLAGER = "<green>Removed villager with uuid <uuid></green>";
private static void loadMessages() {
NOT_ENOUGH_MONEY = config.getString("messages.not-enough-money", NOT_ENOUGH_MONEY);
NOT_ENOUGH_ITEMS = config.getString("messages.not-enough-items", NOT_ENOUGH_ITEMS);
PURCHASED_ITEM = config.getString("messages.purchased-item", PURCHASED_ITEM);
SOLD_ITEM = config.getString("messages.sold-item", SOLD_ITEM);
REMOVED_VILLAGER = config.getString("messages.removed-villager", REMOVED_VILLAGER);
}
public static boolean DEBUG = false;
@ -105,108 +45,4 @@ public final class Config extends AbstractConfig {
private static void loadSettings() {
DEBUG = config.getBoolean("settings.debug", DEBUG);
}
private static void loadVillagerTypes() {
VillagerType.clearVillagerTypes();
ConfigurationSection configurationSection = config.getConfigurationSection("villager-types");
if (configurationSection == null) {
Logger.warning("No villager types found in config.");
return;
}
Set<String> keys = configurationSection.getKeys(false);
if (keys.isEmpty())
Logger.warning("No villager types found in config.");
keys.forEach(key -> {
ConfigurationSection villagerType = configurationSection.getConfigurationSection(key);
if (villagerType == null)
return;
VillagerType.addVillagerType(new VillagerType(
key,
villagerType.getString("name"),
loadProducts(villagerType.getConfigurationSection("buying")),
loadProducts(villagerType.getConfigurationSection("selling")),
villagerType.getDouble("price-modifier"),
villagerType.getString("profession"))
);
});
}
public static Int2ObjectAVLTreeMap<Range<Double>> pointsRangeMap = new Int2ObjectAVLTreeMap<>();
private static void loadPointRange() {
pointsRangeMap.clear();
Pattern pattern = Pattern.compile("(0|([1-9][0-9]{0,2}))(.[0-9]{1,2})?-(0|([1-9][0-9]{0,2}))(.[0-9]{1,2})?");
ConfigurationSection configurationSection = config.getConfigurationSection("points");
if (configurationSection == null) {
Logger.severe("""
No point entries in config (see example). Please add them and restart the plugin.
points:
\t1: 0.5-1.5
\t3: 1.5-2.25
\t5: 2.25-0 #2.25 and higher""");
VillagerUI.getInstance().getServer().getPluginManager().disablePlugin(VillagerUI.getInstance());
return;
}
Set<String> keys = configurationSection.getKeys(false);
for (String key : keys) {
int points = Integer.parseInt(key);
if (points == 0) {
Logger.warning("Invalid point entry % in config", key);
continue;
}
String range = configurationSection.getString(key);
if (range == null) {
Logger.warning("Invalid point value for % in config", key);
continue;
}
Matcher matcher = pattern.matcher(range);
if (!matcher.matches()) {
Logger.warning("Invalid point value % for % in config " +
"should be double-double (0-2.05)", range, key);
continue;
}
String[] split = range.split("-");
if (split.length != 2) {
Logger.severe("""
The logic for the regex failed when loading points.
key:%
value:%""", key, range);
continue;
}
double d1 = Double.parseDouble(split[0]);
double d2 = Double.parseDouble(split[1]);
Range<Double> doubleRange;
if (d2 == 0 && d1 > d2)
doubleRange = Range.greaterThan(d1);
else if (d2 > d1)
doubleRange = Range.closed(d1, d2);
else {
Logger.warning("Invalid range d1:% to d2:%, can't be the same, d1 can't be bigger " +
"than d2 unless d2 is 0 (infinite)", String.valueOf(d1), String.valueOf(d2));
continue;
}
pointsRangeMap.put(points, doubleRange);
}
}
private static HashSet<ItemStack> loadProducts(ConfigurationSection productsSection) {
HashSet<ItemStack> products = new HashSet<>();
if (productsSection == null)
return products;
productsSection.getKeys(false).forEach(item -> {
Material material = Material.getMaterial(item);
if (material == null) {
Logger.warning("Invalid key in products -> " + item);
return;
}
products.add(new ItemStack(material, productsSection.getInt(item)));
});
return products;
}
}

View File

@ -0,0 +1,35 @@
package com.alttd.config;
import java.io.File;
public class DatabaseConfig extends AbstractConfig {
static DatabaseConfig config;
static int version;
public DatabaseConfig() {
super(new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs"
+ File.separator + "AltitudeParticles"), "database.yml");
}
public static void reload() {
config = new DatabaseConfig();
config.readConfig(Config.class, null);
}
public static String DRIVER = "mysql";
public static String IP = "localhost";
public static String PORT = "3306";
public static String DATABASE_NAME = "AltitudeParticles";
public static String USERNAME = "root";
public static String PASSWORD = "root";
private static void loadDatabase() {
DRIVER = config.getString("database.driver", DRIVER);
IP = config.getString("database.ip", IP);
PORT = config.getString("database.port", PORT);
DATABASE_NAME = config.getString("database.name", DATABASE_NAME);
USERNAME = config.getString("database.username", USERNAME);
PASSWORD = config.getString("database.password", PASSWORD);
}
}

View File

@ -1,13 +1,13 @@
package com.alttd.util;
import com.alttd.VillagerUI;
import com.alttd.AltitudeParticles;
public class Logger {
static private final java.util.logging.Logger logger;
static {
logger = VillagerUI.getInstance().getLogger();
logger = AltitudeParticles.getInstance().getLogger();
}
public static void info(String info, String... variables)