to Database stuff

This commit is contained in:
Hiajen Hiajen 2021-06-12 18:00:44 +02:00
parent 810ce8adb9
commit 6587d3aff0
5 changed files with 264 additions and 15 deletions

View file

@ -22,6 +22,7 @@ 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'
// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

View file

@ -1,5 +1,6 @@
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;
@ -9,6 +10,8 @@ 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 net.saltymc.eaa.util.mojangApi.PlayerInfo;
import static net.fabricmc.fabric.api.client.command.v1.ClientCommandManager.argument;
@ -16,35 +19,45 @@ import static net.fabricmc.fabric.api.client.command.v1.ClientCommandManager.lit
public class TagCommand extends EaaModCommand{
@Override
public int run(CommandContext<FabricClientCommandSource> context) throws CommandSyntaxException {
public int run(CommandContext<FabricClientCommandSource> context) {
FabricClientCommandSource source = context.getSource();
String player = StringArgumentType.getString(context,"player");
String tag = StringArgumentType.getString(context,"tag");
DB_Tag.Type tag = DB_Tag.Type.valueOf(StringArgumentType.getString(context,"tag"));
int grade = IntegerArgumentType.getInteger(context, "grade");
SystemToast.add(source.getClient().getToastManager(), SystemToast.Type.TUTORIAL_HINT, Text.of(tag), Text.of(player));
Text text = null;
try {
String playerUUID = PlayerInfo.playerNameToUUID(player);
String playerName = PlayerInfo.playerUUIDToCurrentName(playerUUID);
text = Text.of("You tagged player " + playerName + " : " + playerUUID);
DB_Player db_player = DB_Player.getPlayer(playerUUID);
new DB_Tag(db_player, tag, grade).put();
} catch (Exception e) {
text = Text.of(e.toString());
source.sendFeedback(Text.of(e.toString()));
}
source.sendFeedback(text);
SystemToast.add(source.getClient().getToastManager(), SystemToast.Type.TUTORIAL_HINT, Text.of("Player Tagged"),
Text.of(player + " | " + tag.name() + " | " + grade));
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())
.executes(this)
));
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)
)));
}
}

View file

@ -0,0 +1,104 @@
package net.saltymc.eaa.util.database;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DB_Player {
public static final String tableName = "Player";
int id = -1;
String uuid;
public DB_Player(String uuid){
this.uuid = uuid;
}
private DB_Player(int id, String uuid) {
this.id = id;
this.uuid = uuid;
}
public void put() {
putPlayer(this);
}
public DB_Player get(){
return getPlayer(this.uuid);
}
public static void putPlayer(DB_Player player) {
try {
PreparedStatement statement;
if (player.id != -1) {
statement = Postgre.getInstance().connection.prepareStatement("UPDATE " + tableName + " SET id = ?, UUID = ? WHERE id = ?;");
statement.setInt(1, player.id);
statement.setString(2, player.uuid);
statement.setInt(3, player.id);
} else {
statement = Postgre.getInstance().connection.prepareStatement("INSERT INTO " + tableName + " (UUID) " +
"VALUES(?);");
statement.setString(1, player.uuid);
}
statement.executeQuery();
} catch (SQLException e){
throw new RuntimeException("Could not safe Player", e);
}
}
public static DB_Player getPlayer(int id){
try {
PreparedStatement statement = Postgre.getInstance().connection.prepareStatement("SELECT * FROM " + tableName + " WHERE id = ?");
statement.setInt(1, id);
ResultSet rs = statement.executeQuery();
rs.next();
return new DB_Player(rs.getInt("id"), rs.getString("UUID"));
} catch (SQLException e){
throw new RuntimeException("Could not put Player into Database!", e);
}
}
public static DB_Player getPlayer(String uuid){
try {
PreparedStatement statement = Postgre.getInstance().connection.prepareStatement("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 {
//unknown Player - create new
DB_Player db_player = new DB_Player(uuid);
db_player.put();
return getPlayer(uuid);
}
} catch (SQLException e){
throw new RuntimeException("Could not get Player from Database!", e);
}
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
}

View file

@ -0,0 +1,77 @@
package net.saltymc.eaa.util.database;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class DB_Tag {
public static final String tableName = "Tag";
int id;
DB_Player player;
int grade;
Type type;
Date tagDate;
public DB_Tag(DB_Player db_player){
this.player = db_player;
}
public DB_Tag(DB_Player db_player, Type type, int grade){
this.player = db_player;
this.type = type;
this.grade = grade;
}
public void put(){
putTag(this);
}
public static void putTag(DB_Tag tag){
try {
PreparedStatement statement = Postgre.getInstance().connect().prepareStatement(
"INSERT INTO " + tableName + " (Player_ID, Grade, TagType, TagDate) " +
"VALUES (?, ?, ?, ?);");
statement.setInt(1, tag.player.getId());
statement.setInt(2,tag.grade);
statement.setString(3, tag.type.name());
statement.setDate(4 , new java.sql.Date(new Date().getTime()));
} catch (SQLException e){
throw new RuntimeException("Could not save Tag!", e);
}
}
public static List<DB_Tag> getTagsFromPlayer(DB_Player player){
try {
PreparedStatement statement = Postgre.getInstance().connect().prepareStatement("SELECT * FROM " + tableName + " WHERE Player_ID = ? ORDER BY TagDate ASC;");
statement.setInt(1, player.getId());
ResultSet rs = statement.executeQuery();
List<DB_Tag> ret = new ArrayList<>();
while (rs.next()){
DB_Tag tmp = new DB_Tag(player);
tmp.id = rs.getInt("ID");
tmp.grade = rs.getInt("Grade");
tmp.type = Type.valueOf(rs.getString("TagType"));
tmp.tagDate = rs.getDate("TagDate");
ret.add(tmp);
}
return ret;
} catch (SQLException e){
throw new RuntimeException("Error getting Tag!", e);
}
}
public enum Type {
HACKER, FRIENDLY, GOODPLAYER, IDIOT
}
}

View file

@ -0,0 +1,54 @@
package net.saltymc.eaa.util.database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Postgre {
private final String url = "jdbc:postgresql://localhost/myDB";
private final String user = "postgres";
private final String password = "root";
private static Postgre postgre;
public Connection connection;
public Postgre(){
this.connection = connect();
}
static Postgre getInstance(){
if (postgre == null)
postgre = new Postgre();
return postgre;
}
/**
* Connect to the PostgreSQL database
*
* @return a Connection object
*/
public Connection connect() {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, password);
Class.forName("org.postgresql.Driver");
if (conn != null) {
System.out.println("Connected to the PostgreSQL server successfully.");
} else {
System.out.println("Failed to make connection!");
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println("PostgreSQL JDBC driver not found.");
e.printStackTrace();
}
return conn;
}
}