implement tag tools and check tools

This commit is contained in:
Hiajen Hiajen 2021-06-20 16:32:54 +02:00
parent 07dd923927
commit 2577a71ca7
10 changed files with 252 additions and 50 deletions

View file

@ -22,7 +22,13 @@ dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
include group: 'org.postgresql', name: 'postgresql', version: '42.2.9'
//include group: 'org.postgresql', name: 'postgresql', version: '42.2.9'
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
//implementation(include('mysql:mysql-connector-java:8.0.25'))
include group: 'mysql', name: 'mysql-connector-java', version: '8.0.25'
implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.25'
//modImplementation(group: 'mysql', name: 'mysql-connector-java', version: '8.0.25')
// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

View file

@ -24,6 +24,7 @@ public final class EaaMod implements ClientModInitializer {
//Register Commands
new EchoCommand();
new TagCommand();
new CheckPlayerCommand();
}
}

View file

@ -0,0 +1,69 @@
package net.saltymc.eaa.commands;
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.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 net.saltymc.eaa.util.mojangApi.PlayerInfo;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
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 CheckPlayerCommand extends EaaModCommand{
private static final Map<String, DB_Tag> player_tags = new HashMap<>();
public static final SimpleDateFormat dateformat = new SimpleDateFormat("dd.MM.yyyy");
@Override
public int run(CommandContext<FabricClientCommandSource> context) {
FabricClientCommandSource source = context.getSource();
String player = StringArgumentType.getString(context,"player");
try {
String playerUUID = MinecraftClient.getInstance().getNetworkHandler().getPlayerListEntry(player).getProfile().getId().toString();
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(Text.of(
dateformat.format(curr.getTagDate()) + " | " + curr.getType().getTag() + " (" + curr.getGrade() + ")"
));
}
} else {
source.sendFeedback(Text.of("Nothing to appeal"));
}
} catch (Exception e) {
source.sendFeedback(Text.of(e.toString()));
}
return 1;
}
@Override
public LiteralArgumentBuilder<FabricClientCommandSource> getCommandSpecification() {
return literal("/check")
.then(
argument("player", StringArgumentType.word())
.suggests((ctx, builder) -> EntityArgumentType.player().listSuggestions(ctx, builder))
.executes(this));
}
}

View file

