134 lines
4.6 KiB
Java
134 lines
4.6 KiB
Java
package net.saltymc.eaa.commands;
|
|
|
|
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
|
import com.mojang.brigadier.arguments.StringArgumentType;
|
|
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
|
import com.mojang.brigadier.context.CommandContext;
|
|
import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;
|
|
import net.minecraft.client.MinecraftClient;
|
|
import net.minecraft.client.toast.SystemToast;
|
|
import net.minecraft.command.argument.EntityArgumentType;
|
|
import net.minecraft.text.Text;
|
|
|
|
import net.saltymc.eaa.util.database.DB_Player;
|
|
import net.saltymc.eaa.util.database.DB_Tag;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import static net.fabricmc.fabric.api.client.command.v1.ClientCommandManager.argument;
|
|
import static net.fabricmc.fabric.api.client.command.v1.ClientCommandManager.literal;
|
|
|
|
public class TagCommand extends EaaModCommand{
|
|
|
|
private static final Map<String, DB_Tag> player_tags = new HashMap<>();
|
|
private static volatile boolean free = true;
|
|
|
|
@Override
|
|
public int run(CommandContext<FabricClientCommandSource> context) {
|
|
FabricClientCommandSource source = context.getSource();
|
|
|
|
String player = StringArgumentType.getString(context,"player");
|
|
DB_Tag.Type tag = DB_Tag.Type.valueOf(StringArgumentType.getString(context,"tag"));
|
|
int grade = IntegerArgumentType.getInteger(context, "grade");
|
|
|
|
try {
|
|
String playerUUID = MinecraftClient.getInstance().getNetworkHandler().getPlayerListEntry(player).getProfile().getId().toString();
|
|
|
|
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));
|
|
} catch (Exception e) {
|
|
source.sendFeedback(Text.of(e.toString()));
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
@Override
|
|
public LiteralArgumentBuilder<FabricClientCommandSource> getCommandSpecification() {
|
|
return literal("/tag")
|
|
.then(
|
|
argument("player", StringArgumentType.word())
|
|
.suggests((ctx, builder) -> EntityArgumentType.player().listSuggestions(ctx, builder))
|
|
.then(argument("tag", StringArgumentType.word())
|
|
.suggests(((context, builder) -> {
|
|
for (DB_Tag.Type tag : DB_Tag.Type.values())
|
|
builder.suggest(tag.name());
|
|
return builder.buildFuture();
|
|
}))
|
|
.then(
|
|
argument("grade", IntegerArgumentType.integer(0,10))
|
|
.executes(this)
|
|
)));
|
|
}
|
|
|
|
public static void loadPlayer(String uuid, boolean reload){
|
|
if ( (!player_tags.containsKey(uuid) || reload)) {
|
|
new Thread(new LoadTag(uuid)).start();
|
|
}
|
|
}
|
|
|
|
public static Map<String, DB_Tag> getHashMap(){
|
|
return player_tags;
|
|
}
|
|
|
|
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 class CustomText {
|
|
private final Text text;
|
|
private final int color;
|
|
|
|
public CustomText(Text text, int color) {
|
|
this.text = text;
|
|
this.color = color;
|
|
}
|
|
|
|
public Text getText() {
|
|
return text;
|
|
}
|
|
|
|
public int getColor() {
|
|
return color;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|