Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
The massive update.
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoNekoDev committed Dec 21, 2023
1 parent f35e895 commit 90e93ff
Show file tree
Hide file tree
Showing 70 changed files with 2,262 additions and 2,445 deletions.
13 changes: 6 additions & 7 deletions astralbooks-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
</properties>

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>1.20.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ro.niconeko</groupId>
<artifactId>astralbooks-api</artifactId>
Expand All @@ -53,13 +59,6 @@
<scope>provided</scope>
</dependency>
<!-- Compiles -->
<dependency>
<groupId>me.lucko</groupId>
<artifactId>commodore</artifactId>
<version>2.2</version>
<scope>compile</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
Expand Down
1,298 changes: 0 additions & 1,298 deletions astralbooks-core/src/main/java/ro/niconeko/astralbooks/AstralBooksCommand.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,10 @@
import lombok.Getter;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.context.ContextManager;
import net.luckperms.api.model.user.User;
import net.luckperms.api.query.QueryOptions;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -345,28 +339,6 @@ public boolean openBook(Player player, ItemStack book) {
return true;
}

public boolean hasPermission(CommandSender sender, String permission) {
try {
if (sender.isOp()) return true;
Optional<LuckPerms> luckPerms = this.plugin.isLuckPermsEnabled() ? Optional.of(this.plugin.getLuckPerms()) : Optional.empty(); //If LuckPerms not enabled, this will return empty
Optional<Permission> vaultPerms = this.plugin.isVaultEnabled() ? Optional.of(this.plugin.getVaultPerms()) : Optional.empty(); //If vault not enabled or luckperms is used, this will return empty

return (luckPerms.isPresent() && this.hasLuckPermission(luckPerms.get().getUserManager().getUser(sender.getName()), permission)) ||
(vaultPerms.isPresent() && vaultPerms.get().has(sender, permission)) || sender.hasPermission(permission);
} catch (NullPointerException ex) {
return false;
}
}

protected boolean hasLuckPermission(User user, String permission) {
if (user == null)
throw new NullPointerException();
ContextManager contextManager = this.plugin.getLuckPerms().getContextManager();
return user.getCachedData().getPermissionData(
QueryOptions.contextual(contextManager.getContext(user).orElseGet(contextManager::getStaticContext))
).checkPermission(permission).asBoolean();
}