@ -1,24 +1,29 @@
package net.saltymc.eaa.commands;
import com.mojang.brigadier.Message;
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.custom.ping.PingColors;
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;
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<>();
@Override
public int run(CommandContext<FabricClientCommandSource> context) {
FabricClientCommandSource source = context.getSource();
@ -28,17 +33,18 @@ public class TagCommand extends EaaModCommand{
int grade = IntegerArgumentType.getInteger(context, "grade");
try {
String playerUUID = PlayerInfo.playerNameToUUID(player);
String playerUUID = MinecraftClient.getInstance().getNetworkHandler().getPlayerListEntry(player).getProfile().getId().toString();
DB_Player db_player = DB_Player.getPlayer(playerUUID);
DB_Player db_player = DB_Player.getPlayer(playerUUID, true);
new DB_Tag(db_player, tag, grade).put();
} catch (Exception e) {
source.sendFeedback(Text.of(e.toString()));
}
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;
}
@ -61,17 +67,30 @@ public class TagCommand extends EaaModCommand{
)));
}
public static CustomText getScoreboardTag(String uuid){
public static void loadPlayer(String uuid, boolean reload){
if (!player_tags.containsKey(uuid) || reload){
DB_Player db_player = DB_Player.getPlayer(uuid);
if (db_player == null)
return;
return new CustomText(Text.of("[NaN]"), PingColors.COLOR_GREY);
List<DB_Tag> tag = DB_Tag.getTagsFromPlayer(db_player);
if (tag.size() > 0)
player_tags.put(uuid, tag.get(0));
}
}
public static DB_Tag.Type getScoreboardTag(String uuid){
loadPlayer(uuid, false);
if (player_tags.containsKey(uuid))
return player_tags.get(uuid).getType();
else
return DB_Tag.Type.NOTTAGGED;
}
public static class CustomText {
private final Text text;
private final int color;
public CustomText(Text text, int color) {
this.text = text;
this.color = color;

View file

@ -27,6 +27,7 @@ import net.minecraft.text.OrderedText;
import net.minecraft.text.Text;
import net.minecraft.world.GameMode;
import net.saltymc.eaa.commands.TagCommand;
import net.saltymc.eaa.util.database.DB_Tag;
/**
* By: https://github.com/vladmarica/better-ping-display-fabric/
@ -160,10 +161,13 @@ public final class CustomPlayerListHud {
aa += 9;
}
/*
* PLAYER TAG
*/
int offset_tag_playername = aa;
TagCommand.CustomText playerTAG = TagCommand.getScoreboardTag(player.getProfile().getId().toString());
mc.textRenderer.drawWithShadow(stack, playerTAG.getText(), (float)aa, (float)ab, playerTAG.getColor());
offset_tag_playername += mc.textRenderer.getWidth(playerTAG.getText().asOrderedText()) + 2;
DB_Tag.Type playerTAG = TagCommand.getScoreboardTag(player.getProfile().getId().toString());
mc.textRenderer.drawWithShadow(stack, playerTAG.getTag(), (float)aa, (float)ab, playerTAG.getColor());
offset_tag_playername += mc.textRenderer.getWidth(playerTAG.getTag()) + 2;
Text playerName = hud.getPlayerName(player);

View file

@ -33,7 +33,7 @@ public class DB_Player {
try {
PreparedStatement statement;
if (player.id != -1) {
statement = Postgre.getInstance().connection.prepareStatement("UPDATE " + tableName + " SET id = ?, UUID = ? WHERE id = ?;");
statement = Postgre.getStatement("UPDATE " + tableName + " SET id = ?, UUID = ? WHERE id = ?;");
statement.setInt(1, player.id);
statement.setString(2, player.uuid);
@ -41,13 +41,13 @@ public class DB_Player {
statement.setInt(3, player.id);
} else {
statement = Postgre.getInstance().connection.prepareStatement("INSERT INTO " + tableName + " (UUID) " +
statement = Postgre.getStatement("INSERT INTO " + tableName + " (UUID) " +
"VALUES(?);");
statement.setString(1, player.uuid);
}
statement.executeQuery();
statement.execute();
} catch (SQLException e){
throw new RuntimeException("Could not safe Player", e);
}
@ -56,7 +56,7 @@ public class DB_Player {
public static DB_Player getPlayer(int id){
try {
PreparedStatement statement = Postgre.getInstance().connection.prepareStatement("SELECT * FROM " + tableName + " WHERE id = ?");
PreparedStatement statement = Postgre.getStatement("SELECT * FROM " + tableName + " WHERE id = ?");
statement.setInt(1, id);
ResultSet rs = statement.executeQuery();
rs.next();
@ -67,19 +67,25 @@ public class DB_Player {
}
public static DB_Player getPlayer(String uuid){
return getPlayer(uuid, false);
}
public static DB_Player getPlayer(String uuid, boolean createIfNull){
try {
PreparedStatement statement = Postgre.getInstance().connection.prepareStatement("SELECT * FROM Player WHERE UUID = ?");
PreparedStatement statement = Postgre.getStatement("SELECT * FROM Player WHERE UUID = ?");
statement.setString(1, uuid);
ResultSet rs = statement.executeQuery();
if (rs.next()){
return new DB_Player(rs.getInt("id"), rs.getString("UUID"));
} else {
} else if (createIfNull){
//unknown Player - create new
DB_Player db_player = new DB_Player(uuid);
db_player.put();
return getPlayer(uuid);
} else {
return null;
}
} catch (SQLException e){
throw new RuntimeException("Could not get Player from Database!", e);
@ -101,4 +107,9 @@ public class DB_Player {
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String toString(){
return "{ \"id\" : " + id + "," +
"\"uuid\" :" + uuid + "}";
}
}

View file

@ -1,5 +1,7 @@
package net.saltymc.eaa.util.database;
import net.saltymc.eaa.custom.ping.PingColors;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -34,7 +36,7 @@ public class DB_Tag {
public static void putTag(DB_Tag tag){
try {
PreparedStatement statement = Postgre.getInstance().connect().prepareStatement(
PreparedStatement statement = Postgre.getStatement(
"INSERT INTO " + tableName + " (Player_ID, Grade, TagType, TagDate) " +
"VALUES (?, ?, ?, ?);");
@ -42,14 +44,19 @@ public class DB_Tag {
statement.setInt(2,tag.grade);
statement.setString(3, tag.type.name());
statement.setDate(4 , new java.sql.Date(new Date().getTime()));
statement.execute();
} catch (SQLException e){
throw new RuntimeException("Could not save Tag!", e);
}
}
public static List<DB_Tag> getTagsFromPlayer(DB_Player player){
if (player == null)
return new ArrayList<>();
try {
PreparedStatement statement = Postgre.getInstance().connect().prepareStatement("SELECT * FROM " + tableName + " WHERE Player_ID = ? ORDER BY TagDate ASC;");
PreparedStatement statement = Postgre.getStatement("SELECT * FROM " + tableName + " WHERE Player_ID = ? ORDER BY TagDate ASC;");
statement.setInt(1, player.getId());
ResultSet rs = statement.executeQuery();
@ -71,7 +78,55 @@ public class DB_Tag {
}
}
public DB_Player getPlayer() {
return player;
}
public int getGrade() {
return grade;
}
public Type getType() {
return type;
}
public Date getTagDate() {
return tagDate;
}
public enum Type {
HACKER, FRIENDLY, GOODPLAYER, IDIOT
HACKER(
"HAX", "PLayer who hacks", PingColors.COLOR_END
), FRIENDLY(
"FND", "Player you like", PingColors.COLOR_START
), GOODPLAYER(
"PRO", "Fair player with good skill", PingColors.COLOR_START
), IDIOT(
"BAD", "Person who is annoying and bad", PingColors.COLOR_MID
), NOTTAGGED(
"NaN", "Player is not tagged", PingColors.COLOR_GREY
);
String tag, description;
int color;
Type(String tag, String description, int color){
this.tag = tag;
this.description = description;
this.color = color;
}
public String getTag() {
return tag;
}
public String getDescription() {
return description;
}
public int getColor() {
return color;
}
}
}

View file

@ -1,8 +1,9 @@
package net.saltymc.eaa.util.database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import net.saltymc.eaa.EaaMod;
import java.sql.*;
import com.mysql.cj.jdbc.Driver;
public class Postgre {
@ -11,11 +12,24 @@ public class Postgre {
private final String password = "root";
private static Postgre postgre;
public Connection connection;
private Connection connection;
public Postgre() {
this.connection = connect();
try {
//load driver / config driver
EaaMod.LOGGER.debug("Lade DB Treiber");
new Driver();
connection = DriverManager.getConnection("jdbc:" + url + db_name , user, password);
} catch (SQLException e){
EaaMod.LOGGER.error(e.getMessage());
e.printStackTrace();
}
}
static Postgre getInstance(){
if (postgre == null)
@ -24,31 +38,52 @@ public class Postgre {
return postgre;
}
static PreparedStatement getStatement(String statement) throws SQLException{
return getInstance().connection.prepareStatement(statement);
}
/**
* Connect to the PostgreSQL database
*
* @return a Connection object
* closes current connection and reload it
*/
public Connection connect() {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, password);
Class.forName("org.postgresql.Driver");
private void reload(){
cleanUp();
if (conn != null) {
System.out.println("Connected to the PostgreSQL server successfully.");
} else {
System.out.println("Failed to make connection!");
try {
//load driver / config driver
EaaMod.LOGGER.debug("Lade DB Treiber");
Class driver_class = Class.forName(driverName);
Driver dbdriver = (Driver) driver_class.newInstance();
DriverManager.registerDriver(dbdriver);
try {
EaaMod.LOGGER.debug("Create connection to Database");
//Create connection
connection = DriverManager.getConnection("jdbc:" + url + db_name , user, password);
} catch (SQLException e) {
EaaMod.LOGGER.error(e.getMessage());
}
} catch (Exception e){
EaaMod.LOGGER.error(e.getMessage());
}
}
/**
* Inspired by https://www.vogella.com/tutorials/MySQLJava/article.html
*
* CleanUp of SQL connection
*/
public void cleanUp(){
EaaMod.LOGGER.debug("Close database connection");
try {
connection.setAutoCommit(true);
if (connection != null)
connection.close();
connection = null;
} catch(SQLException e){
System.out.println(e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println("PostgreSQL JDBC driver not found.");
e.printStackTrace();
}
return conn;
}
}

View file

@ -3,6 +3,7 @@ package net.saltymc.eaa.util.mojangApi;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.minecraft.client.network.PlayerListEntry;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
@ -14,6 +15,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class PlayerInfo {