some performance shit #2 and loading non present players, Refactoring project

This commit is contained in:
Hiajen Hiajen 2021-06-21 19:02:28 +02:00
parent 8fea7f4701
commit 9af6f4c073
12 changed files with 339 additions and 200 deletions

View file

@ -0,0 +1,82 @@
package net.saltymc.eaa.function;
import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.toast.SystemToast;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.saltymc.eaa.util.database.DB_Player;
import net.saltymc.eaa.util.database.DB_Tag;
import net.saltymc.eaa.util.mojangApi.PlayerInfo;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.ListIterator;
public class CheckFunction {
public static final SimpleDateFormat dateformat = new SimpleDateFormat("dd.MM.yyyy");
public static void checkPlayer(String player, FabricClientCommandSource source){
new Thread(new Check(player, source)).start();
}
private static class Check implements Runnable {
String player;
FabricClientCommandSource source;
public Check(String player, FabricClientCommandSource source) {
this.player = player;
this.source = source;
}
@Override
public void run() {
try {
String playerUUID = PlayerInfo.playerNameToUUID(player);
if (playerUUID == null){
SystemToast.add(MinecraftClient.getInstance().getToastManager(), SystemToast.Type.WORLD_ACCESS_FAILURE,
Text.of("ERROR!"), Text.of("Player does not exist"));
return;
}
TagFunction.loadPlayer(playerUUID, true);
List<String> names = PlayerInfo.playerUUIDToNames(playerUUID);
for (int i = 1; i <= names.size(); i++)
source.sendFeedback(Text.of(i + ". " + names.get(i-1)));
List<DB_Tag> tags = DB_Tag.getTagsFromPlayer(DB_Player.getPlayer(playerUUID));
if (tags.size() > 0) {
ListIterator<DB_Tag> it = tags.listIterator(tags.size());
while (it.hasPrevious()) {
DB_Tag curr = it.previous();
source.sendFeedback(new LiteralText(dateformat.format(curr.getTagDate())).formatted(Formatting.GRAY)
.append(new LiteralText(" | "))
.append(new LiteralText(curr.getType().getTag()).formatted(curr.getType().getFormatting()))
.append(new LiteralText(" (" + curr.getGrade() + ") ").formatted(getScaleFormat(curr.getGrade())))
);
}
} else {
source.sendFeedback(Text.of("No Tags Set!"));
}
} catch (Exception e) {
source.sendFeedback(Text.of(e.toString()));
}
}
}
private static Formatting getScaleFormat(int grade){
if (grade <= 5)
return Formatting.WHITE;
else if (grade < 9)
return Formatting.DARK_AQUA;
else
return Formatting.BLUE;
}
}

View file

@ -0,0 +1,63 @@
package net.saltymc.eaa.function;
import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.toast.SystemToast;
import net.minecraft.text.Text;
import net.saltymc.eaa.util.database.DB_Tag;
import java.util.ArrayList;
import java.util.List;
public class LobbyFunction {
public static void checkLobby(FabricClientCommandSource source){
new Thread(new CheckLobby(source)).start();
}
private static class CheckLobby implements Runnable {
FabricClientCommandSource source;
public CheckLobby(FabricClientCommandSource source) {
this.source = source;
}
@Override
public void run() {
try {
List<String> uuids = new ArrayList<>();
MinecraftClient.getInstance().getNetworkHandler().getPlayerList().forEach(x -> uuids.add(x.getProfile().getId().toString()));
int dangerLvl = 0; // 0 = nothing; 1 = idiot; 2 = hacker
for (String uuid: uuids){
TagFunction.loadPlayer(uuid, false);
if (TagFunction.getHashMap().containsKey(uuid)){
DB_Tag.Type type = TagFunction.getHashMap().get(uuid).getType();
if (type == DB_Tag.Type.HACKER){
dangerLvl = 2;
break;
} else if (type == DB_Tag.Type.IDIOT){
dangerLvl = 1;
}
}
}
switch (dangerLvl){
case 0:
SystemToast.add(source.getClient().getToastManager(), SystemToast.Type.WORLD_BACKUP, Text.of("OKAY!"), Text.of("No Hackers found"));
break;
case 1:
SystemToast.add(source.getClient().getToastManager(), SystemToast.Type.TUTORIAL_HINT, Text.of("MEH!"), Text.of("There is an Idiot!"));
break;
case 2:
SystemToast.add(source.getClient().getToastManager(), SystemToast.Type.WORLD_ACCESS_FAILURE, Text.of("ALARM!"), Text.of("There is a Hacker!"));
}
} catch (Exception e) {
source.sendFeedback(Text.of(e.toString()));
}
}
}
}

