use a better event system
This commit is contained in:
@ -1,15 +1,19 @@
|
||||
package cc.fascinated.account;
|
||||
|
||||
import cc.fascinated.Aetheria;
|
||||
import cc.fascinated.config.Config;
|
||||
import cc.fascinated.config.Lang;
|
||||
import cc.fascinated.utils.DiscordWebhook;
|
||||
import cc.fascinated.utils.Manager;
|
||||
import cc.fascinated.utils.Style;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerLoginEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.event.server.PluginDisableEvent;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
@ -23,7 +27,7 @@ public class AccountManager extends Manager {
|
||||
|
||||
public AccountManager() {
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
registerAccount(player);
|
||||
loadAccount(player);
|
||||
}
|
||||
|
||||
Bukkit.getAsyncScheduler().runAtFixedRate(Aetheria.INSTANCE, (task) -> {
|
||||
@ -40,7 +44,7 @@ public class AccountManager extends Manager {
|
||||
public static Account getAccount(Player player) {
|
||||
UUID uuid = player.getUniqueId();
|
||||
if (!ACCOUNTS.containsKey(uuid)) {
|
||||
return registerAccount(player);
|
||||
return loadAccount(player);
|
||||
}
|
||||
return ACCOUNTS.get(uuid);
|
||||
}
|
||||
@ -51,7 +55,7 @@ public class AccountManager extends Manager {
|
||||
* @param player the player
|
||||
* @return true if the account is already registered, false otherwise
|
||||
*/
|
||||
private boolean accountRegistered(Player player) {
|
||||
private boolean isAccountLoaded(Player player) {
|
||||
return ACCOUNTS.containsKey(player.getUniqueId());
|
||||
}
|
||||
|
||||
@ -60,7 +64,7 @@ public class AccountManager extends Manager {
|
||||
*
|
||||
* @param player the player
|
||||
*/
|
||||
private static Account registerAccount(Player player) {
|
||||
private static Account loadAccount(Player player) {
|
||||
UUID uuid = player.getUniqueId();
|
||||
Account account = new Account(player);
|
||||
ACCOUNTS.put(uuid, account);
|
||||
@ -79,30 +83,53 @@ public class AccountManager extends Manager {
|
||||
log.info("Saved {} accounts. ({}ms)", ACCOUNTS.size(), System.currentTimeMillis() - before);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onJoin(PlayerJoinEvent event) {
|
||||
@Override
|
||||
public void onPlayerJoin(Account account, PlayerJoinEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
if (accountRegistered(player)) { // Account already registered
|
||||
if (player.hasPlayedBefore()) {
|
||||
return;
|
||||
}
|
||||
registerAccount(player);
|
||||
|
||||
event.joinMessage(null); // Remove the join message so we can send our own
|
||||
Bukkit.broadcast(Style.getMiniMessage().deserialize(Lang.FIRST_JOIN_MESSAGE.getAsString()
|
||||
.replace("%player%", player.getName())
|
||||
));
|
||||
|
||||
Aetheria.EXECUTOR.execute(() -> {
|
||||
// todo: re-code this it's so ugly
|
||||
DiscordWebhook discordWebhook = new DiscordWebhook(Config.DISCORD_LOG_WEBHOOK.getAsString());
|
||||
DiscordWebhook.EmbedObject embed = new DiscordWebhook.EmbedObject();
|
||||
embed.setTitle("New Player Joined");
|
||||
embed.addField("Name", player.getName(), true);
|
||||
embed.addField("UUID", player.getUniqueId().toString(), true);
|
||||
|
||||
discordWebhook.addEmbed(embed);
|
||||
try {
|
||||
discordWebhook.execute();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onQuit(PlayerQuitEvent event) {
|
||||
@Override
|
||||
public void onPlayerLogin(Account account, PlayerLoginEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
Account account = getAccount(player);
|
||||
if (isAccountLoaded(player)) { // Account already loaded
|
||||
return;
|
||||
}
|
||||
loadAccount(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerQuit(Account account, PlayerQuitEvent event) {
|
||||
account.save(true);
|
||||
|
||||
ACCOUNTS.remove(player.getUniqueId());
|
||||
ACCOUNTS.remove(account.getUuid());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPluginDisable(PluginDisableEvent event) {
|
||||
if (event.getPlugin() != Aetheria.INSTANCE) {
|
||||
return;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAetheriaDisable() {
|
||||
this.saveAccounts(); // Save the accounts to disk
|
||||
ACCOUNTS.clear(); // Remove the accounts from the cache
|
||||
}
|
||||
|
Reference in New Issue
Block a user