Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
+ Introducing mailboxes database
  • Loading branch information
leothawne committed Dec 17, 2023
1 parent 3d23b1c commit 90597d0
Show file tree
Hide file tree
Showing 21 changed files with 1,050 additions and 1,037 deletions.
6 changes: 3 additions & 3 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: LTItemMail
description: A way of sending items to another players without needing to teleport to them.
version: 0.2.5
version: 0.2.6
api-version: 1.13
load: POSTWORLD
load: STARTUP
author: [leothawne]
website: https://leothawne.github.io/LTItemMail/
url: https://leothawne.github.io/LTItemMail/
main: io.github.leothawne.LTItemMail.LTItemMail
database: false
database: true
soft-depend: [Vault, Essentials]
commands:
itemmail:
Expand Down
14 changes: 0 additions & 14 deletions src/main/java/io/github/leothawne/LTItemMail/App.java

This file was deleted.

117 changes: 69 additions & 48 deletions src/main/java/io/github/leothawne/LTItemMail/LTItemMail.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package io.github.leothawne.LTItemMail;

import java.sql.Connection;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
Expand All @@ -39,6 +40,7 @@
import io.github.leothawne.LTItemMail.listener.PlayerListener;
import io.github.leothawne.LTItemMail.module.ConfigurationModule;
import io.github.leothawne.LTItemMail.module.ConsoleModule;
import io.github.leothawne.LTItemMail.module.DatabaseModule;
import io.github.leothawne.LTItemMail.module.LanguageModule;
import io.github.leothawne.LTItemMail.module.MetricsModule;
import io.github.leothawne.LTItemMail.module.VaultModule;
Expand All @@ -52,65 +54,62 @@
*
*/
public final class LTItemMail extends JavaPlugin {
private final ConsoleModule console = new ConsoleModule(this);
private static LTItemMail instance;
private final void registerEvents(final Listener...listeners) {
for(final Listener listener : listeners) {
Bukkit.getServer().getPluginManager().registerEvents(listener, this);
}
for(final Listener listener : listeners) Bukkit.getServer().getPluginManager().registerEvents(listener, this);
}
private static FileConfiguration configuration;
private static FileConfiguration language;
private static HashMap<UUID, Boolean> playerBusy = new HashMap<UUID, Boolean>();
private static MetricsAPI metrics;
private static BukkitScheduler scheduler;
private static int versionTask = 0;
private final ConsoleModule console = new ConsoleModule(this);
private FileConfiguration configuration;
private FileConfiguration language;
private Connection con;
private HashMap<UUID, Boolean> playerBusy = new HashMap<UUID, Boolean>();
private MetricsAPI metrics;
private BukkitScheduler scheduler;
private Economy economyPlugin;
/**
*
* @deprecated Not for public use.
*
*/
@Override
public final void onEnable() {
instance = this;
this.console.Hello();
this.console.info("Loading...");
for(final Player player : getServer().getOnlinePlayers()) {
LTItemMail.playerBusy.put(player.getUniqueId(), false);
}
ConfigurationModule.check(this, this.console);
LTItemMail.configuration = ConfigurationModule.load(this, this.console);
if(LTItemMail.configuration.getBoolean("enable-plugin") == true) {
LTItemMail.metrics = MetricsModule.init(this, this.console);
for(final Player player : getServer().getOnlinePlayers()) playerBusy.put(player.getUniqueId(), false);
ConfigurationModule.check();
configuration = ConfigurationModule.load();
if(configuration.getBoolean("enable-plugin")) {
metrics = MetricsModule.init();
Economy economyPlugin = null;
if(LTItemMail.configuration.getBoolean("use-vault") == true) {
this.console.info("Loading Vault...");
if(VaultModule.isVaultInstalled(this)) {
this.console.info("Vault loaded.");
this.console.info("Looking for an Economy plugin...");
economyPlugin = VaultModule.getEconomy(this);
if(configuration.getBoolean("use-vault")) {
console.info("Loading Vault...");
if(VaultModule.isVaultInstalled()) {
console.info("Vault loaded.");
console.info("Looking for an Economy plugin...");
economyPlugin = VaultModule.getEconomy();
if(economyPlugin != null) {
this.console.info("Economy plugin found.");
} else {
this.console.info("Economy plugin is missing. Skipping...");
}
} else {
this.console.info("Vault is not installed. Skipping...");
}
console.info("Economy plugin found.");
} else console.info("Economy plugin is missing. Skipping...");
} else console.info("Vault is not installed. Skipping...");
}
LanguageModule.check(this, this.console, LTItemMail.configuration);
LTItemMail.language = LanguageModule.load(this, this.console, LTItemMail.configuration);
this.getCommand("itemmail").setExecutor(new ItemMailCommand(this, this.console, LTItemMail.configuration, LTItemMail.language));
this.getCommand("itemmail").setTabCompleter(new ItemMailCommandTabCompleter());
this.getCommand("itemmailadmin").setExecutor(new ItemMailAdminCommand(this, this.console, LTItemMail.configuration, LTItemMail.language));
this.getCommand("itemmailadmin").setTabCompleter(new ItemMailAdminCommandTabCompleter());
this.getCommand("mailitem").setExecutor(new MailItemCommand(this, this.console, LTItemMail.configuration, LTItemMail.language));
this.getCommand("mailitem").setTabCompleter(new MailItemCommandTabCompleter(this));
LTItemMail.scheduler = this.getServer().getScheduler();
LTItemMail.versionTask = scheduler.scheduleAsyncRepeatingTask(this, new VersionTask(this, this.console), 0, 20 * 60 * 60);
registerEvents(new MailboxListener(this, LTItemMail.configuration, LTItemMail.language, LTItemMail.playerBusy, economyPlugin), new PlayerListener(LTItemMail.configuration, LTItemMail.playerBusy));
new WarnIntegrationsAPI(this, new LinkedList<String>(Arrays.asList("Vault", "Essentials")));
LanguageModule.check();
language = LanguageModule.load();
DatabaseModule.check();
con = DatabaseModule.load();
getCommand("itemmail").setExecutor(new ItemMailCommand());
getCommand("itemmail").setTabCompleter(new ItemMailCommandTabCompleter());
getCommand("itemmailadmin").setExecutor(new ItemMailAdminCommand());
getCommand("itemmailadmin").setTabCompleter(new ItemMailAdminCommandTabCompleter());
getCommand("mailitem").setExecutor(new MailItemCommand());
getCommand("mailitem").setTabCompleter(new MailItemCommandTabCompleter());
scheduler = Bukkit.getScheduler();
scheduler.scheduleSyncRepeatingTask(this, new VersionTask(), 0, 20 * 60 * 60);
registerEvents(new MailboxListener(), new PlayerListener());
new WarnIntegrationsAPI(new LinkedList<String>(Arrays.asList("Vault", "Essentials")));
} else {
this.console.severe("You've choosen to disable me.");
this.getServer().getPluginManager().disablePlugin(this);
Bukkit.getPluginManager().disablePlugin(this);
}
}
/**
Expand All @@ -120,10 +119,8 @@ public final void onEnable() {
*/
@Override
public final void onDisable() {
this.console.info("Unloading...");
if(LTItemMail.scheduler.isCurrentlyRunning(LTItemMail.versionTask) || scheduler.isQueued(LTItemMail.versionTask)) {
LTItemMail.scheduler.cancelTask(LTItemMail.versionTask);
}
console.info("Unloading...");
scheduler.cancelTasks(this);
}
/**
*
Expand All @@ -134,6 +131,30 @@ public final void onDisable() {
*/
@SuppressWarnings("deprecation")
public final LTItemMailAPI getAPI() {
return new LTItemMailAPI(this, LTItemMail.configuration, LTItemMail.language, LTItemMail.playerBusy, LTItemMail.metrics);
return new LTItemMailAPI();
}
public static final LTItemMail getInstance() {
return instance;
}
public final FileConfiguration getConfiguration() {
return configuration;
}
public final FileConfiguration getLanguage() {
return language;
}
public final HashMap<UUID, Boolean> getPlayerBusy(){
return playerBusy;
}
public final ConsoleModule getConsole() {
return console;
}
public final MetricsAPI getMetrics() {
return metrics;
}
public final Economy getEconomy() {
return economyPlugin;
}
public final Connection getConnection() {
return con;
}
}
4 changes: 1 addition & 3 deletions src/main/java/io/github/leothawne/LTItemMail/api/HTTP.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ public static final String getData(final String url) {
final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));
final StringBuilder builder = new StringBuilder();
String string;
while((string = reader.readLine()) != null) {
builder.append(string);
}
while((string = reader.readLine()) != null) builder.append(string);
return builder.toString();
} catch(final Exception exception) {
return null;
Expand Down
42 changes: 10 additions & 32 deletions src/main/java/io/github/leothawne/LTItemMail/api/LTItemMailAPI.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.github.leothawne.LTItemMail.api;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
Expand All @@ -18,25 +18,14 @@
*
*/
public final class LTItemMailAPI {
private static LTItemMail plugin;
private static FileConfiguration configuration;
private static FileConfiguration language;
private static HashMap<UUID, Boolean> playerBusy;
private static MetricsAPI metrics;
/**
*
* @deprecated There is no need to manually create
* an object with this constructor when
* you can easily use {@link LTItemMail#getAPI()}.
*
*/
public LTItemMailAPI(final LTItemMail plugin, final FileConfiguration configuration, final FileConfiguration language, final HashMap<UUID, Boolean> playerBusy, final MetricsAPI metrics) {
LTItemMailAPI.plugin = plugin;
LTItemMailAPI.configuration = configuration;
LTItemMailAPI.language = language;
LTItemMailAPI.playerBusy = playerBusy;
LTItemMailAPI.metrics = metrics;
}
public LTItemMailAPI() {}
/**
*
* Returns a boolean type value that can be used to determine
Expand All @@ -46,7 +35,7 @@ public LTItemMailAPI(final LTItemMail plugin, final FileConfiguration configurat
*
*/
public final boolean isUsingVault() {
return LTItemMailAPI.configuration.getBoolean("use-vault");
return LTItemMail.getInstance().getConfiguration().getBoolean("use-vault");
}
/**
*
Expand All @@ -61,7 +50,7 @@ public final boolean isUsingVault() {
*
*/
public final boolean isPlayerBusy(final Player player) {
return (boolean) LTItemMailAPI.playerBusy.get(player.getUniqueId()).booleanValue();
return (boolean) LTItemMail.getInstance().getPlayerBusy().get(player.getUniqueId()).booleanValue();
}
/**
*
Expand All @@ -76,7 +65,7 @@ public final boolean isPlayerBusy(final Player player) {
*
*/
public final boolean isPlayerBusy(final UUID playerUUID) {
return this.isPlayerBusy(LTItemMailAPI.plugin.getServer().getPlayer(playerUUID));
return isPlayerBusy(Bukkit.getPlayer(playerUUID));
}
/**
*
Expand All @@ -91,7 +80,7 @@ public final boolean isPlayerBusy(final UUID playerUUID) {
*
*/
public final boolean isPlayerBusy(final String playerName) {
return this.isPlayerBusy(LTItemMailAPI.plugin.getServer().getPlayer(playerName));
return isPlayerBusy(Bukkit.getPlayer(playerName));
}
/**
*
Expand All @@ -102,18 +91,7 @@ public final boolean isPlayerBusy(final String playerName) {
*
*/
public final FileConfiguration getLanguageMap(){
return LTItemMailAPI.language;
}
/**
*
* Returns a boolean type value that can be used to determine
* if the plugin is currently using bStats.
*
* @return A boolean type value.
*
*/
public final boolean isMetricsEnabled() {
return LTItemMailAPI.metrics.isEnabled();
return LTItemMail.getInstance().getLanguage();
}
/**
*
Expand All @@ -126,7 +104,7 @@ public final boolean isMetricsEnabled() {
*
*/
public final void sendSpecialMailbox(final Player player, final LinkedList<ItemStack> items) {
MailboxAPI.sendSpecial(LTItemMailAPI.plugin, LTItemMailAPI.configuration, LTItemMailAPI.language, LTItemMailAPI.playerBusy, player, items);
MailboxAPI.sendSpecial(player, items);
}
/**
*
Expand All @@ -139,7 +117,7 @@ public final void sendSpecialMailbox(final Player player, final LinkedList<ItemS
*
*/
public final void sendSpecialMailbox(final UUID playerUUID, final LinkedList<ItemStack> items) {
this.sendSpecialMailbox(LTItemMailAPI.plugin.getServer().getPlayer(playerUUID), items);
sendSpecialMailbox(Bukkit.getPlayer(playerUUID), items);
}
/**
*
Expand All @@ -152,6 +130,6 @@ public final void sendSpecialMailbox(final UUID playerUUID, final LinkedList<Ite
*
*/
public final void sendSpecialMailbox(final String playerName, final LinkedList<ItemStack> items) {
this.sendSpecialMailbox(LTItemMailAPI.plugin.getServer().getPlayer(playerName), items);
sendSpecialMailbox(Bukkit.getPlayer(playerName), items);
}
}
21 changes: 9 additions & 12 deletions src/main/java/io/github/leothawne/LTItemMail/api/MailboxAPI.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package io.github.leothawne.LTItemMail.api;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.UUID;

import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
Expand All @@ -15,21 +12,21 @@
import io.github.leothawne.LTItemMail.type.MailboxType;

public final class MailboxAPI {
public static final void sendSpecial(final LTItemMail plugin, final FileConfiguration configuration, final FileConfiguration language, final HashMap<UUID, Boolean> playerBusy, final Player receiver, final LinkedList<ItemStack> items) {
final String mailboxFrom = language.getString("special-mailbox");
final String[] mailboxOpening = language.getString("mailbox-opening-seconds").split("%");
playerBusy.put(receiver.getUniqueId(), true);
if(configuration.getBoolean("use-title") == true) {
receiver.sendTitle(ChatColor.LIGHT_PURPLE + "" + mailboxFrom, ChatColor.AQUA + "" + mailboxOpening[0] + "" + ChatColor.GREEN + "" + configuration.getInt("mail-time") + "" + ChatColor.AQUA + "" + mailboxOpening[1] + " " + ChatColor.DARK_RED + "" + language.getString("mailbox-lose"), 20 * 1, 20 * configuration.getInt("mail-time"), 20 * 1);
public static final void sendSpecial(final Player receiver, final LinkedList<ItemStack> items) {
final String mailboxFrom = LTItemMail.getInstance().getLanguage().getString("special-mailbox");
final String[] mailboxOpening = LTItemMail.getInstance().getLanguage().getString("mailbox-opening-seconds").split("%");
LTItemMail.getInstance().getPlayerBusy().put(receiver.getUniqueId(), true);
if(LTItemMail.getInstance().getConfiguration().getBoolean("use-title") == true) {
receiver.sendTitle(ChatColor.LIGHT_PURPLE + "" + mailboxFrom, ChatColor.AQUA + "" + mailboxOpening[0] + "" + ChatColor.GREEN + "" + LTItemMail.getInstance().getConfiguration().getInt("mail-time") + "" + ChatColor.AQUA + "" + mailboxOpening[1] + " " + ChatColor.DARK_RED + "" + LTItemMail.getInstance().getLanguage().getString("mailbox-lose"), 20 * 1, 20 * LTItemMail.getInstance().getConfiguration().getInt("mail-time"), 20 * 1);
} else {
receiver.sendMessage(ChatColor.DARK_GREEN + "[" + configuration.getString("plugin-tag") + "] " + ChatColor.AQUA + "" + mailboxFrom);
receiver.sendMessage(ChatColor.DARK_GREEN + "[" + configuration.getString("plugin-tag") + "] " + ChatColor.AQUA + "" + mailboxOpening[0] + "" + ChatColor.GREEN + "" + configuration.getInt("mail-time") + "" + ChatColor.AQUA + "" + mailboxOpening[1] + " " + ChatColor.DARK_RED + "" + language.getString("mailbox-lose"));
receiver.sendMessage(ChatColor.DARK_GREEN + "[" + LTItemMail.getInstance().getConfiguration().getString("plugin-tag") + "] " + ChatColor.AQUA + "" + mailboxFrom);
receiver.sendMessage(ChatColor.DARK_GREEN + "[" + LTItemMail.getInstance().getConfiguration().getString("plugin-tag") + "] " + ChatColor.AQUA + "" + mailboxOpening[0] + "" + ChatColor.GREEN + "" + LTItemMail.getInstance().getConfiguration().getInt("mail-time") + "" + ChatColor.AQUA + "" + mailboxOpening[1] + " " + ChatColor.DARK_RED + "" + LTItemMail.getInstance().getLanguage().getString("mailbox-lose"));
}
new BukkitRunnable() {
@Override
public final void run() {
receiver.openInventory(MailboxInventory.getMailboxInventory(MailboxType.IN, null, items));
}
}.runTaskLater(plugin, 20 * configuration.getInt("mail-time") + 2);
}.runTaskLater(LTItemMail.getInstance(), 20 * LTItemMail.getInstance().getConfiguration().getInt("mail-time") + 2);
}
}
Loading

0 comments on commit 90597d0

Please sign in to comment.