Writes a lot of code necessary for the scrapper

Adds the classes necessary for the new scrapper
Partly implements some scrapper functionality
Restructures some classes to reduce code duplication
Moves some classes to make some classes easier to find
Adds a bunch of TODOs where things have an unfinished implementation
This commit is contained in:
2023-11-14 16:04:48 +01:00
parent 1938a690c6
commit f3f3f66c38
40 changed files with 1788 additions and 594 deletions

View File

@ -1,13 +1,18 @@
package net.knarcraft.blacksmith;
import net.citizensnpcs.api.CitizensAPI;
import net.knarcraft.blacksmith.command.BlackSmithConfigCommand;
import net.knarcraft.blacksmith.command.BlackSmithConfigTabCompleter;
import net.knarcraft.blacksmith.command.BlackSmithEditCommand;
import net.knarcraft.blacksmith.command.BlackSmithEditTabCompleter;
import net.knarcraft.blacksmith.command.PresetCommand;
import net.knarcraft.blacksmith.command.PresetTabCompleter;
import net.knarcraft.blacksmith.command.blacksmith.BlackSmithConfigCommand;
import net.knarcraft.blacksmith.command.blacksmith.BlackSmithConfigTabCompleter;
import net.knarcraft.blacksmith.command.blacksmith.BlackSmithEditCommand;
import net.knarcraft.blacksmith.command.blacksmith.BlackSmithEditTabCompleter;
import net.knarcraft.blacksmith.command.scrapper.ScrapperConfigCommand;
import net.knarcraft.blacksmith.command.scrapper.ScrapperConfigTabCompleter;
import net.knarcraft.blacksmith.command.scrapper.ScrapperEditCommand;
import net.knarcraft.blacksmith.command.scrapper.ScrapperEditTabCompleter;
import net.knarcraft.blacksmith.config.blacksmith.GlobalBlacksmithSettings;
import net.knarcraft.blacksmith.config.scrapper.GlobalScrapperSettings;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage;
import net.knarcraft.blacksmith.listener.NPCClickListener;
import net.knarcraft.blacksmith.listener.PlayerListener;
@ -17,12 +22,16 @@ import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.knarlib.formatting.TranslatableTimeUnit;
import net.knarcraft.knarlib.formatting.Translator;
import net.knarcraft.knarlib.util.UpdateChecker;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.TabCompleter;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.java.JavaPluginLoader;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.util.logging.Level;
@ -33,7 +42,8 @@ import java.util.logging.Level;
public class BlacksmithPlugin extends JavaPlugin {
private static BlacksmithPlugin instance;
private GlobalBlacksmithSettings config;
private GlobalBlacksmithSettings blacksmithConfig;
private GlobalScrapperSettings scrapperConfig;
private static Translator translator;
private static StringFormatter stringFormatter;
@ -49,7 +59,8 @@ public class BlacksmithPlugin extends JavaPlugin {
* Constructor required for MockBukkit
*/
@SuppressWarnings("unused")
protected BlacksmithPlugin(JavaPluginLoader loader, PluginDescriptionFile descriptionFile, File dataFolder, File file) {
protected BlacksmithPlugin(@NotNull JavaPluginLoader loader, @NotNull PluginDescriptionFile descriptionFile,
@NotNull File dataFolder, @NotNull File file) {
super(loader, descriptionFile, dataFolder, file);
}
@ -58,17 +69,26 @@ public class BlacksmithPlugin extends JavaPlugin {
*
* @return <p>An instance of the blacksmith plugin</p>
*/
public static BlacksmithPlugin getInstance() {
public static @NotNull BlacksmithPlugin getInstance() {
return instance;
}
/**
* Gets settings for the blacksmith plugin
* Gets settings for the blacksmith plugin's blacksmiths
*
* @return <p>Settings for the blacksmith plugin</p>
* @return <p>Settings for the blacksmith plugin's blacksmith</p>
*/
public GlobalBlacksmithSettings getSettings() {
return config;
public @NotNull GlobalBlacksmithSettings getGlobalBlacksmithSettings() {
return blacksmithConfig;
}
/**
* Gets settings for the blacksmith plugin's scrapper
*
* @return <p>Settings for the blacksmith plugin's scrapper</p>
*/
public @NotNull GlobalScrapperSettings getGlobalScrapperSettings() {
return scrapperConfig;
}
/**
@ -76,8 +96,10 @@ public class BlacksmithPlugin extends JavaPlugin {
*/
public void reload() {
this.reloadConfig();
config.load();
translator.loadLanguages(this.getDataFolder(), "en", this.getConfig().getString("language", "en"));
blacksmithConfig.load();
scrapperConfig.load();
translator.loadLanguages(this.getDataFolder(), "en",
this.getConfig().getString("language", "en"));
}
/**
@ -85,7 +107,7 @@ public class BlacksmithPlugin extends JavaPlugin {
*
* @return <p>The translator to use</p>
*/
public static Translator getTranslator() {
public static @NotNull Translator getTranslator() {
return BlacksmithPlugin.translator;
}
@ -94,7 +116,7 @@ public class BlacksmithPlugin extends JavaPlugin {
*
* @return <p>The string formatter to use</p>
*/
public static StringFormatter getStringFormatter() {
public static @NotNull StringFormatter getStringFormatter() {
return BlacksmithPlugin.stringFormatter;
}
@ -114,23 +136,8 @@ public class BlacksmithPlugin extends JavaPlugin {
this.reloadConfig();
this.saveConfig();
//Load settings
config = new GlobalBlacksmithSettings(this);
config.load();
//Prepare the translator
translator = new Translator();
translator.registerMessageCategory(TranslatableTimeUnit.UNIT_SECOND);
translator.registerMessageCategory(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT);
translator.loadLanguages(this.getDataFolder(), "en", fileConfiguration.getString("language", "en"));
PluginDescriptionFile description = this.getDescription();
String prefix;
if (description.getPrefix() == null) {
prefix = "Blacksmith";
} else {
prefix = description.getPrefix();
}
BlacksmithPlugin.stringFormatter = new StringFormatter(prefix, translator);
// Initialize custom configuration files
initializeConfigurations(fileConfiguration);
//Set up Vault integration
if (!setUpVault()) {
@ -153,6 +160,34 @@ public class BlacksmithPlugin extends JavaPlugin {
() -> this.getDescription().getVersion(), null);
}
/**
* Initializes custom configuration and translation
*
* @param fileConfiguration <p>The configuration file to get values from</p>
*/
private void initializeConfigurations(@NotNull FileConfiguration fileConfiguration) {
//Load settings
blacksmithConfig = new GlobalBlacksmithSettings(this);
blacksmithConfig.load();
scrapperConfig = new GlobalScrapperSettings(this);
scrapperConfig.load();
//Prepare the translator
translator = new Translator();
translator.registerMessageCategory(TranslatableTimeUnit.UNIT_SECOND);
translator.registerMessageCategory(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT);
translator.loadLanguages(this.getDataFolder(), "en",
fileConfiguration.getString("language", "en"));
PluginDescriptionFile description = this.getDescription();
String prefix;
if (description.getPrefix() == null) {
prefix = "Blacksmith";
} else {
prefix = description.getPrefix();
}
BlacksmithPlugin.stringFormatter = new StringFormatter(prefix, translator);
}
/**
* Tries to set up Vault
*
@ -182,24 +217,28 @@ public class BlacksmithPlugin extends JavaPlugin {
* Registers all commands used by this plugin
*/
private void registerCommands() {
//Register the blacksmith NPC edit main-command
PluginCommand blacksmithCommand = this.getCommand("blacksmith");
if (blacksmithCommand != null) {
blacksmithCommand.setExecutor(new BlackSmithEditCommand());
blacksmithCommand.setTabCompleter(new BlackSmithEditTabCompleter());
}
registerCommand("blacksmith", new BlackSmithEditCommand(), new BlackSmithEditTabCompleter());
registerCommand("blacksmithConfig", new BlackSmithConfigCommand(), new BlackSmithConfigTabCompleter());
registerCommand("scrapper", new ScrapperEditCommand(), new ScrapperEditTabCompleter());
registerCommand("scrapperConfig", new ScrapperConfigCommand(), new ScrapperConfigTabCompleter());
registerCommand("preset", new PresetCommand(), new PresetTabCompleter());
}
//Register the global config edit command
PluginCommand blacksmithConfigCommand = this.getCommand("blacksmithConfig");
if (blacksmithConfigCommand != null) {
blacksmithConfigCommand.setExecutor(new BlackSmithConfigCommand());
blacksmithConfigCommand.setTabCompleter(new BlackSmithConfigTabCompleter());
}
PluginCommand presetCommand = this.getCommand("preset");
if (presetCommand != null) {
presetCommand.setExecutor(new PresetCommand());
presetCommand.setTabCompleter(new PresetTabCompleter());
/**
* Registers a command
*
* @param commandName <p>The name of the command</p>
* @param executor <p>The executor to bind to the command</p>
* @param tabCompleter <p>The tab completer to bind to the command, or null</p>
*/
private void registerCommand(@NotNull String commandName, @NotNull CommandExecutor executor,
@Nullable TabCompleter tabCompleter) {
PluginCommand command = this.getCommand(commandName);
if (command != null) {
command.setExecutor(executor);
if (tabCompleter != null) {
command.setTabCompleter(tabCompleter);
}
}
}