[17.1k Lines] Added login logging to the account repository, and added more chat emotes

This commit is contained in:
Rainnny7 2021-02-20 17:49:18 -05:00
parent 6e983ae807
commit 15e199b94b
8 changed files with 83 additions and 30 deletions

@ -305,7 +305,7 @@ public class AccountManager extends Module {
if (reference.get() != null) if (reference.get() != null)
consumer.accept(reference.get()); consumer.accept(reference.get());
else { else {
CompletableFuture.runAsync(() -> { Runnable fetchAccount = () -> {
try { try {
reference.set(repository.login(uuid, name, "")); reference.set(repository.login(uuid, name, ""));
if (reference.get() != null) if (reference.get() != null)
@ -314,7 +314,15 @@ public class AccountManager extends Module {
ex.printStackTrace(); ex.printStackTrace();
} }
consumer.accept(reference.get()); consumer.accept(reference.get());
}); };
// If the method is being executed on the primary thread, we wanna execute it asynchronously instead
if (Bukkit.isPrimaryThread())
CompletableFuture.runAsync(fetchAccount);
else {
// If the method is being executed off of the primary thread, we wanna execute it normally to stay
// on that thread
fetchAccount.run();
}
} }
} }

@ -1,6 +1,7 @@
package zone.themcgamer.core.account; package zone.themcgamer.core.account;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import zone.themcgamer.common.HashUtils; import zone.themcgamer.common.HashUtils;
import zone.themcgamer.core.account.event.AccountPreLoadEvent; import zone.themcgamer.core.account.event.AccountPreLoadEvent;
@ -27,6 +28,7 @@ import java.util.stream.Collectors;
/** /**
* @author Braydon * @author Braydon
*/ */
@Slf4j(topic = "Account Repository")
public class AccountRepository extends MySQLRepository { public class AccountRepository extends MySQLRepository {
private static final String SELECT_ACCOUNT = "SELECT * FROM `accounts` WHERE `uuid` = ? LIMIT 1"; private static final String SELECT_ACCOUNT = "SELECT * FROM `accounts` WHERE `uuid` = ? LIMIT 1";
private static final String INSERT_ACCOUNT = "INSERT INTO `accounts` " + private static final String INSERT_ACCOUNT = "INSERT INTO `accounts` " +
@ -49,6 +51,7 @@ public class AccountRepository extends MySQLRepository {
if (uuid == null || (name == null || name.trim().isEmpty())) if (uuid == null || (name == null || name.trim().isEmpty()))
return null; return null;
boolean offlineLookup = ipAddress.trim().isEmpty(); boolean offlineLookup = ipAddress.trim().isEmpty();
log.info("Logging in client " + name + " (" + uuid.toString() + ")" + (offlineLookup ? " - OFFLINE LOOKUP" : ""));
String encryptedIpAddress = offlineLookup ? "" : HashUtils.encryptSha256(ipAddress); String encryptedIpAddress = offlineLookup ? "" : HashUtils.encryptSha256(ipAddress);
int accountId = -1; int accountId = -1;
boolean loadedFromCache = false; boolean loadedFromCache = false;
@ -62,6 +65,7 @@ public class AccountRepository extends MySQLRepository {
} }
accountId = cache.getAccountId(); accountId = cache.getAccountId();
loadedFromCache = true; loadedFromCache = true;
log.info("Account id for " + name + " loaded from cache: " + accountId);
} }
} }
Account account = null; Account account = null;
@ -77,8 +81,11 @@ public class AccountRepository extends MySQLRepository {
String query = ""; String query = "";
if (resultSet.next()) { // If the account exists in the database, we wanna load its values if (resultSet.next()) { // If the account exists in the database, we wanna load its values
if (accountId <= 0) // If the account id has not been loaded from the cache, we wanna fetch it from the database // If the account id has not been loaded from the cache, we wanna fetch it from the database
if (accountId <= 0) {
accountId = resultSet.getInt(1); accountId = resultSet.getInt(1);
log.info("Account id for " + name + " loaded from MySQL: " + accountId);
}
account = constructAccount(accountId, uuid, name, resultSet, ipAddress, encryptedIpAddress, offlineLookup ? -1L : now); account = constructAccount(accountId, uuid, name, resultSet, ipAddress, encryptedIpAddress, offlineLookup ? -1L : now);
// If the account exists in the database and we're not doing an offline account lookup, we wanna update // If the account exists in the database and we're not doing an offline account lookup, we wanna update
@ -106,11 +113,14 @@ public class AccountRepository extends MySQLRepository {
} }
}); });
accountId = idArray[0]; accountId = idArray[0];
log.info("Successfully inserted a new account for " + name + ", their account id is " + accountId);
} }
Bukkit.getPluginManager().callEvent(new AccountPreLoadEvent(uuid, name, ipAddress)); Bukkit.getPluginManager().callEvent(new AccountPreLoadEvent(uuid, name, ipAddress));
int finalAccountId = accountId; int finalAccountId = accountId;
query+= AccountManager.MINI_ACCOUNTS.parallelStream().map(miniAccount -> miniAccount.getQuery(finalAccountId, uuid, name, ipAddress, encryptedIpAddress)).collect(Collectors.joining()); query+= AccountManager.MINI_ACCOUNTS.parallelStream().map(miniAccount -> miniAccount.getQuery(finalAccountId, uuid, name, ipAddress, encryptedIpAddress)).collect(Collectors.joining());
if (!query.trim().isEmpty()) { if (!query.trim().isEmpty()) {
log.info("Executing mini account tasks (" + AccountManager.MINI_ACCOUNTS.size() + ") for " + name);
statement.execute(query); statement.execute(query);
statement.getUpdateCount(); statement.getUpdateCount();
statement.getMoreResults(); statement.getMoreResults();
@ -145,8 +155,10 @@ public class AccountRepository extends MySQLRepository {
now now
); );
} }
if (!loadedFromCache && cacheRepository != null) if (!loadedFromCache && cacheRepository != null) {
cacheRepository.post(new PlayerCache(uuid, name, accountId)); cacheRepository.post(new PlayerCache(uuid, name, accountId));
log.info("Stored new cache object for " + name + " in Redis");
}
return account; return account;
} }

