Skip to content

Commit

Permalink
Added a initialization method for Addon Initiation to avoid heavy lif…
Browse files Browse the repository at this point in the history
…ting on constructor initiation.

Faction Addon:
 - Refactored Entirety of Configuration Handling
 - Added New Configuration Manager w/Loading Capabilities From Jar File
 - Refactored All Constructor Behavior to Avoid Heavy Lifting Loads.
 - Implemented Friendly Name Addon Behavior To Allow For User Friendly Implementation Of Addons

Demonstrated Proper Immutability Via FAuditMenu, FactionLogs, FLogManager, FLogType, and LogTimer
Implemented Method Separation For Better Readability in FAuditMenu, FactionLogs, FLogManager, FLogType, and LogTimer
Refactored FAuditMenu, FactionLogs, FLogManager, FLogType, and LogTimer for Better Readability and Structure for Future Recode

Improved Readability in BrigadierManager
Implemented Method Separation In CmdDisband

Fixed Issue With F List, Asyncrounous Task Causing List To Make Faction List Not In Proper Order.

Added Configuration In FCommand Parsed NO_BALANCE Placeholder.
Recoded Entirety Of FactionData Management System.
Removed Macro Detection For SaberGUI Registry

Added Method Separation For Better Readability In AsyncPlayerMap and FlightEnhance

Fixed Issue With Slot Initiation For F Upgrade Not Registering Disabled Ones
Fixed Issue With BannerManager Initiation Nulling On Disable
Fixed Issue With Armor Swap CoreX Module.
  • Loading branch information
