From 37f11e4723a90a062a4474472676661d663c8617 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 3 Nov 2021 17:14:45 +0100 Subject: [PATCH] Add Schematic SchematicNode Wrapper Signed-off-by: Chaoscaot --- .../comms/packets/PrepareSchemPacket.java | 8 + .../src/de/steamwar/inventory/SWListInv.java | 7 + .../src/de/steamwar/sql/CheckedSchematic.java | 56 +++-- .../de/steamwar/sql/DownloadSchematic.java | 10 +- .../src/de/steamwar/sql/Schematic.java | 193 ++++++++++++++++++ .../src/de/steamwar/sql/SchematicMember.java | 96 +++++++++ .../src/de/steamwar/sql/SchematicNode.java | 11 +- 7 files changed, 342 insertions(+), 39 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/sql/Schematic.java create mode 100644 SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/PrepareSchemPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/PrepareSchemPacket.java index aba2380..8c0968d 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/packets/PrepareSchemPacket.java +++ b/SpigotCore_Main/src/de/steamwar/comms/packets/PrepareSchemPacket.java @@ -21,6 +21,7 @@ package de.steamwar.comms.packets; import com.google.common.io.ByteArrayDataOutput; import de.steamwar.comms.PacketIdManager; +import de.steamwar.sql.Schematic; import de.steamwar.sql.SchematicNode; import de.steamwar.sql.SchematicType; import de.steamwar.sql.SteamwarUser; @@ -37,6 +38,13 @@ public class PrepareSchemPacket extends SpigotPacket{ this.schematicType = schematicType; } + @Deprecated + public PrepareSchemPacket(SteamwarUser user, Schematic schematic, SchematicType schematicType) { + this.user = user; + this.schematic = schematic.getNode(); + this.schematicType = schematicType; + } + @Override public int getName() { return PacketIdManager.PREPARE_SCHEM; diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java b/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java index 5f35f55..6b84527 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java @@ -19,6 +19,7 @@ package de.steamwar.inventory; +import de.steamwar.sql.Schematic; import de.steamwar.sql.SchematicNode; import de.steamwar.sql.SchematicType; import org.bukkit.Bukkit; @@ -27,6 +28,7 @@ import org.bukkit.entity.Player; import org.bukkit.event.inventory.ClickType; import java.util.*; +import java.util.stream.Collectors; public class SWListInv extends SWInventory { @@ -136,6 +138,11 @@ public class SWListInv extends SWInventory { } return schemList; } + + @Deprecated + public static List> getSchemList(SchematicType type, int steamwarUserId){ + return getSchemnodeList(type, steamwarUserId).stream().map(schematicNodeSWListEntry -> new SWListEntry(schematicNodeSWListEntry.getItem(), Schematic.wrap(schematicNodeSWListEntry.getObject()))).collect(Collectors.toList()); + } private boolean sizeBiggerMax(){ return dynamicSize ? elements.size() > 54 : elements.size() > 45; diff --git a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java index e7a5ccc..652d041 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java @@ -24,6 +24,7 @@ import org.bukkit.Bukkit; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.UUID; @@ -32,6 +33,9 @@ import java.util.logging.Level; public class CheckedSchematic { + private static final SQL.Statement checkHistory = new SQL.Statement("SELECT * FROM CheckedSchematic WHERE NodeId IN (SELECT NodeId FROM SchematicNode WHERE NodeOwner = ?) AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC"); + private static final SQL.Statement nodeHistory = new SQL.Statement("SELECT * FROM CheckedSchematic WHERE NodeId = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC"); + private final int node; private final int validator; private final Timestamp startTime; @@ -62,43 +66,35 @@ public class CheckedSchematic { node, validator, startTime, endTime, declineReason); } - public static List getLastDeclined(SchematicNode node){ - return getLastDeclined(node.getId()); + public static List getLastDeclinedOfNode(SchematicNode node){ + return getLastDeclinedOfNode(node.getId()); } - public static List getLastDeclined(int node){ - List lastDeclined = new LinkedList<>(); - try{ - ResultSet lastRS = SQL.select("SELECT * FROM CheckedSchematic WHERE NodeId = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC", node); - while(lastRS.next()){ - int validator = lastRS.getInt("Validator"); - Timestamp startTime = lastRS.getTimestamp("StartTime"); - Timestamp endTime = lastRS.getTimestamp("EndTime"); - String declineReason = lastRS.getString("DeclineReason"); + public static List getLastDeclinedOfNode(int node){ + return nodeHistory.select(rs -> { + List lastDeclined = new ArrayList<>(); + while(rs.next()){ + int validator = rs.getInt("Validator"); + Timestamp startTime = rs.getTimestamp("StartTime"); + Timestamp endTime = rs.getTimestamp("EndTime"); + String declineReason = rs.getString("DeclineReason"); lastDeclined.add(new CheckedSchematic(node, validator, startTime, endTime, declineReason, false)); } - }catch(SQLException e){ - Bukkit.getLogger().log(Level.SEVERE, "getLastDeclined failed", e); - } - return lastDeclined; + return lastDeclined; + }, node); } public static List getLastDeclined(UUID uuid){ - List lastDeclined = new LinkedList<>(); - try{ - ResultSet lastRS = SQL.select("SELECT * FROM CheckedSchematic WHERE NodeId IN (SELECT NodeId FROM SchematicNode WHERE NodeOwner = ?) AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC", SteamwarUser.get(uuid).getId()); - while(lastRS.next()){ - int node = lastRS.getInt("NodeId"); - int validator = lastRS.getInt("Validator"); - Timestamp startTime = lastRS.getTimestamp("StartTime"); - Timestamp endTime = lastRS.getTimestamp("EndTime"); - String declineReason = lastRS.getString("DeclineReason"); - lastDeclined.add(new CheckedSchematic(node, validator, startTime, endTime, declineReason, false)); - } - }catch(SQLException e){ - Bukkit.getLogger().log(Level.SEVERE, "getLastDeclined failed", e); - } - return lastDeclined; + return getLastDelined(SteamwarUser.get(uuid).getId()); + } + + public static List getLastDelined(int schemOwner){ + return checkHistory.select(rs -> { + List history = new ArrayList<>(); + while(rs.next()) + history.add(new CheckedSchematic(rs.getInt("NodeId"), rs.getInt("Validator"), rs.getTimestamp("StartTime"), rs.getTimestamp("EndTime"), rs.getString("DeclineReason"), false)); + return history; + }, schemOwner); } public void remove() { diff --git a/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java index e941b8d..7f4dc2b 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java @@ -28,6 +28,9 @@ import java.time.Instant; public class DownloadSchematic { private DownloadSchematic(){} + private static final SQL.Statement createLink = new SQL.Statement("INSERT INTO SchemDownload (SchemID, Link) VALUES (?, ?) ON DUPLICATE KEY UPDATE Link = VALUES(Link)"); + + private static final String BASE = "https://steamwar.de/download.php?schem="; public static String getLink(SchematicNode schem){ @@ -42,7 +45,12 @@ public class DownloadSchematic { digest.reset(); digest.update((Instant.now().toString() + schem.getOwner() + schem.getId()).getBytes()); String hash = BaseEncoding.base16().encode(digest.digest()); - SQL.update("INSERT INTO SchemDownload (SchemID, Link) VALUES (?, ?) ON DUPLICATE KEY UPDATE Link = VALUES(Link)", schem.getId(), hash); + createLink.update(schem.getId(), hash); return BASE + hash; } + + @Deprecated + public static String getLink(Schematic schem){ + return getLink(schem.getNode()); + } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java new file mode 100644 index 0000000..555a615 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -0,0 +1,193 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package de.steamwar.sql; + +import com.sk89q.worldedit.extent.clipboard.Clipboard; +import de.steamwar.core.Core; +import de.steamwar.core.WorldEditWrapper; +import org.bukkit.entity.Player; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +@Deprecated +public class Schematic { + + private static final SQL.Statement getSchemsOfType = new SQL.Statement("SELECT DISTINCT s.SchemID, s.SchemName, s.SchemOwner, s.Item, s.SchemType, s.Rank, s.SchemFormat FROM Schematic s LEFT JOIN SchemMember sm ON sm.SchemName = s.SchemName AND sm.SchemOwner = s.SchemOwner WHERE s.SchemType = ? AND (s.SchemOwner = ? OR sm.Member = ?) ORDER BY s.SchemName"); + + private final SchematicNode node; + + private Schematic(SchematicNode node) { + this.node = node; + } + + public static Schematic wrap(SchematicNode node) { + return new Schematic(node); + } + + public static void createSchem(String schemName, UUID schemOwner, String item, SchematicType schemType){ + createSchem(schemName, SteamwarUser.get(schemOwner).getId(), item, schemType); + } + + public static void createSchem(String schemName, int schemOwner, String item, SchematicType schemType){ + SchematicNode.createSchematicNode(schemOwner, schemName, null, schemType.toDB(), item); + } + + public static Schematic getSchemFromDB(String schemName, UUID schemOwner){ + return getSchemFromDB(schemName, SteamwarUser.get(schemOwner).getId()); + } + + public static Schematic getSchemFromDB(String schemName, int schemOwner){ + SchematicNode node = SchematicNode.getSchematicNode(schemOwner, schemName, 0); + if(node != null) { + return new Schematic(node); + } + return null; + } + + public static Schematic getSchemFromDB(int schemID){ + SchematicNode node = SchematicNode.getSchematicNode(schemID); + if(node != null) { + return new Schematic(node); + } else { + throw new SecurityException("Failed to load Schematics"); + } + } + + public static List getSchemsAccessibleByUser(UUID schemOwner){ + return getSchemsAccessibleByUser(SteamwarUser.get(schemOwner).getId()); + } + + public static List getSchemsAccessibleByUser(int schemOwner){ + List schematics = new ArrayList<>(); + SchematicNode.getSchematicsAccessibleByUser(schemOwner, null) + .forEach(node1 -> schematics.add(new Schematic(node1))); + return schematics; + } + + public static List getSchemsOfType(UUID schemOwner, SchematicType schemType){ + return getSchemsOfType(SteamwarUser.get(schemOwner).getId(), schemType); + } + + public static List getSchemsOfType(int schemOwner, SchematicType schemType){ + List schematics = new ArrayList<>(); + SchematicNode.getSchematicsOfType(schemOwner, schemType.toDB(), null) + .forEach(node1 -> schematics.add(new Schematic(node1))); + return schematics; + } + + public static List getAllSchemsOfType(SchematicType schemType){ + List schematics = new ArrayList<>(); + SchematicNode.getAllSchematicsOfType(schemType.toDB()) + .forEach(node1 -> schematics.add(new Schematic(node1))); + return schematics; + } + + public int getSchemID() { + return node.getId(); + } + + public String getSchemName() { + return node.getName(); + } + + public int getSchemOwner() { + return node.getOwner(); + } + + public int getRank(){ + return node.getRank(); + } + + public String getItem() { + return node.getItem(); + } + + public void setItem(String item) { + node.setItem(item); + } + + public void setRank(int rank){ + node.setRank(rank); + } + + public SchematicType getSchemType() { + return node.getSchemtype(); + } + + public void setSchemType(SchematicType schemType) { + node.setType(schemType.toDB()); + } + + public boolean availible(){ + return true; + } + + public InputStream schemData() throws IOException { + return node.schemData(); + } + + public static Clipboard clipboardFromStream(InputStream is, boolean schemFormat) { + try { + return WorldEditWrapper.impl.getClipboard(is, schemFormat); + } catch (IOException e) { + throw new SecurityException("Could not read schem", e); + } + } + + public Clipboard load() throws IOException, NoClipboardException { + return clipboardFromStream(schemData(), node.getSchemFormat()); + } + + public void loadToPlayer(Player player) throws IOException, NoClipboardException { + InputStream is = schemData(); + WorldEditWrapper.impl.setPlayerClipboard(player, is, node.getSchemFormat()); + } + + public void saveOldFormatFromPlayer(Player player) throws IOException, NoClipboardException { + saveFromPlayer(player, false); + } + + public void saveFromPlayer(Player player) throws IOException, NoClipboardException { + saveFromPlayer(player, Core.getVersion() > 12); + } + + public void saveFromBytes(byte[] bytes, boolean newFormat) { + node.saveFromBytes(bytes, newFormat); + } + + private void saveFromPlayer(Player player, boolean newFormat) throws IOException, NoClipboardException { + node.saveFromPlayer(player, newFormat); + } + + public void remove(){ + node.delete(); + } + + public SchematicNode getNode() { + return node; + } + + @Deprecated + public static class WrongVersionException extends Exception{} +} diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java new file mode 100644 index 0000000..e84fa54 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java @@ -0,0 +1,96 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package de.steamwar.sql; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +@Deprecated +public class SchematicMember { + + private final NodeMember member; + + private SchematicMember(NodeMember member){ + this.member = member; + } + + public SchematicMember(String schemName, int schemOwner, int schemMember){ + this(NodeMember.createNodeMember(Schematic.getSchemFromDB(schemName, schemOwner).getSchemID(), schemMember)); + } + + public SchematicMember(String schemName, UUID schemOwner, UUID schemMember){ + this(NodeMember.createNodeMember(Schematic.getSchemFromDB(schemName, schemOwner).getSchemID(), SteamwarUser.get(schemMember).getId())); + } + + public static SchematicMember getSchemMemberFromDB(String schemName, UUID schemOwner, UUID schemMember){ + return getSchemMemberFromDB(schemName, SteamwarUser.get(schemOwner).getId(), SteamwarUser.get(schemMember).getId()); + } + + public static SchematicMember getSchemMemberFromDB(String schemName, int schemOwner, int schemMember){ + NodeMember member = NodeMember.getNodeMember(Schematic.getSchemFromDB(schemName, schemOwner).getSchemID(), schemMember); + if(member == null) { + return null; + } + return new SchematicMember(member); + } + + public static SchematicMember getMemberBySchematic(String schemName, int schemMember){ + Optional nodeMember = NodeMember.getSchematics(schemMember) + .stream().filter(member1 -> SchematicNode.getSchematicNode(member1.getNode()).getName().equalsIgnoreCase(schemName)).findFirst(); + return nodeMember.map(SchematicMember::new).orElse(null); + } + + public static List getSchemMembers(String schemName, UUID schemOwner){ + return getSchemMembers(schemName, SteamwarUser.get(schemOwner).getId()); + } + + public static List getSchemMembers(String schemName, int schemOwner){ + List members = new ArrayList<>(); + NodeMember.getNodeMembers(Schematic.getSchemFromDB(schemName, schemOwner).getSchemID()) + .forEach(member1 -> members.add(new SchematicMember(member1))); + return members; + } + + public static List getAccessibleSchems(UUID schemMember){ + return getAccessibleSchems(SteamwarUser.get(schemMember).getId()); + } + + public static List getAccessibleSchems(int schemMember){ + List members = new ArrayList<>(); + NodeMember.getSchematics(schemMember) + .forEach(member1 -> members.add(new SchematicMember(member1))); + return members; + } + + public int getSchemOwner() { + return SchematicNode.getSchematicNode(member.getNode()).getOwner(); + } + + public String getSchemName() { + return SchematicNode.getSchematicNode(member.getNode()).getName(); + } + + public int getMember() { + return member.getMember(); + } + + public void remove(){ + member.delete(); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index f554f31..3f124e9 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -20,6 +20,7 @@ package de.steamwar.sql; import com.sk89q.worldedit.extent.clipboard.Clipboard; +import de.steamwar.core.Core; import de.steamwar.core.VersionedCallable; import de.steamwar.core.VersionedRunnable; import de.steamwar.core.WorldEditWrapper; @@ -462,16 +463,10 @@ public class SchematicNode { WorldEditWrapper.impl.setPlayerClipboard(player, schemData(), schemFormat); } - public void saveOldFormatFromPlayer(Player player) throws IOException, NoClipboardException { - if(isDir) - throw new SecurityException("Node is Directory"); - saveFromPlayer(player, false); - } - public void saveFromPlayer(Player player) throws IOException, NoClipboardException { if(isDir) throw new SecurityException("Node is Directory"); - saveFromPlayer(player, true); + saveFromPlayer(player, Core.getVersion() > 12); } public void saveFromBytes(byte[] bytes, boolean newFormat) { @@ -480,7 +475,7 @@ public class SchematicNode { updateDatabase(new ByteArrayInputStream(bytes), newFormat); } - private void saveFromPlayer(Player player, boolean newFormat) throws IOException, NoClipboardException { + public void saveFromPlayer(Player player, boolean newFormat) throws IOException, NoClipboardException { if(isDir) throw new SecurityException("Node is Directory"); updateDatabase(WorldEditWrapper.impl.getPlayerClipboard(player, newFormat), newFormat);