@ -1,5 +1,6 @@
package zone.themcgamer.core.chat; package zone.themcgamer.core.chat;
import lombok.Getter;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.ComponentBuilder;
@ -16,6 +17,7 @@ import zone.themcgamer.core.badSportSystem.BadSportSystem;
import zone.themcgamer.core.badSportSystem.Punishment; import zone.themcgamer.core.badSportSystem.Punishment;
import zone.themcgamer.core.badSportSystem.PunishmentCategory; import zone.themcgamer.core.badSportSystem.PunishmentCategory;
import zone.themcgamer.core.chat.command.ClearChatCommand; import zone.themcgamer.core.chat.command.ClearChatCommand;
import zone.themcgamer.core.chat.command.EmotesCommand;
import zone.themcgamer.core.chat.command.message.MessageCommand; import zone.themcgamer.core.chat.command.message.MessageCommand;
import zone.themcgamer.core.chat.command.message.ReplyCommand; import zone.themcgamer.core.chat.command.message.ReplyCommand;
import zone.themcgamer.core.chat.component.IChatComponent; import zone.themcgamer.core.chat.component.IChatComponent;
@ -35,12 +37,22 @@ import java.util.concurrent.TimeUnit;
public class ChatManager extends Module { public class ChatManager extends Module {
private final BadSportSystem badSportSystem; private final BadSportSystem badSportSystem;
private final IChatComponent[] chatComponents; private final IChatComponent[] chatComponents;
private final Map<String, String> emotes = new HashMap<>(); @Getter private final Map<String, String> emotes = new HashMap<>();
{ {
emotes.put("shrug", "¯\\_(ツ)_/¯"); emotes.put("¯\\_(ツ)_/¯", ":shrug:");
emotes.put("tableflip", "(╯°□°)╯︵ ┻━┻"); emotes.put("⊂(´・◡・⊂ )∘˚˳°", ":happyghost:");
emotes.put("unflip", "┬─┬ ( ゜-゜ノ)"); emotes.put("ヽ༼ຈل͜ຈ༽ノ", ":donger:");
emotes.put("ಠ_ಠ", ":disapproval:");
emotes.put("(≖_≖)", ":squint:");
emotes.put("(౮⦦ʖ౮)", ":lenny:");
emotes.put("┬─┬ ( ゜-゜ノ)", ":unflip:");
emotes.put("(☞゚ヮ゚)☞", ":same:");
emotes.put("ლ(ಥ Д ಥ )ლ", ":why:");
emotes.put("(╯°□°)╯︵ ┻━┻", ":tableflip:");
emotes.put("⊂(•̀_•́⊂ )∘˚˳°", ":angryghost:");
emotes.put("( ˘ ³˘)♥", ":kiss:");
emotes.put("༼ つ ◕_◕ ༽つ", ":ameno:");
} }
public ChatManager(JavaPlugin plugin, AccountManager accountManager, BadSportSystem badSportSystem, IChatComponent[] chatComponents) { public ChatManager(JavaPlugin plugin, AccountManager accountManager, BadSportSystem badSportSystem, IChatComponent[] chatComponents) {
@ -50,15 +62,7 @@ public class ChatManager extends Module {
registerCommand(new ClearChatCommand()); registerCommand(new ClearChatCommand());
registerCommand(new MessageCommand(accountManager, badSportSystem)); registerCommand(new MessageCommand(accountManager, badSportSystem));
registerCommand(new ReplyCommand(accountManager, badSportSystem)); registerCommand(new ReplyCommand(accountManager, badSportSystem));
registerCommand(new EmotesCommand(this));
/* TODO
/chatmanager blackwords add <word>
/chatmanager blackwords remove <word>
/chatmanager emote add <unicode>
/chatmanager emote remove <unicode>
/chatmanager urls add <url>
/chatmanager remove <url>
*/
} }
@EventHandler @EventHandler
@ -91,13 +95,13 @@ public class ChatManager extends Module {
return; return;
} }
if (!CooldownHandler.canUse(player, "Chat", TimeUnit.SECONDS.toMillis(3L), false) if (!CooldownHandler.canUse(player, "Chat", TimeUnit.SECONDS.toMillis(3L), false)
&& !optionalAccount.get().hasRank(Rank.GAMER)) { && !optionalAccount.get().hasRank(Rank.GAMER) && !optionalAccount.get().hasRank(Rank.HELPER)) {
player.sendMessage(Style.error("Chat", "You are chatting too quickly! To bypass this cooldown," + player.sendMessage(Style.error("Chat", "You are chatting too quickly! To bypass this cooldown," +
" please consider purchasing a donator rank over at §bstore.mcgamerzone.net§7.")); " please consider purchasing a donator rank over at §bstore.mcgamerzone.net§7."));
return; return;
} }
for (Map.Entry<String, String> emote : emotes.entrySet()) for (Map.Entry<String, String> emote : emotes.entrySet())
message = message.replace(":" + emote.getKey() + ":", emote.getValue()); message = message.replace(emote.getValue(), emote.getKey());
List<BaseComponent> components = new ArrayList<>(); List<BaseComponent> components = new ArrayList<>();
for (IChatComponent chatComponent : chatComponents) { for (IChatComponent chatComponent : chatComponents) {
BaseComponent component = chatComponent.getComponent(player); BaseComponent component = chatComponent.getComponent(player);

@ -11,9 +11,10 @@ public class ClearChatCommand {
@Command(name = "clearchat", aliases = { "cc" }, description = "Clear the chat", ranks = { Rank.HELPER }, playersOnly = true) @Command(name = "clearchat", aliases = { "cc" }, description = "Clear the chat", ranks = { Rank.HELPER }, playersOnly = true)
public void onCommand(CommandProvider command) { public void onCommand(CommandProvider command) {
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) { for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
for (int i = 0; i < 150; i++) for (int i = 0; i < 150; i++) {
onlinePlayer.sendMessage(" "); onlinePlayer.sendMessage(" ");
onlinePlayer.sendMessage(Style.main("Chat", "The chat has been cleared by &6" + command.getPlayer().getName()));
} }
} }
Bukkit.broadcastMessage(Style.main("Chat", "The chat has been cleared by &6" + command.getPlayer().getName()));
}
} }

@ -0,0 +1,26 @@
package zone.themcgamer.core.chat.command;
import lombok.AllArgsConstructor;
import org.bukkit.entity.Player;
import zone.themcgamer.core.chat.ChatManager;
import zone.themcgamer.core.command.Command;
import zone.themcgamer.core.command.CommandProvider;
import zone.themcgamer.core.common.Style;
import java.util.Map;
/**
* @author Braydon
*/
@AllArgsConstructor
public class EmotesCommand {
private final ChatManager chatManager;
@Command(name = "emotes", aliases = { "emote" }, description = "View chat emotes", playersOnly = true)
public void onCommand(CommandProvider command) {
Player player = command.getPlayer();
player.sendMessage(Style.main("Chat", "Chat Emotes:"));
for (Map.Entry<String, String> entry : chatManager.getEmotes().entrySet())
player.sendMessage(" §6" + entry.getValue() + " §7-> §b" + entry.getKey());
}
}

@ -36,12 +36,15 @@ public class KitManager extends MiniAccount<KitClient> {
if (client.isEmpty()) if (client.isEmpty())
return; return;
while (resultSet.next()) { while (resultSet.next()) {
MGZGame game = EnumUtils.fromString(MGZGame.class, resultSet.getString("game")); int gameColumnIndex = resultSet.findColumn("game");
if (gameColumnIndex == -1)
continue;
MGZGame game = EnumUtils.fromString(MGZGame.class, resultSet.getString(gameColumnIndex));
if (game == null) if (game == null)
return; continue;
KitDisplay kitDisplay = game.getKitDisplay(resultSet.getString("kit")); KitDisplay kitDisplay = game.getKitDisplay(resultSet.getString("kit"));
if (kitDisplay == null) if (kitDisplay == null)
return; continue;
client.get().getSelectedKit().put(game, kitDisplay); client.get().getSelectedKit().put(game, kitDisplay);
} }
} }