Driftay committed Oct 2, 2023
1 parent 3e999ff commit 65b789e
Show file tree
Hide file tree
Showing 30 changed files with 684 additions and 652 deletions.
12 changes: 8 additions & 4 deletions src/main/java/com/massivecraft/factions/FactionsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public void onEnable() {

Bukkit.getScheduler().runTaskLater(this, () -> {
//To Add Addon Commands Into "Tab Completion Format"
if(factionsAddonHashMap.size() > 0) {
if (factionsAddonHashMap.size() > 0) {
FCmdRoot.instance.addVariableCommands();
FCmdRoot.instance.rebuild();
}
Expand Down Expand Up @@ -299,6 +299,13 @@ public Gson getGson() {

@Override
public void onDisable() {

if (bannerManager != null) {
bannerManager.onDisable(this);
}

ShutdownParameter.initShutdown(this);

if (this.AutoLeaveTask != null) {
getServer().getScheduler().cancelTask(this.AutoLeaveTask);
this.AutoLeaveTask = null;
Expand All @@ -307,9 +314,6 @@ public void onDisable() {
TextUtil.AUDIENCES.close();
}

bannerManager.onDisable(this);
ShutdownParameter.initShutdown(this);

super.onDisable();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void loadAddons() {
constructor = addonMainClass.getConstructor(FactionsPlugin.class);
factionsAddon = (FactionsAddon) constructor.newInstance(plugin);
FactionsPlugin.getInstance().getFactionsAddonHashMap().put(factionsAddon.getAddonName(), factionsAddon);
factionsAddon.initializeAddon();
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException |
InvocationTargetException e) {
e.printStackTrace();
Expand Down
90 changes: 74 additions & 16 deletions src/main/java/com/massivecraft/factions/addon/FactionsAddon.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,61 @@
package com.massivecraft.factions.addon;

import com.massivecraft.factions.FactionsPlugin;
import com.massivecraft.factions.cmd.FCmdRoot;
import com.massivecraft.factions.cmd.FCommand;
import com.massivecraft.factions.util.Logger;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;

import java.util.Collections;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Set;

/**
* @author SavageLabs Team
*/

public abstract class FactionsAddon {

private final String addonName;
private final FactionsPlugin plugin;

private File configFile;
private FileConfiguration config;

public FactionsAddon(final FactionsPlugin plugin) {
this.plugin = plugin;
this.addonName = getClass().getName();
enableAddon();
this.addonName = getFriendlyName();
}

private void enableAddon() {
public void initializeAddon() {
loadConfig();
onEnable();
registerListeners();
registerFCommands();
Logger.print("Addon: " + getAddonName() + " loaded successfully!", Logger.PrefixType.DEFAULT);
}

public void disableAddon() {
public void terminateAddon() {
unregisterListeners();
onDisable();
}

public abstract void onEnable();
protected abstract void onEnable();

protected abstract void onDisable();

public abstract void onDisable();
protected abstract String getFriendlyName();

public Set<Listener> listenersToRegister() {
return Collections.emptySet();
protected Set<Listener> listenersToRegister() {
return new HashSet<>();
}

public Set<FCommand> fCommandsToRegister() {
return Collections.emptySet();
protected Set<FCommand> fCommandsToRegister() {
return new HashSet<>();
}

public String getAddonName() {
Expand Down Expand Up @@ -78,4 +87,53 @@ private void registerFCommands() {
}
}
}

public void loadConfig() {
Path path = Paths.get(plugin.getDataFolder().toString(), "configuration/addons", getAddonName().toLowerCase() + ".yml");
configFile = path.toFile();

if (!Files.exists(path)) {
try {
exportConfig("/" + getAddonName().toLowerCase() + ".yml");
} catch (Exception e) {
Logger.print("Error transferring config for " + getAddonName() + ": " + e.getMessage(), Logger.PrefixType.FAILED);
e.printStackTrace();
}
}
config = YamlConfiguration.loadConfiguration(configFile);
}

private void exportConfig(String resourceName) throws Exception {
try (InputStream stream = this.getClass().getResourceAsStream(resourceName);
OutputStream resStreamOut = Files.newOutputStream(Paths.get(plugin.getDataFolder().toString(), "configuration/addons", resourceName.toLowerCase()))) {

if (stream == null) {
throw new Exception("Cannot get resource \"" + resourceName + "\" from Jar file.");
}

byte[] buffer = new byte[4096];
int readBytes;
while ((readBytes = stream.read(buffer)) > 0) {
resStreamOut.write(buffer, 0, readBytes);
}
Logger.print(getAddonName() + " config file successfully transferred!", Logger.PrefixType.DEFAULT);
}
}

public void saveConfig() {
if (config == null || configFile == null) return;
try {
getConfig().save(configFile);
} catch (IOException e) {
Logger.print("Error saving config for " + getAddonName() + ": " + e.getMessage(), Logger.PrefixType.FAILED);
e.printStackTrace();
}
}

public FileConfiguration getConfig() {
if (config == null) {
loadConfig();
}
return config;
}
}
116 changes: 60 additions & 56 deletions src/main/java/com/massivecraft/factions/cmd/BrigadierManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,81 +16,85 @@

public class BrigadierManager {

/**
* @author FactionsUUID Team - Modified By CmdrKittens
*/

public Commodore commodore;
public LiteralArgumentBuilder<Object> brigadier = LiteralArgumentBuilder.literal("factions");
private final Commodore commodore;
private final LiteralArgumentBuilder<Object> brigadier;

public BrigadierManager() {
commodore = CommodoreProvider.getCommodore(FactionsPlugin.getInstance());
brigadier = LiteralArgumentBuilder.literal("factions");
}

public void build() {
commodore.register(brigadier.build());

// Add factions children to f alias
// Register 'f' alias with all children of 'factions'
LiteralArgumentBuilder<Object> fLiteral = LiteralArgumentBuilder.literal("f");
for (CommandNode<Object> node : brigadier.getArguments()) fLiteral.then(node);
for (CommandNode<Object> node : brigadier.getArguments()) {

fLiteral.then(node);
}
commodore.register(fLiteral.build());
}

public void addSubCommand(FCommand subCommand) {
// Register brigadier to all command aliases
for (String alias : subCommand.aliases) {
LiteralArgumentBuilder<Object> literal = LiteralArgumentBuilder.literal(alias);

if (subCommand.requirements.brigadier != null) {
// If the requirements explicitly provide a BrigadierProvider then use it
Class<? extends BrigadierProvider> brigadierProvider = subCommand.requirements.brigadier;
try {
Constructor<? extends BrigadierProvider> constructor = brigadierProvider.getDeclaredConstructor();
brigadier.then(constructor.newInstance().get(literal));
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException |
InvocationTargetException exception) {
exception.printStackTrace();
}
registerUsingProvider(subCommand, literal);
} else {
registerGeneratedBrigadier(subCommand, literal);
}
}
}

private void registerUsingProvider(FCommand subCommand, LiteralArgumentBuilder<Object> literal) {
Class<? extends BrigadierProvider> brigadierProvider = subCommand.requirements.brigadier;
try {
Constructor<? extends BrigadierProvider> constructor = brigadierProvider.getDeclaredConstructor();
brigadier.then(constructor.newInstance().get(literal));
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException |
InvocationTargetException exception) {
exception.printStackTrace();
}
}

private void registerGeneratedBrigadier(FCommand subCommand, LiteralArgumentBuilder<Object> literal) {
List<RequiredArgumentBuilder<Object, ?>> argsStack = generateArgsStack(subCommand);

RequiredArgumentBuilder<Object, ?> previous = null;
for (int i = argsStack.size() - 1; i >= 0; i--) {
if (previous == null) {
previous = argsStack.get(i);
} else {
// Generate our own based on args - quite ugly

// We create an orderly stack of all args, required and optional, format them differently
List<RequiredArgumentBuilder<Object, ?>> stack = new ArrayList<>(subCommand.requiredArgs.size() + subCommand.optionalArgs.size());
for (String required : subCommand.requiredArgs) {
// Simply add the arg name as required
stack.add(RequiredArgumentBuilder.argument(required, StringArgumentType.word()));
}

for (Map.Entry<String, String> optionalEntry : subCommand.optionalArgs.entrySet()) {
RequiredArgumentBuilder<Object, ?> optional;

// Optional without default
if (optionalEntry.getKey().equalsIgnoreCase(optionalEntry.getValue())) {
optional = RequiredArgumentBuilder.argument(":" + optionalEntry.getKey(), StringArgumentType.word());
// Optional with default, explain
} else {
optional = RequiredArgumentBuilder.argument(optionalEntry.getKey() + "|" + optionalEntry.getValue(), StringArgumentType.word());
}

stack.add(optional);
}

// Reverse the stack and apply .then()
RequiredArgumentBuilder<Object, ?> previous = null;
for (int i = stack.size() - 1; i >= 0; i--) {
if (previous == null) {
previous = stack.get(i);
} else {
previous = stack.get(i).then(previous);
}
}

if (previous == null) {
brigadier.then(literal);
} else {
brigadier.then(literal.then(previous));
}
previous = argsStack.get(i).then(previous);
}
}

if (previous == null) {
brigadier.then(literal);
} else {
brigadier.then(literal.then(previous));
}
}

private List<RequiredArgumentBuilder<Object, ?>> generateArgsStack(FCommand subCommand) {
List<RequiredArgumentBuilder<Object, ?>> stack = new ArrayList<>(subCommand.requiredArgs.size() + subCommand.optionalArgs.size());

for (String required : subCommand.requiredArgs) {
stack.add(RequiredArgumentBuilder.argument(required, StringArgumentType.word()));
}

for (Map.Entry<String, String> optionalEntry : subCommand.optionalArgs.entrySet()) {
RequiredArgumentBuilder<Object, ?> optional;
if (optionalEntry.getKey().equalsIgnoreCase(optionalEntry.getValue())) {
optional = RequiredArgumentBuilder.argument(":" + optionalEntry.getKey(), StringArgumentType.word());
} else {
optional = RequiredArgumentBuilder.argument(optionalEntry.getKey() + "|" + optionalEntry.getValue(), StringArgumentType.word());
}
stack.add(optional);
}

return stack;
}
}
Loading

0 comments on commit 65b789e

Please sign in to comment.