From 52f56e55dc1310702fe5abb27f0f6e24b3e38ed6 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 19 Mar 2019 19:48:39 +0100 Subject: [PATCH] Full Core MySQL API --- src/de/warking/hunjy/MySQL/Bauwelt.java | 84 ++++++++++ src/de/warking/hunjy/MySQL/BauweltMember.java | 147 ++++++++++++++++++ src/de/warking/hunjy/MySQL/Schematic.java | 23 +-- .../warking/hunjy/MySQL/SchematicMember.java | 105 ++++++++++++- src/de/warking/hunjy/MySQL/UserGroup.java | 12 ++ src/de/warking/hunjy/MySQL/WarkingUser.java | 48 ++++-- .../PlayerManager/events/PlayerJoin.java | 19 +-- 7 files changed, 397 insertions(+), 41 deletions(-) create mode 100644 src/de/warking/hunjy/MySQL/Bauwelt.java create mode 100644 src/de/warking/hunjy/MySQL/BauweltMember.java create mode 100644 src/de/warking/hunjy/MySQL/UserGroup.java diff --git a/src/de/warking/hunjy/MySQL/Bauwelt.java b/src/de/warking/hunjy/MySQL/Bauwelt.java new file mode 100644 index 0000000..bf09846 --- /dev/null +++ b/src/de/warking/hunjy/MySQL/Bauwelt.java @@ -0,0 +1,84 @@ +package de.warking.hunjy.MySQL; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.UUID; + +import static de.warking.hunjy.Core.sql; + +public class Bauwelt { + + private final int UserID; + private boolean TNTDmg; + private boolean FireDmg; + + private Bauwelt(int userID, boolean tntDmg, boolean fireDmg, boolean updateDB){ + UserID = userID; + TNTDmg = tntDmg; + FireDmg = fireDmg; + if(updateDB) + updateDB(); + } + + public Bauwelt(int userID, boolean tntDmg, boolean fireDmg){ + this(userID, tntDmg, fireDmg, true); + } + + public Bauwelt(UUID userID, boolean tntDmg, boolean fireDmg){ + this(WarkingUser.get(userID).getId(), tntDmg, fireDmg, true); + } + + private void updateDB(){ + sql.update("INSERT INTO Bauwelt" + + " (UserID, TNTDmg, FireDmg)" + + " VALUES" + + " ('" + UserID + "', '" + TNTDmg + "', '" + FireDmg + "')" + + " ON DUPLICATE KEY UPDATE" + + " TNTDmg = VALUES(TNTDmg), FireDmg = VALUES(FireDmg)"); + } + + public static Bauwelt getBauwelt(UUID user){ + return getBauwelt(WarkingUser.get(user).getId()); + } + + public static Bauwelt getBauwelt(int userId){ + ResultSet bauwelt = sql.select("SELECT * FROM Schematic WHERE UserID = '" + userId + "'"); + try { + if(bauwelt == null || !bauwelt.next()){ + return null; + } + boolean tntDmg = bauwelt.getBoolean("TNTDmg"); + boolean fireDmg = bauwelt.getBoolean("FireDmg"); + return new Bauwelt(userId, tntDmg, fireDmg, false); + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + public int getUserID() { + return UserID; + } + + public UUID getUUID(){ + return WarkingUser.get(UserID).getUUID(); + } + + public boolean isTNTDmg() { + return TNTDmg; + } + + public void setTNTDmg(boolean TNTDmg) { + this.TNTDmg = TNTDmg; + updateDB(); + } + + public boolean isFireDmg() { + return FireDmg; + } + + public void setFireDmg(boolean fireDmg) { + FireDmg = fireDmg; + updateDB(); + } +} diff --git a/src/de/warking/hunjy/MySQL/BauweltMember.java b/src/de/warking/hunjy/MySQL/BauweltMember.java new file mode 100644 index 0000000..d082d6a --- /dev/null +++ b/src/de/warking/hunjy/MySQL/BauweltMember.java @@ -0,0 +1,147 @@ +package de.warking.hunjy.MySQL; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import static de.warking.hunjy.Core.sql; + +public class BauweltMember{ + private final int BauweltID; + private final int MemberID; + private boolean Build; + private boolean ChangeGM; + private boolean Teleport; + private boolean WorldEdit; + private boolean Testblock; + + private BauweltMember(int bauweltID, int memberID, boolean build, boolean changeGM, boolean teleport, boolean worldEdit, boolean testblock, boolean updateDB){ + BauweltID = bauweltID; + MemberID = memberID; + Build = build; + ChangeGM = changeGM; + Teleport = teleport; + WorldEdit = worldEdit; + Testblock = testblock; + if(updateDB) + updateDB(); + } + + public BauweltMember(int bauweltID, int memberID, boolean build, boolean changeGM, boolean teleport, boolean worldEdit, boolean testblock){ + this(bauweltID, memberID, build, changeGM, teleport, worldEdit, testblock, true); + } + + public void remove(){ + sql.update("DELETE FROM BauweltMember WHERE BauweltID = ? AND MemberID = ?"); + } + + private void updateDB(){ + sql.update("INSERT INTO BauweltMember" + + " (BauweltID, MemberID, Build, ChangeGM, Teleport, WorldEdit, Testblock)" + + " VALUES" + + " ('" + BauweltID + "', '" + MemberID + "', '" + Build + "', '" + ChangeGM + "', '" + Teleport + "', '" + WorldEdit + "', '" + Testblock + "')" + + " ON DUPLICATE KEY UPDATE" + + " Build = VALUES(Build), ChangeGM = VALUES(ChangeGM), Teleport = VALUES(Teleport), WorldEdit = VALUES(WorldEdit), Testblock = VALUES(Testblock)"); + } + + public static BauweltMember getBauMember(UUID bauweltID, UUID memberID){ + return getBauMember(WarkingUser.get(bauweltID).getId(), WarkingUser.get(memberID).getId()); + } + + public static BauweltMember getBauMember(int bauweltID, int memberID){ + ResultSet member = sql.select("SELECT * FROM BauweltMember WHERE BauweltID = '" + bauweltID + "' AND MemberID = '" + memberID + "'"); + try { + if(member == null || !member.next()){ + return null; + } + boolean build = member.getBoolean("Build"); + boolean changeGM = member.getBoolean("ChangeGM"); + boolean teleport = member.getBoolean("Teleport"); + boolean worldEdit = member.getBoolean("WorldEdit"); + boolean testblock = member.getBoolean("Testblock"); + return new BauweltMember(bauweltID, memberID, build, changeGM, teleport, worldEdit, testblock, false); + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + public static List getMembers(UUID bauweltID){ + return getMembers(WarkingUser.get(bauweltID).getId()); + } + + public static List getMembers(int bauweltID){ + try{ + ResultSet memberlist = sql.select("SELECT * FROM BauweltMember WHERE BauweltID = '" + bauweltID + "'"); + List members = new ArrayList<>(); + while(memberlist.next()){ + int memberID = memberlist.getInt("MemberID"); + boolean build = memberlist.getBoolean("Build"); + boolean changeGM = memberlist.getBoolean("ChangeGM"); + boolean teleport = memberlist.getBoolean("Teleport"); + boolean worldEdit = memberlist.getBoolean("WorldEdit"); + boolean testblock = memberlist.getBoolean("Testblock"); + members.add(new BauweltMember(bauweltID, memberID, build, changeGM, teleport, worldEdit, testblock, false)); + } + return members; + }catch(SQLException e){ + e.printStackTrace(); + } + return null; + } + + public int getBauweltID() { + return BauweltID; + } + + public int getMemberID() { + return MemberID; + } + + public boolean isBuild() { + return Build; + } + + public void setBuild(boolean build) { + Build = build; + updateDB(); + } + + public boolean isChangeGM() { + return ChangeGM; + } + + public void setChangeGM(boolean changeGM) { + ChangeGM = changeGM; + updateDB(); + } + + public boolean isTeleport() { + return Teleport; + } + + public void setTeleport(boolean teleport) { + Teleport = teleport; + updateDB(); + } + + public boolean isWorldEdit() { + return WorldEdit; + } + + public void setWorldEdit(boolean worldEdit) { + WorldEdit = worldEdit; + updateDB(); + } + + public boolean isTestblock() { + return Testblock; + } + + public void setTestblock(boolean testblock) { + Testblock = testblock; + updateDB(); + } +} diff --git a/src/de/warking/hunjy/MySQL/Schematic.java b/src/de/warking/hunjy/MySQL/Schematic.java index 5d8fcde..361cc8b 100644 --- a/src/de/warking/hunjy/MySQL/Schematic.java +++ b/src/de/warking/hunjy/MySQL/Schematic.java @@ -10,8 +10,8 @@ import static de.warking.hunjy.Core.sql; public class Schematic { - private String SchemName; - private int SchemOwner; + private final String SchemName; + private final int SchemOwner; private String Item; private SchematicType SchemType; @@ -46,13 +46,12 @@ public class Schematic { } public static Schematic getSchemFromDB(String schemName, int schemOwner){ - ResultSet schematic = sql.select("SELECT * FROM Schematic WHERE SchemName = '" + schemName + "', SchemOwner = '" + schemOwner + "'"); + ResultSet schematic = sql.select("SELECT * FROM Schematic WHERE SchemName = '" + schemName + "' AND SchemOwner = '" + schemOwner + "'"); try { if(schematic == null || !schematic.next()){ - System.err.println("Fatal ERROR: Could not load schematic!"); return null; } - String item = schematic.getString("Item");; + String item = schematic.getString("Item"); SchematicType schemType = SchematicType.valueOf(schematic.getString("SchemType")); return new Schematic(schemName, schemOwner, item, schemType, false); } catch (SQLException e) { @@ -75,6 +74,10 @@ public class Schematic { SchematicType schemType = SchematicType.valueOf(schematic.getString("SchemType")); schematics.add(new Schematic(schemName, schemOwner, item, schemType, false)); } + List addedSchems = SchematicMember.getAccessibleSchems(schemOwner); + for(SchematicMember schem : addedSchems){ + schematics.add(getSchemFromDB(schem.getSchemName(), schem.getSchemOwner())); + } return schematics; }catch(SQLException e){ e.printStackTrace(); @@ -106,20 +109,10 @@ public class Schematic { return SchemName; } - public void setSchemName(String schemName) { - SchemName = schemName; - updateDB(); - } - public int getSchemOwner() { return SchemOwner; } - public void setSchemOwner(int schemOwner) { - SchemOwner = schemOwner; - updateDB(); - } - public String getItem() { return Item; } diff --git a/src/de/warking/hunjy/MySQL/SchematicMember.java b/src/de/warking/hunjy/MySQL/SchematicMember.java index 5d16331..5e3e99f 100644 --- a/src/de/warking/hunjy/MySQL/SchematicMember.java +++ b/src/de/warking/hunjy/MySQL/SchematicMember.java @@ -1,7 +1,106 @@ package de.warking.hunjy.MySQL; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import static de.warking.hunjy.Core.sql; + public class SchematicMember { - private int SchemOwner; - private String SchemName; - private int Member; + private final int SchemOwner; + private final String SchemName; + private final int Member; + + private SchematicMember(String schemName, int schemOwner, int schemMember, boolean updateDB){ + SchemOwner = schemOwner; + Member = schemMember; + SchemName = schemName; + if(updateDB) + updateDB(); + } + + public SchematicMember(String schemName, int schemOwner, int schemMember){ + this(schemName, schemOwner, schemMember, true); + } + + public SchematicMember(String schemName, UUID schemOwner, UUID schemMember){ + this(schemName, WarkingUser.get(schemOwner).getId(), WarkingUser.get(schemMember).getId(), true); + } + + private void updateDB(){ + sql.update("INSERT INTO SchemMember" + + " (SchemName, SchemOwner, Member)" + + " VALUES" + + " ('" + SchemName + "', '" + SchemOwner + "', '" + Member + "')"); + } + + public static SchematicMember getSchemMemberFromDB(String schemName, UUID schemOwner, UUID schemMember){ + return getSchemMemberFromDB(schemName, WarkingUser.get(schemOwner).getId(), WarkingUser.get(schemMember).getId()); + } + + public static SchematicMember getSchemMemberFromDB(String schemName, int schemOwner, int schemMember){ + ResultSet schematicMember = sql.select("SELECT * FROM SchematicMember WHERE SchemName = '" + schemName + "' AND SchemOwner = '" + schemOwner + "' AND Member = '" + schemMember + "'"); + try { + if(schematicMember == null || !schematicMember.next()){ + return null; + } + return new SchematicMember(schemName, schemOwner, schemMember, false); + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + public static List getSchemMembers(String schemName, UUID schemOwner){ + return getSchemMembers(schemName, WarkingUser.get(schemOwner).getId()); + } + + public static List getSchemMembers(String schemName, int schemOwner){ + ResultSet schematicMember = sql.select("SELECT * FROM SchematicMember WHERE SchemName = '" + schemName + "' AND SchemOwner = '" + schemOwner + "'"); + try { + List schematicMembers = new ArrayList<>(); + while(schematicMember.next()){ + int schemMember = schematicMember.getInt("Member"); + schematicMembers.add(new SchematicMember(schemName, schemOwner, schemMember, false)); + } + return schematicMembers; + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + public static List getAccessibleSchems(UUID schemMember){ + return getAccessibleSchems(WarkingUser.get(schemMember).getId()); + } + + public static List getAccessibleSchems(int schemMember){ + ResultSet schematicMember = sql.select("SELECT * FROM SchematicMember WHERE Member = '" + schemMember + "'"); + try { + List schematicMembers = new ArrayList<>(); + while(schematicMember.next()){ + String schemName = schematicMember.getString("SchemName"); + int schemOwner = schematicMember.getInt("SchemOwner"); + schematicMembers.add(new SchematicMember(schemName, schemOwner, schemMember, false)); + } + return schematicMembers; + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + public int getSchemOwner() { + return SchemOwner; + } + + public String getSchemName() { + return SchemName; + } + + public int getMember() { + return Member; + } } diff --git a/src/de/warking/hunjy/MySQL/UserGroup.java b/src/de/warking/hunjy/MySQL/UserGroup.java new file mode 100644 index 0000000..60150f2 --- /dev/null +++ b/src/de/warking/hunjy/MySQL/UserGroup.java @@ -0,0 +1,12 @@ +package de.warking.hunjy.MySQL; + +public enum UserGroup { + Admin, + Developer, + Moderator, + Supporter, + Builder, + Youtuber, + Premium, + Member +} diff --git a/src/de/warking/hunjy/MySQL/WarkingUser.java b/src/de/warking/hunjy/MySQL/WarkingUser.java index 05654eb..32af1f0 100644 --- a/src/de/warking/hunjy/MySQL/WarkingUser.java +++ b/src/de/warking/hunjy/MySQL/WarkingUser.java @@ -17,23 +17,30 @@ public class WarkingUser { private int id; private UUID UUID; private String UserName; - private String UserGroup; + private UserGroup UserGroup; - public WarkingUser(Player player){ - ResultSet dbplayer = sql.select("SELECT * FROM UserData WHERE UUID = '" + player.getUniqueId().toString() + "'"); + public WarkingUser(UUID uuid){ + ResultSet dbplayer = sql.select("SELECT * FROM UserData WHERE UUID = '" + uuid.toString() + "'"); + init(dbplayer); + } + + public WarkingUser(int id){ + ResultSet dbplayer = sql.select("SELECT * FROM UserData WHERE id = '" + id + "'"); + init(dbplayer); + } + + public WarkingUser(String name){ + ResultSet dbplayer = sql.select("SELECT * FROM UserData WHERE UserName = '" + name + "'"); init(dbplayer); } private void init(ResultSet dbplayer){ try { - if(dbplayer == null || !dbplayer.next()){ - System.err.println("Fatal ERROR: Could not load player!"); - return; - } + dbplayer.next(); id = dbplayer.getInt("id"); this.UUID = java.util.UUID.fromString(dbplayer.getString("UUID")); UserName = dbplayer.getString("UserName"); - UserGroup = dbplayer.getString("UserGroup"); + UserGroup = de.warking.hunjy.MySQL.UserGroup.valueOf(dbplayer.getString("UserGroup")); allUsers.add(this); } catch (SQLException e) { e.printStackTrace(); @@ -52,27 +59,40 @@ public class WarkingUser { return UserName; } - public String getUserGroup() { + public UserGroup getUserGroup() { return UserGroup; } public static WarkingUser get(String userName){ for(WarkingUser user : allUsers) - if(user.getUserName().equals(userName)) + if(user.UserName.equals(userName)) return user; - return null; + WarkingUser offline = new WarkingUser(userName); + allUsers.remove(offline); + return offline; } public static WarkingUser get(UUID uuid){ for(WarkingUser user : allUsers) - if(user.getUserName().equals(uuid)) + if(user.UserName.equals(uuid)) return user; - return null; + WarkingUser offline = new WarkingUser(uuid); + allUsers.remove(offline); + return offline; + } + + public static WarkingUser get(int id){ + for(WarkingUser user : allUsers) + if(user.id == id) + return user; + WarkingUser offline = new WarkingUser(id); + allUsers.remove(offline); + return offline; } public static void remove(Player player){ for(int i = 0; i < allUsers.size(); i++) - if(allUsers.get(i).getUUID().equals(player.getUniqueId())){ + if(allUsers.get(i).UUID.equals(player.getUniqueId())){ allUsers.remove(i); return; } diff --git a/src/de/warking/hunjy/PlayerManager/events/PlayerJoin.java b/src/de/warking/hunjy/PlayerManager/events/PlayerJoin.java index 50b323f..826d354 100644 --- a/src/de/warking/hunjy/PlayerManager/events/PlayerJoin.java +++ b/src/de/warking/hunjy/PlayerManager/events/PlayerJoin.java @@ -1,5 +1,6 @@ package de.warking.hunjy.PlayerManager.events; +import de.warking.hunjy.MySQL.UserGroup; import de.warking.hunjy.MySQL.WarkingUser; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -13,31 +14,31 @@ public class PlayerJoin implements Listener{ @EventHandler private void onJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); - WarkingUser user = new WarkingUser(player); + WarkingUser user = new WarkingUser(player.getUniqueId()); switch(user.getUserGroup()){ - case "Admin": + case Admin: player.setDisplayName("§4Admin " + player.getName() + "§r"); break; - case "Developer": + case Developer: player.setDisplayName("§3Developer " + player.getName() + "§r"); break; - case "Moderator": + case Moderator: player.setDisplayName("§bModerator " + player.getName() + "§r"); break; - case "Supporter": + case Supporter: player.setDisplayName("§1Supporter " + player.getName() + "§r"); break; - case "Builder": + case Builder: player.setDisplayName("§2Architekt " + player.getName() + "§r"); break; - case "Youtuber": + case Youtuber: player.setDisplayName("§5Youtuber " + player.getName() + "§r"); break; - case "Premium": + case Premium: player.setDisplayName("§6Premium " + player.getName() + "§r"); break; - case "Member": + case Member: default: player.setDisplayName("§7" + player.getName() + "§r"); break;