public String encodeItemStack(ItemStack item) {
if (item != null && item.getType() != Material.AIR)
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,173 +18,87 @@
package ro.niconeko.astralbooks;

import lombok.Getter;
import me.lucko.commodore.Commodore;
import me.lucko.commodore.CommodoreProvider;
import me.lucko.commodore.file.CommodoreFileReader;
import net.luckperms.api.LuckPerms;
import net.milkbowl.vault.permission.Permission;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.SimplePie;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import ro.niconeko.astralbooks.api.AstralBooks;
import ro.niconeko.astralbooks.listeners.AuthmeActions;
import ro.niconeko.astralbooks.listeners.CitizensActions;
import ro.niconeko.astralbooks.commands.AstralBooksCommand;
import ro.niconeko.astralbooks.listeners.PlayerActions;
import ro.niconeko.astralbooks.listeners.ServerActions;
import ro.niconeko.astralbooks.settings.PluginSettings;
import ro.niconeko.astralbooks.managers.BossBarManager;
import ro.niconeko.astralbooks.managers.HooksManager;
import ro.niconeko.astralbooks.managers.SettingsManager;
import ro.niconeko.astralbooks.storage.PluginStorage;
import ro.niconeko.astralbooks.utils.MessageUtils;
import ro.niconeko.astralbooks.utils.PersistentKey;
import ro.niconeko.astralbooks.utils.UpdateChecker;
import ro.niconeko.astralbooks.values.Settings;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;

public class AstralBooksPlugin extends JavaPlugin implements AstralBooks {
private final File settingsFile = new File(getDataFolder(), "settings.yml");
@Getter private Permission vaultPerms;
@Getter private LuckPerms luckPerms;
@Getter private final AstralBooksCore API = new AstralBooksCore(this);
@Getter private final PluginSettings settings = new PluginSettings(this);
@Getter private PluginStorage pluginStorage;
@Getter private boolean PlaceholderAPIEnabled, AuthMeEnabled, CitizensEnabled, LuckPermsEnabled, VaultEnabled;
@Getter private PlayerActions playerActionsListener;
@Getter private ServerActions serverActionsListener;

@Override
public void onEnable() {
try {
this.getLogger().info("============== BEGIN LOAD ==============");
MessageUtils.sendMessage(Bukkit.getConsoleSender(), "&a============== &fAstralBooks &a=============");

if (!this.API.loadDistribution()) {
this.getLogger().info("Failed to load distribution... disabling the plugin!");
this.setEnabled(false);
this.getLogger().info("============== END LOAD ==============");
return;
}
if (!PersistentKey.init(this)) {
this.getLogger().info("Failed to load PersistentKey!");
this.setEnabled(false);
this.getLogger().info("============== END LOAD ==============");
return;
}
if (!this.API.loadDistribution()) {
MessageUtils.sendMessage(Bukkit.getConsoleSender(), "Failed to load distribution... disabling the plugin!");
this.setEnabled(false);
MessageUtils.sendMessage(Bukkit.getConsoleSender(), "&a========================================");
return;
}
if (!PersistentKey.init(this)) {
MessageUtils.sendMessage(Bukkit.getConsoleSender(), "&cFailed to load PersistentKey!");
this.setEnabled(false);
MessageUtils.sendMessage(Bukkit.getConsoleSender(), "&a========================================");
return;
}

PluginManager manager = this.getServer().getPluginManager();
if (!manager.isPluginEnabled("LuckPerms")) {
this.getLogger().info("LuckPerms not found!");
if (!manager.isPluginEnabled("Vault"))
this.getLogger().info("Vault not found!");
else {
this.getLogger().info("Vault found, try hooking...");
RegisteredServiceProvider<Permission> provider = this.getServer().getServicesManager().getRegistration(Permission.class);
if (provider != null) {
this.VaultEnabled = true;
this.vaultPerms = provider.getProvider();
this.getLogger().info("Successfully hooked into Vault!");
} else
this.getLogger().info("Failed to hook into Vault!");
}
} else {
this.getLogger().info("LuckPerms found, try hooking...");
Plugin plugin = manager.getPlugin("LuckPerms");
if (plugin != null) {
if (plugin.getDescription().getVersion().startsWith("5")) {
RegisteredServiceProvider<LuckPerms> provider = this.getServer().getServicesManager().getRegistration(LuckPerms.class);
if (provider != null) {
this.LuckPermsEnabled = true;
this.luckPerms = provider.getProvider();
if (manager.isPluginEnabled("Vault"))
this.getLogger().info("Vault plugin found, but we'll use LuckPerms!");
this.getLogger().info("Successfully hooked into LuckPerms!");
} else
this.getLogger().info("Failed to hook into LuckPerms!");
} else {
this.getLogger().info("Your LuckPerms version is outdated! :(");
if (manager.isPluginEnabled("Vault")) {
RegisteredServiceProvider<Permission> provider = this.getServer().getServicesManager().getRegistration(Permission.class);
if (provider != null) {
this.getLogger().info("Vault found instead! Try hooking...");
this.VaultEnabled = true;
this.vaultPerms = provider.getProvider();
this.getLogger().info("Successfully hooked into Vault!");
} else // do we need it?
this.getLogger().info("Failed to hook into Vault!");
}
}
} else
this.getLogger().info("Failed to hook into LuckPerms!");
}
if (!manager.isPluginEnabled("PlaceholderAPI"))
this.getLogger().info("PlaceholderAPI not found!");
else {
this.getLogger().info("PlaceholderAPI found, try hooking...");
this.PlaceholderAPIEnabled = true;
this.getLogger().info("Successfully hooked into PlaceholderAPI!");
}
if (!manager.isPluginEnabled("Citizens"))
this.getLogger().info("Citizens not found!");
else {
this.getLogger().info("Citizens found, try hooking...");
manager.registerEvents(new CitizensActions(this), this);
this.CitizensEnabled = true;
this.getLogger().info("Successfully hooked into Citizens!");
}
if (!manager.isPluginEnabled("Authme"))
this.getLogger().info("Authme not found!");
else {
this.getLogger().info("AuthMe found, try hooking...");
manager.registerEvents(new AuthmeActions(this), this);
this.AuthMeEnabled = true;
this.getLogger().info("Successfully hooked into AuthMe!");
}
HooksManager.load(this);
BossBarManager.load(this);

this.playerActionsListener = new PlayerActions(this);
this.serverActionsListener = new ServerActions(this);
this.playerActionsListener = new PlayerActions(this);
this.serverActionsListener = new ServerActions(this);

if (!this.reloadPlugin())
throw new IllegalStateException("Failed to load settings!");
if (!this.reloadPlugin()) {
this.setEnabled(false);
MessageUtils.sendMessage(Bukkit.getConsoleSender(), "&a========================================");
return;
}

if (this.settings.isMetricsEnabled()) {
Metrics metrics = new Metrics(this, 18026);
metrics.addCustomChart(new SimplePie("database_type", () ->
switch (this.settings.getStorageSettings().getDatabaseType()) {
case JSON -> "JSON";
case MYSQL -> "MySQL";
case SQLITE -> "SQLite";
case H2 -> "H2";
case MARIADB -> "MariaDB";
}));
}
if (Settings.METRICS_ENABLED.get()) {
Metrics metrics = new Metrics(this, 18026);
metrics.addCustomChart(new SimplePie("database_type", () ->
switch (Settings.STORAGE.get().TYPE.get()) {
case JSON -> "JSON";
case MYSQL -> "MySQL";
case SQLITE -> "SQLite";
case H2 -> "H2";
case MARIADB -> "MariaDB";
}));
}

manager.registerEvents(this.playerActionsListener, this);
manager.registerEvents(this.serverActionsListener, this);
Bukkit.getPluginManager().registerEvents(this.playerActionsListener, this);
Bukkit.getPluginManager().registerEvents(this.serverActionsListener, this);

PluginCommand astralBooksCommand = this.getCommand("abook");
if (astralBooksCommand != null)
astralBooksCommand.setExecutor(new AstralBooksCommand(this));
if (CommodoreProvider.isSupported()) {
this.getLogger().info("Loading Brigardier support...");
Commodore commodore = CommodoreProvider.getCommodore(this);
this.getLogger().info(" Command /abook: " + (this.registerCompletions(commodore, astralBooksCommand) ? "supported" : "unsupported"));
} else
this.getLogger().info("Brigardier is not supported on this version!");
//Update checker, by default enabled
if (this.settings.isUpdateCheck())
manager.registerEvents(new UpdateChecker(this), this);
this.getLogger().info("============== END LOAD ==============");
} catch (Exception ex) {
this.getLogger().log(Level.WARNING, "Error detected, disabling the plugin!", ex);
this.getLogger().info("============== END LOAD ==============");
this.setEnabled(false);
}
// I hate reflections
API.getDistribution().register(new AstralBooksCommand(this));

//Update checker, by default enabled
if (Settings.UPDATE_CHECK.get())
Bukkit.getPluginManager().registerEvents(new UpdateChecker(this), this);
MessageUtils.sendMessage(Bukkit.getConsoleSender(), "&a========================================");
}

@Override
Expand All @@ -197,76 +111,16 @@ public void onDisable() {
this.pluginStorage.unload();
}

private boolean registerCompletions(Commodore commodore, PluginCommand command) {
if (command == null)
return false;
try (InputStream is = this.getResource("command.commodore")) {
if (is == null)
throw new FileNotFoundException();
commodore.register(command, CommodoreFileReader.INSTANCE.parse(is), player -> this.API.hasPermission(player, "astralbooks.tab.completer"));
return true;
} catch (Exception ex) {
return false;
}
}

public boolean loadSettings() {
try {
YamlConfiguration config = YamlConfiguration.loadConfiguration(this.settingsFile);
config.options().setHeader(
List.of("""
_ _ ____ _ \s
/\\ | | | | _ \\ | | \s
/ \\ ___| |_ _ __ __ _| | |_) | ___ ___ | | _____\s
/ /\\ \\ / __| __| '__/ _` | | _ < / _ \\ / _ \\| |/ / __|
/ ____ \\\\__ \\ |_| | | (_| | | |_) | (_) | (_) | <\\__ \\
/_/ \\_\\___/\\__|_| \\__,_|_|____/ \\___/ \\___/|_|\\_\\___/
\s
\s
""".split("\n"))
);
this.settings.load(config);
return true;
} catch (Exception ex) {
this.getLogger().log(Level.WARNING, "Failed to load settings", ex);
return false;
}
}

public boolean saveSettings() {
try {
YamlConfiguration config = new YamlConfiguration();
this.settings.load(config);
config.options().setHeader(
List.of("""
_ _ ____ _ \s
/\\ | | | | _ \\ | | \s
/ \\ ___| |_ _ __ __ _| | |_) | ___ ___ | | _____\s
/ /\\ \\ / __| __| '__/ _` | | _ < / _ \\ / _ \\| |/ / __|
/ ____ \\\\__ \\ |_| | | (_| | | |_) | (_) | (_) | <\\__ \\
/_/ \\_\\___/\\__|_| \\__,_|_|____/ \\___/ \\___/|_|\\_\\___/
\s
\s
""".split("\n"))
);
config.save(this.settingsFile);
return true;
} catch (Exception ex) {
this.getLogger().log(Level.WARNING, "Failed to save settings", ex);
return false;
}
}

public boolean reloadPlugin() {
if (this.loadSettings()) {
this.saveSettings();
if (SettingsManager.loadAndSave(this)) {
try {
if (this.pluginStorage != null)
this.pluginStorage.unload();
this.pluginStorage = new PluginStorage(this);
this.pluginStorage.load(this.settings.getStorageSettings());
this.pluginStorage.load(Settings.STORAGE.get());
} catch (SQLException ex) {
this.getLogger().log(Level.SEVERE, "Could not load storage!", ex);
MessageUtils.sendMessage(Bukkit.getConsoleSender(), "&cFailed to load storage!");
ex.printStackTrace();
return false;
}
if (this.playerActionsListener != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ro.niconeko.astralbooks.commands;

import ro.niconeko.astralbooks.AstralBooksPlugin;
import ro.niconeko.astralbooks.commands.subs.HelpCommand;
import ro.niconeko.astralbooks.commands.subs.ReloadCommand;
import ro.niconeko.astralbooks.values.Messages;
import ro.niconeko.astralbooks.values.Permissions;

public class AstralBooksCommand extends AstralCommand {
public AstralBooksCommand(AstralBooksPlugin plugin) {
super(plugin, "abooks", Permissions.COMMAND);
super.then(new ReloadCommand(plugin))
.then(new HelpCommand(plugin))
// etc...
.then(super.greedyArgument("unknown").executes(context -> Messages.COMMAND_MAIN_UNKNOWN.send(getSender(context), replaceable(context, "%command%", "unknown"))));
super.executes(context -> Messages.COMMAND_MAIN_HELP.send(getSender(context)));
}
}
Loading

0 comments on commit 90e93ff

Please sign in to comment.