View file

@ -0,0 +1,125 @@
package net.saltymc.eaa.function;
import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.toast.SystemToast;
import net.minecraft.text.Text;
import net.saltymc.eaa.util.database.DB_Player;
import net.saltymc.eaa.util.database.DB_Tag;
import net.saltymc.eaa.util.mojangApi.PlayerInfo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TagFunction {
private static final Map<String, DB_Tag> player_tags = new HashMap<>();
private static volatile boolean free = true;
public static void loadPlayer(String uuid, boolean reload){
if ( (!player_tags.containsKey(uuid) || reload)) {
new Thread(new LoadTag(uuid)).start();
}
}
public static DB_Tag.Type getScoreboardTag(String uuid){
if (player_tags.containsKey(uuid))
return player_tags.get(uuid).getType();
else {
if (free) {
free = false;
loadPlayer(uuid, false);
}
}
return DB_Tag.Type.NOTLOADED;
}
public static void tagPlayer(String player, DB_Tag.Type tag, int grade, FabricClientCommandSource source){
new Thread(new TagPlayer(player, tag, grade, source)).start();
}
/*
*
* GETTER SETTER
*
*/
public static Map<String, DB_Tag> getHashMap(){
return player_tags;
}
/*
*
* THREADS
*
*/
/**
* Thread for Tagging a Player
*/
private static class TagPlayer implements Runnable {
String player;
DB_Tag.Type tag;
int grade;
FabricClientCommandSource source;
public TagPlayer(String player, DB_Tag.Type tag, int grade, FabricClientCommandSource source) {
this.player = player;
this.tag = tag;
this.grade = grade;
this.source = source;
}
@Override
public void run() {
try {
String playerUUID = PlayerInfo.playerNameToUUID(player);
if (playerUUID != null) {
DB_Player db_player = DB_Player.getPlayer(playerUUID, true);
new DB_Tag(db_player, tag, grade).put();
loadPlayer(playerUUID, true);
SystemToast.add(source.getClient().getToastManager(), SystemToast.Type.TUTORIAL_HINT,
Text.of("Player Tagged"), Text.of(player + " | " + tag.name() + " | " + grade));
} else {
SystemToast.add(source.getClient().getToastManager(), SystemToast.Type.WORLD_ACCESS_FAILURE,
Text.of("ERROR!"), Text.of("Player does not exist"));
}
} catch (Exception e) {
source.sendFeedback(Text.of(e.toString()));
}
}
}
/**
* Thread for loading a tag of a player in to hashmap
*/
private static class LoadTag implements Runnable {
String uuid;
public LoadTag(String uuid){
this.uuid = uuid;
}
@Override
public void run() {
DB_Player db_player = DB_Player.getPlayer(uuid);
if (db_player == null) {
player_tags.put(uuid, new DB_Tag(null, DB_Tag.Type.NOTTAGGED, 0));
} else {
List<DB_Tag> tag = DB_Tag.getTagsFromPlayer(db_player);
if (tag.size() > 0)
player_tags.put(uuid, tag.get(tag.size() - 1));
}
free = true;
}
}
}