From 2b26254e8285385e9549d76b549c1b75c63933b7 Mon Sep 17 00:00:00 2001 From: zhangyuheng Date: Mon, 5 Feb 2024 11:43:51 +0800 Subject: [PATCH] finish dtos --- .../controllers/DominionController.java | 18 + .../lunadeer/dominion/dtos/DominionDTO.java | 752 ++++++++++++++++++ .../dtos/{DPlayer.java => PlayerDTO.java} | 37 +- .../dominion/dtos/PlayerPrivilegeDTO.java | 129 +++ .../dominion/dtos/PrivilegeTemplateDTO.java | 532 +++++++++++++ .../dominion/utils/ConfigManager.java | 2 +- .../cn/lunadeer/dominion/utils/Database.java | 13 +- src/main/resources/config.yml | 8 + 8 files changed, 1464 insertions(+), 27 deletions(-) create mode 100644 src/main/java/cn/lunadeer/dominion/controllers/DominionController.java create mode 100644 src/main/java/cn/lunadeer/dominion/dtos/DominionDTO.java rename src/main/java/cn/lunadeer/dominion/dtos/{DPlayer.java => PlayerDTO.java} (74%) create mode 100644 src/main/java/cn/lunadeer/dominion/dtos/PlayerPrivilegeDTO.java create mode 100644 src/main/java/cn/lunadeer/dominion/dtos/PrivilegeTemplateDTO.java create mode 100644 src/main/resources/config.yml diff --git a/src/main/java/cn/lunadeer/dominion/controllers/DominionController.java b/src/main/java/cn/lunadeer/dominion/controllers/DominionController.java new file mode 100644 index 0000000..2da83f2 --- /dev/null +++ b/src/main/java/cn/lunadeer/dominion/controllers/DominionController.java @@ -0,0 +1,18 @@ +package cn.lunadeer.dominion.controllers; + +import cn.lunadeer.dominion.dtos.DominionDTO; + +import java.util.List; +import java.util.UUID; + +public class DominionController { + public DominionDTO create(UUID owner, String name, String world, + Integer x1, Integer y1, Integer z1, Integer x2, Integer y2, Integer z2) { + // todo 检查冲突 + return DominionDTO.insert(new DominionDTO(owner, name, world, x1, y1, z1, x2, y2, z2)); + } + + public List all(){ + return DominionDTO.selectAll(); + } +} diff --git a/src/main/java/cn/lunadeer/dominion/dtos/DominionDTO.java b/src/main/java/cn/lunadeer/dominion/dtos/DominionDTO.java new file mode 100644 index 0000000..aa2ddb0 --- /dev/null +++ b/src/main/java/cn/lunadeer/dominion/dtos/DominionDTO.java @@ -0,0 +1,752 @@ +package cn.lunadeer.dominion.dtos; + +import cn.lunadeer.dominion.utils.Database; +import cn.lunadeer.dominion.utils.XLogger; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class DominionDTO { + + private static List query(String sql) { + List dominions = new ArrayList<>(); + try (ResultSet rs = Database.query(sql)) { + if (rs == null) return dominions; + while (rs.next()) { + Integer id = rs.getInt("id"); + UUID owner = UUID.fromString(rs.getString("owner")); + String name = rs.getString("name"); + String world = rs.getString("world"); + Integer x1 = rs.getInt("x1"); + Integer y1 = rs.getInt("y1"); + Integer z1 = rs.getInt("z1"); + Integer x2 = rs.getInt("x2"); + Integer y2 = rs.getInt("y2"); + Integer z2 = rs.getInt("z2"); + Integer parentDomId = rs.getInt("parent_dom_id"); + DominionDTO dominion = new DominionDTO(id, owner, name, world, x1, y1, z1, x2, y2, z2, parentDomId, + rs.getString("join_message"), + rs.getString("leave_message"), + rs.getBoolean("anchor"), + rs.getBoolean("animal_killing"), + rs.getBoolean("anvil"), + rs.getBoolean("beacon"), + rs.getBoolean("bed"), + rs.getBoolean("brew"), + rs.getBoolean("button"), + rs.getBoolean("cake"), + rs.getBoolean("container"), + rs.getBoolean("craft"), + rs.getBoolean("creeper_explode"), + rs.getBoolean("diode"), + rs.getBoolean("door"), + rs.getBoolean("dye"), + rs.getBoolean("egg"), + rs.getBoolean("enchant"), + rs.getBoolean("ender_pearl"), + rs.getBoolean("feed"), + rs.getBoolean("fire_spread"), + rs.getBoolean("flow_in_protection"), + rs.getBoolean("glow"), + rs.getBoolean("grow"), + rs.getBoolean("honey"), + rs.getBoolean("hook"), + rs.getBoolean("ignite"), + rs.getBoolean("mob_killing"), + rs.getBoolean("move"), + rs.getBoolean("place"), + rs.getBoolean("pressure"), + rs.getBoolean("riding"), + rs.getBoolean("shear"), + rs.getBoolean("shoot"), + rs.getBoolean("tnt_explode"), + rs.getBoolean("trade"), + rs.getBoolean("vehicle_destroy"), + rs.getBoolean("wither_spawn"), + rs.getBoolean("harvest")); + dominions.add(dominion); + } + } catch (SQLException e) { + XLogger.err("Database query failed: " + e.getMessage()); + XLogger.err("SQL: " + sql); + } + return dominions; + } + + public static List selectAll() { + String sql = "SELECT * FROM dominion"; + return query(sql); + } + + public static List search(String name){ + String sql = "SELECT * FROM dominion WHERE name LIKE '%" + name + "%'"; + return query(sql); + } + + public static List select(UUID owner) { + String sql = "SELECT * FROM dominion WHERE owner = '" + owner.toString() + "'"; + return query(sql); + } + + public static DominionDTO insert(DominionDTO dominion) { + String sql = "INSERT INTO dominion (" + + "owner, name, world, x1, y1, z1, x2, y2, z2" + + ") VALUES (" + + "'" + dominion.getOwner().toString() + "', " + + "'" + dominion.getName() + "', " + + "'" + dominion.getWorld() + "', " + + dominion.getX1() + ", " + + dominion.getY1() + ", " + + dominion.getZ1() + ", " + + dominion.getX2() + ", " + + dominion.getY2() + ", " + + dominion.getZ2() + + ") RETURNING *"; + List dominions = query(sql); + if (dominions.size() == 0) return null; + return dominions.get(0); + } + + public static void delete(DominionDTO dominion){ + String sql = "DELETE FROM dominion WHERE id = " + dominion.getId(); + query(sql); + } + + private static DominionDTO update(DominionDTO dominion) { + String sql = "UPDATE dominion SET " + + "name = '" + dominion.getName() + "', " + + "world = '" + dominion.getWorld() + "', " + + "x1 = " + dominion.getX1() + ", " + + "y1 = " + dominion.getY1() + ", " + + "z1 = " + dominion.getZ1() + ", " + + "x2 = " + dominion.getX2() + ", " + + "y2 = " + dominion.getY2() + ", " + + "z2 = " + dominion.getZ2() + ", " + + "parent_dom_id = " + dominion.getParentDomId() + ", " + + "join_message = '" + dominion.getJoinMessage() + "', " + + "leave_message = '" + dominion.getLeaveMessage() + "', " + + "anchor = " + dominion.getAnchor() + ", " + + "animal_killing = " + dominion.getAnimalKilling() + ", " + + "anvil = " + dominion.getAnvil() + ", " + + "beacon = " + dominion.getBeacon() + ", " + + "bed = " + dominion.getBed() + ", " + + "brew = " + dominion.getBrew() + ", " + + "button = " + dominion.getButton() + ", " + + "cake = " + dominion.getCake() + ", " + + "container = " + dominion.getContainer() + ", " + + "craft = " + dominion.getCraft() + ", " + + "creeper_explode = " + dominion.getCreeperExplode() + ", " + + "diode = " + dominion.getDiode() + ", " + + "door = " + dominion.getDoor() + ", " + + "dye = " + dominion.getDye() + ", " + + "egg = " + dominion.getEgg() + ", " + + "enchant = " + dominion.getEnchant() + ", " + + "ender_pearl = " + dominion.getEnderPearl() + ", " + + "feed = " + dominion.getFeed() + ", " + + "fire_spread = " + dominion.getFireSpread() + ", " + + "flow_in_protection = " + dominion.getFlowInProtection() + ", " + + "glow = " + dominion.getGlow() + ", " + + "grow = " + dominion.getGrow() + ", " + + "honey = " + dominion.getHoney() + ", " + + "hook = " + dominion.getHook() + ", " + + "ignite = " + dominion.getIgnite() + ", " + + "mob_killing = " + dominion.getMobKilling() + ", " + + "move = " + dominion.getMove() + ", " + + "place = " + dominion.getPlace() + ", " + + "pressure = " + dominion.getPressure() + ", " + + "riding = " + dominion.getRiding() + ", " + + "shear = " + dominion.getShear() + ", " + + "shoot = " + dominion.getShoot() + ", " + + "tnt_explode = " + dominion.getTntExplode() + ", " + + "trade = " + dominion.getTrade() + ", " + + "vehicle_destroy = " + dominion.getVehicleDestroy() + ", " + + "wither_spawn = " + dominion.getWitherSpawn() + ", " + + "harvest = " + dominion.getHarvest() + + " WHERE id = " + dominion.getId() + + " RETURNING *"; + List dominions = query(sql); + if (dominions.size() == 0) return null; + return dominions.get(0); + } + + private DominionDTO(Integer id, UUID owner, String name, String world, + Integer x1, Integer y1, Integer z1, Integer x2, Integer y2, Integer z2, + Integer parentDomId, + String joinMessage, String leaveMessage, + Boolean anchor, Boolean animalKilling, Boolean anvil, + Boolean beacon, Boolean bed, Boolean brew, Boolean button, Boolean cake, + Boolean container, Boolean craft, Boolean creeperExplode, Boolean diode, + Boolean door, Boolean dye, Boolean egg, Boolean enchant, Boolean enderPearl, + Boolean feed, Boolean fireSpread, Boolean flowInProtection, Boolean glow, + Boolean grow, Boolean honey, Boolean hook, Boolean ignite, Boolean mobKilling, + Boolean move, Boolean place, Boolean pressure, Boolean riding, Boolean shear, + Boolean shoot, Boolean tntExplode, Boolean trade, Boolean vehicleDestroy, + Boolean witherSpawn, Boolean harvest) { + this.id = id; + this.owner = owner; + this.name = name; + this.world = world; + this.x1 = x1; + this.y1 = y1; + this.z1 = z1; + this.x2 = x2; + this.y2 = y2; + this.z2 = z2; + this.parentDomId = parentDomId; + this.joinMessage = joinMessage; + this.leaveMessage = leaveMessage; + this.anchor = anchor; + this.animalKilling = animalKilling; + this.anvil = anvil; + this.beacon = beacon; + this.bed = bed; + this.brew = brew; + this.button = button; + this.cake = cake; + this.container = container; + this.craft = craft; + this.creeperExplode = creeperExplode; + this.diode = diode; + this.door = door; + this.dye = dye; + this.egg = egg; + this.enchant = enchant; + this.enderPearl = enderPearl; + this.feed = feed; + this.fireSpread = fireSpread; + this.flowInProtection = flowInProtection; + this.glow = glow; + this.grow = grow; + this.honey = honey; + this.hook = hook; + this.ignite = ignite; + this.mobKilling = mobKilling; + this.move = move; + this.place = place; + this.pressure = pressure; + this.riding = riding; + this.shear = shear; + this.shoot = shoot; + this.tntExplode = tntExplode; + this.trade = trade; + this.vehicleDestroy = vehicleDestroy; + this.witherSpawn = witherSpawn; + this.harvest = harvest; + } + + + private DominionDTO(Integer id, UUID owner, String name, String world, + Integer x1, Integer y1, Integer z1, Integer x2, Integer y2, Integer z2, + Integer parentDomId) { + this(id, owner, name, world, x1, y1, z1, x2, y2, z2, parentDomId, + "欢迎", "再见", + false, false, false, false, + false, false, false, false, false, false, + false, false, false, false, false, false, + false, false, false, false, false, + false, false, false, false, false, true, + false, false, false, false, false, false, + false, false, false, false); + } + + public DominionDTO(UUID owner, String name, String world, + Integer x1, Integer y1, Integer z1, Integer x2, Integer y2, Integer z2) { + this(null, owner, name, world, x1, y1, z1, x2, y2, z2, -1); + } + + private Integer id; + private UUID owner; + private final String name; + private final String world; + private Integer x1; + private Integer y1; + private Integer z1; + private Integer x2; + private Integer y2; + private Integer z2; + private Integer parentDomId = -1; + private String joinMessage = "欢迎"; + private String leaveMessage = "再见"; + private Boolean anchor = false; + private Boolean animalKilling = false; + private Boolean anvil = false; + private Boolean beacon = false; + private Boolean bed = false; + private Boolean brew = false; + private Boolean button = false; + private Boolean cake = false; + private Boolean container = false; + private Boolean craft = false; + private Boolean creeperExplode = false; + private Boolean diode = false; + private Boolean door = false; + private Boolean dye = false; + private Boolean egg = false; + private Boolean enchant = false; + private Boolean enderPearl = false; + private Boolean feed = false; + private Boolean fireSpread = false; + private Boolean flowInProtection = false; + private Boolean glow = false; + private Boolean grow = false; + private Boolean honey = false; + private Boolean hook = false; + private Boolean ignite = false; + private Boolean mobKilling = false; + private Boolean move = true; + private Boolean place = false; + private Boolean pressure = false; + private Boolean riding = false; + private Boolean shear = false; + private Boolean shoot = false; + private Boolean tntExplode = false; + private Boolean trade = false; + private Boolean vehicleDestroy = false; + private Boolean witherSpawn = false; + private Boolean harvest = false; + + // getters and setters + public Integer getId() { + return id; + } + + public DominionDTO setId(Integer id) { + this.id = id; + return update(this); + } + + public UUID getOwner() { + return owner; + } + + public DominionDTO setOwner(UUID owner) { + this.owner = owner; + return update(this); + } + + public String getName() { + return name; + } + + public String getWorld() { + return world; + } + + public Integer getX1() { + return x1; + } + + public DominionDTO setX1(Integer x1) { + this.x1 = x1; + return update(this); + } + + public Integer getY1() { + return y1; + } + + public DominionDTO setY1(Integer y1) { + this.y1 = y1; + return update(this); + } + + public Integer getZ1() { + return z1; + } + + public DominionDTO setZ1(Integer z1) { + this.z1 = z1; + return update(this); + } + + public Integer getX2() { + return x2; + } + + public DominionDTO setX2(Integer x2) { + this.x2 = x2; + return update(this); + } + + public Integer getY2() { + return y2; + } + + public DominionDTO setY2(Integer y2) { + this.y2 = y2; + return update(this); + } + + public Integer getZ2() { + return z2; + } + + public DominionDTO setZ2(Integer z2) { + this.z2 = z2; + return update(this); + } + + public Integer getParentDomId() { + return parentDomId; + } + + public DominionDTO setParentDomId(Integer parentDomId) { + this.parentDomId = parentDomId; + return update(this); + } + + public String getJoinMessage() { + return joinMessage; + } + + public DominionDTO setJoinMessage(String joinMessage) { + this.joinMessage = joinMessage; + return update(this); + } + + public String getLeaveMessage() { + return leaveMessage; + } + + public DominionDTO setLeaveMessage(String leaveMessage) { + this.leaveMessage = leaveMessage; + return update(this); + } + + public Boolean getAnchor() { + return anchor; + } + + public DominionDTO setAnchor(Boolean anchor) { + this.anchor = anchor; + return update(this); + } + + public Boolean getAnimalKilling() { + return animalKilling; + } + + public DominionDTO setAnimalKilling(Boolean animalKilling) { + this.animalKilling = animalKilling; + return update(this); + } + + public Boolean getAnvil() { + return anvil; + } + + public DominionDTO setAnvil(Boolean anvil) { + this.anvil = anvil; + return update(this); + } + + public Boolean getBeacon() { + return beacon; + } + + public DominionDTO setBeacon(Boolean beacon) { + this.beacon = beacon; + return update(this); + } + + public Boolean getBed() { + return bed; + } + + public DominionDTO setBed(Boolean bed) { + this.bed = bed; + return update(this); + } + + public Boolean getBrew() { + return brew; + } + + public DominionDTO setBrew(Boolean brew) { + this.brew = brew; + return update(this); + } + + public Boolean getButton() { + return button; + } + + public DominionDTO setButton(Boolean button) { + this.button = button; + return update(this); + } + + public Boolean getCake() { + return cake; + } + + public DominionDTO setCake(Boolean cake) { + this.cake = cake; + return update(this); + } + + public Boolean getContainer() { + return container; + } + + public DominionDTO setContainer(Boolean container) { + this.container = container; + return update(this); + } + + public Boolean getCraft() { + return craft; + } + + public DominionDTO setCraft(Boolean craft) { + this.craft = craft; + return update(this); + } + + public Boolean getCreeperExplode() { + return creeperExplode; + } + + public DominionDTO setCreeperExplode(Boolean creeperExplode) { + this.creeperExplode = creeperExplode; + return update(this); + } + + public Boolean getDiode() { + return diode; + } + + public DominionDTO setDiode(Boolean diode) { + this.diode = diode; + return update(this); + } + + public Boolean getDoor() { + return door; + } + + public DominionDTO setDoor(Boolean door) { + this.door = door; + return update(this); + } + + public Boolean getDye() { + return dye; + } + + public DominionDTO setDye(Boolean dye) { + this.dye = dye; + return update(this); + } + + public Boolean getEgg() { + return egg; + } + + public DominionDTO setEgg(Boolean egg) { + this.egg = egg; + return update(this); + } + + public Boolean getEnchant() { + return enchant; + } + + public DominionDTO setEnchant(Boolean enchant) { + this.enchant = enchant; + return update(this); + } + + public Boolean getEnderPearl() { + return enderPearl; + } + + public DominionDTO setEnderPearl(Boolean enderPearl) { + this.enderPearl = enderPearl; + return update(this); + } + + public Boolean getFeed() { + return feed; + } + + public DominionDTO setFeed(Boolean feed) { + this.feed = feed; + return update(this); + } + + public Boolean getFireSpread() { + return fireSpread; + } + + public DominionDTO setFireSpread(Boolean fireSpread) { + this.fireSpread = fireSpread; + return update(this); + } + + public Boolean getFlowInProtection() { + return flowInProtection; + } + + public DominionDTO setFlowInProtection(Boolean flowInProtection) { + this.flowInProtection = flowInProtection; + return update(this); + } + + public Boolean getGlow() { + return glow; + } + + public DominionDTO setGlow(Boolean glow) { + this.glow = glow; + return update(this); + } + + public Boolean getGrow() { + return grow; + } + + public DominionDTO setGrow(Boolean grow) { + this.grow = grow; + return update(this); + } + + public Boolean getHoney() { + return honey; + } + + public DominionDTO setHoney(Boolean honey) { + this.honey = honey; + return update(this); + } + + public Boolean getHook() { + return hook; + } + + public DominionDTO setHook(Boolean hook) { + this.hook = hook; + return update(this); + } + + public Boolean getIgnite() { + return ignite; + } + + public DominionDTO setIgnite(Boolean ignite) { + this.ignite = ignite; + return update(this); + } + + public Boolean getMobKilling() { + return mobKilling; + } + + public DominionDTO setMobKilling(Boolean mobKilling) { + this.mobKilling = mobKilling; + return update(this); + } + + public Boolean getMove() { + return move; + } + + public DominionDTO setMove(Boolean move) { + this.move = move; + return update(this); + } + + public Boolean getPlace() { + return place; + } + + public DominionDTO setPlace(Boolean place) { + this.place = place; + return update(this); + } + + public Boolean getPressure() { + return pressure; + } + + public DominionDTO setPressure(Boolean pressure) { + this.pressure = pressure; + return update(this); + } + + public Boolean getRiding() { + return riding; + } + + public DominionDTO setRiding(Boolean riding) { + this.riding = riding; + return update(this); + } + + public Boolean getShear() { + return shear; + } + + public DominionDTO setShear(Boolean shear) { + this.shear = shear; + return update(this); + } + + public Boolean getShoot() { + return shoot; + } + + public DominionDTO setShoot(Boolean shoot) { + this.shoot = shoot; + return update(this); + } + + public Boolean getTntExplode() { + return tntExplode; + } + + public DominionDTO setTntExplode(Boolean tntExplode) { + this.tntExplode = tntExplode; + return update(this); + } + + public Boolean getTrade() { + return trade; + } + + public DominionDTO setTrade(Boolean trade) { + this.trade = trade; + return update(this); + } + + public Boolean getVehicleDestroy() { + return vehicleDestroy; + } + + public DominionDTO setVehicleDestroy(Boolean vehicleDestroy) { + this.vehicleDestroy = vehicleDestroy; + return update(this); + } + + public Boolean getWitherSpawn() { + return witherSpawn; + } + + public DominionDTO setWitherSpawn(Boolean witherSpawn) { + this.witherSpawn = witherSpawn; + return update(this); + } + + public Boolean getHarvest() { + return harvest; + } + + public DominionDTO setHarvest(Boolean harvest) { + this.harvest = harvest; + return update(this); + } + +} diff --git a/src/main/java/cn/lunadeer/dominion/dtos/DPlayer.java b/src/main/java/cn/lunadeer/dominion/dtos/PlayerDTO.java similarity index 74% rename from src/main/java/cn/lunadeer/dominion/dtos/DPlayer.java rename to src/main/java/cn/lunadeer/dominion/dtos/PlayerDTO.java index 6711bf2..4fed5e2 100644 --- a/src/main/java/cn/lunadeer/dominion/dtos/DPlayer.java +++ b/src/main/java/cn/lunadeer/dominion/dtos/PlayerDTO.java @@ -1,7 +1,6 @@ package cn.lunadeer.dominion.dtos; import cn.lunadeer.dominion.utils.Database; -import cn.lunadeer.dominion.utils.Notification; import cn.lunadeer.dominion.utils.XLogger; import org.bukkit.entity.Player; @@ -11,26 +10,26 @@ import java.util.ArrayList; import java.util.List; import java.util.UUID; -public class DPlayer { +public class PlayerDTO { - public static DPlayer get(Player player) { - DPlayer re = select(player.getUniqueId()); + public static PlayerDTO get(Player player) { + PlayerDTO re = select(player.getUniqueId()); if (re == null) { - re = insert(new DPlayer(player.getUniqueId(), player.getName(), System.currentTimeMillis())); + re = insert(new PlayerDTO(player.getUniqueId(), player.getName(), System.currentTimeMillis())); } return re; } - public DPlayer onJoin() { + public PlayerDTO onJoin() { return update(this); } - public static List search(String name) { + public static List search(String name) { return select(name); } - private static List query(String sql) { - List players = new ArrayList<>(); + private static List query(String sql) { + List players = new ArrayList<>(); try (ResultSet rs = Database.query(sql)) { if (rs == null) return players; while (rs.next()) { @@ -38,7 +37,7 @@ public class DPlayer { UUID uuid = UUID.fromString(rs.getString("uuid")); String lastKnownName = rs.getString("last_known_name"); Long lastJoinAt = rs.getTimestamp("last_join_at").getTime(); - DPlayer player = new DPlayer(id, uuid, lastKnownName, lastJoinAt); + PlayerDTO player = new PlayerDTO(id, uuid, lastKnownName, lastJoinAt); players.add(player); } } catch (SQLException e) { @@ -48,48 +47,48 @@ public class DPlayer { return players; } - private static DPlayer select(UUID uuid) { + private static PlayerDTO select(UUID uuid) { String sql = "SELECT * FROM player_name WHERE uuid = '" + uuid.toString() + "'"; - List players = query(sql); + List players = query(sql); if (players.size() == 0) return null; return players.get(0); } - private static List select(String name) { + private static List select(String name) { // 模糊搜索 String sql = "SELECT * FROM player_name WHERE last_known_name LIKE '%" + name + "%'"; return query(sql); } - private static DPlayer insert(DPlayer player) { + private static PlayerDTO insert(PlayerDTO player) { String sql = "INSERT INTO player_name (uuid, last_known_name, last_join_at) " + "VALUES" + " ('" + player.getUuid().toString() + "', '" + player.getLastKnownName() + "', CURRENT_TIMESTAMP) " + "RETURNING *"; - List players = query(sql); + List players = query(sql); if (players.size() == 0) return null; return players.get(0); } - private static DPlayer update(DPlayer player) { + private static PlayerDTO update(PlayerDTO player) { String sql = "UPDATE player_name SET " + "last_known_name = '" + player.getLastKnownName() + "', " + "last_join_at = CURRENT_TIMESTAMP " + "WHERE uuid = '" + player.getUuid().toString() + "' " + "RETURNING *"; - List players = query(sql); + List players = query(sql); if (players.size() == 0) return null; return players.get(0); } - private DPlayer(Integer id, UUID uuid, String lastKnownName, Long lastJoinAt) { + private PlayerDTO(Integer id, UUID uuid, String lastKnownName, Long lastJoinAt) { this.id = id; this.uuid = uuid; this.lastKnownName = lastKnownName; this.lastJoinAt = lastJoinAt; } - private DPlayer(UUID uuid, String lastKnownName, Long lastJoinAt) { + private PlayerDTO(UUID uuid, String lastKnownName, Long lastJoinAt) { this(null, uuid, lastKnownName, lastJoinAt); } diff --git a/src/main/java/cn/lunadeer/dominion/dtos/PlayerPrivilegeDTO.java b/src/main/java/cn/lunadeer/dominion/dtos/PlayerPrivilegeDTO.java new file mode 100644 index 0000000..6da13af --- /dev/null +++ b/src/main/java/cn/lunadeer/dominion/dtos/PlayerPrivilegeDTO.java @@ -0,0 +1,129 @@ +package cn.lunadeer.dominion.dtos; + +import cn.lunadeer.dominion.utils.Database; +import cn.lunadeer.dominion.utils.XLogger; + +import java.sql.ResultSet; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class PlayerPrivilegeDTO { + + public static PlayerPrivilegeDTO insert(PlayerPrivilegeDTO player) { + String sql = "INSERT INTO player_privilege (player_uuid, admin, dom_id, privilege_template_id) VALUES (" + + "'" + player.getPlayerUUID() + "', " + + player.getAdmin() + ", " + + player.getDomID() + ", " + + player.getPrivilegeTemplateID() + ")" + + "RETURNING *"; + List players = query(sql); + if (players.size() == 0) return null; + return players.get(0); + } + + public static PlayerPrivilegeDTO select(UUID playerUUID) { + String sql = "SELECT * FROM player_privilege WHERE player_uuid = '" + playerUUID + "'"; + List players = query(sql); + if (players.size() == 0) return null; + return players.get(0); + } + + public static PlayerPrivilegeDTO select(Integer dom_id) { + String sql = "SELECT * FROM player_privilege WHERE dom_id = " + dom_id; + List players = query(sql); + if (players.size() == 0) return null; + return players.get(0); + } + + public static PlayerPrivilegeDTO select(Integer dom_id, UUID playerUUID) { + String sql = "SELECT * FROM player_privilege WHERE dom_id = " + dom_id + " AND player_uuid = '" + playerUUID + "'"; + List players = query(sql); + if (players.size() == 0) return null; + return players.get(0); + } + + public static void delete(PlayerPrivilegeDTO player) { + String sql = "DELETE FROM player_privilege WHERE id = " + player.getId(); + query(sql); + } + + public static List selectAll() { + String sql = "SELECT * FROM player_privilege"; + return query(sql); + } + + private final Integer id; + private final UUID playerUUID; + private Boolean admin; + private final Integer domID; + private final Integer privilegeTemplateID; + + public Integer getId() { + return id; + } + + public UUID getPlayerUUID() { + return playerUUID; + } + + public Boolean getAdmin() { + return admin; + } + + public Integer getDomID() { + return domID; + } + + public Integer getPrivilegeTemplateID() { + return privilegeTemplateID; + } + + public void setAdmin(Boolean admin) { + this.admin = admin; + update(this); + } + + private PlayerPrivilegeDTO(Integer id, UUID playerUUID, Boolean admin, Integer domID, Integer privilegeTemplateID) { + this.id = id; + this.playerUUID = playerUUID; + this.admin = admin; + this.domID = domID; + this.privilegeTemplateID = privilegeTemplateID; + } + + public PlayerPrivilegeDTO(UUID playerUUID, Boolean admin, Integer domID, Integer privilegeTemplateID) { + this(null, playerUUID, admin, domID, privilegeTemplateID); + } + + private static List query(String sql) { + List players = new ArrayList<>(); + try (ResultSet rs = Database.query(sql)) { + if (rs == null) return players; + while (rs.next()) { + Integer id = rs.getInt("id"); + UUID uuid = UUID.fromString(rs.getString("player_uuid")); + Boolean admin = rs.getBoolean("admin"); + Integer domID = rs.getInt("dom_id"); + Integer privilegeTemplateID = rs.getInt("privilege_template_id"); + PlayerPrivilegeDTO player = new PlayerPrivilegeDTO(id, uuid, admin, domID, privilegeTemplateID); + players.add(player); + } + } catch (Exception e) { + XLogger.err("Database query failed: " + e.getMessage()); + XLogger.err("SQL: " + sql); + } + return players; + } + + private static PlayerPrivilegeDTO update(PlayerPrivilegeDTO player) { + String sql = "UPDATE player_privilege SET " + + "admin = " + player.getAdmin() + ", " + + "dom_id = " + player.getDomID() + ", " + + "privilege_template_id = " + player.getPrivilegeTemplateID() + " " + + "WHERE id = " + player.getId(); + List players = query(sql); + if (players.size() == 0) return null; + return players.get(0); + } +} diff --git a/src/main/java/cn/lunadeer/dominion/dtos/PrivilegeTemplateDTO.java b/src/main/java/cn/lunadeer/dominion/dtos/PrivilegeTemplateDTO.java new file mode 100644 index 0000000..7c698bd --- /dev/null +++ b/src/main/java/cn/lunadeer/dominion/dtos/PrivilegeTemplateDTO.java @@ -0,0 +1,532 @@ +package cn.lunadeer.dominion.dtos; + +import cn.lunadeer.dominion.utils.Database; +import cn.lunadeer.dominion.utils.XLogger; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class PrivilegeTemplateDTO { + + public static PrivilegeTemplateDTO insert(PrivilegeTemplateDTO privilege) { + String sql = "INSERT INTO privilege_template (name, creator, group) " + + "VALUES ('" + + privilege.getName() + "', '" + + privilege.getCreator().toString() + "', " + + privilege.getGroup() + ") " + + "RETURNING *"; + List templates = query(sql); + if (templates.size() == 0) return null; + return templates.get(0); + } + + public static List selectAll() { + String sql = "SELECT * FROM privilege_template"; + return query(sql); + } + + public static List search(String name) { + String sql = "SELECT * FROM privilege_template WHERE name LIKE '%" + name + "%'"; + return query(sql); + } + + public static List search(UUID creator) { + String sql = "SELECT * FROM privilege_template WHERE creator = '" + creator.toString() + "'"; + return query(sql); + } + + public static void delete(PrivilegeTemplateDTO privilege) { + String sql = "DELETE FROM privilege_template WHERE id = " + privilege.getId(); + query(sql); + } + + private static PrivilegeTemplateDTO update(PrivilegeTemplateDTO privilege) { + String sql = "UPDATE privilege_template SET " + + "name = '" + privilege.getName() + "', " + + "creator = '" + privilege.getCreator().toString() + "', " + + "group = " + privilege.getGroup() + ", " + + "anchor = " + privilege.getAnchor() + ", " + + "animal_killing = " + privilege.getAnimalKilling() + ", " + + "anvil = " + privilege.getAnvil() + ", " + + "beacon = " + privilege.getBeacon() + ", " + + "bed = " + privilege.getBed() + ", " + + "brew = " + privilege.getBrew() + ", " + + "button = " + privilege.getButton() + ", " + + "cake = " + privilege.getCake() + ", " + + "container = " + privilege.getContainer() + ", " + + "craft = " + privilege.getCraft() + ", " + + "diode = " + privilege.getDiode() + ", " + + "door = " + privilege.getDoor() + ", " + + "dye = " + privilege.getDye() + ", " + + "egg = " + privilege.getEgg() + ", " + + "enchant = " + privilege.getEnchant() + ", " + + "ender_pearl = " + privilege.getEnderPearl() + ", " + + "feed = " + privilege.getFeed() + ", " + + "glow = " + privilege.getGlow() + ", " + + "honey = " + privilege.getHoney() + ", " + + "hook = " + privilege.getHook() + ", " + + "ignite = " + privilege.getIgnite() + ", " + + "mob_killing = " + privilege.getMobKilling() + ", " + + "move = " + privilege.getMove() + ", " + + "place = " + privilege.getPlace() + ", " + + "pressure = " + privilege.getPressure() + ", " + + "riding = " + privilege.getRiding() + ", " + + "shear = " + privilege.getShear() + ", " + + "shoot = " + privilege.getShoot() + ", " + + "trade = " + privilege.getTrade() + ", " + + "vehicle_destroy = " + privilege.getVehicleDestroy() + ", " + + "harvest = " + privilege.getHarvest() + " " + + "WHERE id = " + privilege.getId() + " " + + "RETURNING *"; + List templates = query(sql); + if (templates.size() == 0) return null; + return templates.get(0); + } + + private final Integer id; + private String name; + private final UUID creator; + private final Boolean group; + private Boolean anchor; + private Boolean animalKilling; + private Boolean anvil; + private Boolean beacon; + private Boolean bed; + private Boolean brew; + private Boolean button; + private Boolean cake; + private Boolean container; + private Boolean craft; + private Boolean diode; + private Boolean door; + private Boolean dye; + private Boolean egg; + private Boolean enchant; + private Boolean enderPearl; + private Boolean feed; + private Boolean glow; + private Boolean honey; + private Boolean hook; + private Boolean ignite; + private Boolean mobKilling; + private Boolean move; + private Boolean place; + private Boolean pressure; + private Boolean riding; + private Boolean shear; + private Boolean shoot; + private Boolean trade; + private Boolean vehicleDestroy; + private Boolean harvest; + + public Integer getId() { + return id; + } + + public String getName() { + return name; + } + + public UUID getCreator() { + return creator; + } + + public Boolean getGroup() { + return group; + } + + public Boolean getAnchor() { + return anchor; + } + + public Boolean getAnimalKilling() { + return animalKilling; + } + + public Boolean getAnvil() { + return anvil; + } + + public Boolean getBeacon() { + return beacon; + } + + public Boolean getBed() { + return bed; + } + + public Boolean getBrew() { + return brew; + } + + public Boolean getButton() { + return button; + } + + public Boolean getCake() { + return cake; + } + + public Boolean getContainer() { + return container; + } + + public Boolean getCraft() { + return craft; + } + + public Boolean getDiode() { + return diode; + } + + public Boolean getDoor() { + return door; + } + + public Boolean getDye() { + return dye; + } + + public Boolean getEgg() { + return egg; + } + + public Boolean getEnchant() { + return enchant; + } + + public Boolean getEnderPearl() { + return enderPearl; + } + + public Boolean getFeed() { + return feed; + } + + public Boolean getGlow() { + return glow; + } + + public Boolean getHoney() { + return honey; + } + + public Boolean getHook() { + return hook; + } + + public Boolean getIgnite() { + return ignite; + } + + public Boolean getMobKilling() { + return mobKilling; + } + + public Boolean getMove() { + return move; + } + + public Boolean getPlace() { + return place; + } + + public Boolean getPressure() { + return pressure; + } + + public Boolean getRiding() { + return riding; + } + + public Boolean getShear() { + return shear; + } + + public Boolean getShoot() { + return shoot; + } + + public Boolean getTrade() { + return trade; + } + + public Boolean getVehicleDestroy() { + return vehicleDestroy; + } + + public Boolean getHarvest() { + return harvest; + } + + public PrivilegeTemplateDTO setName(String name) { + this.name = name; + return update(this); + } + + public PrivilegeTemplateDTO setAnchor(Boolean anchor) { + this.anchor = anchor; + return update(this); + } + + public PrivilegeTemplateDTO setAnimalKilling(Boolean animalKilling) { + this.animalKilling = animalKilling; + return update(this); + } + + public PrivilegeTemplateDTO setAnvil(Boolean anvil) { + this.anvil = anvil; + return update(this); + } + + public PrivilegeTemplateDTO setBeacon(Boolean beacon) { + this.beacon = beacon; + return update(this); + } + + public PrivilegeTemplateDTO setBed(Boolean bed) { + this.bed = bed; + return update(this); + } + + public PrivilegeTemplateDTO setBrew(Boolean brew) { + this.brew = brew; + return update(this); + } + + public PrivilegeTemplateDTO setButton(Boolean button) { + this.button = button; + return update(this); + } + + public PrivilegeTemplateDTO setCake(Boolean cake) { + this.cake = cake; + return update(this); + } + + public PrivilegeTemplateDTO setContainer(Boolean container) { + this.container = container; + return update(this); + } + + public PrivilegeTemplateDTO setCraft(Boolean craft) { + this.craft = craft; + return update(this); + } + + public PrivilegeTemplateDTO setDiode(Boolean diode) { + this.diode = diode; + return update(this); + } + + public PrivilegeTemplateDTO setDoor(Boolean door) { + this.door = door; + return update(this); + } + + public PrivilegeTemplateDTO setDye(Boolean dye) { + this.dye = dye; + return update(this); + } + + public PrivilegeTemplateDTO setEgg(Boolean egg) { + this.egg = egg; + return update(this); + } + + public PrivilegeTemplateDTO setEnchant(Boolean enchant) { + this.enchant = enchant; + return update(this); + } + + public PrivilegeTemplateDTO setEnderPearl(Boolean enderPearl) { + this.enderPearl = enderPearl; + return update(this); + } + + public PrivilegeTemplateDTO setFeed(Boolean feed) { + this.feed = feed; + return update(this); + } + + public PrivilegeTemplateDTO setGlow(Boolean glow) { + this.glow = glow; + return update(this); + } + + public PrivilegeTemplateDTO setHoney(Boolean honey) { + this.honey = honey; + return update(this); + } + + public PrivilegeTemplateDTO setHook(Boolean hook) { + this.hook = hook; + return update(this); + } + + public PrivilegeTemplateDTO setIgnite(Boolean ignite) { + this.ignite = ignite; + return update(this); + } + + public PrivilegeTemplateDTO setMobKilling(Boolean mobKilling) { + this.mobKilling = mobKilling; + return update(this); + } + + public PrivilegeTemplateDTO setMove(Boolean move) { + this.move = move; + return update(this); + } + + public PrivilegeTemplateDTO setPlace(Boolean place) { + this.place = place; + return update(this); + } + + public PrivilegeTemplateDTO setPressure(Boolean pressure) { + this.pressure = pressure; + return update(this); + } + + public PrivilegeTemplateDTO setRiding(Boolean riding) { + this.riding = riding; + return update(this); + } + + public PrivilegeTemplateDTO setShear(Boolean shear) { + this.shear = shear; + return update(this); + } + + public PrivilegeTemplateDTO setShoot(Boolean shoot) { + this.shoot = shoot; + return update(this); + } + + public PrivilegeTemplateDTO setTrade(Boolean trade) { + this.trade = trade; + return update(this); + } + + public PrivilegeTemplateDTO setVehicleDestroy(Boolean vehicleDestroy) { + this.vehicleDestroy = vehicleDestroy; + return update(this); + } + + public PrivilegeTemplateDTO setHarvest(Boolean harvest) { + this.harvest = harvest; + return update(this); + } + + public PrivilegeTemplateDTO(String name, UUID creator, Boolean group) { + this(null, name, creator, group, + false, false, false, + false, false, false, false, false, + false, false, false, false, false, + false, false, false, false, false, + false, false, false, false, false, + false, false, false, false, false, + false, false, false); + } + + private PrivilegeTemplateDTO(Integer id, String name, UUID creator, Boolean group, + Boolean anchor, Boolean animalKilling, Boolean anvil, + Boolean beacon, Boolean bed, Boolean brew, Boolean button, Boolean cake, + Boolean container, Boolean craft, Boolean diode, Boolean door, Boolean dye, + Boolean egg, Boolean enchant, Boolean enderPearl, Boolean feed, Boolean glow, + Boolean honey, Boolean hook, Boolean ignite, Boolean mobKilling, Boolean move, + Boolean place, Boolean pressure, Boolean riding, Boolean shear, Boolean shoot, + Boolean trade, Boolean vehicleDestroy, Boolean harvest) { + this.id = id; + this.name = name; + this.creator = creator; + this.group = group; + this.anchor = anchor; + this.animalKilling = animalKilling; + this.anvil = anvil; + this.beacon = beacon; + this.bed = bed; + this.brew = brew; + this.button = button; + this.cake = cake; + this.container = container; + this.craft = craft; + this.diode = diode; + this.door = door; + this.dye = dye; + this.egg = egg; + this.enchant = enchant; + this.enderPearl = enderPearl; + this.feed = feed; + this.glow = glow; + this.honey = honey; + this.hook = hook; + this.ignite = ignite; + this.mobKilling = mobKilling; + this.move = move; + this.place = place; + this.pressure = pressure; + this.riding = riding; + this.shear = shear; + this.shoot = shoot; + this.trade = trade; + this.vehicleDestroy = vehicleDestroy; + this.harvest = harvest; + } + + private static List query(String sql) { + List privilegeTemplates = new ArrayList<>(); + try (ResultSet rs = Database.query(sql)) { + if (rs == null) return privilegeTemplates; + while (rs.next()) { + PrivilegeTemplateDTO privilegeTemplate = new PrivilegeTemplateDTO( + rs.getInt("id"), + rs.getString("name"), + UUID.fromString(rs.getString("creator")), + rs.getBoolean("group"), + rs.getBoolean("anchor"), + rs.getBoolean("animal_killing"), + rs.getBoolean("anvil"), + rs.getBoolean("beacon"), + rs.getBoolean("bed"), + rs.getBoolean("brew"), + rs.getBoolean("button"), + rs.getBoolean("cake"), + rs.getBoolean("container"), + rs.getBoolean("craft"), + rs.getBoolean("diode"), + rs.getBoolean("door"), + rs.getBoolean("dye"), + rs.getBoolean("egg"), + rs.getBoolean("enchant"), + rs.getBoolean("ender_pearl"), + rs.getBoolean("feed"), + rs.getBoolean("glow"), + rs.getBoolean("honey"), + rs.getBoolean("hook"), + rs.getBoolean("ignite"), + rs.getBoolean("mob_killing"), + rs.getBoolean("move"), + rs.getBoolean("place"), + rs.getBoolean("pressure"), + rs.getBoolean("riding"), + rs.getBoolean("shear"), + rs.getBoolean("shoot"), + rs.getBoolean("trade"), + rs.getBoolean("vehicle_destroy"), + rs.getBoolean("harvest") + ); + privilegeTemplates.add(privilegeTemplate); + } + } catch (SQLException e) { + XLogger.err("Database query failed: " + e.getMessage()); + XLogger.err("SQL: " + sql); + } + return privilegeTemplates; + } + +} diff --git a/src/main/java/cn/lunadeer/dominion/utils/ConfigManager.java b/src/main/java/cn/lunadeer/dominion/utils/ConfigManager.java index 8eb6820..21afba7 100644 --- a/src/main/java/cn/lunadeer/dominion/utils/ConfigManager.java +++ b/src/main/java/cn/lunadeer/dominion/utils/ConfigManager.java @@ -17,7 +17,7 @@ public class ConfigManager { _debug = _file.getBoolean("Debug", false); _db_host = _file.getString("Database.Host", "localhost"); _db_port = _file.getString("Database.Port", "5432"); - _db_name = _file.getString("Database.Name", "miniplayertitle"); + _db_name = _file.getString("Database.Name", "dominion"); _db_user = _file.getString("Database.User", "postgres"); _db_pass = _file.getString("Database.Pass", "postgres"); } diff --git a/src/main/java/cn/lunadeer/dominion/utils/Database.java b/src/main/java/cn/lunadeer/dominion/utils/Database.java index 06cb22c..d8beab9 100644 --- a/src/main/java/cn/lunadeer/dominion/utils/Database.java +++ b/src/main/java/cn/lunadeer/dominion/utils/Database.java @@ -66,7 +66,6 @@ public class Database { " anchor BOOLEAN NOT NULL DEFAULT FALSE," + " animal_killing BOOLEAN NOT NULL DEFAULT FALSE," + " anvil BOOLEAN NOT NULL DEFAULT FALSE," + - " anvil_break BOOLEAN NOT NULL DEFAULT FALSE," + " beacon BOOLEAN NOT NULL DEFAULT FALSE," + " bed BOOLEAN NOT NULL DEFAULT FALSE," + " brew BOOLEAN NOT NULL DEFAULT FALSE," + @@ -111,12 +110,11 @@ public class Database { " id SERIAL PRIMARY KEY," + " name TEXT NOT NULL," + " creator VARCHAR(36) NOT NULL," + - " is_group BOOLEAN NOT NULL DEFAULT TRUE," + + " group BOOLEAN NOT NULL DEFAULT TRUE," + " anchor BOOLEAN NOT NULL DEFAULT FALSE," + " animal_killing BOOLEAN NOT NULL DEFAULT FALSE," + " anvil BOOLEAN NOT NULL DEFAULT FALSE," + - " anvil_break BOOLEAN NOT NULL DEFAULT FALSE," + " beacon BOOLEAN NOT NULL DEFAULT FALSE," + " bed BOOLEAN NOT NULL DEFAULT FALSE," + " brew BOOLEAN NOT NULL DEFAULT FALSE," + @@ -144,21 +142,22 @@ public class Database { " shoot BOOLEAN NOT NULL DEFAULT FALSE," + " trade BOOLEAN NOT NULL DEFAULT FALSE," + " vehicle_destroy BOOLEAN NOT NULL DEFAULT FALSE," + - " harvest BOOLEAN NOT NULL DEFAULT FALSE" + + " harvest BOOLEAN NOT NULL DEFAULT FALSE," + + " FOREIGN KEY (creator) REFERENCES player_name(uuid)" + ")"; // player dominion privilege sql += "CREATE TABLE IF NOT EXISTS player_dom_privilege (" + " id SERIAL PRIMARY KEY," + " player_uuid VARCHAR(36) NOT NULL," + + " admin BOOLEAN NOT NULL DEFAULT FALSE," + " dom_id INT NOT NULL," + " privilege_template_id INT NOT NULL," + " FOREIGN KEY (player_uuid) REFERENCES player_name(uuid)," + - " FOREIGN KEY (dom_id) REFERENCES dominion(id)" + + " FOREIGN KEY (dom_id) REFERENCES dominion(id)," + + " FOREIGN KEY (privilege_template_id) REFERENCES privilege_template(id)" + ")"; - - query(sql); } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..f56d9ba --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,8 @@ +Database: + Host: localhost + Port: 5432 + Name: dominion + User: dominion + Pass: dominion + +Debug: false \ No newline at end of file