@ -24,7 +24,7 @@ public class ServerCommand {
private final ServerTraveler traveler; private final ServerTraveler traveler;
private final MinecraftServerRepository minecraftServerRepository; private final MinecraftServerRepository minecraftServerRepository;
@Command(name = "server", aliases = { "join", "play" }, description = "Join a server", playersOnly = true) @Command(name = "server", aliases = { "sv", "join", "play" }, description = "Join a server", playersOnly = true)
public void onCommand(CommandProvider command) { public void onCommand(CommandProvider command) {
Player player = command.getPlayer(); Player player = command.getPlayer();
String[] args = command.getArgs(); String[] args = command.getArgs();

@ -11,7 +11,6 @@ import zone.themcgamer.core.chat.component.impl.BasicRankComponent;
import zone.themcgamer.core.common.MathUtils; import zone.themcgamer.core.common.MathUtils;
import zone.themcgamer.core.common.scoreboard.ScoreboardHandler; import zone.themcgamer.core.common.scoreboard.ScoreboardHandler;
import zone.themcgamer.core.deliveryMan.DeliveryManManager; import zone.themcgamer.core.deliveryMan.DeliveryManManager;
import zone.themcgamer.core.game.kit.KitManager;
import zone.themcgamer.core.kingdom.KingdomManager; import zone.themcgamer.core.kingdom.KingdomManager;
import zone.themcgamer.core.plugin.MGZPlugin; import zone.themcgamer.core.plugin.MGZPlugin;
import zone.themcgamer.core.plugin.Startup; import zone.themcgamer.core.plugin.Startup;
@ -44,7 +43,7 @@ public class Hub extends MGZPlugin {
spawn.setYaw(MathUtils.getFacingYaw(spawn, world.getDataPoints("LOOK_AT"))); spawn.setYaw(MathUtils.getFacingYaw(spawn, world.getDataPoints("LOOK_AT")));
else spawn = new Location(world.getWorld(), 0, 150, 0); else spawn = new Location(world.getWorld(), 0, 150, 0);
AccountManager.addMiniAccount(new KitManager(this)); //AccountManager.addMiniAccount(new KitManager(this));
new PlayerListener(this); new PlayerListener(this);
new WorldListener(this); new WorldListener(this);