From 5d96c5a084952029f91794f97b4f73d3e2bf6836 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Mon, 25 Jan 2021 23:58:11 +0100 Subject: [PATCH 01/75] Add Basic SchematicNode System --- .../src/de/steamwar/inventory/SWListInv.java | 17 +- .../de/steamwar/sql/DownloadSchematic.java | 8 +- .../src/de/steamwar/sql/NodeData.java | 157 +++++++++++ .../src/de/steamwar/sql/NodeMember.java | 83 ++++++ .../src/de/steamwar/sql/Schematic.java | 263 ------------------ .../src/de/steamwar/sql/SchematicMember.java | 134 --------- .../src/de/steamwar/sql/SchematicNode.java | 228 +++++++++++++++ 7 files changed, 482 insertions(+), 408 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/sql/NodeData.java create mode 100644 SpigotCore_Main/src/de/steamwar/sql/NodeMember.java delete mode 100644 SpigotCore_Main/src/de/steamwar/sql/Schematic.java delete mode 100644 SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java create mode 100644 SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java b/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java index 43c294f..6803257 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java @@ -19,7 +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; import org.bukkit.Material; @@ -115,22 +115,23 @@ public class SWListInv extends SWInventory { return onlinePlayers; } - public static List> getSchemList(SchematicType type, int steamwarUserId){ - List> schemList = new ArrayList<>(); + public static List> getSchemList(SchematicType type, int steamwarUserId){ + List> schemList = new ArrayList<>(); - List schems; + List schems; if(type == null) - schems = Schematic.getSchemsAccessibleByUser(steamwarUserId); + schems = SchematicNode.getSchematicsAccessibleByUser(steamwarUserId, 0); else - schems = Schematic.getSchemsOfType(steamwarUserId, type); + schems = SchematicNode.getSchematicsOfType(steamwarUserId, type.toDB(), 0); - for(Schematic s : schems){ + for(SchematicNode s : schems){ Material m; if(s.getItem().isEmpty()) m = SWItem.getMaterial("CAULDRON_ITEM"); else m = SWItem.getMaterial(s.getItem()); - SWItem item = new SWItem(m,"§e" + s.getSchemName()); + SWItem item = new SWItem(m,"§e" + s.getName()); + item.setEnchanted(s.isDir()); schemList.add(new SWListEntry<>(item, s)); } return schemList; diff --git a/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java index 4f55754..e941b8d 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java @@ -30,7 +30,9 @@ public class DownloadSchematic { private static final String BASE = "https://steamwar.de/download.php?schem="; - public static String getLink(Schematic schem){ + public static String getLink(SchematicNode schem){ + if(schem.isDir()) + throw new SecurityException("Can not Download Directorys"); MessageDigest digest; try { digest = MessageDigest.getInstance("SHA-1"); @@ -38,9 +40,9 @@ public class DownloadSchematic { throw new SecurityException(e); } digest.reset(); - digest.update((Instant.now().toString() + schem.getSchemOwner() + schem.getSchemID()).getBytes()); + 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.getSchemID(), hash); + SQL.update("INSERT INTO SchemDownload (SchemID, Link) VALUES (?, ?) ON DUPLICATE KEY UPDATE Link = VALUES(Link)", schem.getId(), hash); return BASE + hash; } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/NodeData.java b/SpigotCore_Main/src/de/steamwar/sql/NodeData.java new file mode 100644 index 0000000..c3d6e36 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/NodeData.java @@ -0,0 +1,157 @@ +/* + * 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.VersionedCallable; +import de.steamwar.core.VersionedRunnable; +import org.bukkit.entity.Player; + +import java.io.IOException; +import java.io.InputStream; +import java.sql.Blob; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class NodeData { + + public static NodeData getSchemdata(int node) { + ResultSet set = SQL.select("SELECT NodeId, SchemFormat, NodeRank FROM NodeData WHERE NodeId = ?", node); + try { + if(!set.next()) + return null; + return new NodeData(set); + } catch (SQLException e) { + throw new SecurityException("Could not load NodeData", e); + } + } + + public static NodeData createNodeDate(SchematicNode node) { + SQL.update("INSERT INTO NodeData (NodeId) VALUES(?)", node.getId()); + return getSchemdata(node.getId()); + } + + private final int node; + private boolean schemFormat; + private int rank; + + private NodeData(ResultSet set) throws SQLException { + node = set.getInt("NodeId"); + schemFormat = set.getBoolean("SchemFormat"); + rank = set.getInt("NodeRank"); + } + + public Clipboard load() throws IOException, NoClipboardException { + ResultSet rs = SQL.select("SELECT SchemData FROM NodeData WHERE NodeId = ?", node); + try { + rs.next(); + Blob schemData = rs.getBlob("SchemData"); + if(schemData == null) + throw new IOException("SchemData is null"); + InputStream is = schemData.getBinaryStream(); + return VersionedCallable.call(new VersionedCallable<>(() -> Schematic_8.getClipboard(is, schemFormat), 8), + new VersionedCallable<>(() -> Schematic_14.getClipboard(is, schemFormat), 14)); + } catch (SQLException e) { + throw new IOException(e); + } + } + + public void loadToPlayer(Player player) throws IOException, NoClipboardException { + ResultSet rs = SQL.select("SELECT SchemData FROM NodeData WHERE NodeId = ?", node); + try { + rs.next(); + Blob blob = rs.getBlob("SchemData"); + if(blob == null) + throw new NoClipboardException(); + InputStream is = blob.getBinaryStream(); + VersionedRunnable.call(new VersionedRunnable(() -> Schematic_8.setPlayerClipboard(player, is, schemFormat), 8), + new VersionedRunnable(() -> Schematic_14.setPlayerClipboard(player, is, schemFormat), 14)); + } catch (SQLException e) { + throw new IOException(e); + } + } + + public void saveOldFormatFromPlayer(Player player) throws IOException, NoClipboardException { + saveFromPlayer(player, false); + } + + public void saveFromPlayer(Player player) throws IOException, NoClipboardException { + saveFromPlayer(player, true); + } + + public void saveFromBytes(byte[] bytes, boolean newFormat) { + Blob blob = SQL.blob(); + try { + blob.setBytes(1, bytes); + updateDatabase(blob, newFormat); + } catch (SQLException e) { + throw new SecurityException(e); + } + } + + private void saveFromPlayer(Player player, boolean newFormat) throws IOException, NoClipboardException { + Blob blob = SQL.blob(); + VersionedRunnable.call(new VersionedRunnable(() -> { + try { + blob.setBytes(1, Schematic_8.getPlayerClipboard(player)); + } catch (SQLException e) { + throw new RuntimeException(e.getMessage(), e); + } + updateDatabase(blob, false); + }, 8), new VersionedRunnable(() -> { + try { + blob.setBytes(1, Schematic_14.getPlayerClipboard(player, newFormat)); + } catch (SQLException exception) { + throw new RuntimeException(exception.getMessage(), exception); + } + updateDatabase(blob, newFormat); + }, 14)); + } + + private void updateDatabase(Blob blob, boolean newFormat) { + SQL.update("UPDATE NodeData SET SchemData = ?, SchemFormat = ? WHERE NodeId = ?", blob, newFormat, node); + schemFormat = newFormat; + } + + public int getNode() { + return node; + } + + public boolean isSchemFormat() { + return schemFormat; + } + + public int getRank() { + return rank; + } + + public void setRank(int rank) { + this.rank = rank; + updateDB(); + } + + private void updateDB() { + SQL.update("UPDATE NodeData SET Rank = ? WHERE NodeId = ?", rank, node); + } + + public void delete() { + SQL.update("DELETE FROM NodeData WHERE NodeId = ?", node); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java b/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java new file mode 100644 index 0000000..22c23c3 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java @@ -0,0 +1,83 @@ +/* + * 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.sql.ResultSet; +import java.sql.SQLException; +import java.util.HashSet; +import java.util.Set; + +public class NodeMember { + + public static NodeMember getNodeMember(int node, int member) { + ResultSet set = SQL.select("SELECT * FROM NodeMember WHERE NodeId = ? AND UserId = ?", node, member); + try { + if(!set.next()) + return null; + return new NodeMember(set); + } catch (SQLException e) { + throw new SecurityException("Could not load NodeMember", e); + } + } + + public static Set getNodeMembers(int node) { + ResultSet set = SQL.select("SELECT * FROM NodeMember WHERE NodeId = ?", node); + try { + Set members = new HashSet<>(); + while (set.next()) + members.add(new NodeMember(set)); + return members; + } catch (SQLException e) { + throw new SecurityException("Could not load NodeMember", e); + } + } + + public static Set getSchematics(int member) { + ResultSet set = SQL.select("SELECT * FROM NodeMember WHERE UserId = ?", member); + try { + Set members = new HashSet<>(); + while (set.next()) + members.add(new NodeMember(set)); + return members; + } catch (SQLException e) { + throw new SecurityException("Could not load NodeMember", e); + } + } + + final int node; + final int member; + + private NodeMember(ResultSet set) throws SQLException { + node = set.getInt("NodeId"); + member = set.getInt("UserId"); + } + + public int getNode() { + return node; + } + + public int getMember() { + return member; + } + + public void delete() { + SQL.update("DELETE FROM NodeMember WHERE NodeId = ? AND UserId = ?", node, member); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java deleted file mode 100644 index dbdee0f..0000000 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ /dev/null @@ -1,263 +0,0 @@ -/* - 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.VersionedCallable; -import de.steamwar.core.VersionedRunnable; -import org.bukkit.entity.Player; - -import java.io.IOException; -import java.io.InputStream; -import java.sql.Blob; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -public class Schematic { - - private final int schemID; - private final String schemName; - private final int schemOwner; - private boolean schemFormat; - private String item; - private int rank; - private SchematicType schemType; - - private Schematic(ResultSet rs) throws SQLException { - this.schemID = rs.getInt("SchemID"); - this.schemName = rs.getString("SchemName"); - this.schemOwner = rs.getInt("SchemOwner"); - this.item = rs.getString("Item"); - this.rank = rs.getInt("Rank"); - this.schemType = SchematicType.fromDB(rs.getString("SchemType")); - this.schemFormat = rs.getBoolean("SchemFormat"); - } - - private void updateDB(){ - createSchem(schemName, schemOwner, item, schemType); - } - - 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){ - SQL.update("INSERT INTO Schematic (SchemName, SchemOwner, Item, SchemType) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE Item = VALUES(Item), SchemType = VALUES(SchemType)", - schemName, schemOwner, item, schemType.toDB()); - } - - public static Schematic getSchemFromDB(String schemName, UUID schemOwner){ - return getSchemFromDB(schemName, SteamwarUser.get(schemOwner).getId()); - } - - public static Schematic getSchemFromDB(String schemName, int schemOwner){ - ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, Item, SchemType, Rank, SchemFormat FROM Schematic WHERE SchemName = ? AND SchemOwner = ?", schemName, schemOwner); - try { - if(schematic == null || !schematic.next()){ - SchematicMember member = SchematicMember.getMemberBySchematic(schemName, schemOwner); - if(member == null){ - return null; - } - return getSchemFromDB(schemName, member.getSchemOwner()); - } - return new Schematic(schematic); - } catch (SQLException e) { - throw new SecurityException("Failed loading schematic", e); - } - } - - public static Schematic getSchemFromDB(int schemID){ - ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, Item, SchemType, Rank, SchemFormat FROM Schematic WHERE SchemID = ?", schemID); - try { - if(!schematic.next()) - throw new SecurityException("Failed loading schematic " + schemID); - return new Schematic(schematic); - } catch (SQLException e) { - throw new SecurityException("Failed loading schematic", e); - } - } - - public static List getSchemsAccessibleByUser(UUID schemOwner){ - return getSchemsAccessibleByUser(SteamwarUser.get(schemOwner).getId()); - } - - public static List getSchemsAccessibleByUser(int schemOwner){ - try{ - ResultSet schematic = SQL.select("SELECT 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.SchemOwner = ? OR sm.Member = ? GROUP BY s.SchemID ORDER BY s.SchemName", schemOwner, schemOwner); - List schematics = new ArrayList<>(); - while(schematic.next()) - schematics.add(new Schematic(schematic)); - return schematics; - }catch(SQLException e){ - throw new SecurityException("Failed listing schematics", e); - } - } - - public static List getSchemsOfType(UUID schemOwner, SchematicType schemType){ - return getSchemsOfType(SteamwarUser.get(schemOwner).getId(), schemType); - } - - public static List getSchemsOfType(int schemOwner, SchematicType schemType){ - //Unsauber, dafür auch geaddede Schematics dabei - List schems = getSchemsAccessibleByUser(schemOwner); - for(int i = schems.size()-1; i >= 0; i--) - if(!schems.get(i).getSchemType().equals(schemType)) - schems.remove(i); - return schems; - } - - public static List getAllSchemsOfType(SchematicType schemType){ - try{ - ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, Item, SchemType, Rank, SchemFormat FROM Schematic WHERE SchemType = ?", schemType.toDB()); - List schematics = new ArrayList<>(); - while(schematic.next()){ - schematics.add(new Schematic(schematic)); - } - return schematics; - }catch(SQLException e){ - throw new SecurityException("Failed loading all schems of type", e); - } - } - - public int getSchemID() { - return schemID; - } - - public String getSchemName() { - return schemName; - } - - public int getSchemOwner() { - return schemOwner; - } - - public int getRank(){ - return rank; - } - - public String getItem() { - return item; - } - - public void setItem(String item) { - this.item = item; - updateDB(); - } - - public void setRank(int rank){ - this.rank = rank; - SQL.update("UPDATE Schematic SET Rank = ? WHERE SchemID = ?", rank, schemID); - } - - public SchematicType getSchemType() { - return schemType; - } - - public void setSchemType(SchematicType schemType) { - this.schemType = schemType; - updateDB(); - } - - public boolean availible(){ - return true; - } - - public Clipboard load() throws IOException, NoClipboardException { - ResultSet rs = SQL.select("SELECT SchemData FROM Schematic WHERE SchemID = ?", schemID); - try { - rs.next(); - Blob schemData = rs.getBlob("SchemData"); - if(schemData == null) - throw new IOException("SchemData is null"); - InputStream is = schemData.getBinaryStream(); - return VersionedCallable.call(new VersionedCallable<>(() -> Schematic_8.getClipboard(is, schemFormat), 8), - new VersionedCallable<>(() -> Schematic_14.getClipboard(is, schemFormat), 14)); - } catch (SQLException e) { - throw new IOException(e); - } - } - - public void loadToPlayer(Player player) throws IOException, NoClipboardException { - ResultSet rs = SQL.select("SELECT SchemData FROM Schematic WHERE SchemID = ?", schemID); - try { - rs.next(); - Blob blob = rs.getBlob("SchemData"); - if(blob == null) - throw new NoClipboardException(); - InputStream is = blob.getBinaryStream(); - VersionedRunnable.call(new VersionedRunnable(() -> Schematic_8.setPlayerClipboard(player, is, schemFormat), 8), - new VersionedRunnable(() -> Schematic_14.setPlayerClipboard(player, is, schemFormat), 14)); - } catch (SQLException e) { - throw new IOException(e); - } - } - - public void saveOldFormatFromPlayer(Player player) throws IOException, NoClipboardException { - saveFromPlayer(player, false); - } - - public void saveFromPlayer(Player player) throws IOException, NoClipboardException { - saveFromPlayer(player, true); - } - - public void saveFromBytes(byte[] bytes, boolean newFormat) { - Blob blob = SQL.blob(); - try { - blob.setBytes(1, bytes); - updateDatabase(blob, newFormat); - } catch (SQLException e) { - throw new SecurityException(e); - } - } - - private void saveFromPlayer(Player player, boolean newFormat) throws IOException, NoClipboardException { - Blob blob = SQL.blob(); - VersionedRunnable.call(new VersionedRunnable(() -> { - try { - blob.setBytes(1, Schematic_8.getPlayerClipboard(player)); - } catch (SQLException e) { - throw new RuntimeException(e.getMessage(), e); - } - updateDatabase(blob, false); - }, 8), new VersionedRunnable(() -> { - try { - blob.setBytes(1, Schematic_14.getPlayerClipboard(player, newFormat)); - } catch (SQLException exception) { - throw new RuntimeException(exception.getMessage(), exception); - } - updateDatabase(blob, newFormat); - }, 14)); - } - - private void updateDatabase(Blob blob, boolean newFormat) { - SQL.update("UPDATE Schematic SET SchemData = ?, SchemFormat = ? WHERE SchemID = ?", blob, newFormat, schemID); - schemFormat = newFormat; - } - - public void remove(){ - SQL.update("DELETE FROM SchemMember WHERE SchemOwner = ? AND SchemName = ?", schemOwner, schemName); - SQL.update("DELETE FROM Schematic WHERE SchemOwner = ? AND SchemName = ?", schemOwner, schemName); - } - - 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 deleted file mode 100644 index 87bf2fb..0000000 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - 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.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -public class SchematicMember { - private final int schemOwner; - private final String schemName; - private final int member; - - private SchematicMember(String schemName, int schemOwner, int schemMember, boolean updateDB){ - this.schemOwner = schemOwner; - member = schemMember; - this.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, SteamwarUser.get(schemOwner).getId(), SteamwarUser.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, SteamwarUser.get(schemOwner).getId(), SteamwarUser.get(schemMember).getId()); - } - - public static SchematicMember getSchemMemberFromDB(String schemName, int schemOwner, int schemMember){ - ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND SchemOwner = ? AND Member = ?", schemName, schemOwner, schemMember); - try { - if(schematicMember == null || !schematicMember.next()){ - return null; - } - return new SchematicMember(schemName, schemOwner, schemMember, false); - } catch (SQLException e) { - throw new SecurityException("Could not get schemmember", e); - } - } - - public static SchematicMember getMemberBySchematic(String schemName, int schemMember){ - ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND Member = ?", schemName, schemMember); - try { - if(schematicMember == null || !schematicMember.next()){ - return null; - } - int schemOwner = schematicMember.getInt("SchemOwner"); - return new SchematicMember(schemName, schemOwner, schemMember, false); - } catch (SQLException e) { - throw new SecurityException("Could not get member", e); - } - } - - public static List getSchemMembers(String schemName, UUID schemOwner){ - return getSchemMembers(schemName, SteamwarUser.get(schemOwner).getId()); - } - - public static List getSchemMembers(String schemName, int schemOwner){ - ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND SchemOwner = ?", schemName, 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) { - throw new SecurityException("Could not get schemmembers", e); - } - } - - public static List getAccessibleSchems(UUID schemMember){ - return getAccessibleSchems(SteamwarUser.get(schemMember).getId()); - } - - public static List getAccessibleSchems(int schemMember){ - ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember 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) { - throw new SecurityException("Could not get accessible schems", e); - } - } - - public int getSchemOwner() { - return schemOwner; - } - - public String getSchemName() { - return schemName; - } - - public int getMember() { - return member; - } - - public void remove(){ - SQL.update("DELETE FROM SchemMember WHERE SchemOwner = ? AND SchemName = ? AND Member = ?", schemOwner, schemName, member); - } -} diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java new file mode 100644 index 0000000..244c2a4 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -0,0 +1,228 @@ +/* + * 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.sql.ResultSet; +import java.sql.SQLException; +import java.util.*; + +public class SchematicNode { + + private static final String DIR_TYPE = "directory"; + public static final int ROOT_DIR = 0; + + public static SchematicNode createSchematic(int owner, String name, int parent) { + return createSchematicNode(owner, name, parent, SchematicType.Normal.toDB()); + } + + public static SchematicNode createSchematicDirectory(int owner, String name, int parent) { + return createSchematicNode(owner, name, parent, DIR_TYPE); + } + + private static SchematicNode createSchematicNode(int owner, String name, int parent, String type) { + SQL.update("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeType) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUE(NodeName), ParentNode = VALUE(ParentNode), NodeItem = VALUE(NodeItem), NodeType = VALUE(NodeType)", + name, owner, parent, type); + return getSchematicNode(owner, name, type, parent); + } + + public static SchematicNode getSchematicNode(int owner, String name, String type, int parent) { + ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND NodeType = ? AND ParentNode = ?", owner, name, type, parent); + try { + if(!set.next()) + return null; + return new SchematicNode(set); + }catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); + } + } + + public static SchematicNode getSchematicNode(int owner, String name, String type, SchematicNode parent) { + return getSchematicNode(owner, name, type, parent.getId()); + } + + public static List getSchematicNodeInNode(int owner, int parent) { + ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE ParentNode = ? AND NodeOwner = ?", parent, owner); + try { + List nodes = new ArrayList<>(); + while (set.next()) + nodes.add(new SchematicNode(set)); + return nodes; + }catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); + } + } + + public static List getSchematicNodeInNode(int owner, SchematicNode parent) { + return getSchematicNodeInNode(owner, parent.getId()); + } + + public static SchematicNode getSchematicDirectory(int owner, String name, SchematicNode parent) { + return getSchematicNode(owner, name, DIR_TYPE, parent); + } + + public static SchematicNode getSchematicDirectory(int owner, String name, int parent) { + return getSchematicNode(owner, name, DIR_TYPE, parent); + } + + public static SchematicNode getSchematicNode(int id) { + ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeId", id); + try { + if(!set.next()) + return null; + return new SchematicNode(set); + }catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); + } + } + + public static List getAllSchematicsOfType(int owner, String schemType) { + ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND SchemType = ?", owner, schemType); + try { + List nodes = new ArrayList<>(); + while (set.next()) + nodes.add(new SchematicNode(set)); + return nodes; + }catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); + } + } + + public static List getSchematicsOfType(int owner, String schemType, int parent) { + List schems = getAllSchematicsOfType(owner, schemType); + Map nodesInParent = new LinkedHashMap<>(); + for (SchematicNode schematicNode : schems) { + SchematicNode currentNode = schematicNode; + while (currentNode.getParent() != parent) { + currentNode = currentNode.getParentNode(); + } + nodesInParent.putIfAbsent(currentNode.getId(), currentNode); + } + return new ArrayList<>(nodesInParent.values()); + } + + public static List getSchematicsAccessibleByUser(int user, int parent) { + ResultSet set = SQL.select("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode FROM SchematicNode s LEFT JOIN NodeMember nm ON nm.NodeId = s.NodeId WHERE ( s.NodeOwner = ? OR nm.UserId = ? ) AND s.ParentNode = ? GROUP BY s.NodeId ORDER BY s.NodeName", user, user, parent); + try{ + List nodes = new ArrayList<>(); + while(set.next()) + nodes.add(new SchematicNode(set)); + return nodes; + }catch(SQLException e){ + throw new SecurityException("Failed listing schematics", e); + } + } + + private final int id; + private final int owner; + private String name; + private int parent; + private String item; + private String type; + + private SchematicNode(ResultSet set) throws SQLException { + id = set.getInt("NodeId"); + owner = set.getInt("NodeOwner"); + name = set.getString("NodeName"); + parent = set.getInt("ParentNode"); + item = set.getString("NodeItem"); + type = set.getString("NodeType"); + } + + public int getId() { + return id; + } + + public int getOwner() { + return owner; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + updateDB(); + } + + public int getParent() { + return parent; + } + + public void setParent(int parent) { + this.parent = parent; + updateDB(); + } + + public String getItem() { + return item; + } + + public void setItem(String item) { + this.item = item; + updateDB(); + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + updateDB(); + } + + public boolean isDir() { + return type.equals(DIR_TYPE); + } + + public SchematicType getSchemtype() { + if(!isDir()) + throw new RuntimeException("Is Directory"); + return SchematicType.fromDB(type); + } + + public SchematicNode getParentNode() { + return SchematicNode.getSchematicNode(parent); + } + + public NodeData getNodeData() { + if(isDir()) + throw new SecurityException("Could not get NodeData of Directory"); + return NodeData.getSchemdata(id); + } + + public boolean accessibleByUser(int user) { + return NodeMember.getNodeMember(id, user) != null; + } + + public Set getMembers() { + return NodeMember.getNodeMembers(id); + } + + private void updateDB() { + SQL.update("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeItem, NodeType) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUE(NodeName), ParentNode = VALUE(ParentNode), NodeItem = VALUE(NodeItem), NodeType = VALUE(NodeType)", + name, owner, parent, item, type); + } + + public void delete() { + SQL.update("DELETE FROM SchematicNode WHERE NodeId = ?", id); + } +} From 0895035b08600d2052f30ea6b83795c9438d4e82 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 26 Jan 2021 00:16:05 +0100 Subject: [PATCH 02/75] Fix Delete --- .../src/de/steamwar/sql/SchematicNode.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 244c2a4..5dd4d2f 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -57,8 +57,8 @@ public class SchematicNode { return getSchematicNode(owner, name, type, parent.getId()); } - public static List getSchematicNodeInNode(int owner, int parent) { - ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE ParentNode = ? AND NodeOwner = ?", parent, owner); + public static List getSchematicNodeInNode(int parent) { + ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE ParentNode = ?", parent); try { List nodes = new ArrayList<>(); while (set.next()) @@ -69,8 +69,8 @@ public class SchematicNode { } } - public static List getSchematicNodeInNode(int owner, SchematicNode parent) { - return getSchematicNodeInNode(owner, parent.getId()); + public static List getSchematicNodeInNode(SchematicNode parent) { + return getSchematicNodeInNode(parent.getId()); } public static SchematicNode getSchematicDirectory(int owner, String name, SchematicNode parent) { @@ -223,6 +223,9 @@ public class SchematicNode { } public void delete() { + if(isDir()) { + getSchematicNodeInNode(getId()).forEach(SchematicNode::delete); + } SQL.update("DELETE FROM SchematicNode WHERE NodeId = ?", id); } } From 5a840b219451cea5de1cdb22bb1c9ba6375b2b5f Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 26 Jan 2021 00:26:58 +0100 Subject: [PATCH 03/75] Fix CheckedSchematic --- .../src/de/steamwar/sql/CheckedSchematic.java | 41 +++++++------------ 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java index 4f13f10..9097420 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java @@ -32,16 +32,14 @@ import java.util.logging.Level; public class CheckedSchematic { - private final String schemName; - private final int schemOwner; + private final int node; private final int validator; private final Timestamp startTime; private final Timestamp endTime; private final String declineReason; - private CheckedSchematic(String schemName, int schemOwner, int validator, Timestamp startTime, Timestamp endTime, String declineReason, boolean insertDB){ - this.schemName = schemName; - this.schemOwner = schemOwner; + private CheckedSchematic(int node, int validator, Timestamp startTime, Timestamp endTime, String declineReason, boolean insertDB){ + this.node = node; this.validator = validator; this.startTime = startTime; this.endTime = endTime; @@ -50,35 +48,34 @@ public class CheckedSchematic { insertDB(); } - public CheckedSchematic(String schemName, int schemOwner, int validator, Timestamp startTime, Timestamp endTime, String declineReason){ - this(schemName, schemOwner, validator, startTime, endTime, declineReason, true); + public CheckedSchematic(int node, int validator, Timestamp startTime, Timestamp endTime, String declineReason){ + this(node, validator, startTime, endTime, declineReason, true); } - public CheckedSchematic(String schemName, UUID schemOwner, UUID validator, Timestamp startTime, Timestamp endTime, String declineReason){ - this(schemName, SteamwarUser.get(schemOwner).getId(), SteamwarUser.get(validator).getId(), startTime, endTime, declineReason, true); + public CheckedSchematic(int node, UUID validator, Timestamp startTime, Timestamp endTime, String declineReason){ + this(node, SteamwarUser.get(validator).getId(), startTime, endTime, declineReason, true); } private void insertDB(){ SQL.update("INSERT INTO CheckedSchematic" + - " (SchemName, SchemOwner, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?, ?)", - schemName, schemOwner, validator, startTime, endTime, declineReason); + " (NodeId, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?)", + node, validator, startTime, endTime, declineReason); } - public static List getLastDeclined(UUID schemOwner){ - return getLastDelined(SteamwarUser.get(schemOwner).getId()); + public static List getLastDeclined(SchematicNode node){ + return getLastDeclined(node.getId()); } - public static List getLastDelined(int schemOwner){ + public static List getLastDeclined(int node){ List lastDeclined = new LinkedList<>(); try{ - ResultSet lastRS = SQL.select("SELECT * FROM CheckedSchematic WHERE SchemOwner = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC", schemOwner); + ResultSet lastRS = SQL.select("SELECT * FROM CheckedSchematic WHERE NodeId = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC", node); while(lastRS.next()){ - String schemName = lastRS.getString("SchemName"); int validator = lastRS.getInt("Validator"); Timestamp startTime = lastRS.getTimestamp("StartTime"); Timestamp endTime = lastRS.getTimestamp("EndTime"); String declineReason = lastRS.getString("DeclineReason"); - lastDeclined.add(new CheckedSchematic(schemName, schemOwner, validator, startTime, endTime, declineReason, false)); + lastDeclined.add(new CheckedSchematic(node, validator, startTime, endTime, declineReason, false)); } }catch(SQLException e){ Bukkit.getLogger().log(Level.SEVERE, "getLastDeclined failed", e); @@ -87,15 +84,7 @@ public class CheckedSchematic { } public void remove() { - SQL.update("DELETE FROM CheckedSchematic WHERE SchemOwner = ? AND SchemName = ?", schemOwner, schemName); - } - - public String getSchemName() { - return schemName; - } - - public int getSchemOwner() { - return schemOwner; + SQL.update("DELETE FROM CheckedSchematic WHERE NodeId", node); } public int getValidator() { From 305a9dab2a2403459669621b242236bc8319544f Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 26 Jan 2021 00:44:56 +0100 Subject: [PATCH 04/75] Fix SchematicNode Type --- .../src/de/steamwar/sql/SchematicNode.java | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 5dd4d2f..d584ddb 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -39,22 +39,25 @@ public class SchematicNode { private static SchematicNode createSchematicNode(int owner, String name, int parent, String type) { SQL.update("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeType) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUE(NodeName), ParentNode = VALUE(ParentNode), NodeItem = VALUE(NodeItem), NodeType = VALUE(NodeType)", name, owner, parent, type); - return getSchematicNode(owner, name, type, parent); + return getSchematicNode(owner, name, parent); } - public static SchematicNode getSchematicNode(int owner, String name, String type, int parent) { - ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND NodeType = ? AND ParentNode = ?", owner, name, type, parent); + public static SchematicNode getSchematicNode(int owner, String name, int parent) { + ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?", owner, name, parent); try { - if(!set.next()) - return null; - return new SchematicNode(set); + while (set.next()) { + SchematicNode node = new SchematicNode(set); + if(!node.isDir()) + return node; + } + return null; }catch (SQLException e) { throw new SecurityException("Failed to load Schemnodes", e); } } - public static SchematicNode getSchematicNode(int owner, String name, String type, SchematicNode parent) { - return getSchematicNode(owner, name, type, parent.getId()); + public static SchematicNode getSchematicNode(int owner, String name, SchematicNode parent) { + return getSchematicNode(owner, name, parent.getId()); } public static List getSchematicNodeInNode(int parent) { @@ -74,11 +77,21 @@ public class SchematicNode { } public static SchematicNode getSchematicDirectory(int owner, String name, SchematicNode parent) { - return getSchematicNode(owner, name, DIR_TYPE, parent); + return getSchematicDirectory(owner, name, parent.getId()); } public static SchematicNode getSchematicDirectory(int owner, String name, int parent) { - return getSchematicNode(owner, name, DIR_TYPE, parent); + ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?", owner, name, parent); + try { + while (set.next()) { + SchematicNode node = new SchematicNode(set); + if(node.isDir()) + return node; + } + return null; + }catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); + } } public static SchematicNode getSchematicNode(int id) { From 65f5160dcac233f09ef7ff4845894f34de95ed63 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 26 Jan 2021 10:21:52 +0100 Subject: [PATCH 05/75] getNode() --- SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java index 9097420..7bbd963 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java @@ -102,4 +102,8 @@ public class CheckedSchematic { public String getDeclineReason() { return declineReason; } + + public int getNode() { + return node; + } } From 18c4318df3d893f16f65fcb2004504323a72adbf Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 27 Jan 2021 12:19:56 +0100 Subject: [PATCH 06/75] WIP Translation Layer and Node Merging --- .../src/de/steamwar/sql/NodeData.java | 157 --------------- .../src/de/steamwar/sql/NodeMember.java | 5 + .../src/de/steamwar/sql/Schematic.java | 160 +++++++++++++++ .../src/de/steamwar/sql/SchematicMember.java | 129 ++++++++++++ .../src/de/steamwar/sql/SchematicNode.java | 186 ++++++++++++++++-- 5 files changed, 464 insertions(+), 173 deletions(-) delete mode 100644 SpigotCore_Main/src/de/steamwar/sql/NodeData.java 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/sql/NodeData.java b/SpigotCore_Main/src/de/steamwar/sql/NodeData.java deleted file mode 100644 index c3d6e36..0000000 --- a/SpigotCore_Main/src/de/steamwar/sql/NodeData.java +++ /dev/null @@ -1,157 +0,0 @@ -/* - * 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.VersionedCallable; -import de.steamwar.core.VersionedRunnable; -import org.bukkit.entity.Player; - -import java.io.IOException; -import java.io.InputStream; -import java.sql.Blob; -import java.sql.ResultSet; -import java.sql.SQLException; - -public class NodeData { - - public static NodeData getSchemdata(int node) { - ResultSet set = SQL.select("SELECT NodeId, SchemFormat, NodeRank FROM NodeData WHERE NodeId = ?", node); - try { - if(!set.next()) - return null; - return new NodeData(set); - } catch (SQLException e) { - throw new SecurityException("Could not load NodeData", e); - } - } - - public static NodeData createNodeDate(SchematicNode node) { - SQL.update("INSERT INTO NodeData (NodeId) VALUES(?)", node.getId()); - return getSchemdata(node.getId()); - } - - private final int node; - private boolean schemFormat; - private int rank; - - private NodeData(ResultSet set) throws SQLException { - node = set.getInt("NodeId"); - schemFormat = set.getBoolean("SchemFormat"); - rank = set.getInt("NodeRank"); - } - - public Clipboard load() throws IOException, NoClipboardException { - ResultSet rs = SQL.select("SELECT SchemData FROM NodeData WHERE NodeId = ?", node); - try { - rs.next(); - Blob schemData = rs.getBlob("SchemData"); - if(schemData == null) - throw new IOException("SchemData is null"); - InputStream is = schemData.getBinaryStream(); - return VersionedCallable.call(new VersionedCallable<>(() -> Schematic_8.getClipboard(is, schemFormat), 8), - new VersionedCallable<>(() -> Schematic_14.getClipboard(is, schemFormat), 14)); - } catch (SQLException e) { - throw new IOException(e); - } - } - - public void loadToPlayer(Player player) throws IOException, NoClipboardException { - ResultSet rs = SQL.select("SELECT SchemData FROM NodeData WHERE NodeId = ?", node); - try { - rs.next(); - Blob blob = rs.getBlob("SchemData"); - if(blob == null) - throw new NoClipboardException(); - InputStream is = blob.getBinaryStream(); - VersionedRunnable.call(new VersionedRunnable(() -> Schematic_8.setPlayerClipboard(player, is, schemFormat), 8), - new VersionedRunnable(() -> Schematic_14.setPlayerClipboard(player, is, schemFormat), 14)); - } catch (SQLException e) { - throw new IOException(e); - } - } - - public void saveOldFormatFromPlayer(Player player) throws IOException, NoClipboardException { - saveFromPlayer(player, false); - } - - public void saveFromPlayer(Player player) throws IOException, NoClipboardException { - saveFromPlayer(player, true); - } - - public void saveFromBytes(byte[] bytes, boolean newFormat) { - Blob blob = SQL.blob(); - try { - blob.setBytes(1, bytes); - updateDatabase(blob, newFormat); - } catch (SQLException e) { - throw new SecurityException(e); - } - } - - private void saveFromPlayer(Player player, boolean newFormat) throws IOException, NoClipboardException { - Blob blob = SQL.blob(); - VersionedRunnable.call(new VersionedRunnable(() -> { - try { - blob.setBytes(1, Schematic_8.getPlayerClipboard(player)); - } catch (SQLException e) { - throw new RuntimeException(e.getMessage(), e); - } - updateDatabase(blob, false); - }, 8), new VersionedRunnable(() -> { - try { - blob.setBytes(1, Schematic_14.getPlayerClipboard(player, newFormat)); - } catch (SQLException exception) { - throw new RuntimeException(exception.getMessage(), exception); - } - updateDatabase(blob, newFormat); - }, 14)); - } - - private void updateDatabase(Blob blob, boolean newFormat) { - SQL.update("UPDATE NodeData SET SchemData = ?, SchemFormat = ? WHERE NodeId = ?", blob, newFormat, node); - schemFormat = newFormat; - } - - public int getNode() { - return node; - } - - public boolean isSchemFormat() { - return schemFormat; - } - - public int getRank() { - return rank; - } - - public void setRank(int rank) { - this.rank = rank; - updateDB(); - } - - private void updateDB() { - SQL.update("UPDATE NodeData SET Rank = ? WHERE NodeId = ?", rank, node); - } - - public void delete() { - SQL.update("DELETE FROM NodeData WHERE NodeId = ?", node); - } -} diff --git a/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java b/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java index 22c23c3..ad265e7 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java +++ b/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java @@ -61,6 +61,11 @@ public class NodeMember { } } + public static NodeMember createNodeMember(int node, int member) { + SQL.update("INSERT INTO NodeMember (NodeId, UserId) VALUES (?, ?)", node, member); + return getNodeMember(node, member); + } + final int node; final int member; 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..b4b2115 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -0,0 +1,160 @@ +/* + * 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.VersionedCallable; +import de.steamwar.core.VersionedRunnable; +import org.bukkit.entity.Player; + +import java.io.IOException; +import java.io.InputStream; +import java.sql.Blob; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class Schematic { + + private SchematicNode node; + + private Schematic(SchematicNode node) { + this.node = 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, 0, 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){ + return new Schematic(SchematicNode.getSchematicNode(schemOwner, schemName, 0)); + } + + public static Schematic getSchemFromDB(int schemID){ + return new Schematic(SchematicNode.getSchematicNode(schemID)); + } + + public static List getSchemsAccessibleByUser(UUID schemOwner){ + return getSchemsAccessibleByUser(SteamwarUser.get(schemOwner).getId()); + } + + public static List getSchemsAccessibleByUser(int schemOwner){ + List nodes = SchematicNode.getSchematicsAccessibleByUser(schemOwner, 0); + List schematics = new ArrayList<>(); + nodes.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){ + //Unsauber, dafür auch geaddede Schematics dabei + List schems = getSchemsAccessibleByUser(schemOwner); + for(int i = schems.size()-1; i >= 0; i--) + if(!schems.get(i).getSchemType().equals(schemType)) + schems.remove(i); + return schems; + } + + public static List getAllSchemsOfType(SchematicType schemType){ + List nodes = SchematicNode.getAllSchematicsOfType(schemType.toDB()); + List schematics = new ArrayList<>(); + nodes.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 Clipboard load() throws IOException, NoClipboardException { + return node.load(); + } + + public void loadToPlayer(Player player) throws IOException, NoClipboardException { + node.loadToPlayer(player); + } + + public void saveOldFormatFromPlayer(Player player) throws IOException, NoClipboardException { + node.saveOldFormatFromPlayer(player); + } + + public void saveFromPlayer(Player player) throws IOException, NoClipboardException { + node.saveFromPlayer(player); + } + + public void saveFromBytes(byte[] bytes, boolean newFormat) { + node.saveFromBytes(bytes, newFormat); + } + + public void remove(){ + node.delete(); + } + + 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..cf1f07e --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java @@ -0,0 +1,129 @@ +/* + * 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.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class SchematicMember { + + private NodeMember member; + + private SchematicMember(NodeMember member){ + this.member = member; + } + + public SchematicMember(String schemName, int schemOwner, int schemMember){ + this(schemName, schemOwner, schemMember, true); + } + + public SchematicMember(String schemName, UUID schemOwner, UUID schemMember){ + this(schemName, SteamwarUser.get(schemOwner).getId(), SteamwarUser.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, SteamwarUser.get(schemOwner).getId(), SteamwarUser.get(schemMember).getId()); + } + + public static SchematicMember getSchemMemberFromDB(String schemName, int schemOwner, int schemMember){ + ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND SchemOwner = ? AND Member = ?", schemName, schemOwner, schemMember); + try { + if(schematicMember == null || !schematicMember.next()){ + return null; + } + return new SchematicMember(schemName, schemOwner, schemMember, false); + } catch (SQLException e) { + throw new SecurityException("Could not get schemmember", e); + } + } + + public static SchematicMember getMemberBySchematic(String schemName, int schemMember){ + ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND Member = ?", schemName, schemMember); + try { + if(schematicMember == null || !schematicMember.next()){ + return null; + } + int schemOwner = schematicMember.getInt("SchemOwner"); + return new SchematicMember(schemName, schemOwner, schemMember, false); + } catch (SQLException e) { + throw new SecurityException("Could not get member", e); + } + } + + public static List getSchemMembers(String schemName, UUID schemOwner){ + return getSchemMembers(schemName, SteamwarUser.get(schemOwner).getId()); + } + + public static List getSchemMembers(String schemName, int schemOwner){ + ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND SchemOwner = ?", schemName, 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) { + throw new SecurityException("Could not get schemmembers", e); + } + } + + public static List getAccessibleSchems(UUID schemMember){ + return getAccessibleSchems(SteamwarUser.get(schemMember).getId()); + } + + public static List getAccessibleSchems(int schemMember){ + ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember 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) { + throw new SecurityException("Could not get accessible schems", e); + } + } + + public int getSchemOwner() { + return schemOwner; + } + + public String getSchemName() { + return schemName; + } + + public int getMember() { + return member; + } + + public void remove(){ + SQL.update("DELETE FROM SchemMember WHERE SchemOwner = ? AND SchemName = ? AND Member = ?", schemOwner, schemName, member); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index d584ddb..226f3c3 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -19,6 +19,14 @@ package de.steamwar.sql; +import com.sk89q.worldedit.extent.clipboard.Clipboard; +import de.steamwar.core.VersionedCallable; +import de.steamwar.core.VersionedRunnable; +import org.bukkit.entity.Player; + +import java.io.IOException; +import java.io.InputStream; +import java.sql.Blob; import java.sql.ResultSet; import java.sql.SQLException; import java.util.*; @@ -29,17 +37,28 @@ public class SchematicNode { public static final int ROOT_DIR = 0; public static SchematicNode createSchematic(int owner, String name, int parent) { - return createSchematicNode(owner, name, parent, SchematicType.Normal.toDB()); + return createSchematicNode(owner, name, parent, SchematicType.Normal.toDB(), ""); } public static SchematicNode createSchematicDirectory(int owner, String name, int parent) { - return createSchematicNode(owner, name, parent, DIR_TYPE); + return createSchematicNode(owner, name, parent, DIR_TYPE, ""); } - private static SchematicNode createSchematicNode(int owner, String name, int parent, String type) { - SQL.update("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeType) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUE(NodeName), ParentNode = VALUE(ParentNode), NodeItem = VALUE(NodeItem), NodeType = VALUE(NodeType)", - name, owner, parent, type); - return getSchematicNode(owner, name, parent); + public static SchematicNode createSchematicNode(int owner, String name, int parent, String type, String item) { + SQL.update("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeType, NodeItem) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUES(NodeName), ParentNode = VALUES(ParentNode), NodeItem = VALUES(NodeItem), NodeType = VALUES(NodeType), NodeItem = VALUES(NodeItem)", + name, owner, parent, type, item); + return getSchematicNode(owner, name, type, parent); + } + + public static SchematicNode getSchematicNode(int owner, String name, String type, int parent) { + ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ? AND NodeType = ?", owner, name, parent, type); + try { + if(!set.next()) + return null; + return new SchematicNode(set); + }catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); + } } public static SchematicNode getSchematicNode(int owner, String name, int parent) { @@ -94,8 +113,22 @@ public class SchematicNode { } } + public static SchematicNode getSchematicInParent(String name, int parent) { + ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); + try { + while (set.next()) { + SchematicNode node = new SchematicNode(set); + if(!node.isDir()) + return node; + } + return null; + }catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); + } + } + public static SchematicNode getSchematicNode(int id) { - ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeId", id); + ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeId = ?", id); try { if(!set.next()) return null; @@ -117,6 +150,18 @@ public class SchematicNode { } } + public static List getAllSchematicsOfType(String schemType) { + ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE SchemType = ?", schemType); + try { + List nodes = new ArrayList<>(); + while (set.next()) + nodes.add(new SchematicNode(set)); + return nodes; + }catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); + } + } + public static List getSchematicsOfType(int owner, String schemType, int parent) { List schems = getAllSchematicsOfType(owner, schemType); Map nodesInParent = new LinkedHashMap<>(); @@ -131,6 +176,21 @@ public class SchematicNode { } public static List getSchematicsAccessibleByUser(int user, int parent) { + if(parent != 0) { + SchematicNode node = SchematicNode.getSchematicNode(parent); + boolean isAdded = false; + while (node.getId() != 0) { + for (NodeMember member:node.getMembers()) { + if (member.getMember() == user) { + isAdded = true; + break; + } + } + node = SchematicNode.getSchematicNode(node.getParent()); + } + if(isAdded) + return getSchematicNodeInNode(parent); + } ResultSet set = SQL.select("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode FROM SchematicNode s LEFT JOIN NodeMember nm ON nm.NodeId = s.NodeId WHERE ( s.NodeOwner = ? OR nm.UserId = ? ) AND s.ParentNode = ? GROUP BY s.NodeId ORDER BY s.NodeName", user, user, parent); try{ List nodes = new ArrayList<>(); @@ -142,12 +202,26 @@ public class SchematicNode { } } + public static List getAllSchematicsAccessibleByUser(int user) { + ResultSet set = SQL.select("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode FROM SchematicNode s LEFT JOIN NodeMember nm ON nm.NodeId = s.NodeId WHERE s.NodeOwner = ? OR nm.UserId = ? GROUP BY s.NodeId ORDER BY s.NodeName", user, user); + try{ + List nodes = new ArrayList<>(); + while(set.next()) + nodes.add(new SchematicNode(set)); + return nodes; + }catch(SQLException e){ + throw new SecurityException("Failed listing schematics", e); + } + } + private final int id; private final int owner; private String name; private int parent; private String item; private String type; + private boolean schemFormat; + private int rank; private SchematicNode(ResultSet set) throws SQLException { id = set.getInt("NodeId"); @@ -156,6 +230,8 @@ public class SchematicNode { parent = set.getInt("ParentNode"); item = set.getString("NodeItem"); type = set.getString("NodeType"); + rank = set.getInt("NodeRank"); + schemFormat = set.getBoolean("SchemFormat"); } public int getId() { @@ -206,8 +282,20 @@ public class SchematicNode { return type.equals(DIR_TYPE); } + public boolean getSchemFormat() { + return schemFormat; + } + + public int getRank() { + return rank; + } + + public void setRank(int rank) { + this.rank = rank; + } + public SchematicType getSchemtype() { - if(!isDir()) + if(isDir()) throw new RuntimeException("Is Directory"); return SchematicType.fromDB(type); } @@ -216,12 +304,6 @@ public class SchematicNode { return SchematicNode.getSchematicNode(parent); } - public NodeData getNodeData() { - if(isDir()) - throw new SecurityException("Could not get NodeData of Directory"); - return NodeData.getSchemdata(id); - } - public boolean accessibleByUser(int user) { return NodeMember.getNodeMember(id, user) != null; } @@ -231,8 +313,8 @@ public class SchematicNode { } private void updateDB() { - SQL.update("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeItem, NodeType) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUE(NodeName), ParentNode = VALUE(ParentNode), NodeItem = VALUE(NodeItem), NodeType = VALUE(NodeType)", - name, owner, parent, item, type); + SQL.update("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ? WHERE NodeId = ?", + name, owner, parent, item, type, id); } public void delete() { @@ -241,4 +323,76 @@ public class SchematicNode { } SQL.update("DELETE FROM SchematicNode WHERE NodeId = ?", id); } + + public Clipboard load() throws IOException, NoClipboardException { + ResultSet rs = SQL.select("SELECT NodeData FROM SchematicNode WHERE NodeId = ?", id); + try { + rs.next(); + Blob schemData = rs.getBlob("SchemData"); + if(schemData == null) + throw new IOException("SchemData is null"); + InputStream is = schemData.getBinaryStream(); + return VersionedCallable.call(new VersionedCallable<>(() -> Schematic_8.getClipboard(is, schemFormat), 8), + new VersionedCallable<>(() -> Schematic_14.getClipboard(is, schemFormat), 14)); + } catch (SQLException e) { + throw new IOException(e); + } + } + + public void loadToPlayer(Player player) throws IOException, NoClipboardException { + ResultSet rs = SQL.select("SELECT NodeData FROM SchematicNode WHERE NodeId = ?", id); + try { + rs.next(); + Blob blob = rs.getBlob("SchemData"); + if(blob == null) + throw new NoClipboardException(); + InputStream is = blob.getBinaryStream(); + VersionedRunnable.call(new VersionedRunnable(() -> Schematic_8.setPlayerClipboard(player, is, schemFormat), 8), + new VersionedRunnable(() -> Schematic_14.setPlayerClipboard(player, is, schemFormat), 14)); + } catch (SQLException e) { + throw new IOException(e); + } + } + + public void saveOldFormatFromPlayer(Player player) throws IOException, NoClipboardException { + saveFromPlayer(player, false); + } + + public void saveFromPlayer(Player player) throws IOException, NoClipboardException { + saveFromPlayer(player, true); + } + + public void saveFromBytes(byte[] bytes, boolean newFormat) { + Blob blob = SQL.blob(); + try { + blob.setBytes(1, bytes); + updateDatabase(blob, newFormat); + } catch (SQLException e) { + throw new SecurityException(e); + } + } + + private void saveFromPlayer(Player player, boolean newFormat) throws IOException, NoClipboardException { + Blob blob = SQL.blob(); + VersionedRunnable.call(new VersionedRunnable(() -> { + try { + blob.setBytes(1, Schematic_8.getPlayerClipboard(player)); + } catch (SQLException e) { + throw new RuntimeException(e.getMessage(), e); + } + updateDatabase(blob, false); + }, 8), new VersionedRunnable(() -> { + try { + blob.setBytes(1, Schematic_14.getPlayerClipboard(player, newFormat)); + } catch (SQLException exception) { + throw new RuntimeException(exception.getMessage(), exception); + } + updateDatabase(blob, newFormat); + }, 14)); + } + + private void updateDatabase(Blob blob, boolean newFormat) { + SQL.update("UPDATE SchematicNode SET NodeData = ?, SchemFormat = ? WHERE NodeId = ?", blob, newFormat, id); + schemFormat = newFormat; + } } From c274c5e1d01e8d6bad0e268715ac1a80781e38ef Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 27 Jan 2021 13:47:47 +0100 Subject: [PATCH 07/75] Complete Translation Layer --- .../src/de/steamwar/sql/SchematicMember.java | 73 +++++-------------- 1 file changed, 18 insertions(+), 55 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java index cf1f07e..988efb3 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java @@ -19,11 +19,11 @@ package de.steamwar.sql; -import java.sql.ResultSet; -import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import java.util.Set; import java.util.UUID; +import java.util.stream.Collectors; public class SchematicMember { @@ -34,44 +34,23 @@ public class SchematicMember { } public SchematicMember(String schemName, int schemOwner, int schemMember){ - this(schemName, schemOwner, schemMember, true); + this(NodeMember.getNodeMember(SchematicNode.getSchematicNode(schemOwner, schemName, 0).getId(), schemMember)); } public SchematicMember(String schemName, UUID schemOwner, UUID schemMember){ - this(schemName, SteamwarUser.get(schemOwner).getId(), SteamwarUser.get(schemMember).getId(), true); - } - - private void updateDB(){ - SQL.update("INSERT INTO SchemMember (SchemName, SchemOwner, Member) VALUES (?, ?, ?)", schemName, schemOwner, member); + this(schemName, SteamwarUser.get(schemOwner).getId(), 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){ - ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND SchemOwner = ? AND Member = ?", schemName, schemOwner, schemMember); - try { - if(schematicMember == null || !schematicMember.next()){ - return null; - } - return new SchematicMember(schemName, schemOwner, schemMember, false); - } catch (SQLException e) { - throw new SecurityException("Could not get schemmember", e); - } + public static SchematicMember getSchemMemberFromDB(String schemName, int schemOwner, int schemMember) { + return new SchematicMember(NodeMember.getNodeMember(SchematicNode.getSchematicNode(schemOwner, schemName, 0).getOwner(), schemMember)); } public static SchematicMember getMemberBySchematic(String schemName, int schemMember){ - ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND Member = ?", schemName, schemMember); - try { - if(schematicMember == null || !schematicMember.next()){ - return null; - } - int schemOwner = schematicMember.getInt("SchemOwner"); - return new SchematicMember(schemName, schemOwner, schemMember, false); - } catch (SQLException e) { - throw new SecurityException("Could not get member", e); - } + return new SchematicMember(NodeMember.getSchematics(schemMember).stream().filter(member1 -> SchematicNode.getSchematicNode(member1.getNode()).getName().equals(schemName)).limit(1).collect(Collectors.toList()).get(0)); } public static List getSchemMembers(String schemName, UUID schemOwner){ @@ -79,17 +58,10 @@ public class SchematicMember { } public static List getSchemMembers(String schemName, int schemOwner){ - ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND SchemOwner = ?", schemName, 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) { - throw new SecurityException("Could not get schemmembers", e); - } + Set members = NodeMember.getNodeMembers(SchematicNode.getSchematicNode(schemOwner, schemName, 0).getId()); + List retMembers = new ArrayList<>(); + members.forEach(member1 -> retMembers.add(new SchematicMember(member1))); + return retMembers; } public static List getAccessibleSchems(UUID schemMember){ @@ -97,33 +69,24 @@ public class SchematicMember { } public static List getAccessibleSchems(int schemMember){ - ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember 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) { - throw new SecurityException("Could not get accessible schems", e); - } + List members = new ArrayList<>(); + NodeMember.getSchematics(schemMember).forEach(member1 -> members.add(new SchematicMember(member1))); + return members; } public int getSchemOwner() { - return schemOwner; + return SchematicNode.getSchematicNode(member.getNode()).getOwner(); } public String getSchemName() { - return schemName; + return SchematicNode.getSchematicNode(member.getNode()).getName(); } public int getMember() { - return member; + return member.getMember(); } public void remove(){ - SQL.update("DELETE FROM SchemMember WHERE SchemOwner = ? AND SchemName = ? AND Member = ?", schemOwner, schemName, member); + member.delete(); } } From 4b4d5f92bfed64624a13cdc73391b1cf88747caf Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 30 Jan 2021 00:22:27 +0100 Subject: [PATCH 08/75] Add Schematic Translation Layer --- SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java | 8 ++++++++ .../src/de/steamwar/sql/DownloadSchematic.java | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java index 7bbd963..741f637 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java @@ -106,4 +106,12 @@ public class CheckedSchematic { public int getNode() { return node; } + + public String getSchemName() { + return SchematicNode.getSchematicNode(node).getName(); + } + + public int getSchemOwner() { + return SchematicNode.getSchematicNode(node).getId(); + } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java index e941b8d..0bda812 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java @@ -45,4 +45,8 @@ public class DownloadSchematic { SQL.update("INSERT INTO SchemDownload (SchemID, Link) VALUES (?, ?) ON DUPLICATE KEY UPDATE Link = VALUES(Link)", schem.getId(), hash); return BASE + hash; } + + public static String getLink(Schematic schematic) { + return getLink(SchematicNode.getSchematicNode(schematic.getSchemID())); + } } From 3efe00c16f40ae2c5c77ee8ee00ddc9c989a6938 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 2 Feb 2021 18:02:15 +0100 Subject: [PATCH 09/75] Add Fix --- .../src/de/steamwar/sql/Schematic.java | 6 --- .../src/de/steamwar/sql/SchematicNode.java | 39 +++++++++++++++---- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java index b4b2115..038e574 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -20,15 +20,9 @@ package de.steamwar.sql; import com.sk89q.worldedit.extent.clipboard.Clipboard; -import de.steamwar.core.VersionedCallable; -import de.steamwar.core.VersionedRunnable; import org.bukkit.entity.Player; import java.io.IOException; -import java.io.InputStream; -import java.sql.Blob; -import java.sql.ResultSet; -import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.UUID; diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 226f3c3..a9f63fd 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -33,15 +33,12 @@ import java.util.*; public class SchematicNode { - private static final String DIR_TYPE = "directory"; - public static final int ROOT_DIR = 0; - public static SchematicNode createSchematic(int owner, String name, int parent) { return createSchematicNode(owner, name, parent, SchematicType.Normal.toDB(), ""); } public static SchematicNode createSchematicDirectory(int owner, String name, int parent) { - return createSchematicNode(owner, name, parent, DIR_TYPE, ""); + return createSchematicNode(owner, name, parent, null, ""); } public static SchematicNode createSchematicNode(int owner, String name, int parent, String type, String item) { @@ -222,6 +219,7 @@ public class SchematicNode { private String type; private boolean schemFormat; private int rank; + private boolean isDir; private SchematicNode(ResultSet set) throws SQLException { id = set.getInt("NodeId"); @@ -230,8 +228,13 @@ public class SchematicNode { parent = set.getInt("ParentNode"); item = set.getString("NodeItem"); type = set.getString("NodeType"); - rank = set.getInt("NodeRank"); - schemFormat = set.getBoolean("SchemFormat"); + if(type != null) { + isDir = false; + rank = set.getInt("NodeRank"); + schemFormat = set.getBoolean("SchemFormat"); + }else { + isDir = true; + } } public int getId() { @@ -270,27 +273,37 @@ public class SchematicNode { } public String getType() { + if(isDir) + throw new SecurityException("Node is Directory"); return type; } public void setType(String type) { + if(isDir) + throw new SecurityException("Node is Directory"); this.type = type; updateDB(); } public boolean isDir() { - return type.equals(DIR_TYPE); + return isDir; } public boolean getSchemFormat() { + if(isDir) + throw new SecurityException("Node is Directory"); return schemFormat; } public int getRank() { + if(isDir) + throw new SecurityException("Node is Directory"); return rank; } public void setRank(int rank) { + if(isDir) + throw new SecurityException("Node is Directory"); this.rank = rank; } @@ -325,6 +338,8 @@ public class SchematicNode { } public Clipboard load() throws IOException, NoClipboardException { + if(isDir) + throw new SecurityException("Node is Directory"); ResultSet rs = SQL.select("SELECT NodeData FROM SchematicNode WHERE NodeId = ?", id); try { rs.next(); @@ -340,6 +355,8 @@ public class SchematicNode { } public void loadToPlayer(Player player) throws IOException, NoClipboardException { + if(isDir) + throw new SecurityException("Node is Directory"); ResultSet rs = SQL.select("SELECT NodeData FROM SchematicNode WHERE NodeId = ?", id); try { rs.next(); @@ -355,14 +372,20 @@ public class SchematicNode { } 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); } public void saveFromBytes(byte[] bytes, boolean newFormat) { + if(isDir) + throw new SecurityException("Node is Directory"); Blob blob = SQL.blob(); try { blob.setBytes(1, bytes); @@ -373,6 +396,8 @@ public class SchematicNode { } private void saveFromPlayer(Player player, boolean newFormat) throws IOException, NoClipboardException { + if(isDir) + throw new SecurityException("Node is Directory"); Blob blob = SQL.blob(); VersionedRunnable.call(new VersionedRunnable(() -> { try { From bdfc5e8b0e6da8e0cdae6d87d5d4698a91b5524f Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 2 Mar 2021 19:17:33 +0100 Subject: [PATCH 10/75] Fixing SchemNode Translation Layer --- .../src/de/steamwar/sql/CheckedSchematic.java | 18 +++ .../src/de/steamwar/sql/Schematic.java | 9 +- .../src/de/steamwar/sql/SchematicNode.java | 104 +++++++++++------- 3 files changed, 91 insertions(+), 40 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java index 741f637..e7a5ccc 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java @@ -83,6 +83,24 @@ public class CheckedSchematic { return lastDeclined; } + 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; + } + public void remove() { SQL.update("DELETE FROM CheckedSchematic WHERE NodeId", node); } diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java index 038e574..1439379 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -48,7 +48,10 @@ public class Schematic { } public static Schematic getSchemFromDB(String schemName, int schemOwner){ - return new Schematic(SchematicNode.getSchematicNode(schemOwner, schemName, 0)); + SchematicNode node = SchematicNode.getSchematicNode(schemOwner, schemName, (Integer) null); + if(node == null) + return null; + return new Schematic(node); } public static Schematic getSchemFromDB(int schemID){ @@ -62,7 +65,9 @@ public class Schematic { public static List getSchemsAccessibleByUser(int schemOwner){ List nodes = SchematicNode.getSchematicsAccessibleByUser(schemOwner, 0); List schematics = new ArrayList<>(); - nodes.forEach(node1 -> schematics.add(new Schematic(node1))); + nodes.forEach(node1 -> { + if (!node1.isDir()) schematics.add(new Schematic(node1)); + }); return schematics; } diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index a9f63fd..a168cdc 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -33,39 +33,42 @@ import java.util.*; public class SchematicNode { - public static SchematicNode createSchematic(int owner, String name, int parent) { + public static SchematicNode createSchematic(int owner, String name, Integer parent) { return createSchematicNode(owner, name, parent, SchematicType.Normal.toDB(), ""); } - public static SchematicNode createSchematicDirectory(int owner, String name, int parent) { + public static SchematicNode createSchematicDirectory(int owner, String name, Integer parent) { return createSchematicNode(owner, name, parent, null, ""); } - public static SchematicNode createSchematicNode(int owner, String name, int parent, String type, String item) { + public static SchematicNode createSchematicNode(int owner, String name, Integer parent, String type, String item) { + if(parent == 0) + parent = null; SQL.update("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeType, NodeItem) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUES(NodeName), ParentNode = VALUES(ParentNode), NodeItem = VALUES(NodeItem), NodeType = VALUES(NodeType), NodeItem = VALUES(NodeItem)", name, owner, parent, type, item); - return getSchematicNode(owner, name, type, parent); + return getSchematicNode(owner, name, parent); } - public static SchematicNode getSchematicNode(int owner, String name, String type, int parent) { - ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ? AND NodeType = ?", owner, name, parent, type); - try { - if(!set.next()) - return null; - return new SchematicNode(set); - }catch (SQLException e) { - throw new SecurityException("Failed to load Schemnodes", e); + public static SchematicNode getSchematicNode(int owner, String name, Integer parent) { + if(parent != null && parent == 0) + parent = null; + ResultSet set; + if(parent == null) { + set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode is NULL", owner, name); + }else { + set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?", owner, name, parent); } - } - - public static SchematicNode getSchematicNode(int owner, String name, int parent) { - ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?", owner, name, parent); try { while (set.next()) { SchematicNode node = new SchematicNode(set); if(!node.isDir()) return node; } + List nodes = getSchematicNodeInNode(parent); + for (SchematicNode node:nodes) { + if(!node.isDir() && node.name.equals(name) && NodeMember.getNodeMember(node.getId(), owner) != null) + return node; + } return null; }catch (SQLException e) { throw new SecurityException("Failed to load Schemnodes", e); @@ -76,8 +79,15 @@ public class SchematicNode { return getSchematicNode(owner, name, parent.getId()); } - public static List getSchematicNodeInNode(int parent) { - ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE ParentNode = ?", parent); + public static List getSchematicNodeInNode(Integer parent) { + if(parent != null && parent == 0) + parent = null; + ResultSet set; + if(parent == null) { + set = SQL.select("SELECT * FROM SchematicNode WHERE ParentNode is NULL"); + }else { + set = SQL.select("SELECT * FROM SchematicNode WHERE ParentNode = ?", parent); + } try { List nodes = new ArrayList<>(); while (set.next()) @@ -92,12 +102,19 @@ public class SchematicNode { return getSchematicNodeInNode(parent.getId()); } - public static SchematicNode getSchematicDirectory(int owner, String name, SchematicNode parent) { - return getSchematicDirectory(owner, name, parent.getId()); + public static SchematicNode getSchematicDirectory(String name, SchematicNode parent) { + return getSchematicDirectory(name, parent.getId()); } - public static SchematicNode getSchematicDirectory(int owner, String name, int parent) { - ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?", owner, name, parent); + public static SchematicNode getSchematicDirectory(String name, Integer parent) { + if(parent != null && parent == 0) + parent = null; + ResultSet set; + if(parent == null) { + set = SQL.select("SELECT * FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name, parent); + }else { + set = SQL.select("SELECT * FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); + } try { while (set.next()) { SchematicNode node = new SchematicNode(set); @@ -110,8 +127,15 @@ public class SchematicNode { } } - public static SchematicNode getSchematicInParent(String name, int parent) { - ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); + public static SchematicNode getSchematicInParent(String name, Integer parent) { + if(parent != null && parent == 0) + parent = null; + ResultSet set; + if(parent == null) { + set = SQL.select("SELECT * FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name, parent); + }else { + set = SQL.select("SELECT * FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); + } try { while (set.next()) { SchematicNode node = new SchematicNode(set); @@ -159,7 +183,9 @@ public class SchematicNode { } } - public static List getSchematicsOfType(int owner, String schemType, int parent) { + public static List getSchematicsOfType(int owner, String schemType, Integer parent) { + if(parent != null && parent == 0) + parent = null; List schems = getAllSchematicsOfType(owner, schemType); Map nodesInParent = new LinkedHashMap<>(); for (SchematicNode schematicNode : schems) { @@ -172,8 +198,8 @@ public class SchematicNode { return new ArrayList<>(nodesInParent.values()); } - public static List getSchematicsAccessibleByUser(int user, int parent) { - if(parent != 0) { + public static List getSchematicsAccessibleByUser(int user, Integer parent) { + if(parent != null && parent != 0) { SchematicNode node = SchematicNode.getSchematicNode(parent); boolean isAdded = false; while (node.getId() != 0) { @@ -187,16 +213,18 @@ public class SchematicNode { } if(isAdded) return getSchematicNodeInNode(parent); + } else { + ResultSet set = SQL.select("SELECT * FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND s.ParentNode is NULL GROUP BY s.NodeId ORDER BY s.NodeName", user, user); + try{ + List nodes = new ArrayList<>(); + while(set.next()) + nodes.add(new SchematicNode(set)); + return nodes; + }catch(SQLException e){ + throw new SecurityException("Failed listing schematics", e); + } } - ResultSet set = SQL.select("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode FROM SchematicNode s LEFT JOIN NodeMember nm ON nm.NodeId = s.NodeId WHERE ( s.NodeOwner = ? OR nm.UserId = ? ) AND s.ParentNode = ? GROUP BY s.NodeId ORDER BY s.NodeName", user, user, parent); - try{ - List nodes = new ArrayList<>(); - while(set.next()) - nodes.add(new SchematicNode(set)); - return nodes; - }catch(SQLException e){ - throw new SecurityException("Failed listing schematics", e); - } + return null; } public static List getAllSchematicsAccessibleByUser(int user) { @@ -231,7 +259,7 @@ public class SchematicNode { if(type != null) { isDir = false; rank = set.getInt("NodeRank"); - schemFormat = set.getBoolean("SchemFormat"); + schemFormat = set.getBoolean("NodeFormat"); }else { isDir = true; } @@ -417,7 +445,7 @@ public class SchematicNode { } private void updateDatabase(Blob blob, boolean newFormat) { - SQL.update("UPDATE SchematicNode SET NodeData = ?, SchemFormat = ? WHERE NodeId = ?", blob, newFormat, id); + SQL.update("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?", blob, newFormat, id); schemFormat = newFormat; } } From 6310c9f92128eb7f8b32d6ea67cb0409e5bb61a3 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 3 Mar 2021 10:56:18 +0100 Subject: [PATCH 11/75] Fixing SchemNode Translation Layer --- .../src/de/steamwar/sql/SchematicMember.java | 31 +++++++++++++------ .../src/de/steamwar/sql/SchematicNode.java | 6 ++-- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java index 988efb3..fedd0a6 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java @@ -29,16 +29,20 @@ public class SchematicMember { private NodeMember member; - private SchematicMember(NodeMember member){ - this.member = member; + private SchematicMember(SchematicNode node, int user, boolean updateDB){ + if(updateDB) { + member = NodeMember.createNodeMember(node.getId(), user); + }else { + member = NodeMember.getNodeMember(node.getId(), user); + } } - public SchematicMember(String schemName, int schemOwner, int schemMember){ - this(NodeMember.getNodeMember(SchematicNode.getSchematicNode(schemOwner, schemName, 0).getId(), schemMember)); + public SchematicMember(String schemName, int schemOwner, int schemMember, boolean updateDb){ + this(SchematicNode.getSchematicNode(schemOwner, schemName, 0), schemMember, updateDb); } public SchematicMember(String schemName, UUID schemOwner, UUID schemMember){ - this(schemName, SteamwarUser.get(schemOwner).getId(), SteamwarUser.get(schemMember).getId()); + this(schemName, SteamwarUser.get(schemOwner).getId(), SteamwarUser.get(schemMember).getId(), true); } public static SchematicMember getSchemMemberFromDB(String schemName, UUID schemOwner, UUID schemMember){ @@ -46,11 +50,17 @@ public class SchematicMember { } public static SchematicMember getSchemMemberFromDB(String schemName, int schemOwner, int schemMember) { - return new SchematicMember(NodeMember.getNodeMember(SchematicNode.getSchematicNode(schemOwner, schemName, 0).getOwner(), schemMember)); + return getSchemMemberFromDB(schemName, schemOwner, schemMember, 0); + } + + public static SchematicMember getSchemMemberFromDB(String schemName, int schemOwner, int schemMember, int parent) { + SchematicNode node = SchematicNode.getSchematicNode(schemOwner, schemName, parent); + NodeMember member = NodeMember.getNodeMember(node.getId(), schemMember); + return member == null?null:new SchematicMember(node, schemMember, false); } public static SchematicMember getMemberBySchematic(String schemName, int schemMember){ - return new SchematicMember(NodeMember.getSchematics(schemMember).stream().filter(member1 -> SchematicNode.getSchematicNode(member1.getNode()).getName().equals(schemName)).limit(1).collect(Collectors.toList()).get(0)); + return new SchematicMember(SchematicNode.getSchematicNode(NodeMember.getSchematics(schemMember).stream().filter(member1 -> SchematicNode.getSchematicNode(member1.getNode()).getName().equals(schemName)).limit(1).collect(Collectors.toList()).get(0).node), schemMember, false); } public static List getSchemMembers(String schemName, UUID schemOwner){ @@ -58,9 +68,10 @@ public class SchematicMember { } public static List getSchemMembers(String schemName, int schemOwner){ - Set members = NodeMember.getNodeMembers(SchematicNode.getSchematicNode(schemOwner, schemName, 0).getId()); + SchematicNode node = SchematicNode.getSchematicNode(schemOwner, schemName, 0); + Set members = NodeMember.getNodeMembers(node.getId()); List retMembers = new ArrayList<>(); - members.forEach(member1 -> retMembers.add(new SchematicMember(member1))); + members.forEach(member1 -> retMembers.add(new SchematicMember(node, member1.member, false))); return retMembers; } @@ -70,7 +81,7 @@ public class SchematicMember { public static List getAccessibleSchems(int schemMember){ List members = new ArrayList<>(); - NodeMember.getSchematics(schemMember).forEach(member1 -> members.add(new SchematicMember(member1))); + NodeMember.getSchematics(schemMember).forEach(member1 -> members.add(new SchematicMember(SchematicNode.getSchematicNode(member1.node), member1.member, false))); return members; } diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index a168cdc..df2c121 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -371,9 +371,9 @@ public class SchematicNode { ResultSet rs = SQL.select("SELECT NodeData FROM SchematicNode WHERE NodeId = ?", id); try { rs.next(); - Blob schemData = rs.getBlob("SchemData"); + Blob schemData = rs.getBlob("NodeData"); if(schemData == null) - throw new IOException("SchemData is null"); + throw new IOException("NodeData is null"); InputStream is = schemData.getBinaryStream(); return VersionedCallable.call(new VersionedCallable<>(() -> Schematic_8.getClipboard(is, schemFormat), 8), new VersionedCallable<>(() -> Schematic_14.getClipboard(is, schemFormat), 14)); @@ -388,7 +388,7 @@ public class SchematicNode { ResultSet rs = SQL.select("SELECT NodeData FROM SchematicNode WHERE NodeId = ?", id); try { rs.next(); - Blob blob = rs.getBlob("SchemData"); + Blob blob = rs.getBlob("NodeData"); if(blob == null) throw new NoClipboardException(); InputStream is = blob.getBinaryStream(); From 8ca2d970344d26f09bc56569f8c02330be195b51 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 3 Mar 2021 21:05:04 +0100 Subject: [PATCH 12/75] Just a stupid error --- .../src/de/steamwar/inventory/SWListInv.java | 25 ++++++++++++++++++- .../src/de/steamwar/sql/SchematicNode.java | 10 +++----- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java b/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java index 6803257..8d8cdc8 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; @@ -115,7 +116,29 @@ public class SWListInv extends SWInventory { return onlinePlayers; } - public static List> getSchemList(SchematicType type, int steamwarUserId){ + public static List> getSchemList(SchematicType type, int steamwarUserId){ + List> schemList = new ArrayList<>(); + + List schems; + if(type == null) + schems = Schematic.getSchemsAccessibleByUser(steamwarUserId); + else + schems = Schematic.getSchemsOfType(steamwarUserId, type); + + for(Schematic s : schems){ + Material m; + if(s.getItem().isEmpty()) + m = SWItem.getMaterial("CAULDRON_ITEM"); + else + m = SWItem.getMaterial(s.getItem()); + SWItem item = new SWItem(m,"§e" + s.getSchemName()); + item.setEnchanted(s.getSchemType().fightType()); + schemList.add(new SWListEntry<>(item, s)); + } + return schemList; + } + + public static List> getSchemnodeList(SchematicType type, int steamwarUserId){ List> schemList = new ArrayList<>(); List schems; diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index df2c121..58ff88d 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -160,7 +160,7 @@ public class SchematicNode { } public static List getAllSchematicsOfType(int owner, String schemType) { - ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND SchemType = ?", owner, schemType); + ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?", owner, schemType); try { List nodes = new ArrayList<>(); while (set.next()) @@ -172,7 +172,7 @@ public class SchematicNode { } public static List getAllSchematicsOfType(String schemType) { - ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE SchemType = ?", schemType); + ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeType = ?", schemType); try { List nodes = new ArrayList<>(); while (set.next()) @@ -184,8 +184,6 @@ public class SchematicNode { } public static List getSchematicsOfType(int owner, String schemType, Integer parent) { - if(parent != null && parent == 0) - parent = null; List schems = getAllSchematicsOfType(owner, schemType); Map nodesInParent = new LinkedHashMap<>(); for (SchematicNode schematicNode : schems) { @@ -354,8 +352,8 @@ public class SchematicNode { } private void updateDB() { - SQL.update("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ? WHERE NodeId = ?", - name, owner, parent, item, type, id); + SQL.update("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?", + name, owner, parent, item, type, rank, id); } public void delete() { From a2d8d1eb3a607d9b2786d25ac3c31726a6e22711 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 14 Mar 2021 18:08:03 +0100 Subject: [PATCH 13/75] Fixing minor bugs --- .../src/de/steamwar/sql/SchematicNode.java | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 58ff88d..12f0c90 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -30,6 +30,7 @@ import java.sql.Blob; import java.sql.ResultSet; import java.sql.SQLException; import java.util.*; +import java.util.function.Predicate; public class SchematicNode { @@ -184,7 +185,8 @@ public class SchematicNode { } public static List getSchematicsOfType(int owner, String schemType, Integer parent) { - List schems = getAllSchematicsOfType(owner, schemType); + List schems = getAllSchematicsAccessibleByUser(owner); + schems.removeIf(node -> !node.getType().equals(schemType)); Map nodesInParent = new LinkedHashMap<>(); for (SchematicNode schematicNode : schems) { SchematicNode currentNode = schematicNode; @@ -222,7 +224,7 @@ public class SchematicNode { throw new SecurityException("Failed listing schematics", e); } } - return null; + return Collections.emptyList(); } public static List getAllSchematicsAccessibleByUser(int user) { @@ -237,15 +239,29 @@ public class SchematicNode { } } + public static List deepGet(Integer parent, Predicate filter) { + List finalList = new ArrayList<>(); + List nodes = SchematicNode.getSchematicNodeInNode(parent); + nodes.forEach(node -> { + if(node.isDir()) { + finalList.addAll(deepGet(node.getId(), filter)); + }else { + if(filter.test(node)) + finalList.add(node); + } + }); + return finalList; + } + private final int id; private final int owner; private String name; - private int parent; + private Integer parent; private String item; private String type; private boolean schemFormat; private int rank; - private boolean isDir; + private final boolean isDir; private SchematicNode(ResultSet set) throws SQLException { id = set.getInt("NodeId"); @@ -353,7 +369,7 @@ public class SchematicNode { private void updateDB() { SQL.update("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?", - name, owner, parent, item, type, rank, id); + name, owner, parent == 0?null:parent, item, type, rank, id); } public void delete() { From a8dd024fbaca18ab264f40122d2e29f43634cf71 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Fri, 11 Jun 2021 17:18:28 +0200 Subject: [PATCH 14/75] idk --- .../src/de/steamwar/sql/SchematicNode.java | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 12f0c90..f4128ff 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -55,9 +55,9 @@ public class SchematicNode { parent = null; ResultSet set; if(parent == null) { - set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode is NULL", owner, name); + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode is NULL", owner, name); }else { - set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?", owner, name, parent); + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?", owner, name, parent); } try { while (set.next()) { @@ -85,9 +85,9 @@ public class SchematicNode { parent = null; ResultSet set; if(parent == null) { - set = SQL.select("SELECT * FROM SchematicNode WHERE ParentNode is NULL"); + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem FROM SchematicNode WHERE ParentNode is NULL"); }else { - set = SQL.select("SELECT * FROM SchematicNode WHERE ParentNode = ?", parent); + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem FROM SchematicNode WHERE ParentNode = ?", parent); } try { List nodes = new ArrayList<>(); @@ -112,9 +112,9 @@ public class SchematicNode { parent = null; ResultSet set; if(parent == null) { - set = SQL.select("SELECT * FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name, parent); + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name, parent); }else { - set = SQL.select("SELECT * FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); } try { while (set.next()) { @@ -150,7 +150,7 @@ public class SchematicNode { } public static SchematicNode getSchematicNode(int id) { - ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeId = ?", id); + ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem FROM SchematicNode WHERE NodeId = ?", id); try { if(!set.next()) return null; @@ -161,7 +161,7 @@ public class SchematicNode { } public static List getAllSchematicsOfType(int owner, String schemType) { - ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?", owner, schemType); + ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?", owner, schemType); try { List nodes = new ArrayList<>(); while (set.next()) @@ -173,7 +173,7 @@ public class SchematicNode { } public static List getAllSchematicsOfType(String schemType) { - ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeType = ?", schemType); + ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem FROM SchematicNode WHERE NodeType = ?", schemType); try { List nodes = new ArrayList<>(); while (set.next()) @@ -214,7 +214,7 @@ public class SchematicNode { if(isAdded) return getSchematicNodeInNode(parent); } else { - ResultSet set = SQL.select("SELECT * FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND s.ParentNode is NULL GROUP BY s.NodeId ORDER BY s.NodeName", user, user); + ResultSet set = SQL.select("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND s.ParentNode is NULL GROUP BY s.NodeId ORDER BY s.NodeName", user, user); try{ List nodes = new ArrayList<>(); while(set.next()) @@ -243,16 +243,28 @@ public class SchematicNode { List finalList = new ArrayList<>(); List nodes = SchematicNode.getSchematicNodeInNode(parent); nodes.forEach(node -> { - if(node.isDir()) { + if (node.isDir()) { finalList.addAll(deepGet(node.getId(), filter)); - }else { - if(filter.test(node)) + } else { + if (filter.test(node)) finalList.add(node); } }); return finalList; } + public static List getBaselist(int owner) { + ResultSet set = SQL.select("SELECT "); + try { + List nodes = new ArrayList<>(); + while (set.next()) + nodes.add(new SchematicNode(set)); + return nodes; + } catch (SQLException e) { + throw new SecurityException("Failed listing schematics", e); + } + } + private final int id; private final int owner; private String name; From 99a205526536215be885a70e981c190730b4a953 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 10 Jul 2021 12:13:32 +0200 Subject: [PATCH 15/75] Changing Things around --- .../src/de/steamwar/sql/SchematicNode.java | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index f4128ff..d310a4a 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -253,18 +253,6 @@ public class SchematicNode { return finalList; } - public static List getBaselist(int owner) { - ResultSet set = SQL.select("SELECT "); - try { - List nodes = new ArrayList<>(); - while (set.next()) - nodes.add(new SchematicNode(set)); - return nodes; - } catch (SQLException e) { - throw new SecurityException("Failed listing schematics", e); - } - } - private final int id; private final int owner; private String name; From cffb8798e16055df0bfcfe495d3c8c825f45e755 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 10 Jul 2021 12:19:55 +0200 Subject: [PATCH 16/75] add Generate Breadcrumbs --- .../src/de/steamwar/sql/SchematicNode.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index d310a4a..c36c1a9 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -296,7 +296,7 @@ public class SchematicNode { updateDB(); } - public int getParent() { + public Integer getParent() { return parent; } @@ -367,13 +367,28 @@ public class SchematicNode { return NodeMember.getNodeMembers(id); } + public String generateBreadcrumbs() { + return generateBreadcrumbs("/"); + } + + public String generateBreadcrumbs(String split) { + StringBuilder builder = new StringBuilder(getName()); + SchematicNode currentNode = this; + while (currentNode.parent != null) { + currentNode = currentNode.getParentNode(); + builder.insert(0, split) + .insert(0, currentNode.getName()); + } + return builder.toString(); + } + private void updateDB() { SQL.update("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?", - name, owner, parent == 0?null:parent, item, type, rank, id); + name, owner, parent == 0 ? null : parent, item, type, rank, id); } public void delete() { - if(isDir()) { + if (isDir()) { getSchematicNodeInNode(getId()).forEach(SchematicNode::delete); } SQL.update("DELETE FROM SchematicNode WHERE NodeId = ?", id); From 20996e48ddc51d50695bd34d4dfb2024de4ee437 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 10 Jul 2021 13:07:46 +0200 Subject: [PATCH 17/75] Fix NodeRank --- .../src/de/steamwar/sql/SchematicNode.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index c36c1a9..d3627b9 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -55,9 +55,9 @@ public class SchematicNode { parent = null; ResultSet set; if(parent == null) { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode is NULL", owner, name); + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode is NULL", owner, name); }else { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?", owner, name, parent); + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?", owner, name, parent); } try { while (set.next()) { @@ -85,9 +85,9 @@ public class SchematicNode { parent = null; ResultSet set; if(parent == null) { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem FROM SchematicNode WHERE ParentNode is NULL"); + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE ParentNode is NULL"); }else { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem FROM SchematicNode WHERE ParentNode = ?", parent); + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE ParentNode = ?", parent); } try { List nodes = new ArrayList<>(); @@ -112,9 +112,9 @@ public class SchematicNode { parent = null; ResultSet set; if(parent == null) { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name, parent); + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name, parent); }else { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); } try { while (set.next()) { @@ -133,9 +133,9 @@ public class SchematicNode { parent = null; ResultSet set; if(parent == null) { - set = SQL.select("SELECT * FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name, parent); + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name, parent); }else { - set = SQL.select("SELECT * FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); } try { while (set.next()) { @@ -150,7 +150,7 @@ public class SchematicNode { } public static SchematicNode getSchematicNode(int id) { - ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem FROM SchematicNode WHERE NodeId = ?", id); + ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE NodeId = ?", id); try { if(!set.next()) return null; @@ -161,7 +161,7 @@ public class SchematicNode { } public static List getAllSchematicsOfType(int owner, String schemType) { - ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?", owner, schemType); + ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?", owner, schemType); try { List nodes = new ArrayList<>(); while (set.next()) @@ -173,7 +173,7 @@ public class SchematicNode { } public static List getAllSchematicsOfType(String schemType) { - ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem FROM SchematicNode WHERE NodeType = ?", schemType); + ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE NodeType = ?", schemType); try { List nodes = new ArrayList<>(); while (set.next()) @@ -214,7 +214,7 @@ public class SchematicNode { if(isAdded) return getSchematicNodeInNode(parent); } else { - ResultSet set = SQL.select("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND s.ParentNode is NULL GROUP BY s.NodeId ORDER BY s.NodeName", user, user); + ResultSet set = SQL.select("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND s.ParentNode is NULL GROUP BY s.NodeId ORDER BY s.NodeName", user, user); try{ List nodes = new ArrayList<>(); while(set.next()) @@ -228,7 +228,7 @@ public class SchematicNode { } public static List getAllSchematicsAccessibleByUser(int user) { - ResultSet set = SQL.select("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode FROM SchematicNode s LEFT JOIN NodeMember nm ON nm.NodeId = s.NodeId WHERE s.NodeOwner = ? OR nm.UserId = ? GROUP BY s.NodeId ORDER BY s.NodeName", user, user); + ResultSet set = SQL.select("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank FROM SchematicNode s LEFT JOIN NodeMember nm ON nm.NodeId = s.NodeId WHERE s.NodeOwner = ? OR nm.UserId = ? GROUP BY s.NodeId ORDER BY s.NodeName", user, user); try{ List nodes = new ArrayList<>(); while(set.next()) From 3aa896771e98171d654e9bac28c903e40fd24f74 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Fri, 16 Jul 2021 15:09:58 +0200 Subject: [PATCH 18/75] Latest and Greatest Changes --- .../comms/packets/PrepareSchemPacket.java | 8 +- .../src/de/steamwar/sql/SchematicNode.java | 288 ++++++++++-------- 2 files changed, 167 insertions(+), 129 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/PrepareSchemPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/PrepareSchemPacket.java index 99f2ecc..aba2380 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/packets/PrepareSchemPacket.java +++ b/SpigotCore_Main/src/de/steamwar/comms/packets/PrepareSchemPacket.java @@ -21,17 +21,17 @@ 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; public class PrepareSchemPacket extends SpigotPacket{ private final SteamwarUser user; - private final Schematic schematic; + private final SchematicNode schematic; private final SchematicType schematicType; - public PrepareSchemPacket(SteamwarUser user, Schematic schematic, SchematicType schematicType){ + public PrepareSchemPacket(SteamwarUser user, SchematicNode schematic, SchematicType schematicType) { this.user = user; this.schematic = schematic; this.schematicType = schematicType; @@ -45,7 +45,7 @@ public class PrepareSchemPacket extends SpigotPacket{ @Override public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { byteArrayDataOutput.writeInt(user.getId()); - byteArrayDataOutput.writeInt(schematic.getSchemID()); + byteArrayDataOutput.writeInt(schematic.getId()); byteArrayDataOutput.writeUTF(schematicType.toDB()); } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index d3627b9..f3d7389 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -29,6 +29,8 @@ import java.io.InputStream; import java.sql.Blob; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Timestamp; +import java.time.Instant; import java.util.*; import java.util.function.Predicate; @@ -43,59 +45,33 @@ public class SchematicNode { } public static SchematicNode createSchematicNode(int owner, String name, Integer parent, String type, String item) { - if(parent == 0) + if (parent == 0) parent = null; SQL.update("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeType, NodeItem) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUES(NodeName), ParentNode = VALUES(ParentNode), NodeItem = VALUES(NodeItem), NodeType = VALUES(NodeType), NodeItem = VALUES(NodeItem)", name, owner, parent, type, item); return getSchematicNode(owner, name, parent); } - public static SchematicNode getSchematicNode(int owner, String name, Integer parent) { - if(parent != null && parent == 0) - parent = null; - ResultSet set; - if(parent == null) { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode is NULL", owner, name); - }else { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?", owner, name, parent); - } - try { - while (set.next()) { - SchematicNode node = new SchematicNode(set); - if(!node.isDir()) - return node; - } - List nodes = getSchematicNodeInNode(parent); - for (SchematicNode node:nodes) { - if(!node.isDir() && node.name.equals(name) && NodeMember.getNodeMember(node.getId(), owner) != null) - return node; - } - return null; - }catch (SQLException e) { - throw new SecurityException("Failed to load Schemnodes", e); - } - } + private Timestamp lastUpdate; public static SchematicNode getSchematicNode(int owner, String name, SchematicNode parent) { return getSchematicNode(owner, name, parent.getId()); } - public static List getSchematicNodeInNode(Integer parent) { - if(parent != null && parent == 0) - parent = null; - ResultSet set; - if(parent == null) { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE ParentNode is NULL"); - }else { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE ParentNode = ?", parent); - } - try { - List nodes = new ArrayList<>(); - while (set.next()) - nodes.add(new SchematicNode(set)); - return nodes; - }catch (SQLException e) { - throw new SecurityException("Failed to load Schemnodes", e); + private SchematicNode(ResultSet set) throws SQLException { + id = set.getInt("NodeId"); + owner = set.getInt("NodeOwner"); + name = set.getString("NodeName"); + parent = set.getInt("ParentNode"); + item = set.getString("NodeItem"); + type = set.getString("NodeType"); + lastUpdate = set.getTimestamp("LastUpdate"); + if (type != null) { + isDir = false; + rank = set.getInt("NodeRank"); + schemFormat = set.getBoolean("NodeFormat"); + } else { + isDir = true; } } @@ -107,14 +83,58 @@ public class SchematicNode { return getSchematicDirectory(name, parent.getId()); } + public static SchematicNode getSchematicNode(int owner, String name, Integer parent) { + if (parent != null && parent == 0) + parent = null; + ResultSet set; + if (parent == null) { + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode is NULL", owner, name); + } else { + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?", owner, name, parent); + } + try { + while (set.next()) { + SchematicNode node = new SchematicNode(set); + return node; + } + List nodes = getSchematicNodeInNode(parent); + for (SchematicNode node:nodes) { + if (node.name.equals(name) && NodeMember.getNodeMember(node.getId(), owner) != null) + return node; + } + return null; + }catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); + } + } + + public static List getSchematicNodeInNode(Integer parent) { + if(parent != null && parent == 0) + parent = null; + ResultSet set; + if(parent == null) { + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode is NULL"); + }else { + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ?", parent); + } + try { + List nodes = new ArrayList<>(); + while (set.next()) + nodes.add(new SchematicNode(set)); + return nodes; + }catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); + } + } + public static SchematicNode getSchematicDirectory(String name, Integer parent) { if(parent != null && parent == 0) parent = null; ResultSet set; if(parent == null) { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name, parent); + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name, parent); }else { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); } try { while (set.next()) { @@ -133,9 +153,9 @@ public class SchematicNode { parent = null; ResultSet set; if(parent == null) { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name, parent); + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name); }else { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); + set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); } try { while (set.next()) { @@ -150,36 +170,12 @@ public class SchematicNode { } public static SchematicNode getSchematicNode(int id) { - ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE NodeId = ?", id); + ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ?", id); try { - if(!set.next()) + if (!set.next()) return null; return new SchematicNode(set); - }catch (SQLException e) { - throw new SecurityException("Failed to load Schemnodes", e); - } - } - - public static List getAllSchematicsOfType(int owner, String schemType) { - ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?", owner, schemType); - try { - List nodes = new ArrayList<>(); - while (set.next()) - nodes.add(new SchematicNode(set)); - return nodes; - }catch (SQLException e) { - throw new SecurityException("Failed to load Schemnodes", e); - } - } - - public static List getAllSchematicsOfType(String schemType) { - ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank FROM SchematicNode WHERE NodeType = ?", schemType); - try { - List nodes = new ArrayList<>(); - while (set.next()) - nodes.add(new SchematicNode(set)); - return nodes; - }catch (SQLException e) { + } catch (SQLException e) { throw new SecurityException("Failed to load Schemnodes", e); } } @@ -198,44 +194,27 @@ public class SchematicNode { return new ArrayList<>(nodesInParent.values()); } - public static List getSchematicsAccessibleByUser(int user, Integer parent) { - if(parent != null && parent != 0) { - SchematicNode node = SchematicNode.getSchematicNode(parent); - boolean isAdded = false; - while (node.getId() != 0) { - for (NodeMember member:node.getMembers()) { - if (member.getMember() == user) { - isAdded = true; - break; - } - } - node = SchematicNode.getSchematicNode(node.getParent()); - } - if(isAdded) - return getSchematicNodeInNode(parent); - } else { - ResultSet set = SQL.select("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND s.ParentNode is NULL GROUP BY s.NodeId ORDER BY s.NodeName", user, user); - try{ - List nodes = new ArrayList<>(); - while(set.next()) - nodes.add(new SchematicNode(set)); - return nodes; - }catch(SQLException e){ - throw new SecurityException("Failed listing schematics", e); - } - } - return Collections.emptyList(); - } - - public static List getAllSchematicsAccessibleByUser(int user) { - ResultSet set = SQL.select("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank FROM SchematicNode s LEFT JOIN NodeMember nm ON nm.NodeId = s.NodeId WHERE s.NodeOwner = ? OR nm.UserId = ? GROUP BY s.NodeId ORDER BY s.NodeName", user, user); - try{ + public static List getAllSchematicsOfType(int owner, String schemType) { + ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?", owner, schemType); + try { List nodes = new ArrayList<>(); - while(set.next()) + while (set.next()) nodes.add(new SchematicNode(set)); return nodes; - }catch(SQLException e){ - throw new SecurityException("Failed listing schematics", e); + } catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); + } + } + + public static List getAllSchematicsOfType(String schemType) { + ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeType = ?", schemType); + try { + List nodes = new ArrayList<>(); + while (set.next()) + nodes.add(new SchematicNode(set)); + return nodes; + } catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); } } @@ -253,6 +232,47 @@ public class SchematicNode { return finalList; } + public static List getSchematicsAccessibleByUser(int user, Integer parent) { + if (parent != null && parent != 0) { + SchematicNode node = SchematicNode.getSchematicNode(parent); + boolean isAdded = false; + while (node.getId() != 0) { + for (NodeMember member:node.getMembers()) { + if (member.getMember() == user) { + isAdded = true; + break; + } + } + node = SchematicNode.getSchematicNode(node.getParent()); + } + if(isAdded) + return getSchematicNodeInNode(parent); + } else { + ResultSet set = SQL.select("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND s.ParentNode is NULL GROUP BY s.NodeId ORDER BY s.NodeName", user, user); + try{ + List nodes = new ArrayList<>(); + while(set.next()) + nodes.add(new SchematicNode(set)); + return nodes; + }catch(SQLException e){ + throw new SecurityException("Failed listing schematics", e); + } + } + return Collections.emptyList(); + } + + public static List getAllSchematicsAccessibleByUser(int user) { + ResultSet set = SQL.select("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember nm ON nm.NodeId = s.NodeId WHERE s.NodeOwner = ? OR nm.UserId = ? GROUP BY s.NodeId ORDER BY s.NodeName", user, user); + try{ + List nodes = new ArrayList<>(); + while(set.next()) + nodes.add(new SchematicNode(set)); + return nodes; + }catch(SQLException e){ + throw new SecurityException("Failed listing schematics", e); + } + } + private final int id; private final int owner; private String name; @@ -263,19 +283,28 @@ public class SchematicNode { private int rank; private final boolean isDir; - private SchematicNode(ResultSet set) throws SQLException { - id = set.getInt("NodeId"); - owner = set.getInt("NodeOwner"); - name = set.getString("NodeName"); - parent = set.getInt("ParentNode"); - item = set.getString("NodeItem"); - type = set.getString("NodeType"); - if(type != null) { - isDir = false; - rank = set.getInt("NodeRank"); - schemFormat = set.getBoolean("NodeFormat"); - }else { - isDir = true; + public static List filterSchems(int user, Predicate filter) { + List finalList = new ArrayList<>(); + List nodes = SchematicNode.getSchematicsAccessibleByUser(user, null); + nodes.forEach(node -> { + if (node.isDir()) { + finalList.addAll(deepGet(node.getId(), filter)); + } else { + if (filter.test(node)) + finalList.add(node); + } + }); + return finalList; + } + + public static Integer countNodes() { + ResultSet set = SQL.select("SELECT COUNT(NodeId) AS 'count' FROM SchematicNode"); + try { + if (set.next()) + return set.getInt("count"); + return 0; + } catch (SQLException e) { + throw new SecurityException("Failed listing schematics", e); } } @@ -367,17 +396,25 @@ public class SchematicNode { return NodeMember.getNodeMembers(id); } - public String generateBreadcrumbs() { - return generateBreadcrumbs("/"); + public Timestamp getLastUpdate() { + return lastUpdate; } - public String generateBreadcrumbs(String split) { + public String generateBreadcrumbs(SteamwarUser user) { + return generateBreadcrumbs("/", user); + } + + public String generateBreadcrumbs(String split, SteamwarUser user) { StringBuilder builder = new StringBuilder(getName()); SchematicNode currentNode = this; - while (currentNode.parent != null) { + if (currentNode.isDir()) builder.append("/"); + while (currentNode.getParentNode() != null) { currentNode = currentNode.getParentNode(); builder.insert(0, split) .insert(0, currentNode.getName()); + if (currentNode.getMembers().stream().anyMatch(member -> member.getMember() == user.getId())) { + break; + } } return builder.toString(); } @@ -385,6 +422,7 @@ public class SchematicNode { private void updateDB() { SQL.update("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?", name, owner, parent == 0 ? null : parent, item, type, rank, id); + this.lastUpdate = Timestamp.from(Instant.now()); } public void delete() { From f29309f3ec1d0118693edf8f1f82f7323b3825df Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 18 Jul 2021 19:37:33 +0200 Subject: [PATCH 19/75] More Improvements! --- .../src/de/steamwar/inventory/SWListInv.java | 25 +-- .../de/steamwar/sql/DownloadSchematic.java | 4 - .../src/de/steamwar/sql/Schematic.java | 159 ------------------ .../src/de/steamwar/sql/SchematicMember.java | 103 ------------ .../src/de/steamwar/sql/SchematicNode.java | 9 + 5 files changed, 10 insertions(+), 290 deletions(-) delete mode 100644 SpigotCore_Main/src/de/steamwar/sql/Schematic.java delete mode 100644 SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java b/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java index 8d8cdc8..5f35f55 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java @@ -19,7 +19,6 @@ package de.steamwar.inventory; -import de.steamwar.sql.Schematic; import de.steamwar.sql.SchematicNode; import de.steamwar.sql.SchematicType; import org.bukkit.Bukkit; @@ -116,34 +115,12 @@ public class SWListInv extends SWInventory { return onlinePlayers; } - public static List> getSchemList(SchematicType type, int steamwarUserId){ - List> schemList = new ArrayList<>(); - - List schems; - if(type == null) - schems = Schematic.getSchemsAccessibleByUser(steamwarUserId); - else - schems = Schematic.getSchemsOfType(steamwarUserId, type); - - for(Schematic s : schems){ - Material m; - if(s.getItem().isEmpty()) - m = SWItem.getMaterial("CAULDRON_ITEM"); - else - m = SWItem.getMaterial(s.getItem()); - SWItem item = new SWItem(m,"§e" + s.getSchemName()); - item.setEnchanted(s.getSchemType().fightType()); - schemList.add(new SWListEntry<>(item, s)); - } - return schemList; - } - public static List> getSchemnodeList(SchematicType type, int steamwarUserId){ List> schemList = new ArrayList<>(); List schems; if(type == null) - schems = SchematicNode.getSchematicsAccessibleByUser(steamwarUserId, 0); + schems = SchematicNode.filterSchems(steamwarUserId, node -> true); else schems = SchematicNode.getSchematicsOfType(steamwarUserId, type.toDB(), 0); diff --git a/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java index 0bda812..e941b8d 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java @@ -45,8 +45,4 @@ public class DownloadSchematic { SQL.update("INSERT INTO SchemDownload (SchemID, Link) VALUES (?, ?) ON DUPLICATE KEY UPDATE Link = VALUES(Link)", schem.getId(), hash); return BASE + hash; } - - public static String getLink(Schematic schematic) { - return getLink(SchematicNode.getSchematicNode(schematic.getSchemID())); - } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java deleted file mode 100644 index 1439379..0000000 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ /dev/null @@ -1,159 +0,0 @@ -/* - * 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 org.bukkit.entity.Player; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -public class Schematic { - - private SchematicNode node; - - private Schematic(SchematicNode node) { - this.node = 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, 0, 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, (Integer) null); - if(node == null) - return null; - return new Schematic(node); - } - - public static Schematic getSchemFromDB(int schemID){ - return new Schematic(SchematicNode.getSchematicNode(schemID)); - } - - public static List getSchemsAccessibleByUser(UUID schemOwner){ - return getSchemsAccessibleByUser(SteamwarUser.get(schemOwner).getId()); - } - - public static List getSchemsAccessibleByUser(int schemOwner){ - List nodes = SchematicNode.getSchematicsAccessibleByUser(schemOwner, 0); - List schematics = new ArrayList<>(); - nodes.forEach(node1 -> { - if (!node1.isDir()) 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){ - //Unsauber, dafür auch geaddede Schematics dabei - List schems = getSchemsAccessibleByUser(schemOwner); - for(int i = schems.size()-1; i >= 0; i--) - if(!schems.get(i).getSchemType().equals(schemType)) - schems.remove(i); - return schems; - } - - public static List getAllSchemsOfType(SchematicType schemType){ - List nodes = SchematicNode.getAllSchematicsOfType(schemType.toDB()); - List schematics = new ArrayList<>(); - nodes.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 Clipboard load() throws IOException, NoClipboardException { - return node.load(); - } - - public void loadToPlayer(Player player) throws IOException, NoClipboardException { - node.loadToPlayer(player); - } - - public void saveOldFormatFromPlayer(Player player) throws IOException, NoClipboardException { - node.saveOldFormatFromPlayer(player); - } - - public void saveFromPlayer(Player player) throws IOException, NoClipboardException { - node.saveFromPlayer(player); - } - - public void saveFromBytes(byte[] bytes, boolean newFormat) { - node.saveFromBytes(bytes, newFormat); - } - - public void remove(){ - node.delete(); - } - - 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 deleted file mode 100644 index fedd0a6..0000000 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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.Set; -import java.util.UUID; -import java.util.stream.Collectors; - -public class SchematicMember { - - private NodeMember member; - - private SchematicMember(SchematicNode node, int user, boolean updateDB){ - if(updateDB) { - member = NodeMember.createNodeMember(node.getId(), user); - }else { - member = NodeMember.getNodeMember(node.getId(), user); - } - } - - public SchematicMember(String schemName, int schemOwner, int schemMember, boolean updateDb){ - this(SchematicNode.getSchematicNode(schemOwner, schemName, 0), schemMember, updateDb); - } - - public SchematicMember(String schemName, UUID schemOwner, UUID schemMember){ - this(schemName, SteamwarUser.get(schemOwner).getId(), SteamwarUser.get(schemMember).getId(), true); - } - - 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) { - return getSchemMemberFromDB(schemName, schemOwner, schemMember, 0); - } - - public static SchematicMember getSchemMemberFromDB(String schemName, int schemOwner, int schemMember, int parent) { - SchematicNode node = SchematicNode.getSchematicNode(schemOwner, schemName, parent); - NodeMember member = NodeMember.getNodeMember(node.getId(), schemMember); - return member == null?null:new SchematicMember(node, schemMember, false); - } - - public static SchematicMember getMemberBySchematic(String schemName, int schemMember){ - return new SchematicMember(SchematicNode.getSchematicNode(NodeMember.getSchematics(schemMember).stream().filter(member1 -> SchematicNode.getSchematicNode(member1.getNode()).getName().equals(schemName)).limit(1).collect(Collectors.toList()).get(0).node), schemMember, false); - } - - public static List getSchemMembers(String schemName, UUID schemOwner){ - return getSchemMembers(schemName, SteamwarUser.get(schemOwner).getId()); - } - - public static List getSchemMembers(String schemName, int schemOwner){ - SchematicNode node = SchematicNode.getSchematicNode(schemOwner, schemName, 0); - Set members = NodeMember.getNodeMembers(node.getId()); - List retMembers = new ArrayList<>(); - members.forEach(member1 -> retMembers.add(new SchematicMember(node, member1.member, false))); - return retMembers; - } - - 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(SchematicNode.getSchematicNode(member1.node), member1.member, false))); - 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 f3d7389..b8cc085 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -515,4 +515,13 @@ public class SchematicNode { SQL.update("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?", blob, newFormat, id); schemFormat = newFormat; } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof SchematicNode)) + return false; + + SchematicNode node = (SchematicNode) obj; + return node.getId() == id; + } } From 4afc475161ca8ac9f47899c2a02388f73126c2f5 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Mon, 26 Jul 2021 19:57:59 +0200 Subject: [PATCH 20/75] Add Breadcrumbs Cache --- SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index b8cc085..81e6f0a 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -282,6 +282,7 @@ public class SchematicNode { private boolean schemFormat; private int rank; private final boolean isDir; + private Map brCache = new HashMap<>(); public static List filterSchems(int user, Predicate filter) { List finalList = new ArrayList<>(); @@ -401,7 +402,7 @@ public class SchematicNode { } public String generateBreadcrumbs(SteamwarUser user) { - return generateBreadcrumbs("/", user); + return brCache.computeIfAbsent(user.getId(), integer -> generateBreadcrumbs("/", user)); } public String generateBreadcrumbs(String split, SteamwarUser user) { From 31ecac2f111a7fe4bca4b5ea9130b6fabae24926 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 25 Aug 2021 15:38:09 +0200 Subject: [PATCH 21/75] More Improvements! --- SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java | 1 + 1 file changed, 1 insertion(+) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 81e6f0a..7f5e784 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -424,6 +424,7 @@ public class SchematicNode { SQL.update("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?", name, owner, parent == 0 ? null : parent, item, type, rank, id); this.lastUpdate = Timestamp.from(Instant.now()); + this.brCache.clear(); } public void delete() { From c1c04367e74d534d4cfa73312fcf7c4421c3b911 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Fri, 8 Oct 2021 13:41:19 +0200 Subject: [PATCH 22/75] Updating SchematicNode --- .../src/de/steamwar/sql/SchematicNode.java | 28 ++++++------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 7f5e784..74af8af 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -22,6 +22,7 @@ package de.steamwar.sql; import com.sk89q.worldedit.extent.clipboard.Clipboard; import de.steamwar.core.VersionedCallable; import de.steamwar.core.VersionedRunnable; +import de.steamwar.core.WorldEditWrapper; import org.bukkit.entity.Player; import java.io.IOException; @@ -444,8 +445,7 @@ public class SchematicNode { if(schemData == null) throw new IOException("NodeData is null"); InputStream is = schemData.getBinaryStream(); - return VersionedCallable.call(new VersionedCallable<>(() -> Schematic_8.getClipboard(is, schemFormat), 8), - new VersionedCallable<>(() -> Schematic_14.getClipboard(is, schemFormat), 14)); + return WorldEditWrapper.impl.getClipboard(is, schemFormat); } catch (SQLException e) { throw new IOException(e); } @@ -461,8 +461,7 @@ public class SchematicNode { if(blob == null) throw new NoClipboardException(); InputStream is = blob.getBinaryStream(); - VersionedRunnable.call(new VersionedRunnable(() -> Schematic_8.setPlayerClipboard(player, is, schemFormat), 8), - new VersionedRunnable(() -> Schematic_14.setPlayerClipboard(player, is, schemFormat), 14)); + WorldEditWrapper.impl.setPlayerClipboard(player, is, schemFormat); } catch (SQLException e) { throw new IOException(e); } @@ -496,21 +495,12 @@ public class SchematicNode { if(isDir) throw new SecurityException("Node is Directory"); Blob blob = SQL.blob(); - VersionedRunnable.call(new VersionedRunnable(() -> { - try { - blob.setBytes(1, Schematic_8.getPlayerClipboard(player)); - } catch (SQLException e) { - throw new RuntimeException(e.getMessage(), e); - } - updateDatabase(blob, false); - }, 8), new VersionedRunnable(() -> { - try { - blob.setBytes(1, Schematic_14.getPlayerClipboard(player, newFormat)); - } catch (SQLException exception) { - throw new RuntimeException(exception.getMessage(), exception); - } - updateDatabase(blob, newFormat); - }, 14)); + try { + blob.setBytes(1, WorldEditWrapper.impl.getPlayerClipboard(player, newFormat)); + } catch (SQLException exception) { + throw new RuntimeException(exception.getMessage(), exception); + } + updateDatabase(blob, newFormat); } private void updateDatabase(Blob blob, boolean newFormat) { From 23136219ddb44e0e73d1a6a35514a94e874dfe74 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Mon, 11 Oct 2021 11:25:10 +0200 Subject: [PATCH 23/75] Fix schematic Loading --- .../src/de/steamwar/sql/SchematicNode.java | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 74af8af..c32ae3e 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -34,6 +34,7 @@ import java.sql.Timestamp; import java.time.Instant; import java.util.*; import java.util.function.Predicate; +import java.util.zip.GZIPInputStream; public class SchematicNode { @@ -435,36 +436,29 @@ public class SchematicNode { SQL.update("DELETE FROM SchematicNode WHERE NodeId = ?", id); } - public Clipboard load() throws IOException, NoClipboardException { - if(isDir) - throw new SecurityException("Node is Directory"); + public InputStream schemData() throws IOException { ResultSet rs = SQL.select("SELECT NodeData FROM SchematicNode WHERE NodeId = ?", id); try { rs.next(); Blob schemData = rs.getBlob("NodeData"); if(schemData == null) - throw new IOException("NodeData is null"); - InputStream is = schemData.getBinaryStream(); - return WorldEditWrapper.impl.getClipboard(is, schemFormat); + throw new IOException("SchemData is null"); + return new GZIPInputStream(schemData.getBinaryStream()); } catch (SQLException e) { throw new IOException(e); } } + public Clipboard load() throws IOException, NoClipboardException { + if(isDir) + throw new SecurityException("Node is Directory"); + return WorldEditWrapper.impl.getClipboard(schemData(), schemFormat); + } + public void loadToPlayer(Player player) throws IOException, NoClipboardException { if(isDir) throw new SecurityException("Node is Directory"); - ResultSet rs = SQL.select("SELECT NodeData FROM SchematicNode WHERE NodeId = ?", id); - try { - rs.next(); - Blob blob = rs.getBlob("NodeData"); - if(blob == null) - throw new NoClipboardException(); - InputStream is = blob.getBinaryStream(); - WorldEditWrapper.impl.setPlayerClipboard(player, is, schemFormat); - } catch (SQLException e) { - throw new IOException(e); - } + WorldEditWrapper.impl.setPlayerClipboard(player, schemData(), schemFormat); } public void saveOldFormatFromPlayer(Player player) throws IOException, NoClipboardException { From de6d8bbb1008ae9ba31b470e4ab02d30cc0bbfc0 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 3 Nov 2021 16:28:01 +0100 Subject: [PATCH 24/75] Implement Streaming Signed-off-by: Chaoscaot --- .../de/steamwar/core/WorldEditWrapper14.java | 44 ++++++++++++------- .../de/steamwar/core/WorldEditWrapper8.java | 30 +++++++++---- .../de/steamwar/core/WorldEditWrapper.java | 2 +- .../src/de/steamwar/sql/SchematicNode.java | 19 ++------ 4 files changed, 56 insertions(+), 39 deletions(-) diff --git a/SpigotCore_14/src/de/steamwar/core/WorldEditWrapper14.java b/SpigotCore_14/src/de/steamwar/core/WorldEditWrapper14.java index 96b135b..f23e7fc 100644 --- a/SpigotCore_14/src/de/steamwar/core/WorldEditWrapper14.java +++ b/SpigotCore_14/src/de/steamwar/core/WorldEditWrapper14.java @@ -29,10 +29,9 @@ import com.sk89q.worldedit.world.registry.LegacyMapper; import de.steamwar.sql.NoClipboardException; import org.bukkit.entity.Player; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.util.*; +import java.util.logging.Level; import java.util.stream.Collectors; import static com.google.common.base.Preconditions.checkNotNull; @@ -43,7 +42,7 @@ public class WorldEditWrapper14 implements WorldEditWrapper.IWorldEditWrapper { private static final ClipboardFormat SCHEM = BuiltInClipboardFormat.SPONGE_SCHEMATIC; @Override - public byte[] getPlayerClipboard(Player player, boolean schemFormat) { + public InputStream getPlayerClipboard(Player player, boolean schemFormat) { ClipboardHolder clipboardHolder; try { clipboardHolder = WorldEditWrapper.getWorldEditPlugin().getSession(player).getClipboard(); @@ -55,21 +54,36 @@ public class WorldEditWrapper14 implements WorldEditWrapper.IWorldEditWrapper { if(clipboard == null) throw new NoClipboardException(); - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - try{ - if(schemFormat){ - ClipboardWriter writer = SCHEM.getWriter(outputStream); - writer.write(clipboard); - writer.close(); - }else{ - SCHEMATIC.getWriter(outputStream).write(clipboard); - } + PipedOutputStream outputStream = new PipedOutputStream(); + PipedInputStream inputStream; + try { + inputStream = new PipedInputStream(outputStream, 4096); }catch(NullPointerException e){ throw new RuntimeException(e.getMessage(), new IOException(e)); } catch (IOException e) { - throw new RuntimeException(e.getMessage(), e); + throw new SecurityException("Could not init piped input stream", e); } - return outputStream.toByteArray(); + + new Thread(() -> { + try{ + if(schemFormat){ + ClipboardWriter writer = SCHEM.getWriter(outputStream); + writer.write(clipboard); + writer.close(); + }else{ + SCHEMATIC.getWriter(outputStream).write(clipboard); + } + }catch(NullPointerException | IOException e) { + Core.getInstance().getLogger().log(Level.SEVERE, "Could not write schematic", e); + } + try { + outputStream.close(); + } catch (IOException e) { + Core.getInstance().getLogger().log(Level.SEVERE, "Could not close schem writer", e); + } + }, "SchemWriter").start(); + + return inputStream; } @Override diff --git a/SpigotCore_8/src/de/steamwar/core/WorldEditWrapper8.java b/SpigotCore_8/src/de/steamwar/core/WorldEditWrapper8.java index 3e243bd..2b1cb0e 100644 --- a/SpigotCore_8/src/de/steamwar/core/WorldEditWrapper8.java +++ b/SpigotCore_8/src/de/steamwar/core/WorldEditWrapper8.java @@ -19,19 +19,18 @@ import com.sk89q.worldedit.world.registry.WorldData; import de.steamwar.sql.NoClipboardException; import org.bukkit.entity.Player; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.logging.Level; import java.util.stream.Collectors; public class WorldEditWrapper8 implements WorldEditWrapper.IWorldEditWrapper { @Override - public byte[] getPlayerClipboard(Player player, boolean schemFormat) { + public InputStream getPlayerClipboard(Player player, boolean schemFormat) { ClipboardHolder clipboardHolder; try { clipboardHolder = WorldEditWrapper.getWorldEditPlugin().getSession(player).getClipboard(); @@ -43,13 +42,28 @@ public class WorldEditWrapper8 implements WorldEditWrapper.IWorldEditWrapper { if(clipboard == null) throw new NoClipboardException(); - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + PipedOutputStream outputStream = new PipedOutputStream(); + PipedInputStream inputStream; try { - ClipboardFormat.SCHEMATIC.getWriter(outputStream).write(clipboard, clipboardHolder.getWorldData()); + inputStream = new PipedInputStream(outputStream, 4096); } catch (IOException e) { - throw new RuntimeException(e.getMessage(), e); + throw new SecurityException("Could not init piped input stream", e); } - return outputStream.toByteArray(); + + new Thread(() -> { + try { + ClipboardFormat.SCHEMATIC.getWriter(outputStream).write(clipboard, clipboardHolder.getWorldData()); + } catch (IOException e) { + Core.getInstance().getLogger().log(Level.SEVERE, "Could not write schematic", e); + } + try { + outputStream.close(); + } catch (IOException e) { + Core.getInstance().getLogger().log(Level.SEVERE, "Could not close schem writer", e); + } + }, "SchemWriter").start(); + + return inputStream; } @Override diff --git a/SpigotCore_Main/src/de/steamwar/core/WorldEditWrapper.java b/SpigotCore_Main/src/de/steamwar/core/WorldEditWrapper.java index 920bc9f..0b860ee 100644 --- a/SpigotCore_Main/src/de/steamwar/core/WorldEditWrapper.java +++ b/SpigotCore_Main/src/de/steamwar/core/WorldEditWrapper.java @@ -14,7 +14,7 @@ public class WorldEditWrapper { public static final IWorldEditWrapper impl = VersionDependent.getVersionImpl(Core.getInstance()); public interface IWorldEditWrapper { - byte[] getPlayerClipboard(Player player, boolean schemFormat); + InputStream getPlayerClipboard(Player player, boolean schemFormat); void setPlayerClipboard(Player player, InputStream is, boolean schemFormat); Clipboard getClipboard(InputStream is, boolean schemFormat) throws IOException; diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index c32ae3e..f554f31 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -25,6 +25,7 @@ import de.steamwar.core.VersionedRunnable; import de.steamwar.core.WorldEditWrapper; import org.bukkit.entity.Player; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.sql.Blob; @@ -476,28 +477,16 @@ public class SchematicNode { public void saveFromBytes(byte[] bytes, boolean newFormat) { if(isDir) throw new SecurityException("Node is Directory"); - Blob blob = SQL.blob(); - try { - blob.setBytes(1, bytes); - updateDatabase(blob, newFormat); - } catch (SQLException e) { - throw new SecurityException(e); - } + updateDatabase(new ByteArrayInputStream(bytes), newFormat); } private void saveFromPlayer(Player player, boolean newFormat) throws IOException, NoClipboardException { if(isDir) throw new SecurityException("Node is Directory"); - Blob blob = SQL.blob(); - try { - blob.setBytes(1, WorldEditWrapper.impl.getPlayerClipboard(player, newFormat)); - } catch (SQLException exception) { - throw new RuntimeException(exception.getMessage(), exception); - } - updateDatabase(blob, newFormat); + updateDatabase(WorldEditWrapper.impl.getPlayerClipboard(player, newFormat), newFormat); } - private void updateDatabase(Blob blob, boolean newFormat) { + private void updateDatabase(InputStream blob, boolean newFormat) { SQL.update("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?", blob, newFormat, id); schemFormat = newFormat; } From 37f11e4723a90a062a4474472676661d663c8617 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 3 Nov 2021 17:14:45 +0100 Subject: [PATCH 25/75] 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); From 1865342a0043f6daf8e5c4e1c63cc914e38a62ff Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Thu, 4 Nov 2021 16:07:42 +0100 Subject: [PATCH 26/75] Latest and Greatest Fixes! Signed-off-by: Chaoscaot --- SpigotCore_Main/src/de/steamwar/sql/Schematic.java | 8 ++++++-- SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java index 555a615..f781f09 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -81,7 +81,9 @@ public class Schematic { public static List getSchemsAccessibleByUser(int schemOwner){ List schematics = new ArrayList<>(); SchematicNode.getSchematicsAccessibleByUser(schemOwner, null) - .forEach(node1 -> schematics.add(new Schematic(node1))); + .forEach(node1 -> { + if (!node1.isDir()) schematics.add(new Schematic(node1)); + }); return schematics; } @@ -92,7 +94,9 @@ public class Schematic { 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))); + .forEach(node1 -> { + if (!node1.isDir()) schematics.add(new Schematic(node1)); + }); return schematics; } diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 3f124e9..c86274a 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -186,7 +186,7 @@ public class SchematicNode { public static List getSchematicsOfType(int owner, String schemType, Integer parent) { List schems = getAllSchematicsAccessibleByUser(owner); - schems.removeIf(node -> !node.getType().equals(schemType)); + schems.removeIf(node -> node.isDir() || !node.getType().equals(schemType)); Map nodesInParent = new LinkedHashMap<>(); for (SchematicNode schematicNode : schems) { SchematicNode currentNode = schematicNode; From 474212a340691438e18f655f94304dfc192d0b77 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Fri, 5 Nov 2021 16:31:15 +0100 Subject: [PATCH 27/75] New Statement System Signed-off-by: Chaoscaot --- .../src/de/steamwar/sql/SchematicNode.java | 179 ++++++++---------- 1 file changed, 83 insertions(+), 96 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index c86274a..5089fa1 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -40,6 +40,21 @@ import java.util.zip.GZIPInputStream; public class SchematicNode { + private static final SQL.Statement createNode = new SQL.Statement("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeType, NodeItem) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUES(NodeName), ParentNode = VALUES(ParentNode), NodeItem = VALUES(NodeItem), NodeType = VALUES(NodeType), NodeItem = VALUES(NodeItem)"); + private static final SQL.Statement getSchematicNode_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode is NULL"); + private static final SQL.Statement getSchematicNode = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?"); + private static final SQL.Statement getSchematicsInNode_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode is NULL"); + private static final SQL.Statement getSchematicsInNode = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ?"); + private static final SQL.Statement getSchematicDirectory_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL"); + private static final SQL.Statement getSchematicDirectory = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?"); + private static final SQL.Statement getSchematicNodeO_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL"); + private static final SQL.Statement getSchematicNodeO = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?"); + private static final SQL.Statement getSchematicNodeId = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ?"); + private static final SQL.Statement getAllSchemsOfTypeOwner = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?"); + private static final SQL.Statement getAllSchemsOfType = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeType = ?"); + private static final SQL.Statement getAccessibleByUser = new SQL.Statement("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND s.ParentNode is NULL GROUP BY s.NodeId ORDER BY s.NodeName"); + private static final SQL.Statement countNodes = new SQL.Statement("SELECT COUNT(NodeId) AS 'count' FROM SchematicNode"); + public static SchematicNode createSchematic(int owner, String name, Integer parent) { return createSchematicNode(owner, name, parent, SchematicType.Normal.toDB(), ""); } @@ -51,8 +66,7 @@ public class SchematicNode { public static SchematicNode createSchematicNode(int owner, String name, Integer parent, String type, String item) { if (parent == 0) parent = null; - SQL.update("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeType, NodeItem) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUES(NodeName), ParentNode = VALUES(ParentNode), NodeItem = VALUES(NodeItem), NodeType = VALUES(NodeType), NodeItem = VALUES(NodeItem)", - name, owner, parent, type, item); + createNode.update(name, owner, parent, type, item); return getSchematicNode(owner, name, parent); } @@ -88,100 +102,80 @@ public class SchematicNode { } public static SchematicNode getSchematicNode(int owner, String name, Integer parent) { - if (parent != null && parent == 0) + if (parent != null && parent == 0) { parent = null; - ResultSet set; - if (parent == null) { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode is NULL", owner, name); - } else { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?", owner, name, parent); } - try { - while (set.next()) { - SchematicNode node = new SchematicNode(set); + SQL.Statement.ResultSetUser user = rs -> { + while (rs.next()) { + SchematicNode node = new SchematicNode(rs); return node; } - List nodes = getSchematicNodeInNode(parent); - for (SchematicNode node:nodes) { - if (node.name.equals(name) && NodeMember.getNodeMember(node.getId(), owner) != null) - return node; - } return null; - }catch (SQLException e) { - throw new SecurityException("Failed to load Schemnodes", e); + }; + if(parent == null) { + return getSchematicNode_Null.select(user, owner, name); + } else { + return getSchematicNode.select(user, owner, name, parent); } } public static List getSchematicNodeInNode(Integer parent) { if(parent != null && parent == 0) parent = null; - ResultSet set; - if(parent == null) { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode is NULL"); - }else { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ?", parent); - } - try { + SQL.Statement.ResultSetUser> user = rs -> { List nodes = new ArrayList<>(); - while (set.next()) - nodes.add(new SchematicNode(set)); + while (rs.next()) + nodes.add(new SchematicNode(rs)); return nodes; - }catch (SQLException e) { - throw new SecurityException("Failed to load Schemnodes", e); + }; + if(parent == null) { + return getSchematicsInNode_Null.select(user); + }else { + return getSchematicsInNode.select(user, parent); } } public static SchematicNode getSchematicDirectory(String name, Integer parent) { if(parent != null && parent == 0) parent = null; - ResultSet set; - if(parent == null) { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name, parent); - }else { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); - } - try { - while (set.next()) { - SchematicNode node = new SchematicNode(set); + SQL.Statement.ResultSetUser user = rs -> { + while (rs.next()) { + SchematicNode node = new SchematicNode(rs); if(node.isDir()) return node; } return null; - }catch (SQLException e) { - throw new SecurityException("Failed to load Schemnodes", e); + }; + + if(parent == null) { + return getSchematicDirectory_Null.select(user, name); + }else { + return getSchematicDirectory.select(user, name, parent); } } - public static SchematicNode getSchematicInParent(String name, Integer parent) { + public static SchematicNode getSchematicNode(String name, Integer parent) { if(parent != null && parent == 0) parent = null; - ResultSet set; - if(parent == null) { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name); - }else { - set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); - } - try { - while (set.next()) { - SchematicNode node = new SchematicNode(set); - if(!node.isDir()) - return node; + SQL.Statement.ResultSetUser user = rs -> { + while (rs.next()) { + return new SchematicNode(rs); } return null; - }catch (SQLException e) { - throw new SecurityException("Failed to load Schemnodes", e); + }; + if(parent == null) { + return getSchematicNodeO_Null.select(user, name); + }else { + return getSchematicNodeO.select(user, name, parent); } } public static SchematicNode getSchematicNode(int id) { - ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ?", id); - try { - if (!set.next()) + return getSchematicNodeId.select(rs -> { + if (!rs.next()) return null; - return new SchematicNode(set); - } catch (SQLException e) { - throw new SecurityException("Failed to load Schemnodes", e); - } + return new SchematicNode(rs); + }); } public static List getSchematicsOfType(int owner, String schemType, Integer parent) { @@ -199,27 +193,22 @@ public class SchematicNode { } public static List getAllSchematicsOfType(int owner, String schemType) { - ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?", owner, schemType); - try { + return getAllSchemsOfTypeOwner.select(rs -> { List nodes = new ArrayList<>(); - while (set.next()) - nodes.add(new SchematicNode(set)); + while (rs.next()) + nodes.add(new SchematicNode(rs)); return nodes; - } catch (SQLException e) { - throw new SecurityException("Failed to load Schemnodes", e); - } + }, owner, schemType); } + public static List getAllSchematicsOfType(String schemType) { - ResultSet set = SQL.select("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeType = ?", schemType); - try { + return getAllSchemsOfType.select(rs -> { List nodes = new ArrayList<>(); - while (set.next()) - nodes.add(new SchematicNode(set)); + while (rs.next()) + nodes.add(new SchematicNode(rs)); return nodes; - } catch (SQLException e) { - throw new SecurityException("Failed to load Schemnodes", e); - } + }, schemType); } public static List deepGet(Integer parent, Predicate filter) { @@ -252,29 +241,29 @@ public class SchematicNode { if(isAdded) return getSchematicNodeInNode(parent); } else { - ResultSet set = SQL.select("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND s.ParentNode is NULL GROUP BY s.NodeId ORDER BY s.NodeName", user, user); - try{ + return getAccessibleByUser.select(rs -> { List nodes = new ArrayList<>(); - while(set.next()) - nodes.add(new SchematicNode(set)); + while(rs.next()) + nodes.add(new SchematicNode(rs)); return nodes; - }catch(SQLException e){ - throw new SecurityException("Failed listing schematics", e); - } + }, user, user); } return Collections.emptyList(); } public static List getAllSchematicsAccessibleByUser(int user) { - ResultSet set = SQL.select("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember nm ON nm.NodeId = s.NodeId WHERE s.NodeOwner = ? OR nm.UserId = ? GROUP BY s.NodeId ORDER BY s.NodeName", user, user); - try{ + return getAccessibleByUser.select(rs -> { List nodes = new ArrayList<>(); - while(set.next()) - nodes.add(new SchematicNode(set)); + while(rs.next()) { + SchematicNode node = new SchematicNode(rs); + if(node.isDir()) { + nodes.addAll(deepGet(node.getId(), node1 -> true)); + } else { + nodes.add(node); + } + } return nodes; - }catch(SQLException e){ - throw new SecurityException("Failed listing schematics", e); - } + }, user, user); } private final int id; @@ -303,14 +292,12 @@ public class SchematicNode { } public static Integer countNodes() { - ResultSet set = SQL.select("SELECT COUNT(NodeId) AS 'count' FROM SchematicNode"); - try { - if (set.next()) - return set.getInt("count"); + return countNodes.select(rs -> { + if (rs.next()) { + return rs.getInt("count"); + } return 0; - } catch (SQLException e) { - throw new SecurityException("Failed listing schematics", e); - } + }); } public int getId() { From dfb38c7cda142277ff6cc09ed5bde4740666fdb7 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Fri, 5 Nov 2021 22:09:24 +0100 Subject: [PATCH 28/75] Safe Historic Data in CheckedNode Signed-off-by: Chaoscaot --- ...CheckedSchematic.java => CheckedNode.java} | 48 +++++++++---------- 1 file changed, 23 insertions(+), 25 deletions(-) rename SpigotCore_Main/src/de/steamwar/sql/{CheckedSchematic.java => CheckedNode.java} (60%) diff --git a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/CheckedNode.java similarity index 60% rename from SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java rename to SpigotCore_Main/src/de/steamwar/sql/CheckedNode.java index 652d041..f52137a 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/CheckedNode.java @@ -19,80 +19,78 @@ package de.steamwar.sql; -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; -import java.util.logging.Level; -public class CheckedSchematic { +public class CheckedNode { - 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 static final SQL.Statement checkHistory = new SQL.Statement("SELECT * FROM CheckedNode 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 CheckedNode WHERE NodeId = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC"); - private final int node; + private final Integer node; private final int validator; private final Timestamp startTime; private final Timestamp endTime; private final String declineReason; - private CheckedSchematic(int node, int validator, Timestamp startTime, Timestamp endTime, String declineReason, boolean insertDB){ + private CheckedNode(int node, int validator, Timestamp startTime, Timestamp endTime, String declineReason, boolean insertDB){ this.node = node; this.validator = validator; this.startTime = startTime; this.endTime = endTime; this.declineReason = declineReason; - if(insertDB) + if(insertDB) { insertDB(); + } } - public CheckedSchematic(int node, int validator, Timestamp startTime, Timestamp endTime, String declineReason){ + public CheckedNode(int node, int validator, Timestamp startTime, Timestamp endTime, String declineReason){ this(node, validator, startTime, endTime, declineReason, true); } - public CheckedSchematic(int node, UUID validator, Timestamp startTime, Timestamp endTime, String declineReason){ + public CheckedNode(int node, UUID validator, Timestamp startTime, Timestamp endTime, String declineReason){ this(node, SteamwarUser.get(validator).getId(), startTime, endTime, declineReason, true); } - private void insertDB(){ + private void insertDB() { + SchematicNode sNode = SchematicNode.getSchematicNode(node); + String nodeName = sNode.getName(); + int nodeOwner = sNode.getOwner(); SQL.update("INSERT INTO CheckedSchematic" + - " (NodeId, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?)", - node, validator, startTime, endTime, declineReason); + " (NodeId, NodeName, NodeOwner, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?)", + node, nodeName, nodeOwner, validator, startTime, endTime, declineReason); } - public static List getLastDeclinedOfNode(SchematicNode node){ + public static List getLastDeclinedOfNode(SchematicNode node){ return getLastDeclinedOfNode(node.getId()); } - public static List getLastDeclinedOfNode(int node){ + public static List getLastDeclinedOfNode(int node){ return nodeHistory.select(rs -> { - List lastDeclined = new ArrayList<>(); + 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)); + lastDeclined.add(new CheckedNode(node, validator, startTime, endTime, declineReason, false)); } return lastDeclined; }, node); } - public static List getLastDeclined(UUID uuid){ + public static List getLastDeclined(UUID uuid){ return getLastDelined(SteamwarUser.get(uuid).getId()); } - public static List getLastDelined(int schemOwner){ + public static List getLastDelined(int schemOwner){ return checkHistory.select(rs -> { - List history = new ArrayList<>(); + 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)); + history.add(new CheckedNode(rs.getInt("NodeId"), rs.getInt("Validator"), rs.getTimestamp("StartTime"), rs.getTimestamp("EndTime"), rs.getString("DeclineReason"), false)); return history; }, schemOwner); } From 0dd62f446b0f10963cf3400f263fd91b55d47262 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Fri, 5 Nov 2021 22:24:58 +0100 Subject: [PATCH 29/75] Refractor Signed-off-by: Chaoscaot --- .../src/de/steamwar/sql/CheckedSchematic.java | 79 +++++++++++++++++++ .../de/steamwar/sql/DownloadSchematic.java | 29 +------ .../src/de/steamwar/sql/NodeDownload.java | 49 ++++++++++++ 3 files changed, 129 insertions(+), 28 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java create mode 100644 SpigotCore_Main/src/de/steamwar/sql/NodeDownload.java diff --git a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java new file mode 100644 index 0000000..b3ff430 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java @@ -0,0 +1,79 @@ +/* + 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.sql.Timestamp; +import java.util.List; +import java.util.UUID; +import java.util.stream.Collectors; + +@Deprecated +public class CheckedSchematic { + + private final CheckedNode node; + + private CheckedSchematic(CheckedNode node){ + this.node = node; + } + + @Deprecated + public CheckedSchematic(String schemName, int schemOwner, int validator, Timestamp startTime, Timestamp endTime, String declineReason){ + this(new CheckedNode(Schematic.getSchemFromDB(schemName, schemOwner).getSchemID(), validator ,startTime, endTime, declineReason)); + } + + @Deprecated + public CheckedSchematic(String schemName, UUID schemOwner, UUID validator, Timestamp startTime, Timestamp endTime, String declineReason){ + this(new CheckedNode(Schematic.getSchemFromDB(schemName, schemOwner).getSchemID(), validator ,startTime, endTime, declineReason)); + } + + public static List getLastDeclined(UUID schemOwner){ + return getLastDelined(SteamwarUser.get(schemOwner).getId()); + } + + public static List getLastDelined(int schemOwner){ + return CheckedNode.getLastDelined(schemOwner).stream().map(node1 -> new CheckedSchematic(node1)).collect(Collectors.toList()); + } + + @Deprecated + public void remove() { + node.remove(); + } + + public String getSchemName() { + return node.getSchemName(); + } + + public int getSchemOwner() { + return node.getSchemOwner(); + } + + public int getValidator() { + return node.getValidator(); + } + + public Timestamp getStartTime() { + return node.getStartTime(); + } + + public Timestamp getEndTime() { + return node.getEndTime(); + } + + public String getDeclineReason() { + return node.getDeclineReason(); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java index 7f4dc2b..64812f9 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java @@ -19,38 +19,11 @@ package de.steamwar.sql; -import com.google.common.io.BaseEncoding; - -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -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){ - if(schem.isDir()) - throw new SecurityException("Can not Download Directorys"); - MessageDigest digest; - try { - digest = MessageDigest.getInstance("SHA-1"); - } catch (NoSuchAlgorithmException e) { - throw new SecurityException(e); - } - digest.reset(); - digest.update((Instant.now().toString() + schem.getOwner() + schem.getId()).getBytes()); - String hash = BaseEncoding.base16().encode(digest.digest()); - createLink.update(schem.getId(), hash); - return BASE + hash; - } - @Deprecated public static String getLink(Schematic schem){ - return getLink(schem.getNode()); + return NodeDownload.getLink(schem.getNode()); } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/NodeDownload.java b/SpigotCore_Main/src/de/steamwar/sql/NodeDownload.java new file mode 100644 index 0000000..acc7a11 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/NodeDownload.java @@ -0,0 +1,49 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.google.common.io.BaseEncoding; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.time.Instant; + +public class NodeDownload { + + private static final SQL.Statement createLink = new SQL.Statement("INSERT INTO NodeDownload (NodeId, 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){ + if(schem.isDir()) + throw new SecurityException("Can not Download Directorys"); + MessageDigest digest; + try { + digest = MessageDigest.getInstance("SHA-1"); + } catch (NoSuchAlgorithmException e) { + throw new SecurityException(e); + } + digest.reset(); + digest.update((Instant.now().toString() + schem.getOwner() + schem.getId()).getBytes()); + String hash = BaseEncoding.base16().encode(digest.digest()); + createLink.update(schem.getId(), hash); + return BASE + hash; + } +} From 1d9532d971555933ae99fd818a86070cdc449bac Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 6 Nov 2021 14:42:35 +0100 Subject: [PATCH 30/75] Fixes 'n' Statements Signed-off-by: Chaoscaot --- .../src/de/steamwar/sql/CheckedNode.java | 129 ------------------ .../src/de/steamwar/sql/CheckedSchematic.java | 103 ++++++++++---- .../src/de/steamwar/sql/NodeMember.java | 43 +++--- .../src/de/steamwar/sql/SchematicNode.java | 33 +++-- 4 files changed, 118 insertions(+), 190 deletions(-) delete mode 100644 SpigotCore_Main/src/de/steamwar/sql/CheckedNode.java diff --git a/SpigotCore_Main/src/de/steamwar/sql/CheckedNode.java b/SpigotCore_Main/src/de/steamwar/sql/CheckedNode.java deleted file mode 100644 index f52137a..0000000 --- a/SpigotCore_Main/src/de/steamwar/sql/CheckedNode.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - 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.sql.Timestamp; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - - -public class CheckedNode { - - private static final SQL.Statement checkHistory = new SQL.Statement("SELECT * FROM CheckedNode 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 CheckedNode WHERE NodeId = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC"); - - private final Integer node; - private final int validator; - private final Timestamp startTime; - private final Timestamp endTime; - private final String declineReason; - - private CheckedNode(int node, int validator, Timestamp startTime, Timestamp endTime, String declineReason, boolean insertDB){ - this.node = node; - this.validator = validator; - this.startTime = startTime; - this.endTime = endTime; - this.declineReason = declineReason; - if(insertDB) { - insertDB(); - } - } - - public CheckedNode(int node, int validator, Timestamp startTime, Timestamp endTime, String declineReason){ - this(node, validator, startTime, endTime, declineReason, true); - } - - public CheckedNode(int node, UUID validator, Timestamp startTime, Timestamp endTime, String declineReason){ - this(node, SteamwarUser.get(validator).getId(), startTime, endTime, declineReason, true); - } - - private void insertDB() { - SchematicNode sNode = SchematicNode.getSchematicNode(node); - String nodeName = sNode.getName(); - int nodeOwner = sNode.getOwner(); - SQL.update("INSERT INTO CheckedSchematic" + - " (NodeId, NodeName, NodeOwner, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?)", - node, nodeName, nodeOwner, validator, startTime, endTime, declineReason); - } - - public static List getLastDeclinedOfNode(SchematicNode node){ - return getLastDeclinedOfNode(node.getId()); - } - - 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 CheckedNode(node, validator, startTime, endTime, declineReason, false)); - } - return lastDeclined; - }, node); - } - - public static List getLastDeclined(UUID uuid){ - 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 CheckedNode(rs.getInt("NodeId"), rs.getInt("Validator"), rs.getTimestamp("StartTime"), rs.getTimestamp("EndTime"), rs.getString("DeclineReason"), false)); - return history; - }, schemOwner); - } - - public void remove() { - SQL.update("DELETE FROM CheckedSchematic WHERE NodeId", node); - } - - public int getValidator() { - return validator; - } - - public Timestamp getStartTime() { - return startTime; - } - - public Timestamp getEndTime() { - return endTime; - } - - public String getDeclineReason() { - return declineReason; - } - - public int getNode() { - return node; - } - - public String getSchemName() { - return SchematicNode.getSchematicNode(node).getName(); - } - - public int getSchemOwner() { - return SchematicNode.getSchematicNode(node).getId(); - } -} diff --git a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java index b3ff430..808ff1f 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java @@ -17,63 +17,114 @@ package de.steamwar.sql; import java.sql.Timestamp; +import java.util.ArrayList; import java.util.List; import java.util.UUID; -import java.util.stream.Collectors; -@Deprecated public class CheckedSchematic { - private final CheckedNode node; + 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' AND NodeId is not NULL 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 static final SQL.Statement insert = new SQL.Statement("INSERT INTO CheckedSchematic (NodeId, NodeName, NodeOwner, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?)"); - private CheckedSchematic(CheckedNode node){ + private final Integer node; + private final int validator; + private final Timestamp startTime; + private final Timestamp endTime; + private final String declineReason; + + private CheckedSchematic(int node, int validator, Timestamp startTime, Timestamp endTime, String declineReason, boolean insertDB){ this.node = node; + this.validator = validator; + this.startTime = startTime; + this.endTime = endTime; + this.declineReason = declineReason; + if(insertDB) { + insertDB(); + } } @Deprecated public CheckedSchematic(String schemName, int schemOwner, int validator, Timestamp startTime, Timestamp endTime, String declineReason){ - this(new CheckedNode(Schematic.getSchemFromDB(schemName, schemOwner).getSchemID(), validator ,startTime, endTime, declineReason)); + this(Schematic.getSchemFromDB(schemName, schemOwner).getSchemID(), validator, startTime, endTime, declineReason, true); } @Deprecated public CheckedSchematic(String schemName, UUID schemOwner, UUID validator, Timestamp startTime, Timestamp endTime, String declineReason){ - this(new CheckedNode(Schematic.getSchemFromDB(schemName, schemOwner).getSchemID(), validator ,startTime, endTime, declineReason)); + this(Schematic.getSchemFromDB(schemName, schemOwner).getSchemID(), SteamwarUser.get(validator).getId(), startTime, endTime, declineReason, true); } - public static List getLastDeclined(UUID schemOwner){ - return getLastDelined(SteamwarUser.get(schemOwner).getId()); + public CheckedSchematic(int node, int validator, Timestamp startTime, Timestamp endTime, String declineReason){ + this(node, validator, startTime, endTime, declineReason, true); + } + + public CheckedSchematic(int node, UUID validator, Timestamp startTime, Timestamp endTime, String declineReason){ + this(node, SteamwarUser.get(validator).getId(), startTime, endTime, declineReason, true); + } + + private void insertDB() { + SchematicNode sNode = SchematicNode.getSchematicNode(node); + String nodeName = sNode.getName(); + int nodeOwner = sNode.getOwner(); + insert.update(node, nodeName, nodeOwner, validator, startTime, endTime, declineReason); + } + + public static List getLastDeclinedOfNode(SchematicNode node){ + return getLastDeclinedOfNode(node.getId()); + } + + 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)); + } + return lastDeclined; + }, node); + } + + public static List getLastDeclined(UUID uuid){ + return getLastDelined(SteamwarUser.get(uuid).getId()); } public static List getLastDelined(int schemOwner){ - return CheckedNode.getLastDelined(schemOwner).stream().map(node1 -> new CheckedSchematic(node1)).collect(Collectors.toList()); - } - - @Deprecated - public void remove() { - node.remove(); - } - - public String getSchemName() { - return node.getSchemName(); - } - - public int getSchemOwner() { - return node.getSchemOwner(); + 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 int getValidator() { - return node.getValidator(); + return validator; } public Timestamp getStartTime() { - return node.getStartTime(); + return startTime; } public Timestamp getEndTime() { - return node.getEndTime(); + return endTime; } public String getDeclineReason() { - return node.getDeclineReason(); + return declineReason; + } + + public int getNode() { + return node; + } + + public String getSchemName() { + return SchematicNode.getSchematicNode(node).getName(); + } + + public int getSchemOwner() { + return SchematicNode.getSchematicNode(node).getId(); } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java b/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java index ad265e7..f2f1d90 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java +++ b/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java @@ -26,43 +26,40 @@ import java.util.Set; public class NodeMember { + private static final SQL.Statement getNodeMember = new SQL.Statement("SELECT * FROM NodeMember WHERE NodeId = ? AND UserId = ?"); + private static final SQL.Statement getNodeMembers = new SQL.Statement("SELECT * FROM NodeMember WHERE NodeId = ?"); + private static final SQL.Statement getSchematics = new SQL.Statement("SELECT * FROM NodeMember WHERE UserId = ?"); + private static final SQL.Statement createNodeMember = new SQL.Statement("INSERT INTO NodeMember (NodeId, UserId) VALUES (?, ?)"); + private static final SQL.Statement deleteNodeMember = new SQL.Statement("DELETE FROM NodeMember WHERE NodeId = ? AND UserId = ?"); + public static NodeMember getNodeMember(int node, int member) { - ResultSet set = SQL.select("SELECT * FROM NodeMember WHERE NodeId = ? AND UserId = ?", node, member); - try { - if(!set.next()) + return getNodeMember.select(rs -> { + if(!rs.next()) return null; - return new NodeMember(set); - } catch (SQLException e) { - throw new SecurityException("Could not load NodeMember", e); - } + return new NodeMember(rs); + }, node, member); } public static Set getNodeMembers(int node) { - ResultSet set = SQL.select("SELECT * FROM NodeMember WHERE NodeId = ?", node); - try { + return getNodeMembers.select(rs -> { Set members = new HashSet<>(); - while (set.next()) - members.add(new NodeMember(set)); + while (rs.next()) + members.add(new NodeMember(rs)); return members; - } catch (SQLException e) { - throw new SecurityException("Could not load NodeMember", e); - } + }, node); } public static Set getSchematics(int member) { - ResultSet set = SQL.select("SELECT * FROM NodeMember WHERE UserId = ?", member); - try { + return getSchematics.select(rs -> { Set members = new HashSet<>(); - while (set.next()) - members.add(new NodeMember(set)); + while (rs.next()) + members.add(new NodeMember(rs)); return members; - } catch (SQLException e) { - throw new SecurityException("Could not load NodeMember", e); - } + }, member); } public static NodeMember createNodeMember(int node, int member) { - SQL.update("INSERT INTO NodeMember (NodeId, UserId) VALUES (?, ?)", node, member); + createNodeMember.update(node, member); return getNodeMember(node, member); } @@ -83,6 +80,6 @@ public class NodeMember { } public void delete() { - SQL.update("DELETE FROM NodeMember WHERE NodeId = ? AND UserId = ?", node, member); + deleteNodeMember.update(node, member); } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 5089fa1..3b6ee03 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -54,6 +54,10 @@ public class SchematicNode { private static final SQL.Statement getAllSchemsOfType = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeType = ?"); private static final SQL.Statement getAccessibleByUser = new SQL.Statement("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND s.ParentNode is NULL GROUP BY s.NodeId ORDER BY s.NodeName"); private static final SQL.Statement countNodes = new SQL.Statement("SELECT COUNT(NodeId) AS 'count' FROM SchematicNode"); + private static final SQL.Statement updateDB = new SQL.Statement("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?"); + private static final SQL.Statement updateDatabase = new SQL.Statement("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?"); + private static final SQL.Statement selSchemData = new SQL.Statement("SELECT NodeData FROM SchematicNode WHERE NodeId = ?"); + private static final SQL.Statement deleteNode = new SQL.Statement("DELETE FROM SchematicNode WHERE NodeId = ?"); public static SchematicNode createSchematic(int owner, String name, Integer parent) { return createSchematicNode(owner, name, parent, SchematicType.Normal.toDB(), ""); @@ -321,7 +325,7 @@ public class SchematicNode { return parent; } - public void setParent(int parent) { + public void setParent(Integer parent) { this.parent = parent; updateDB(); } @@ -412,8 +416,7 @@ public class SchematicNode { } private void updateDB() { - SQL.update("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?", - name, owner, parent == 0 ? null : parent, item, type, rank, id); + updateDB.update(name, owner, parent == 0 ? null : parent, item, type, rank, id); this.lastUpdate = Timestamp.from(Instant.now()); this.brCache.clear(); } @@ -422,18 +425,24 @@ public class SchematicNode { if (isDir()) { getSchematicNodeInNode(getId()).forEach(SchematicNode::delete); } - SQL.update("DELETE FROM SchematicNode WHERE NodeId = ?", id); + deleteNode.update(id); } public InputStream schemData() throws IOException { - ResultSet rs = SQL.select("SELECT NodeData FROM SchematicNode WHERE NodeId = ?", id); try { - rs.next(); - Blob schemData = rs.getBlob("NodeData"); - if(schemData == null) - throw new IOException("SchemData is null"); - return new GZIPInputStream(schemData.getBinaryStream()); - } catch (SQLException e) { + return selSchemData.select(rs -> { + rs.next(); + Blob schemData = rs.getBlob("NodeData"); + if(schemData == null) { + throw new SecurityException("SchemData is null"); + } + try { + return new GZIPInputStream(schemData.getBinaryStream()); + } catch (IOException e) { + throw new SecurityException("SchemData is wrong", e); + } + }, id); + } catch (Exception e) { throw new IOException(e); } } @@ -469,7 +478,7 @@ public class SchematicNode { } private void updateDatabase(InputStream blob, boolean newFormat) { - SQL.update("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?", blob, newFormat, id); + updateDatabase.update(blob, newFormat, id); schemFormat = newFormat; } From 3a06ecedf1a526bdca0ddc71cf849bd2e4f62f7f Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 6 Nov 2021 14:44:26 +0100 Subject: [PATCH 31/75] Fix Copyright Signed-off-by: Chaoscaot --- SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java index 808ff1f..c755a2f 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java @@ -2,14 +2,17 @@ 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 . */ From 2e528918bbe9b1321bf1048da0723ccb3f100b67 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 9 Nov 2021 10:45:18 +0100 Subject: [PATCH 32/75] Reimplement auto reconnect --- SpigotCore_Main/src/de/steamwar/sql/SQL.java | 33 ++++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SQL.java b/SpigotCore_Main/src/de/steamwar/sql/SQL.java index 74e68b9..0cffba5 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SQL.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SQL.java @@ -19,6 +19,7 @@ package de.steamwar.sql; +import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException; import de.steamwar.core.Core; import org.bukkit.configuration.file.YamlConfiguration; @@ -33,27 +34,27 @@ public class SQL { private SQL(){} private static Connection con; - private static String url; - private static String user; - private static String password; + private static final String URL; + private static final String USER; + private static final String PASSWORD; - static{ + static { File file = new File(Core.getInstance().getDataFolder(), "MySQL.yml"); YamlConfiguration config = YamlConfiguration.loadConfiguration(file); if(!file.exists()) throw new SecurityException("SQL-ConfigFile not found!"); - url = "jdbc:mysql://" + config.getString("HOST") + ":" + config.getString("PORT") + "/" + config.getString("DATABASE"); - user = config.getString("USER"); - password = config.getString("PASSWORD"); + URL = "jdbc:mysql://" + config.getString("HOST") + ":" + config.getString("PORT") + "/" + config.getString("DATABASE"); + USER = config.getString("USER"); + PASSWORD = config.getString("PASSWORD"); connect(); } private static void connect() { try { - con = DriverManager.getConnection(url + "?autoReconnect=true&useServerPrepStmts=true", user, password); + con = DriverManager.getConnection(URL + "?useServerPrepStmts=true", USER, PASSWORD); } catch (SQLException e) { throw new SecurityException("Could not start SQL connection", e); } @@ -95,7 +96,8 @@ public class SQL { return false; } } - + + @Deprecated static void update(String qry, Object... objects) { try { prepare(qry, objects).executeUpdate(); @@ -105,7 +107,8 @@ public class SQL { } } - static ResultSet select(String qry, Object... objects){ + @Deprecated + static ResultSet select(String qry, Object... objects) { try { return prepare(qry, objects).executeQuery(); } catch (SQLException e) { @@ -114,7 +117,8 @@ public class SQL { } } - static Blob blob(){ + @Deprecated + static Blob blob() { try { return con.createBlob(); } catch (SQLException e) { @@ -123,7 +127,8 @@ public class SQL { } } - private static PreparedStatement prepare(String qry, Object... objects) throws SQLException{ + @Deprecated + private static PreparedStatement prepare(String qry, Object... objects) throws SQLException { PreparedStatement st = con.prepareStatement(qry); for(int i = 0; i < objects.length; i++){ st.setObject(i+1, objects[i]); @@ -168,8 +173,10 @@ public class SQL { try { setObjects(objects); return runnable.run(); - } catch (SQLException e) { + } catch (CommunicationsException e) { reset(); + return prepare(runnable, objects); + } catch (SQLException e) { throw new SecurityException("Could not execute SQL statement", e); } } From d3ebbafedaa7f5b96030eac9098d8d072c58b164 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 9 Nov 2021 10:49:41 +0100 Subject: [PATCH 33/75] Prevent infinite recursion --- SpigotCore_Main/src/de/steamwar/sql/SQL.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SQL.java b/SpigotCore_Main/src/de/steamwar/sql/SQL.java index 0cffba5..fc8c7a2 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SQL.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SQL.java @@ -171,11 +171,14 @@ public class SQL { private synchronized T prepare(SQLRunnable runnable, Object... objects) { try { - setObjects(objects); - return runnable.run(); - } catch (CommunicationsException e) { - reset(); - return prepare(runnable, objects); + try { + setObjects(objects); + return runnable.run(); + } catch (CommunicationsException e) { + reset(); + setObjects(objects); + return runnable.run(); + } } catch (SQLException e) { throw new SecurityException("Could not execute SQL statement", e); } From dba1df777a938da6516e83ba1344e5531a3ccebc Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 10 Nov 2021 14:08:18 +0100 Subject: [PATCH 34/75] Update SWCommand --- .../src/de/steamwar/command/SWCommand.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index e6926da..6ea2ba4 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -51,8 +51,9 @@ public abstract class SWCommand { if (!initialized) { createMapping(); } - if (commandList.stream().anyMatch(s -> s.invoke(sender, args))) return false; - commandHelpList.stream().anyMatch(s -> s.invoke(sender, args)); + if (!commandList.stream().anyMatch(s -> s.invoke(sender, args))) { + commandHelpList.stream().anyMatch(s -> s.invoke(sender, args)); + } return false; } @@ -87,13 +88,13 @@ public abstract class SWCommand { add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> { if (!anno.help()) return; if (parameters.length != 2) { - Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking parameters or has too many"); + Bukkit.getLogger().log(Level.WARNING, () -> "The method '" + method.toString() + "' is lacking parameters or has too many"); } if (!parameters[parameters.length - 1].isVarArgs()) { - Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the varArgs parameters as last Argument"); + Bukkit.getLogger().log(Level.WARNING, () -> "The method '" + method.toString() + "' is lacking the varArgs parameters as last Argument"); } if (parameters[parameters.length - 1].getType().getComponentType() != String.class) { - Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); + Bukkit.getLogger().log(Level.WARNING, () -> "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); return; } commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>())); From ad221d21f6a8af206ad98cb7e778ac6f1e095674 Mon Sep 17 00:00:00 2001 From: YoyoNow Date: Wed, 10 Nov 2021 14:10:40 +0100 Subject: [PATCH 35/75] =?UTF-8?q?=E2=80=9ESpigotCore=5FMain/src/de/steamwa?= =?UTF-8?q?r/command/SWCommand.java=E2=80=9C=20=C3=A4ndern?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 6ea2ba4..1db2988 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -115,7 +115,7 @@ public abstract class SWCommand { } String name = mapper != null ? mapper.value() : clazz.getTypeName(); if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name) && !localTypeMapper.containsKey(name)) { - Bukkit.getLogger().log(Level.WARNING, "The parameter '" + parameter.toString() + "' is using an unsupported Mapper of type '" + name + "'"); + Bukkit.getLogger().log(Level.WARNING, () -> "The parameter '" + parameter.toString() + "' is using an unsupported Mapper of type '" + name + "'"); return; } } @@ -142,15 +142,15 @@ public abstract class SWCommand { Parameter[] parameters = method.getParameters(); if (!parameterTester.test(parameters.length)) { - Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking parameters or has too many"); + Bukkit.getLogger().log(Level.WARNING, () -> "The method '" + method.toString() + "' is lacking parameters or has too many"); return; } if (firstParameter && !CommandSender.class.isAssignableFrom(parameters[0].getType())) { - Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the first parameter of type '" + CommandSender.class.getTypeName() + "'"); + Bukkit.getLogger().log(Level.WARNING, () -> "The method '" + method.toString() + "' is lacking the first parameter of type '" + CommandSender.class.getTypeName() + "'"); return; } if (returnType != null && method.getReturnType() != returnType) { - Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'"); + Bukkit.getLogger().log(Level.WARNING, () -> "The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'"); return; } Arrays.stream(anno).forEach(t -> consumer.accept(t, parameters)); From c49daba060deefba6c472a0c86874d069c364889 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 13 Nov 2021 15:09:53 +0100 Subject: [PATCH 36/75] Some Crazy-Ass SQL-Statements and Performance! Signed-off-by: Chaoscaot --- .../src/de/steamwar/sql/Schematic.java | 2 +- .../src/de/steamwar/sql/SchematicNode.java | 63 ++++++++++--------- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java index f781f09..efe4b66 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -93,7 +93,7 @@ public class Schematic { public static List getSchemsOfType(int schemOwner, SchematicType schemType){ List schematics = new ArrayList<>(); - SchematicNode.getSchematicsOfType(schemOwner, schemType.toDB(), null) + SchematicNode.getAllAccessibleSchematicsOfType(schemOwner, schemType.toDB()) .forEach(node1 -> { if (!node1.isDir()) schematics.add(new Schematic(node1)); }); diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 3b6ee03..878b4b7 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -35,6 +35,7 @@ import java.sql.SQLException; import java.sql.Timestamp; import java.time.Instant; import java.util.*; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Predicate; import java.util.zip.GZIPInputStream; @@ -53,6 +54,11 @@ public class SchematicNode { private static final SQL.Statement getAllSchemsOfTypeOwner = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?"); private static final SQL.Statement getAllSchemsOfType = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeType = ?"); private static final SQL.Statement getAccessibleByUser = new SQL.Statement("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND s.ParentNode is NULL GROUP BY s.NodeId ORDER BY s.NodeName"); + private static final SQL.Statement getAccessibleByUserByTypeInNode = new SQL.Statement("WITH RECURSIVE RSNB AS (WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSNB WHERE SN.NodeId = RSNB.ParentNode)SELECT * FROM RSNB WHERE ParentNode = ?"); + private static final SQL.Statement getAccessibleByUserByTypeInNode_Null = new SQL.Statement("WITH RECURSIVE RSNB AS (WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSNB WHERE SN.NodeId = RSNB.ParentNode)SELECT * FROM RSNB WHERE ParentNode is null"); + private static final SQL.Statement getAccessibleByUserByType = new SQL.Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ?"); + private static final SQL.Statement getAllSchematicsAccessibleByUser = new SQL.Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType is not null"); + private static final SQL.Statement isSchematicAccessibleForUser = new SQL.Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT COUNT(RSN.NodeId) AS `Accessible` FROM RSN Join NodeMember NM On NM.NodeId = RSN.NodeId WHERE NodeOwner = ? OR UserId = ? LIMIT 1"); private static final SQL.Statement countNodes = new SQL.Statement("SELECT COUNT(NodeId) AS 'count' FROM SchematicNode"); private static final SQL.Statement updateDB = new SQL.Statement("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?"); private static final SQL.Statement updateDatabase = new SQL.Statement("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?"); @@ -182,18 +188,29 @@ public class SchematicNode { }); } - public static List getSchematicsOfType(int owner, String schemType, Integer parent) { - List schems = getAllSchematicsAccessibleByUser(owner); - schems.removeIf(node -> node.isDir() || !node.getType().equals(schemType)); - Map nodesInParent = new LinkedHashMap<>(); - for (SchematicNode schematicNode : schems) { - SchematicNode currentNode = schematicNode; - while (currentNode.getParent() != parent) { - currentNode = currentNode.getParentNode(); + public static List getAccessibleSchematicsOfTypeInParent(int owner, String schemType, Integer parent) { + SQL.Statement.ResultSetUser> user = rs -> { + List nodes = new ArrayList<>(); + while (rs.next()) { + nodes.add(new SchematicNode(rs)); } - nodesInParent.putIfAbsent(currentNode.getId(), currentNode); + return nodes; + }; + if(parent == null || parent == 0) { + return getAccessibleByUserByTypeInNode_Null.select(user, owner, owner, schemType); + } else { + return getAccessibleByUserByTypeInNode.select(user, owner, owner, parent, schemType); } - return new ArrayList<>(nodesInParent.values()); + } + + public static List getAllAccessibleSchematicsOfType(int user, String schemType) { + return getAccessibleByUserByType.select(rs -> { + List nodes = new ArrayList<>(); + while (rs.next()) { + nodes.add(new SchematicNode(rs)); + } + return nodes; + }, user, user, schemType); } public static List getAllSchematicsOfType(int owner, String schemType) { @@ -205,7 +222,6 @@ public class SchematicNode { }, owner, schemType); } - public static List getAllSchematicsOfType(String schemType) { return getAllSchemsOfType.select(rs -> { List nodes = new ArrayList<>(); @@ -231,18 +247,10 @@ public class SchematicNode { public static List getSchematicsAccessibleByUser(int user, Integer parent) { if (parent != null && parent != 0) { - SchematicNode node = SchematicNode.getSchematicNode(parent); - boolean isAdded = false; - while (node.getId() != 0) { - for (NodeMember member:node.getMembers()) { - if (member.getMember() == user) { - isAdded = true; - break; - } - } - node = SchematicNode.getSchematicNode(node.getParent()); - } - if(isAdded) + if(isSchematicAccessibleForUser.select(rs -> { + rs.next(); + return rs.getInt("Accessible") > 0; + }, parent, user, user)) return getSchematicNodeInNode(parent); } else { return getAccessibleByUser.select(rs -> { @@ -256,15 +264,10 @@ public class SchematicNode { } public static List getAllSchematicsAccessibleByUser(int user) { - return getAccessibleByUser.select(rs -> { + return getAllSchematicsAccessibleByUser.select(rs -> { List nodes = new ArrayList<>(); while(rs.next()) { - SchematicNode node = new SchematicNode(rs); - if(node.isDir()) { - nodes.addAll(deepGet(node.getId(), node1 -> true)); - } else { - nodes.add(node); - } + nodes.add(new SchematicNode(rs)); } return nodes; }, user, user); From 48b2733470b378ea971178a5cc0267191fb8b4d5 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 13 Nov 2021 16:13:54 +0100 Subject: [PATCH 37/75] Fixing Method renaming Signed-off-by: Chaoscaot --- SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java b/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java index 6b84527..d0d9c22 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java @@ -122,9 +122,9 @@ public class SWListInv extends SWInventory { List schems; if(type == null) - schems = SchematicNode.filterSchems(steamwarUserId, node -> true); + schems = SchematicNode.getAllSchematicsAccessibleByUser(steamwarUserId); else - schems = SchematicNode.getSchematicsOfType(steamwarUserId, type.toDB(), 0); + schems = SchematicNode.getAllAccessibleSchematicsOfType(steamwarUserId, type.toDB()); for(SchematicNode s : schems){ Material m; From ee06dc74c7288c8350d3a05365b3ea6deffcf0eb Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 14 Nov 2021 11:27:18 +0100 Subject: [PATCH 38/75] SchematicNode Fixes Signed-off-by: Chaoscaot --- .../src/de/steamwar/sql/SchematicNode.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 878b4b7..bd658ab 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -59,6 +59,7 @@ public class SchematicNode { private static final SQL.Statement getAccessibleByUserByType = new SQL.Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ?"); private static final SQL.Statement getAllSchematicsAccessibleByUser = new SQL.Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType is not null"); private static final SQL.Statement isSchematicAccessibleForUser = new SQL.Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT COUNT(RSN.NodeId) AS `Accessible` FROM RSN Join NodeMember NM On NM.NodeId = RSN.NodeId WHERE NodeOwner = ? OR UserId = ? LIMIT 1"); + private static final SQL.Statement getAllParentsOfNode = new SQL.Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? UNION SELECT SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT * FROM RSN"); private static final SQL.Statement countNodes = new SQL.Statement("SELECT COUNT(NodeId) AS 'count' FROM SchematicNode"); private static final SQL.Statement updateDB = new SQL.Statement("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?"); private static final SQL.Statement updateDatabase = new SQL.Statement("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?"); @@ -185,7 +186,7 @@ public class SchematicNode { if (!rs.next()) return null; return new SchematicNode(rs); - }); + }, id); } public static List getAccessibleSchematicsOfTypeInParent(int owner, String schemType, Integer parent) { @@ -273,6 +274,20 @@ public class SchematicNode { }, user, user); } + public static List getAllParentsOfNode(SchematicNode node) { + return getAllParentsOfNode(node.getId()); + } + + public static List getAllParentsOfNode(int node) { + return getAllParentsOfNode.select(rs -> { + List nodes = new ArrayList<>(); + while(rs.next()) { + nodes.add(new SchematicNode(rs)); + } + return nodes; + }, node); + } + private final int id; private final int owner; private String name; From 20b8dd568f4425aac0153f76da2d70df0d44385d Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 14 Nov 2021 11:32:11 +0100 Subject: [PATCH 39/75] Fix Signed-off-by: Chaoscaot --- SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java index c755a2f..bfe91f6 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java @@ -29,6 +29,7 @@ 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' AND NodeId is not NULL 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 static final SQL.Statement insert = new SQL.Statement("INSERT INTO CheckedSchematic (NodeId, NodeName, NodeOwner, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?)"); + private static final SQL.Statement setNodeNull = new SQL.Statement("UPDATE CheckedSchematic SET NodeId = NULL WHERE NodeId = ?"); private final Integer node; private final int validator; @@ -130,4 +131,8 @@ public class CheckedSchematic { public int getSchemOwner() { return SchematicNode.getSchematicNode(node).getId(); } + + public void remove() { + setNodeNull.update(node); + } } From 794791145b776f2ee35f48ac40cd48aab28263a9 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 14 Nov 2021 12:00:44 +0100 Subject: [PATCH 40/75] Yes Signed-off-by: Chaoscaot --- SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java index bfe91f6..0411239 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java @@ -28,7 +28,7 @@ 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' AND NodeId is not NULL 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 static final SQL.Statement insert = new SQL.Statement("INSERT INTO CheckedSchematic (NodeId, NodeName, NodeOwner, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?)"); + private static final SQL.Statement insert = new SQL.Statement("INSERT INTO CheckedSchematic (NodeId, NodeName, NodeOwner, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?, ?, ?)"); private static final SQL.Statement setNodeNull = new SQL.Statement("UPDATE CheckedSchematic SET NodeId = NULL WHERE NodeId = ?"); private final Integer node; From 373612f24baa58f354d80657d988974e4f410c90 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 15 Nov 2021 19:38:33 +0100 Subject: [PATCH 41/75] Add GuardChecker --- .../src/de/steamwar/command/GuardChecker.java | 30 ++++++++++++++ .../src/de/steamwar/command/SWCommand.java | 39 +++++++++++++++++- .../de/steamwar/command/SWCommandUtils.java | 13 +++++- .../src/de/steamwar/command/SubCommand.java | 41 ++++++++++++++++++- 4 files changed, 118 insertions(+), 5 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/command/GuardChecker.java diff --git a/SpigotCore_Main/src/de/steamwar/command/GuardChecker.java b/SpigotCore_Main/src/de/steamwar/command/GuardChecker.java new file mode 100644 index 0000000..ac3fa3c --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/command/GuardChecker.java @@ -0,0 +1,30 @@ +/* + * 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.command; + +import org.bukkit.command.CommandSender; + +@FunctionalInterface +public interface GuardChecker { + /** + * While guarding the first parameter of the command the parameter s of this method is {@code null} + */ + boolean guard(CommandSender commandSender, String[] previousArguments, String s); +} diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 1db2988..f649a1a 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -39,6 +39,7 @@ public abstract class SWCommand { private final List commandList = new ArrayList<>(); private final List commandHelpList = new ArrayList<>(); private final Map> localTypeMapper = new HashMap<>(); + private final Map localGuardChecker = new HashMap<>(); protected SWCommand(String command) { this(command, new String[0]); @@ -85,6 +86,13 @@ public abstract class SWCommand { addMapper(ClassMapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> { (anno.local() ? localTypeMapper : SWCommandUtils.MAPPER_FUNCTIONS).putIfAbsent(anno.value().getTypeName(), typeMapper); }); + addGuard(Guard.class, method, i -> i == 0, false, GuardChecker.class, (anno, guardChecker) -> { + (anno.local() ? localGuardChecker : SWCommandUtils.GUARD_FUNCTIONS).putIfAbsent(anno.value(), guardChecker); + }); + addGuard(ClassGuard.class, method, i -> i == 0, false, GuardChecker.class, (anno, guardChecker) -> { + (anno.local() ? localGuardChecker : SWCommandUtils.GUARD_FUNCTIONS).putIfAbsent(anno.value().getTypeName(), guardChecker); + }); + add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> { if (!anno.help()) return; if (parameters.length != 2) { @@ -97,7 +105,7 @@ public abstract class SWCommand { Bukkit.getLogger().log(Level.WARNING, () -> "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); return; } - commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>())); + commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>(), localGuardChecker)); }); } for (Method method : methods) { @@ -119,7 +127,7 @@ public abstract class SWCommand { return; } } - commandList.add(new SubCommand(this, method, anno.value(), localTypeMapper)); + commandList.add(new SubCommand(this, method, anno.value(), localTypeMapper, localGuardChecker)); }); this.commandList.sort((o1, o2) -> { @@ -167,6 +175,17 @@ public abstract class SWCommand { }); } + private void addGuard(Class annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class returnType, BiConsumer consumer) { + add(annotation, method, parameterTester, firstParameter, returnType, (anno, parameters) -> { + try { + method.setAccessible(true); + consumer.accept(anno, (GuardChecker) method.invoke(this)); + } catch (Exception e) { + throw new SecurityException(e.getMessage(), e); + } + }); + } + public void unregister() { SWCommandUtils.knownCommandMap.remove(command.getName()); command.getAliases().forEach(SWCommandUtils.knownCommandMap::remove); @@ -207,4 +226,20 @@ public abstract class SWCommand { boolean local() default false; } + + @Retention(RetentionPolicy.RUNTIME) + @Target({ElementType.PARAMETER, ElementType.METHOD}) + protected @interface Guard { + String value() default ""; + + boolean local() default false; + } + + @Retention(RetentionPolicy.RUNTIME) + @Target({ElementType.METHOD}) + protected @interface ClassGuard { + Class value(); + + boolean local() default false; + } } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java index 64c735d..39e56d5 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java @@ -44,6 +44,7 @@ public class SWCommandUtils { } static final Map> MAPPER_FUNCTIONS = new HashMap<>(); + static final Map GUARD_FUNCTIONS = new HashMap<>(); static final TypeMapper ERROR_FUNCTION = createMapper(s -> { throw new SecurityException(); @@ -100,7 +101,7 @@ public class SWCommandUtils { } } - static Object[] generateArgumentArray(CommandSender commandSender, TypeMapper[] parameters, String[] args, Class varArgType, String[] subCommand) throws CommandParseException { + static Object[] generateArgumentArray(CommandSender commandSender, TypeMapper[] parameters, GuardChecker[] guards, String[] args, Class varArgType, String[] subCommand) throws CommandParseException { Object[] arguments = new Object[parameters.length + 1]; int index = 0; while (index < subCommand.length) { @@ -116,6 +117,7 @@ public class SWCommandUtils { } for (int i = 0; i < parameters.length - (varArgType != null ? 1 : 0); i++) { + if (guards[i] != null && !guards[i].guard(commandSender, Arrays.copyOf(args, index), args[index])) throw new CommandParseException(); arguments[i + 1] = parameters[i].map(commandSender, Arrays.copyOf(args, index), args[index]); index++; if (arguments[i + 1] == null) throw new CommandParseException(); @@ -125,6 +127,7 @@ public class SWCommandUtils { Object varArgument = arguments[arguments.length - 1]; for (int i = 0; i < length; i++) { + if (guards[guards.length - 1] != null && !guards[guards.length - 1].guard(commandSender, Arrays.copyOf(args, index), args[index])) throw new CommandParseException(); Object value = parameters[parameters.length - 1].map(commandSender, Arrays.copyOf(args, index), args[index]); if (value == null) throw new CommandParseException(); Array.set(varArgument, i, value); @@ -142,6 +145,14 @@ public class SWCommandUtils { MAPPER_FUNCTIONS.putIfAbsent(name, mapper); } + public static void addGuard(Class clazz, GuardChecker guardChecker) { + addGuard(clazz.getTypeName(), guardChecker); + } + + public static void addGuard(String name, GuardChecker guardChecker) { + GUARD_FUNCTIONS.putIfAbsent(name, guardChecker); + } + public static TypeMapper createMapper(Function mapper, Function> tabCompleter) { return createMapper(mapper, (commandSender, s) -> tabCompleter.apply(s)); } diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index 15dad9a..e32dc38 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -19,6 +19,7 @@ package de.steamwar.command; +import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import java.lang.reflect.InvocationTargetException; @@ -27,6 +28,7 @@ import java.lang.reflect.Parameter; import java.util.*; import java.util.function.Function; import java.util.function.Predicate; +import java.util.logging.Level; import static de.steamwar.command.SWCommandUtils.*; @@ -36,11 +38,13 @@ class SubCommand { private Method method; String[] subCommand; TypeMapper[] arguments; + GuardChecker[] guards; private Predicate commandSenderPredicate; private Function commandSenderFunction; + private GuardChecker guardChecker; Class varArgType = null; - SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map> localTypeMapper) { + SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map> localTypeMapper, Map localGuardChecker) { this.swCommand = swCommand; this.method = method; @@ -48,8 +52,10 @@ class SubCommand { commandSenderPredicate = sender -> parameters[0].getType().isAssignableFrom(sender.getClass()); commandSenderFunction = sender -> parameters[0].getType().cast(sender); this.subCommand = subCommand; + guardChecker = getGuardChecker(parameters[0], localGuardChecker); arguments = new TypeMapper[parameters.length - 1]; + guards = new GuardChecker[parameters.length - 1]; for (int i = 1; i < parameters.length; i++) { Parameter parameter = parameters[i]; Class clazz = parameter.getType(); @@ -76,9 +82,22 @@ class SubCommand { arguments[i - 1] = localTypeMapper.containsKey(name) ? localTypeMapper.get(name) : MAPPER_FUNCTIONS.getOrDefault(name, ERROR_FUNCTION); + guards[i - 1] = getGuardChecker(parameter, localGuardChecker); } } + private GuardChecker getGuardChecker(Parameter parameter, Map localGuardChecker) { + SWCommand.Guard guard = parameter.getAnnotation(SWCommand.Guard.class); + if (guard != null) { + GuardChecker current = localGuardChecker.getOrDefault(guard.value(), GUARD_FUNCTIONS.getOrDefault(guard.value(), null)); + if (guardChecker == null) { + Bukkit.getLogger().log(Level.WARNING, () -> "The guard checker with name '" + guard.value() + "' is neither a local guard checker nor a global one"); + } + return current; + } + return null; + } + boolean invoke(CommandSender commandSender, String[] args) { if (args.length < arguments.length + subCommand.length - (varArgType != null ? 1 : 0)) { return false; @@ -90,7 +109,10 @@ class SubCommand { if (!commandSenderPredicate.test(commandSender)) { return false; } - Object[] objects = SWCommandUtils.generateArgumentArray(commandSender, arguments, args, varArgType, subCommand); + if (!guardChecker.guard(commandSender, new String[0], null)) { + return false; + } + Object[] objects = SWCommandUtils.generateArgumentArray(commandSender, arguments, guards, args, varArgType, subCommand); objects[0] = commandSenderFunction.apply(commandSender); method.setAccessible(true); method.invoke(swCommand, objects); @@ -106,6 +128,9 @@ class SubCommand { if (varArgType == null && args.length > arguments.length + subCommand.length) { return null; } + if (guardChecker != null && !guardChecker.guard(commandSender, new String[0], null)) { + return null; + } int index = 0; List argsList = new LinkedList<>(Arrays.asList(args)); for (String value : subCommand) { @@ -117,9 +142,15 @@ class SubCommand { for (TypeMapper argument : arguments) { String s = argsList.remove(0); if (argsList.isEmpty()) { + if (guards[index] != null && !guards[index].guard(commandSender, Arrays.copyOf(args, args.length - 1), s)) { + return null; + } return argument.tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s); } try { + if (guards[index] != null && !guards[index].guard(commandSender, Arrays.copyOf(args, index), s)) { + return null; + } if (argument.map(commandSender, Arrays.copyOf(args, index), s) == null) { return null; } @@ -132,9 +163,15 @@ class SubCommand { while (!argsList.isEmpty()) { String s = argsList.remove(0); if (argsList.isEmpty()) { + if (guards[guards.length - 1] != null && !guards[guards.length - 1].guard(commandSender, Arrays.copyOf(args, args.length - 1), s)) { + return null; + } return arguments[arguments.length - 1].tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s); } try { + if (guards[guards.length - 1] != null && !guards[guards.length - 1].guard(commandSender, Arrays.copyOf(args, index), s)) { + return null; + } if (arguments[arguments.length - 1].map(commandSender, Arrays.copyOf(args, index), s) == null) { return null; } From 2d87feb63471c081b84ea445fa40cc1bbcf59575 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Mon, 15 Nov 2021 23:49:58 +0100 Subject: [PATCH 42/75] New Function Signed-off-by: Chaoscaot --- .../src/de/steamwar/sql/SchematicNode.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index bd658ab..e47c405 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -46,6 +46,8 @@ public class SchematicNode { private static final SQL.Statement getSchematicNode = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?"); private static final SQL.Statement getSchematicsInNode_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode is NULL"); private static final SQL.Statement getSchematicsInNode = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ?"); + private static final SQL.Statement getDirsInNode_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode is NULL AND NodeType is NULL"); + private static final SQL.Statement getDirsInNode = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ? AND NodeType is NULL"); private static final SQL.Statement getSchematicDirectory_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL"); private static final SQL.Statement getSchematicDirectory = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?"); private static final SQL.Statement getSchematicNodeO_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL"); @@ -146,6 +148,22 @@ public class SchematicNode { } } + public static List getSchematicDirectoryInNode(Integer parent) { + if(parent != null && parent == 0) + parent = null; + SQL.Statement.ResultSetUser> user = rs -> { + List nodes = new ArrayList<>(); + while (rs.next()) + nodes.add(new SchematicNode(rs)); + return nodes; + }; + if(parent == null) { + return getDirsInNode_Null.select(user); + }else { + return getDirsInNode.select(user, parent); + } + } + public static SchematicNode getSchematicDirectory(String name, Integer parent) { if(parent != null && parent == 0) parent = null; From b1f7f205ad1c8ad3c394e25ae3df2fb9306d17a5 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 15 Nov 2021 23:55:37 +0100 Subject: [PATCH 43/75] Add SubCommand --- SpigotCore_Main/src/de/steamwar/command/SubCommand.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index e32dc38..9496d8c 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -89,6 +89,9 @@ class SubCommand { private GuardChecker getGuardChecker(Parameter parameter, Map localGuardChecker) { SWCommand.Guard guard = parameter.getAnnotation(SWCommand.Guard.class); if (guard != null) { + if (guard.value() == null || guard.value().isEmpty()) { + return GUARD_FUNCTIONS.getOrDefault(parameter.getType().getTypeName(), null); + } GuardChecker current = localGuardChecker.getOrDefault(guard.value(), GUARD_FUNCTIONS.getOrDefault(guard.value(), null)); if (guardChecker == null) { Bukkit.getLogger().log(Level.WARNING, () -> "The guard checker with name '" + guard.value() + "' is neither a local guard checker nor a global one"); From d9bb62d5f3e3173007dface942c425d16fbaeb6c Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 16 Nov 2021 20:41:49 +0100 Subject: [PATCH 44/75] Add CommandNoHelpException Add GuardCheckType Add GuardResult --- .../command/CommandNoHelpException.java | 26 ++++++++++++++++ .../de/steamwar/command/GuardCheckType.java | 25 ++++++++++++++++ .../src/de/steamwar/command/GuardChecker.java | 2 +- .../src/de/steamwar/command/GuardResult.java | 26 ++++++++++++++++ .../src/de/steamwar/command/SWCommand.java | 8 +++-- .../de/steamwar/command/SWCommandUtils.java | 2 -- .../src/de/steamwar/command/SubCommand.java | 30 ++++++++++++++----- 7 files changed, 106 insertions(+), 13 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/command/CommandNoHelpException.java create mode 100644 SpigotCore_Main/src/de/steamwar/command/GuardCheckType.java create mode 100644 SpigotCore_Main/src/de/steamwar/command/GuardResult.java diff --git a/SpigotCore_Main/src/de/steamwar/command/CommandNoHelpException.java b/SpigotCore_Main/src/de/steamwar/command/CommandNoHelpException.java new file mode 100644 index 0000000..41b487b --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/command/CommandNoHelpException.java @@ -0,0 +1,26 @@ +/* + * 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.command; + +class CommandNoHelpException extends RuntimeException { + + CommandNoHelpException() { + } +} diff --git a/SpigotCore_Main/src/de/steamwar/command/GuardCheckType.java b/SpigotCore_Main/src/de/steamwar/command/GuardCheckType.java new file mode 100644 index 0000000..f2b2214 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/command/GuardCheckType.java @@ -0,0 +1,25 @@ +/* + * 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.command; + +public enum GuardCheckType { + COMMAND, + TAB_COMPLETE +} diff --git a/SpigotCore_Main/src/de/steamwar/command/GuardChecker.java b/SpigotCore_Main/src/de/steamwar/command/GuardChecker.java index ac3fa3c..e8f3a1f 100644 --- a/SpigotCore_Main/src/de/steamwar/command/GuardChecker.java +++ b/SpigotCore_Main/src/de/steamwar/command/GuardChecker.java @@ -26,5 +26,5 @@ public interface GuardChecker { /** * While guarding the first parameter of the command the parameter s of this method is {@code null} */ - boolean guard(CommandSender commandSender, String[] previousArguments, String s); + GuardResult guard(CommandSender commandSender, GuardCheckType guardCheckType, String[] previousArguments, String s); } diff --git a/SpigotCore_Main/src/de/steamwar/command/GuardResult.java b/SpigotCore_Main/src/de/steamwar/command/GuardResult.java new file mode 100644 index 0000000..9ffbf77 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/command/GuardResult.java @@ -0,0 +1,26 @@ +/* + * 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.command; + +public enum GuardResult { + ALLOWED, + DENIED_WITH_HELP, + DENIED +} diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index f649a1a..fe6387e 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -52,8 +52,12 @@ public abstract class SWCommand { if (!initialized) { createMapping(); } - if (!commandList.stream().anyMatch(s -> s.invoke(sender, args))) { - commandHelpList.stream().anyMatch(s -> s.invoke(sender, args)); + try { + if (!commandList.stream().anyMatch(s -> s.invoke(sender, args))) { + commandHelpList.stream().anyMatch(s -> s.invoke(sender, args)); + } + } catch (CommandNoHelpException e) { + // Ignored } return false; } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java index 39e56d5..52e362e 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java @@ -117,7 +117,6 @@ public class SWCommandUtils { } for (int i = 0; i < parameters.length - (varArgType != null ? 1 : 0); i++) { - if (guards[i] != null && !guards[i].guard(commandSender, Arrays.copyOf(args, index), args[index])) throw new CommandParseException(); arguments[i + 1] = parameters[i].map(commandSender, Arrays.copyOf(args, index), args[index]); index++; if (arguments[i + 1] == null) throw new CommandParseException(); @@ -127,7 +126,6 @@ public class SWCommandUtils { Object varArgument = arguments[arguments.length - 1]; for (int i = 0; i < length; i++) { - if (guards[guards.length - 1] != null && !guards[guards.length - 1].guard(commandSender, Arrays.copyOf(args, index), args[index])) throw new CommandParseException(); Object value = parameters[parameters.length - 1].map(commandSender, Arrays.copyOf(args, index), args[index]); if (value == null) throw new CommandParseException(); Array.set(varArgument, i, value); diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index 9496d8c..426abc6 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -112,11 +112,25 @@ class SubCommand { if (!commandSenderPredicate.test(commandSender)) { return false; } - if (!guardChecker.guard(commandSender, new String[0], null)) { - return false; - } Object[] objects = SWCommandUtils.generateArgumentArray(commandSender, arguments, guards, args, varArgType, subCommand); objects[0] = commandSenderFunction.apply(commandSender); + for (int i = 0; i < objects.length; i++) { + GuardChecker current; + if (i == 0 && guardChecker != null) { + current = guardChecker; + } else { + current = guards[i - 1]; + } + if (current != null) { + GuardResult guardResult = current.guard(commandSender, GuardCheckType.COMMAND, new String[0], null); + if (guardResult != GuardResult.ALLOWED) { + if (guardResult == GuardResult.DENIED) { + throw new CommandNoHelpException(); + } + return false; + } + } + } method.setAccessible(true); method.invoke(swCommand, objects); } catch (IllegalAccessException | RuntimeException | InvocationTargetException e) { @@ -131,7 +145,7 @@ class SubCommand { if (varArgType == null && args.length > arguments.length + subCommand.length) { return null; } - if (guardChecker != null && !guardChecker.guard(commandSender, new String[0], null)) { + if (guardChecker != null && guardChecker.guard(commandSender, GuardCheckType.TAB_COMPLETE, new String[0], null) != GuardResult.ALLOWED) { return null; } int index = 0; @@ -145,13 +159,13 @@ class SubCommand { for (TypeMapper argument : arguments) { String s = argsList.remove(0); if (argsList.isEmpty()) { - if (guards[index] != null && !guards[index].guard(commandSender, Arrays.copyOf(args, args.length - 1), s)) { + if (guards[index] != null && guards[index].guard(commandSender, GuardCheckType.TAB_COMPLETE, Arrays.copyOf(args, args.length - 1), s) != GuardResult.ALLOWED) { return null; } return argument.tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s); } try { - if (guards[index] != null && !guards[index].guard(commandSender, Arrays.copyOf(args, index), s)) { + if (guards[index] != null && guards[index].guard(commandSender, GuardCheckType.TAB_COMPLETE, Arrays.copyOf(args, index), s) != GuardResult.ALLOWED) { return null; } if (argument.map(commandSender, Arrays.copyOf(args, index), s) == null) { @@ -166,13 +180,13 @@ class SubCommand { while (!argsList.isEmpty()) { String s = argsList.remove(0); if (argsList.isEmpty()) { - if (guards[guards.length - 1] != null && !guards[guards.length - 1].guard(commandSender, Arrays.copyOf(args, args.length - 1), s)) { + if (guards[guards.length - 1] != null && guards[guards.length - 1].guard(commandSender, GuardCheckType.TAB_COMPLETE, Arrays.copyOf(args, args.length - 1), s) != GuardResult.ALLOWED) { return null; } return arguments[arguments.length - 1].tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s); } try { - if (guards[guards.length - 1] != null && !guards[guards.length - 1].guard(commandSender, Arrays.copyOf(args, index), s)) { + if (guards[guards.length - 1] != null && guards[guards.length - 1].guard(commandSender, GuardCheckType.TAB_COMPLETE, Arrays.copyOf(args, index), s) != GuardResult.ALLOWED) { return null; } if (arguments[arguments.length - 1].map(commandSender, Arrays.copyOf(args, index), s) == null) { From cc5d43519997e9516da80850be773c917e3c4c90 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 16 Nov 2021 21:18:42 +0100 Subject: [PATCH 45/75] Add GuardCheckType.HELP_COMMAND --- .../src/de/steamwar/command/GuardCheckType.java | 1 + .../src/de/steamwar/command/SWCommand.java | 4 ++-- .../src/de/steamwar/command/SubCommand.java | 16 +++++++++++----- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/GuardCheckType.java b/SpigotCore_Main/src/de/steamwar/command/GuardCheckType.java index f2b2214..0f023b8 100644 --- a/SpigotCore_Main/src/de/steamwar/command/GuardCheckType.java +++ b/SpigotCore_Main/src/de/steamwar/command/GuardCheckType.java @@ -21,5 +21,6 @@ package de.steamwar.command; public enum GuardCheckType { COMMAND, + HELP_COMMAND, TAB_COMPLETE } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index fe6387e..99864c2 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -109,7 +109,7 @@ public abstract class SWCommand { Bukkit.getLogger().log(Level.WARNING, () -> "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); return; } - commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>(), localGuardChecker)); + commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>(), localGuardChecker, true)); }); } for (Method method : methods) { @@ -131,7 +131,7 @@ public abstract class SWCommand { return; } } - commandList.add(new SubCommand(this, method, anno.value(), localTypeMapper, localGuardChecker)); + commandList.add(new SubCommand(this, method, anno.value(), localTypeMapper, localGuardChecker, false)); }); this.commandList.sort((o1, o2) -> { diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index 426abc6..87b9485 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -43,10 +43,12 @@ class SubCommand { private Function commandSenderFunction; private GuardChecker guardChecker; Class varArgType = null; + private boolean help = false; - SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map> localTypeMapper, Map localGuardChecker) { + SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map> localTypeMapper, Map localGuardChecker, boolean help) { this.swCommand = swCommand; this.method = method; + this.help = help; Parameter[] parameters = method.getParameters(); commandSenderPredicate = sender -> parameters[0].getType().isAssignableFrom(sender.getClass()); @@ -116,13 +118,13 @@ class SubCommand { objects[0] = commandSenderFunction.apply(commandSender); for (int i = 0; i < objects.length; i++) { GuardChecker current; - if (i == 0 && guardChecker != null) { + if (i == 0) { current = guardChecker; } else { current = guards[i - 1]; } if (current != null) { - GuardResult guardResult = current.guard(commandSender, GuardCheckType.COMMAND, new String[0], null); + GuardResult guardResult = current.guard(commandSender, help ? GuardCheckType.HELP_COMMAND : GuardCheckType.COMMAND, new String[0], null); if (guardResult != GuardResult.ALLOWED) { if (guardResult == GuardResult.DENIED) { throw new CommandNoHelpException(); @@ -133,6 +135,8 @@ class SubCommand { } method.setAccessible(true); method.invoke(swCommand, objects); + } catch (CommandNoHelpException e) { + throw e; } catch (IllegalAccessException | RuntimeException | InvocationTargetException e) { throw new SecurityException(e.getMessage(), e); } catch (CommandParseException e) { @@ -156,16 +160,17 @@ class SubCommand { if (!value.equalsIgnoreCase(s)) return null; index++; } + int guardIndex = 0; for (TypeMapper argument : arguments) { String s = argsList.remove(0); if (argsList.isEmpty()) { - if (guards[index] != null && guards[index].guard(commandSender, GuardCheckType.TAB_COMPLETE, Arrays.copyOf(args, args.length - 1), s) != GuardResult.ALLOWED) { + if (guards[guardIndex] != null && guards[guardIndex].guard(commandSender, GuardCheckType.TAB_COMPLETE, Arrays.copyOf(args, args.length - 1), s) != GuardResult.ALLOWED) { return null; } return argument.tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s); } try { - if (guards[index] != null && guards[index].guard(commandSender, GuardCheckType.TAB_COMPLETE, Arrays.copyOf(args, index), s) != GuardResult.ALLOWED) { + if (guards[guardIndex] != null && guards[guardIndex].guard(commandSender, GuardCheckType.TAB_COMPLETE, Arrays.copyOf(args, index), s) != GuardResult.ALLOWED) { return null; } if (argument.map(commandSender, Arrays.copyOf(args, index), s) == null) { @@ -175,6 +180,7 @@ class SubCommand { return null; } index++; + guardIndex++; } if (varArgType != null && !argsList.isEmpty()) { while (!argsList.isEmpty()) { From b1808eb4c5c538da794e503529a17da106bed2a9 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 16 Nov 2021 21:20:32 +0100 Subject: [PATCH 46/75] Add GuardCheckType.HELP_COMMAND --- SpigotCore_Main/src/de/steamwar/command/SubCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index 87b9485..c1cb92a 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -43,7 +43,7 @@ class SubCommand { private Function commandSenderFunction; private GuardChecker guardChecker; Class varArgType = null; - private boolean help = false; + private boolean help; SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map> localTypeMapper, Map localGuardChecker, boolean help) { this.swCommand = swCommand; From 8124189214cb7e6b3e7c93c17fad21155a3e27d2 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 16 Nov 2021 22:28:08 +0100 Subject: [PATCH 47/75] Fix SubCommand behaviour --- .../src/de/steamwar/command/SubCommand.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index c1cb92a..3cd6778 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -92,7 +92,11 @@ class SubCommand { SWCommand.Guard guard = parameter.getAnnotation(SWCommand.Guard.class); if (guard != null) { if (guard.value() == null || guard.value().isEmpty()) { - return GUARD_FUNCTIONS.getOrDefault(parameter.getType().getTypeName(), null); + String s = parameter.getType().getTypeName(); + if (parameter.isVarArgs()) { + s = parameter.getType().getComponentType().getTypeName(); + } + return localGuardChecker.getOrDefault(s, GUARD_FUNCTIONS.getOrDefault(s, null)); } GuardChecker current = localGuardChecker.getOrDefault(guard.value(), GUARD_FUNCTIONS.getOrDefault(guard.value(), null)); if (guardChecker == null) { @@ -116,15 +120,24 @@ class SubCommand { } Object[] objects = SWCommandUtils.generateArgumentArray(commandSender, arguments, guards, args, varArgType, subCommand); objects[0] = commandSenderFunction.apply(commandSender); - for (int i = 0; i < objects.length; i++) { + for (int i = subCommand.length; i < args.length; i++) { GuardChecker current; - if (i == 0) { + if (i == subCommand.length) { current = guardChecker; } else { - current = guards[i - 1]; + if (i >= objects.length + subCommand.length) { + current = guards[guards.length - 1]; + } else { + current = guards[i - 1 - subCommand.length]; + } } if (current != null) { - GuardResult guardResult = current.guard(commandSender, help ? GuardCheckType.HELP_COMMAND : GuardCheckType.COMMAND, new String[0], null); + GuardResult guardResult; + if (i == 0) { + guardResult = current.guard(commandSender, help ? GuardCheckType.HELP_COMMAND : GuardCheckType.COMMAND, new String[0], null); + } else { + guardResult = current.guard(commandSender, help ? GuardCheckType.HELP_COMMAND : GuardCheckType.COMMAND, Arrays.copyOf(args, i - 1), args[i - 1]); + } if (guardResult != GuardResult.ALLOWED) { if (guardResult == GuardResult.DENIED) { throw new CommandNoHelpException(); From da2dfc7a33638edf1781658fa08489c22bdb6c1e Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 20 Nov 2021 11:26:30 +0100 Subject: [PATCH 48/75] Fixing Statements Signed-off-by: Chaoscaot --- .../src/de/steamwar/sql/SchematicNode.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index e47c405..f885d59 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -55,12 +55,12 @@ public class SchematicNode { private static final SQL.Statement getSchematicNodeId = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ?"); private static final SQL.Statement getAllSchemsOfTypeOwner = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?"); private static final SQL.Statement getAllSchemsOfType = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeType = ?"); - private static final SQL.Statement getAccessibleByUser = new SQL.Statement("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND s.ParentNode is NULL GROUP BY s.NodeId ORDER BY s.NodeName"); + private static final SQL.Statement getAccessibleByUser = new SQL.Statement("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND ((s.NodeOwner = ? AND s.ParentNode IS NULL) OR NOT s.NodeOwner = ?) GROUP BY s.NodeId ORDER BY s.NodeName"); private static final SQL.Statement getAccessibleByUserByTypeInNode = new SQL.Statement("WITH RECURSIVE RSNB AS (WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSNB WHERE SN.NodeId = RSNB.ParentNode)SELECT * FROM RSNB WHERE ParentNode = ?"); private static final SQL.Statement getAccessibleByUserByTypeInNode_Null = new SQL.Statement("WITH RECURSIVE RSNB AS (WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSNB WHERE SN.NodeId = RSNB.ParentNode)SELECT * FROM RSNB WHERE ParentNode is null"); private static final SQL.Statement getAccessibleByUserByType = new SQL.Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ?"); - private static final SQL.Statement getAllSchematicsAccessibleByUser = new SQL.Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType is not null"); - private static final SQL.Statement isSchematicAccessibleForUser = new SQL.Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT COUNT(RSN.NodeId) AS `Accessible` FROM RSN Join NodeMember NM On NM.NodeId = RSN.NodeId WHERE NodeOwner = ? OR UserId = ? LIMIT 1"); + private static final SQL.Statement getAllSchematicsAccessibleByUser = new SQL.Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN"); + private static final SQL.Statement isSchematicAccessibleForUser = new SQL.Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT COUNT(RSN.NodeId) AS `Accessible` FROM RSN LEFT Join NodeMember NM On NM.NodeId = RSN.NodeId WHERE NodeOwner = ? OR UserId = ? LIMIT 1"); private static final SQL.Statement getAllParentsOfNode = new SQL.Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? UNION SELECT SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT * FROM RSN"); private static final SQL.Statement countNodes = new SQL.Statement("SELECT COUNT(NodeId) AS 'count' FROM SchematicNode"); private static final SQL.Statement updateDB = new SQL.Statement("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?"); @@ -218,7 +218,7 @@ public class SchematicNode { if(parent == null || parent == 0) { return getAccessibleByUserByTypeInNode_Null.select(user, owner, owner, schemType); } else { - return getAccessibleByUserByTypeInNode.select(user, owner, owner, parent, schemType); + return getAccessibleByUserByTypeInNode.select(user, owner, owner, schemType, parent); } } @@ -277,7 +277,7 @@ public class SchematicNode { while(rs.next()) nodes.add(new SchematicNode(rs)); return nodes; - }, user, user); + }, user, user, user, user); } return Collections.emptyList(); } @@ -452,7 +452,7 @@ public class SchematicNode { } private void updateDB() { - updateDB.update(name, owner, parent == 0 ? null : parent, item, type, rank, id); + updateDB.update(name, owner, parent, item, type, rank, id); this.lastUpdate = Timestamp.from(Instant.now()); this.brCache.clear(); } From 39378b0c30c66a1d34fe0959c8574ace33152719 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 20 Nov 2021 13:23:26 +0100 Subject: [PATCH 49/75] Fix Schematic Creation Signed-off-by: Chaoscaot --- SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index f885d59..70a4b8a 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -77,7 +77,7 @@ public class SchematicNode { } public static SchematicNode createSchematicNode(int owner, String name, Integer parent, String type, String item) { - if (parent == 0) + if (parent != null && parent == 0) parent = null; createNode.update(name, owner, parent, type, item); return getSchematicNode(owner, name, parent); From 0f917ac4f68abcba4b515032e4ad4ec164118a5c Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 20 Nov 2021 14:43:09 +0100 Subject: [PATCH 50/75] Hotfix Member Schematics Info Signed-off-by: Chaoscaot --- SpigotCore_Main/src/de/steamwar/sql/Schematic.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java index efe4b66..ac3cd46 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -28,7 +28,9 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.UUID; +import java.util.stream.Collectors; @Deprecated public class Schematic { @@ -61,6 +63,11 @@ public class Schematic { SchematicNode node = SchematicNode.getSchematicNode(schemOwner, schemName, 0); if(node != null) { return new Schematic(node); + } else { + Optional n = SchematicNode.getSchematicsAccessibleByUser(schemOwner, 0).stream().filter(node1 -> node1.getName().equals(schemName)).findAny(); + if(n.isPresent()) { + return new Schematic(n.get()); + } } return null; } From 0ff834a4d0ad653e0f0415f6843d08878c792801 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 20 Nov 2021 17:39:30 +0100 Subject: [PATCH 51/75] Hotfix Update ParentNode FK Fail Signed-off-by: Chaoscaot --- SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 70a4b8a..a50b48d 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -94,6 +94,9 @@ public class SchematicNode { owner = set.getInt("NodeOwner"); name = set.getString("NodeName"); parent = set.getInt("ParentNode"); + if(set.wasNull()) { + parent = null; + } item = set.getString("NodeItem"); type = set.getString("NodeType"); lastUpdate = set.getTimestamp("LastUpdate"); @@ -417,6 +420,7 @@ public class SchematicNode { } public SchematicNode getParentNode() { + if(parent == null) return null; return SchematicNode.getSchematicNode(parent); } From f1e5e39156b889169135eed1aad113430826397c Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 20 Nov 2021 18:56:38 +0100 Subject: [PATCH 52/75] Add SWCommand.Register.description to simplify help commands --- SpigotCore_Main/build.gradle | 5 ++ .../src/de/steamwar/command/SWCommand.java | 76 ++++++++++++++++++- .../src/de/steamwar/command/SubCommand.java | 8 +- 3 files changed, 82 insertions(+), 7 deletions(-) diff --git a/SpigotCore_Main/build.gradle b/SpigotCore_Main/build.gradle index 59c9f89..7a48ea5 100644 --- a/SpigotCore_Main/build.gradle +++ b/SpigotCore_Main/build.gradle @@ -46,6 +46,11 @@ dependencies { compileOnly files("${project.rootDir}/lib/Spigot-1.15.jar") compileOnly files("${project.rootDir}/lib/WorldEdit-1.12.jar") implementation 'net.wesjd:anvilgui:1.4.0-SNAPSHOT' + + compileOnly 'org.projectlombok:lombok:1.18.6' + testCompileOnly 'org.projectlombok:lombok:1.18.6' + annotationProcessor 'org.projectlombok:lombok:1.18.6' + testAnnotationProcessor 'org.projectlombok:lombok:1.18.6' } processResources { diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 99864c2..f04cf62 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -19,14 +19,18 @@ package de.steamwar.command; +import de.steamwar.message.Message; +import lombok.Setter; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; import java.lang.annotation.*; import java.lang.reflect.Method; import java.lang.reflect.Parameter; import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; import java.util.function.BiConsumer; import java.util.function.IntPredicate; import java.util.logging.Level; @@ -41,6 +45,9 @@ public abstract class SWCommand { private final Map> localTypeMapper = new HashMap<>(); private final Map localGuardChecker = new HashMap<>(); + @Setter + private Message message = null; + protected SWCommand(String command) { this(command, new String[0]); } @@ -82,7 +89,7 @@ public abstract class SWCommand { } private synchronized void createMapping() { - Method[] methods = getClass().getDeclaredMethods(); + List methods = methods(); for (Method method : methods) { addMapper(Mapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> { (anno.local() ? localTypeMapper : SWCommandUtils.MAPPER_FUNCTIONS).putIfAbsent(anno.value(), typeMapper); @@ -109,7 +116,7 @@ public abstract class SWCommand { Bukkit.getLogger().log(Level.WARNING, () -> "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); return; } - commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>(), localGuardChecker, true)); + commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>(), localGuardChecker, true, null)); }); } for (Method method : methods) { @@ -131,7 +138,7 @@ public abstract class SWCommand { return; } } - commandList.add(new SubCommand(this, method, anno.value(), localTypeMapper, localGuardChecker, false)); + commandList.add(new SubCommand(this, method, anno.value(), localTypeMapper, localGuardChecker, false, anno.description())); }); this.commandList.sort((o1, o2) -> { @@ -143,7 +150,15 @@ public abstract class SWCommand { o2.varArgType != null ? Integer.MAX_VALUE : o2.arguments.length); } }); - commandHelpList.sort(Comparator.comparingInt(o -> -o.subCommand.length)); + commandHelpList.sort((o1, o2) -> { + int compare = Integer.compare(-o1.subCommand.length, -o2.subCommand.length); + if (compare != 0) { + return compare; + } else { + return Integer.compare(o1.method.getDeclaringClass() == SWCommand.class ? 1 : 0, + o2.method.getDeclaringClass() == SWCommand.class ? 1 : 0); + } + }); } initialized = true; } @@ -190,6 +205,12 @@ public abstract class SWCommand { }); } + private List methods() { + List methods = new ArrayList<>(Arrays.asList(getClass().getDeclaredMethods())); + methods.addAll(Arrays.asList(SWCommand.class.getDeclaredMethods())); + return methods; + } + public void unregister() { SWCommandUtils.knownCommandMap.remove(command.getName()); command.getAliases().forEach(SWCommandUtils.knownCommandMap::remove); @@ -200,6 +221,51 @@ public abstract class SWCommand { SWCommandUtils.commandMap.register("steamwar", this.command); } + @Register(help = true) + private void internalHelp(Player p, String... args) { + if (message == null) { + return; + } + try { + message.sendPrefixless("COMMAND_HELP_HEAD", p, command.getName()); + } catch (Exception e) { + Bukkit.getLogger().log(Level.WARNING, "Failed to send help message", e); + return; + } + AtomicInteger atomicInteger = new AtomicInteger(); + if (args.length != 0) { + commandList.forEach(subCommand -> { + List tabCompletes = subCommand.tabComplete(p, args); + if (tabCompletes == null || tabCompletes.isEmpty()) { + atomicInteger.incrementAndGet(); + return; + } + boolean hasTabCompletes = tabCompletes.stream() + .anyMatch(s -> s.toLowerCase().startsWith(args[args.length - 1].toLowerCase())); + if (hasTabCompletes) { + try { + message.sendPrefixless(subCommand.description, p); + } catch (Exception e) { + Bukkit.getLogger().log(Level.WARNING, "Failed to send description of registered method '" + subCommand.method + "' with description '" + subCommand.description + "'", e); + } + } else { + atomicInteger.incrementAndGet(); + } + }); + } + if (args.length == 0 || atomicInteger.get() == commandList.size()) { + commandList.forEach(subCommand -> { + if (subCommand.guardChecker == null || subCommand.guardChecker.guard(p, GuardCheckType.TAB_COMPLETE, new String[0], null) == GuardResult.ALLOWED) { + try { + message.sendPrefixless(subCommand.description, p); + } catch (Exception e) { + Bukkit.getLogger().log(Level.WARNING, "Failed to send description of registered method '" + subCommand.method + "' with description '" + subCommand.description + "'", e); + } + } + }); + } + } + @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) @Repeatable(Register.Registeres.class) @@ -208,6 +274,8 @@ public abstract class SWCommand { boolean help() default false; + String description() default ""; + @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) @interface Registeres { diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index 3cd6778..4581c8d 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -35,20 +35,22 @@ import static de.steamwar.command.SWCommandUtils.*; class SubCommand { private SWCommand swCommand; - private Method method; + Method method; + String description; String[] subCommand; TypeMapper[] arguments; GuardChecker[] guards; private Predicate commandSenderPredicate; private Function commandSenderFunction; - private GuardChecker guardChecker; + GuardChecker guardChecker; Class varArgType = null; private boolean help; - SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map> localTypeMapper, Map localGuardChecker, boolean help) { + SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map> localTypeMapper, Map localGuardChecker, boolean help, String description) { this.swCommand = swCommand; this.method = method; this.help = help; + this.description = description; Parameter[] parameters = method.getParameters(); commandSenderPredicate = sender -> parameters[0].getType().isAssignableFrom(sender.getClass()); From 745ac401bb5eaf298ff19eaf5aefb36f436e05e0 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 20 Nov 2021 19:10:22 +0100 Subject: [PATCH 53/75] Add SWCommand.Register.description to simplify help commands --- SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index f04cf62..4125bb7 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -47,6 +47,7 @@ public abstract class SWCommand { @Setter private Message message = null; + private List defaultHelpMessages = new ArrayList<>(); protected SWCommand(String command) { this(command, new String[0]); @@ -205,6 +206,10 @@ public abstract class SWCommand { }); } + public void addDefaultHelpMessage(String message) { + defaultHelpMessages.add(message); + } + private List methods() { List methods = new ArrayList<>(Arrays.asList(getClass().getDeclaredMethods())); methods.addAll(Arrays.asList(SWCommand.class.getDeclaredMethods())); @@ -228,6 +233,7 @@ public abstract class SWCommand { } try { message.sendPrefixless("COMMAND_HELP_HEAD", p, command.getName()); + defaultHelpMessages.forEach(s -> message.sendPrefixless(s, p)); } catch (Exception e) { Bukkit.getLogger().log(Level.WARNING, "Failed to send help message", e); return; From bd6e9a0246ad48e7775b21e6147308f2c398f176 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 21 Nov 2021 10:23:36 +0100 Subject: [PATCH 54/75] Add Back missing Function Signed-off-by: Chaoscaot --- SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index a50b48d..1054b90 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -522,6 +522,14 @@ public class SchematicNode { schemFormat = newFormat; } + 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); + } + } + @Override public boolean equals(Object obj) { if (!(obj instanceof SchematicNode)) From 22df0035c47ac5306310dd22f1fac3072035e82a Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 21 Nov 2021 10:55:08 +0100 Subject: [PATCH 55/75] Add SchematicSelector Signed-off-by: Chaoscaot --- .../de/steamwar/util/SchematicSelector.java | 438 ++++++++++++++++++ .../util/SchematicSelectorInjectable.java | 39 ++ .../SchematicSelectorInjectableAdapter.java | 47 ++ .../src/de/steamwar/util/UtilGui.java | 53 +++ 4 files changed, 577 insertions(+) create mode 100644 SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java create mode 100644 SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java create mode 100644 SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectableAdapter.java create mode 100644 SpigotCore_Main/src/de/steamwar/util/UtilGui.java diff --git a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java new file mode 100644 index 0000000..5cd4ef7 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java @@ -0,0 +1,438 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.util; + +import com.google.common.collect.Lists; +import de.steamwar.inventory.*; +import de.steamwar.sql.SchematicNode; +import de.steamwar.sql.SchematicType; +import de.steamwar.sql.SteamwarUser; +import lombok.*; +import org.bukkit.Material; +import org.bukkit.entity.Player; + +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.function.Consumer; + +public class SchematicSelector { + + @Getter + private final Player player; + @Getter + private final SteamwarUser user; + @Getter + private final Consumer callback; + @Getter + private final SelectorTarget target; + @Getter + private final SelectorFilter filter = new SelectorFilter(); + private SchematicSelectorInjectable injectable; + private boolean useHooks; + @Setter + @Getter + private PublicMode publicMode = PublicMode.ALL; + @Setter + @Getter + private String title = "{0} auswahl: {1}"; + @Setter + @Getter + private boolean singleDirOpen; + + public SchematicSelector(Player player, SelectorTarget target, Consumer callback) { + this.player = player; + this.user = SteamwarUser.get(player.getUniqueId()); + this.target = target; + this.callback = callback; + this.singleDirOpen = !target.target.isDirs(); + } + + public SchematicSelector(Player player, SelectorTarget target, SchematicSelectorInjectable injectable, Consumer callback) { + this(player, target, callback); + this.useHooks = true; + this.injectable = injectable; + } + + public void open() { + if(useHooks) { + injectable.onSelectorCreate(this); + } + openList(null, publicMode == PublicMode.PUBLIC_ONLY); + } + + private void openList(SchematicNode parent, boolean publics) { + List nodes = new ArrayList<>(); + + if(filter.isFilter()) { + nodes.addAll(SchematicNode.getAllSchematicsAccessibleByUser(publics?0:user.getId())); + nodes.removeIf(node -> { + if(useHooks) { + injectable.onNodeFilter(this, node); + } + return !filter.matches(node); + }); + if(target.target == SelectorTarget.Target.DIRECTORY) { + nodes.removeIf(node -> !node.isDir()); + } + if(target.target == SelectorTarget.Target.SCHEMATIC_TYPE) { + nodes.removeIf(node -> node.isDir() || !node.getType().equals(target.type.toDB())); + } + } else { + switch (target.target) { + case DIRECTORY: + if(parent == null) { + nodes.addAll(SchematicNode.getSchematicsAccessibleByUser(publics?0:user.getId(), null)); + nodes.removeIf(node -> !node.isDir()); + } else { + nodes.addAll(SchematicNode.getSchematicDirectoryInNode(parent.getId())); + } + break; + case SCHEMATIC_TYPE: + nodes.addAll(SchematicNode.getAccessibleSchematicsOfTypeInParent(publics?0:user.getId(), target.type.toDB(), parent==null?0:parent.getId())); + if(target.rank >= 0) { + nodes.removeIf(node -> node.getRank() > target.rank); + } + break; + default: + nodes.addAll(SchematicNode.getSchematicsAccessibleByUser(publics?0:user.getId(), parent == null?0:parent.getId())); + } + + if(singleDirOpen && nodes.size() == 1 && nodes.get(0).isDir()) { + openList(nodes.get(0), publics); + return; + } + } + + List> list = new ArrayList<>(); + + if(parent != null) { + list.add(new SWListInv.SWListEntry<>(new SWItem(Material.ARROW, "§eZurück", clickType -> {}), null)); + } + + for (SchematicNode node : nodes) { + if(node.getName().equals("//copy")) continue; + Material m; + if (node.getItem().isEmpty()) + m = node.isDir()?SWItem.getMaterial("CHEST"):SWItem.getMaterial("CAULDRON_ITEM"); + else + m = SWItem.getMaterial(node.getItem()); + + String name = "§" + (filter.getName().isEmpty()?"e":"7") + node.getName(); + + if(!filter.getName().isEmpty()) { + name = name.replace(filter.getName(), "§e" + filter.getName() + "§7"); + } + + SWItem item = new SWItem(m, name, Collections.singletonList(node.isDir() ? "§9Ordner" : "§7" + node.getSchemtype().name()), !node.isDir() && !node.getSchemtype().writeable(), click -> { + }); + if(!node.isDir()) { + if(node.getRank() > 0) + item.setLore(Lists.newArrayList("§7" + node.getSchemtype().name(), "§8Rang " + node.getRank())); + } + list.add(new SWListInv.SWListEntry<>(item, node)); + } + + SWListInv inv = new SWListInv<>(player, MessageFormat.format(title, target.target.getName(), filter.getName().isEmpty()?(parent == null?"/":parent.generateBreadcrumbs(user)):filter.getName()), false, list, (clickType, node) -> { + if(node == null) { + openList(getParent(parent), publics); + return; + } + if(node.isDir()) { + if(filter.isFilter() && target.target.isDirs()) { + player.closeInventory(); + callback.accept(node); + return; + } + filter.reset(); + openList(node, publics); + return; + } + player.closeInventory(); + callback.accept(node); + }); + if(publicMode == PublicMode.ALL) { + if(publics) { + inv.setItem(48, Material.BUCKET, "§7Eigene Schematics", clickType -> openList(null, false)); + } else { + inv.setItem(48, Material.GLASS, "§7Public Schematics", clickType -> openList(null, true)); + } + } + if(target.target.isDirs()) { + inv.setItem(49, SWItem.getDye(10), "§7Ordner auswählen", clickType -> { + player.closeInventory(); + callback.accept(parent); + }); + } + if(!publics) { + inv.setItem(50, Material.CHEST, "§7Neuer Ordner", clickType -> createFolderIn(parent)); + } + inv.setItem(51, Material.NAME_TAG, "§7Filter", clickType -> openFilter(publics)); + + if(useHooks) { + injectable.onListRender(this, inv, parent); + } + inv.open(); + } + + private void createFolderIn(SchematicNode parent) { + SWAnvilInv inv = new SWAnvilInv(player, "Ordner Erstellen"); + inv.setItem(Material.CHEST); + inv.setCallback(s -> { + if(useHooks) { + if(injectable.onFolderCreate(this, s)) { + SchematicNode.createSchematicDirectory(user.getId(), s, parent==null?0:parent.getId()); + openList(parent, false); + } + } else { + SchematicNode.createSchematicDirectory(user.getId(), s, parent==null?0:parent.getId()); + openList(parent, false); + } + }); + inv.open(); + } + + private void openFilter(boolean publics) { + SWInventory inv = new SWInventory(player, 9, "Filter"); + InvCallback nameCallback = clickType -> { + if(clickType.isRightClick()) { + filter.setName(""); + openFilter(publics); + } else { + SWAnvilInv swAnvilInv = new SWAnvilInv(player, "Name eingeben"); + swAnvilInv.setItem(Material.NAME_TAG); + swAnvilInv.setCallback(s -> { + filter.setName(s); + openFilter(publics); + }); + swAnvilInv.open(); + } + }; + if(filter.getName().isEmpty()) { + inv.setItem(0, Material.NAME_TAG, "§7Nach namen suchen...", nameCallback); + } else { + inv.setItem(0, Material.NAME_TAG, "§7Nach namen suchen...", Collections.singletonList("§7Suchwort: §e" + filter.getName()), true, nameCallback); + } + + InvCallback ownerCallback = clickType -> { + if(clickType.isRightClick()) { + filter.setOwner(null); + openFilter(publics); + } else { + SWAnvilInv swAnvilInv = new SWAnvilInv(player, "Besitzer eingeben"); + swAnvilInv.setItem(Material.PLAYER_HEAD); + swAnvilInv.setCallback(s -> { + filter.setOwner(SteamwarUser.get(s).getId()); + openFilter(publics); + }); + swAnvilInv.open(); + } + }; + if(filter.getOwner() == null) { + inv.setItem(1, Material.PLAYER_HEAD, "§7Nach Besitzer suchen...", ownerCallback); + } else { + SteamwarUser user = SteamwarUser.get(filter.getOwner()); + SWItem item = SWItem.getPlayerSkull(user.getUserName()); + item.setName("§7Nach Besitzer suchen..."); + item.setEnchanted(true); + item.setLore(Collections.singletonList("§7Besitzer: §e" + user.getUserName())); + item.setCallback(ownerCallback); + inv.setItem(1, item); + } + + if(target.target != SelectorTarget.Target.SCHEMATIC_TYPE) { + InvCallback schemTypeCallback = clickType -> { + if(clickType.isRightClick()) { + filter.setType(null); + openFilter(publics); + } else { + List> types = new ArrayList<>(); + SchematicType.values().forEach(schematicType -> { + types.add(new SWListInv.SWListEntry<>(new SWItem(SWItem.getMaterial("STONE_BUTTON"), "§e" + schematicType.name(), Collections.emptyList(), schematicType.fightType(), n -> {}), schematicType)); + }); + SWListInv listInv = new SWListInv<>(player, "Typ wählen...", types, (clickType1, schematicType) -> { + filter.setType(schematicType); + openFilter(publics); + }); + listInv.open(); + } + }; + + if(filter.getType() == null) { + inv.setItem(2, SWItem.getMaterial("STONE_BUTTON"), "§7Nach Typ Filtern...", schemTypeCallback); + } else { + inv.setItem(2, SWItem.getMaterial("STONE_BUTTON"), "§7Nach Typ Filtern...", Collections.singletonList("§7Typ: §e" + filter.getType().name()), true, schemTypeCallback); + } + } + + InvCallback materialCallback = clickType -> { + if(clickType.isRightClick()) { + filter.setItem(null); + openFilter(publics); + } else { + UtilGui.openMaterialSelector(player, material -> { + filter.setItem(material); + openFilter(publics); + }); + } + }; + + final int iSlot = target.target== SelectorTarget.Target.SCHEMATIC_TYPE?2:3; + + if(filter.getItem() == null) { + inv.setItem(iSlot, Material.STONE, "§7Nach Item Filtern...", materialCallback); + } else { + inv.setItem(iSlot, filter.getItem(), "§7Nach Item Filtern...", Collections.singletonList("§7Item: §e" + filter.getItem().name()), true, materialCallback); + } + + inv.setItem(7, SWItem.getDye(1), "§eAbbrechen", clickType -> { + filter.reset(); + openList(null, publics); + }); + inv.setItem(8, SWItem.getDye(10), "§eSuchen...", clickType -> { + filter.setFilter(true); + if(useHooks) { + injectable.onFilterApply(this); + } + openList(null, publics); + }); + + if(useHooks) { + injectable.onFilterRender(this, inv); + } + + inv.open(); + } + + private static SchematicNode getParent(SchematicNode node) { + if(node.getParent() == null) { + return null; + } + return node.getParentNode(); + } + + public static SelectorTarget selectSchematic() { + return new SelectorTarget(SelectorTarget.Target.SCHEMATIC, null, -1); + } + + public static SelectorTarget selectDirectory() { + return new SelectorTarget(SelectorTarget.Target.DIRECTORY, null, -1); + } + + public static SelectorTarget selectSchematicNode() { + return new SelectorTarget(SelectorTarget.Target.SCHEMATIC_NODE, null, -1); + } + + public static SelectorTarget selectSchematicType(SchematicType type) { + return new SelectorTarget(SelectorTarget.Target.SCHEMATIC_TYPE, type, -1); + } + + public static SelectorTarget selectSchematicTypeWithRank(SchematicType type, int rank) { + return new SelectorTarget(SelectorTarget.Target.SCHEMATIC_TYPE, type, rank); + } + + private static class SelectorTarget { + + private final Target target; + private final SchematicType type; + private final int rank; + + private SelectorTarget(Target target, SchematicType type, int rank) { + this.target = target; + this.type = type; + this.rank = rank; + } + + @AllArgsConstructor + private enum Target { + SCHEMATIC("Schematic", false), + DIRECTORY("Ordner", true), + SCHEMATIC_NODE("Schematic/Ordner", true), + SCHEMATIC_TYPE("Schematic", false); + + @Getter + private String name; + @Getter + private boolean dirs; + } + } + + @NoArgsConstructor + @Getter + @Setter + public static class SelectorFilter { + + private boolean filter; + + private String name = ""; + private Integer owner = null; + private SchematicType type = null; + private Material item = null; + + public void reset() { + name = ""; + owner = null; + type = null; + item = null; + filter = false; + } + + public boolean matches(SchematicNode node) { + boolean matches = true; + if(!name.isEmpty()) { + if(!node.getName().contains(name)) { + matches = false; + } + } + + if(owner != null) { + if(node.getOwner() != owner) { + matches = false; + } + } + + if(type != null) { + if(node.isDir() || !node.getType().equals(type.toDB())) { + matches = false; + } + } + + if(item != null) { + String i; + if(node.getItem().isEmpty()) { + i = node.isDir()?"CHEST":"CAULDRON"; + } else { + i = node.getItem(); + } + if(!item.name().equals(i)) { + matches = false; + } + } + return matches; + } + } + + public enum PublicMode { + ALL, + PRIVATE_ONLY, + PUBLIC_ONLY + } +} diff --git a/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java b/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java new file mode 100644 index 0000000..35629d0 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java @@ -0,0 +1,39 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.util; + +import de.steamwar.inventory.SWInventory; +import de.steamwar.inventory.SWListInv; +import de.steamwar.sql.SchematicNode; + +public interface SchematicSelectorInjectable { + + void onSelectorCreate(SchematicSelector selector); + + void onListRender(SchematicSelector selector, SWListInv inv, SchematicNode parent); + + void onFilterRender(SchematicSelector selector, SWInventory inventory); + + void onFilterApply(SchematicSelector selector); + + boolean onFolderCreate(SchematicSelector selector, String name); + + void onNodeFilter(SchematicSelector selector, SchematicNode node); +} diff --git a/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectableAdapter.java b/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectableAdapter.java new file mode 100644 index 0000000..883ac84 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectableAdapter.java @@ -0,0 +1,47 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.util; + +import de.steamwar.inventory.SWInventory; +import de.steamwar.inventory.SWListInv; +import de.steamwar.sql.SchematicNode; +import org.bukkit.entity.Player; + +public abstract class SchematicSelectorInjectableAdapter implements SchematicSelectorInjectable { + @Override + public void onSelectorCreate(SchematicSelector selector) {} + + @Override + public void onListRender(SchematicSelector selector, SWListInv inv, SchematicNode parent) {} + + @Override + public void onFilterRender(SchematicSelector selector, SWInventory inventory) {} + + @Override + public void onFilterApply(SchematicSelector selector) {} + + @Override + public boolean onFolderCreate(SchematicSelector selector, String name) { + return true; + } + + @Override + public void onNodeFilter(SchematicSelector selector, SchematicNode node) {} +} diff --git a/SpigotCore_Main/src/de/steamwar/util/UtilGui.java b/SpigotCore_Main/src/de/steamwar/util/UtilGui.java new file mode 100644 index 0000000..cfb9012 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/util/UtilGui.java @@ -0,0 +1,53 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.util; + +import de.steamwar.inventory.SWItem; +import de.steamwar.inventory.SWListInv; +import lombok.experimental.UtilityClass; +import org.bukkit.Material; +import org.bukkit.entity.Player; + +import java.util.LinkedList; +import java.util.List; +import java.util.function.Consumer; + +@UtilityClass +public class UtilGui { + + public static void openMaterialSelector(Player player, Consumer callback) { + openMaterialSelector(player, "Material auswählen", callback); + } + + public static void openMaterialSelector(Player player, String title, Consumer callback) { + List> materials = new LinkedList<>(); + for(Material material : Material.values()){ + if(material.name().startsWith(Material.LEGACY_PREFIX)) + continue; + SWItem item = new SWItem(material, "§7" + material.name()); + if(item.getItemMeta() != null && material.isItem()) { + materials.add(new SWListInv.SWListEntry<>(item, material)); + } + } + + SWListInv swListInv = new SWListInv<>(player, title, materials, (clickType3, material) -> callback.accept(material)); + swListInv.open(); + } +} From 75c9c6bb1e4632bd8e9b178f978cbf4b45545c97 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 21 Nov 2021 11:23:12 +0100 Subject: [PATCH 56/75] Add Deprecation and NodePathParser Signed-off-by: Chaoscaot --- .../src/de/steamwar/sql/SchematicNode.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 1054b90..56dfcff 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -37,6 +37,7 @@ import java.time.Instant; import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Predicate; +import java.util.stream.Collectors; import java.util.zip.GZIPInputStream; public class SchematicNode { @@ -309,6 +310,45 @@ public class SchematicNode { }, node); } + public static SchematicNode getNodeFromPath(SteamwarUser user, String s) { + if (s.startsWith("/")) { + s = s.substring(1); + } + if (s.isEmpty()) { + return null; + } + if (s.contains("/")) { + String[] layers = s.split("/"); + SchematicNode currentNode = null; + for (int i = 0; i < layers.length; i++) { + int finalI = i; + Optional node; + if (currentNode == null) { + node = SchematicNode.getSchematicsAccessibleByUser(user.getId(), 0).stream().filter(node1 -> node1.getName().equals(layers[finalI])).findAny(); + } else { + node = SchematicNode.getSchematicNodeInNode(currentNode).stream().filter(node1 -> node1.getName().equals(layers[finalI])).findAny(); + } + if (!node.isPresent()) { + return null; + } else { + currentNode = node.get(); + if (!currentNode.isDir() && i != layers.length - 1) { + return null; + } + } + } + return currentNode; + } else { + String finalS = s; + List nodes = SchematicNode.getSchematicsAccessibleByUser(user.getId(), 0).stream().filter(node -> node.getName().equals(finalS)).collect(Collectors.toList()); + if (nodes.isEmpty()) { + return null; + } else { + return nodes.get(0); + } + } + } + private final int id; private final int owner; private String name; @@ -378,12 +418,14 @@ public class SchematicNode { updateDB(); } + @Deprecated public String getType() { if(isDir) throw new SecurityException("Node is Directory"); return type; } + @Deprecated public void setType(String type) { if(isDir) throw new SecurityException("Node is Directory"); @@ -419,6 +461,13 @@ public class SchematicNode { return SchematicType.fromDB(type); } + public void setSchemtype(SchematicType type) { + if(isDir()) + throw new RuntimeException("Is Directory"); + this.type = type.toDB(); + updateDB(); + } + public SchematicNode getParentNode() { if(parent == null) return null; return SchematicNode.getSchematicNode(parent); From 42b4ccad2257f3a080eca82e4968be78b3914760 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 21 Nov 2021 11:23:57 +0100 Subject: [PATCH 57/75] Remove Unused Imports Signed-off-by: Chaoscaot --- SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 56dfcff..1ed2e94 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -21,8 +21,6 @@ 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; import org.bukkit.entity.Player; @@ -35,7 +33,6 @@ import java.sql.SQLException; import java.sql.Timestamp; import java.time.Instant; import java.util.*; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.zip.GZIPInputStream; From 802cd6c862c145e48f8682bcbdf8d94927dd29fa Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 21 Nov 2021 11:25:22 +0100 Subject: [PATCH 58/75] Remove Unused Imports Signed-off-by: Chaoscaot --- SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 1ed2e94..82f1ad1 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -34,7 +34,6 @@ import java.sql.Timestamp; import java.time.Instant; import java.util.*; import java.util.function.Predicate; -import java.util.stream.Collectors; import java.util.zip.GZIPInputStream; public class SchematicNode { @@ -337,12 +336,7 @@ public class SchematicNode { return currentNode; } else { String finalS = s; - List nodes = SchematicNode.getSchematicsAccessibleByUser(user.getId(), 0).stream().filter(node -> node.getName().equals(finalS)).collect(Collectors.toList()); - if (nodes.isEmpty()) { - return null; - } else { - return nodes.get(0); - } + return SchematicNode.getSchematicsAccessibleByUser(user.getId(), 0).stream().filter(node1 -> node1.getName().equals(finalS)).findAny().orElse(null); } } From 83d7a4c9887129f66e5d5950927aaee897aa4333 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Mon, 22 Nov 2021 21:10:59 +0100 Subject: [PATCH 59/75] Fix Import for Lixfel Signed-off-by: Chaoscaot --- SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java index 5cd4ef7..74059bc 100644 --- a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java +++ b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java @@ -19,7 +19,6 @@ package de.steamwar.util; -import com.google.common.collect.Lists; import de.steamwar.inventory.*; import de.steamwar.sql.SchematicNode; import de.steamwar.sql.SchematicType; @@ -30,6 +29,7 @@ import org.bukkit.entity.Player; import java.text.MessageFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.function.Consumer; @@ -146,7 +146,7 @@ public class SchematicSelector { }); if(!node.isDir()) { if(node.getRank() > 0) - item.setLore(Lists.newArrayList("§7" + node.getSchemtype().name(), "§8Rang " + node.getRank())); + item.setLore(Arrays.asList("§7" + node.getSchemtype().name(), "§8Rang " + node.getRank())); } list.add(new SWListInv.SWListEntry<>(item, node)); } From 9fe13610306c5ef0487783b7007ad37feaca0d24 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 23 Nov 2021 14:16:30 +0100 Subject: [PATCH 60/75] Add SWCommand.Register.description to simplify help commands --- SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 10 +++++++--- .../src/de/steamwar/command/SubCommand.java | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 4125bb7..bcf15da 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -250,7 +250,9 @@ public abstract class SWCommand { .anyMatch(s -> s.toLowerCase().startsWith(args[args.length - 1].toLowerCase())); if (hasTabCompletes) { try { - message.sendPrefixless(subCommand.description, p); + for (String s : subCommand.description) { + message.sendPrefixless(s, p); + } } catch (Exception e) { Bukkit.getLogger().log(Level.WARNING, "Failed to send description of registered method '" + subCommand.method + "' with description '" + subCommand.description + "'", e); } @@ -263,7 +265,9 @@ public abstract class SWCommand { commandList.forEach(subCommand -> { if (subCommand.guardChecker == null || subCommand.guardChecker.guard(p, GuardCheckType.TAB_COMPLETE, new String[0], null) == GuardResult.ALLOWED) { try { - message.sendPrefixless(subCommand.description, p); + for (String s : subCommand.description) { + message.sendPrefixless(s, p); + } } catch (Exception e) { Bukkit.getLogger().log(Level.WARNING, "Failed to send description of registered method '" + subCommand.method + "' with description '" + subCommand.description + "'", e); } @@ -280,7 +284,7 @@ public abstract class SWCommand { boolean help() default false; - String description() default ""; + String[] description() default {}; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index 4581c8d..05cba2d 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -36,7 +36,7 @@ class SubCommand { private SWCommand swCommand; Method method; - String description; + String[] description; String[] subCommand; TypeMapper[] arguments; GuardChecker[] guards; @@ -46,7 +46,7 @@ class SubCommand { Class varArgType = null; private boolean help; - SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map> localTypeMapper, Map localGuardChecker, boolean help, String description) { + SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map> localTypeMapper, Map localGuardChecker, boolean help, String[] description) { this.swCommand = swCommand; this.method = method; this.help = help; From f69f3b757213550de9d12c479a7a0542ec8c75eb Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 23 Nov 2021 17:35:05 +0100 Subject: [PATCH 61/75] Hotfix: Fix SchematicNode Order by Name Signed-off-by: Chaoscaot --- .../src/de/steamwar/sql/SchematicNode.java | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 82f1ad1..009920c 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -41,24 +41,24 @@ public class SchematicNode { private static final SQL.Statement createNode = new SQL.Statement("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeType, NodeItem) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUES(NodeName), ParentNode = VALUES(ParentNode), NodeItem = VALUES(NodeItem), NodeType = VALUES(NodeType), NodeItem = VALUES(NodeItem)"); private static final SQL.Statement getSchematicNode_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode is NULL"); private static final SQL.Statement getSchematicNode = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?"); - private static final SQL.Statement getSchematicsInNode_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode is NULL"); - private static final SQL.Statement getSchematicsInNode = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ?"); - private static final SQL.Statement getDirsInNode_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode is NULL AND NodeType is NULL"); - private static final SQL.Statement getDirsInNode = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ? AND NodeType is NULL"); - private static final SQL.Statement getSchematicDirectory_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL"); - private static final SQL.Statement getSchematicDirectory = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?"); - private static final SQL.Statement getSchematicNodeO_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL"); - private static final SQL.Statement getSchematicNodeO = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?"); + private static final SQL.Statement getSchematicsInNode_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode is NULL ORDER BY NodeName"); + private static final SQL.Statement getSchematicsInNode = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ? ORDER BY NodeName"); + private static final SQL.Statement getDirsInNode_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode is NULL AND NodeType is NULL ORDER BY NodeName"); + private static final SQL.Statement getDirsInNode = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ? AND NodeType is NULL ORDER BY NodeName"); + private static final SQL.Statement getSchematicDirectory_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL ORDER BY NodeName"); + private static final SQL.Statement getSchematicDirectory = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ? ORDER BY NodeName"); + private static final SQL.Statement getSchematicNodeO_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL "); + private static final SQL.Statement getSchematicNodeO = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ? ORDER BY NodeName"); private static final SQL.Statement getSchematicNodeId = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ?"); - private static final SQL.Statement getAllSchemsOfTypeOwner = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?"); - private static final SQL.Statement getAllSchemsOfType = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeType = ?"); + private static final SQL.Statement getAllSchemsOfTypeOwner = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ? ORDER BY NodeName"); + private static final SQL.Statement getAllSchemsOfType = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeType = ? ORDER BY NodeName"); private static final SQL.Statement getAccessibleByUser = new SQL.Statement("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND ((s.NodeOwner = ? AND s.ParentNode IS NULL) OR NOT s.NodeOwner = ?) GROUP BY s.NodeId ORDER BY s.NodeName"); - private static final SQL.Statement getAccessibleByUserByTypeInNode = new SQL.Statement("WITH RECURSIVE RSNB AS (WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSNB WHERE SN.NodeId = RSNB.ParentNode)SELECT * FROM RSNB WHERE ParentNode = ?"); - private static final SQL.Statement getAccessibleByUserByTypeInNode_Null = new SQL.Statement("WITH RECURSIVE RSNB AS (WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSNB WHERE SN.NodeId = RSNB.ParentNode)SELECT * FROM RSNB WHERE ParentNode is null"); - private static final SQL.Statement getAccessibleByUserByType = new SQL.Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ?"); - private static final SQL.Statement getAllSchematicsAccessibleByUser = new SQL.Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN"); - private static final SQL.Statement isSchematicAccessibleForUser = new SQL.Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT COUNT(RSN.NodeId) AS `Accessible` FROM RSN LEFT Join NodeMember NM On NM.NodeId = RSN.NodeId WHERE NodeOwner = ? OR UserId = ? LIMIT 1"); - private static final SQL.Statement getAllParentsOfNode = new SQL.Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? UNION SELECT SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT * FROM RSN"); + private static final SQL.Statement getAccessibleByUserByTypeInNode = new SQL.Statement("WITH RECURSIVE RSNB AS (WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSNB WHERE SN.NodeId = RSNB.ParentNode)SELECT * FROM RSNB WHERE ParentNode = ? ORDER BY NodeName"); + private static final SQL.Statement getAccessibleByUserByTypeInNode_Null = new SQL.Statement("WITH RECURSIVE RSNB AS (WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSNB WHERE SN.NodeId = RSNB.ParentNode)SELECT * FROM RSNB WHERE ParentNode is null ORDER BY NodeName"); + private static final SQL.Statement getAccessibleByUserByType = new SQL.Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? ORDER BY NodeName"); + private static final SQL.Statement getAllSchematicsAccessibleByUser = new SQL.Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN ORDER BY NodeName"); + private static final SQL.Statement isSchematicAccessibleForUser = new SQL.Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT COUNT(RSN.NodeId) AS `Accessible` FROM RSN LEFT Join NodeMember NM On NM.NodeId = RSN.NodeId WHERE NodeOwner = ? OR UserId = ? LIMIT 1 ORDER BY NodeName"); + private static final SQL.Statement getAllParentsOfNode = new SQL.Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? UNION SELECT SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT * FROM RSN ORDER BY NodeName"); private static final SQL.Statement countNodes = new SQL.Statement("SELECT COUNT(NodeId) AS 'count' FROM SchematicNode"); private static final SQL.Statement updateDB = new SQL.Statement("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?"); private static final SQL.Statement updateDatabase = new SQL.Statement("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?"); From bf796ebce3c1ddf759a52c8a4d6550637f8dc90f Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 23 Nov 2021 19:01:28 +0100 Subject: [PATCH 62/75] Doing Lixfel Stuff Signed-off-by: Chaoscaot --- SpigotCore_Main/src/SpigotCore.properties | 51 ++++ SpigotCore_Main/src/SpigotCore_en.properties | 0 .../src/de/steamwar/core/Core.java | 4 + .../de/steamwar/util/SchematicSelector.java | 237 ++++++++++-------- .../util/SchematicSelectorInjectable.java | 12 +- .../SchematicSelectorInjectableAdapter.java | 47 ---- .../src/de/steamwar/util/UtilGui.java | 3 +- 7 files changed, 194 insertions(+), 160 deletions(-) create mode 100644 SpigotCore_Main/src/SpigotCore.properties create mode 100644 SpigotCore_Main/src/SpigotCore_en.properties delete mode 100644 SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectableAdapter.java diff --git a/SpigotCore_Main/src/SpigotCore.properties b/SpigotCore_Main/src/SpigotCore.properties new file mode 100644 index 0000000..53227e0 --- /dev/null +++ b/SpigotCore_Main/src/SpigotCore.properties @@ -0,0 +1,51 @@ +# +# This file is a part of the SteamWar software. +# +# Copyright (C) 2021 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 . +# + +SCHEM_SELECTOR_TITLE={0} auswählen: {1} +SCHEM_SELECTOR_BACK=§eZurück +SCHEM_SELECTOR_DIR=Ordner +SCHEM_SELECTOR_RANK=§8Rang {0} +SCHEM_SELECTOR_OWN=§7Eigene Schematics +SCHEM_SELECTOR_PUB=§7Public Schematics +SCHEM_SELECTOR_SEL_DIR=§7Ordner auswählen +SCHEM_SELECTOR_NEW_DIR=§7Neuer Ordner +SCHEM_SELECTOR_FILTER=§7Filter + +SCHEM_SELECTOR_CREATE_DIR_TITLE=Ordner Erstellen + +SCHEM_SELECTOR_FILTER_TITLE=Filter +SCHEM_SELECTOR_FILTER_ENTER_NAME=Name eingeben +SCHEM_SELECTOR_FILTER_NAME=§7Nach Namen suchen... +SCHEM_SELECTOR_FILTER_NAME_SEARCH=§7Suchwort: §e{0} +SCHEM_SELECTOR_FILTER_ENTER_OWNER=Besitzer eingeben +SCHEM_SELECTOR_FILTER_OWNER=§7Nach Besitzer suchen... +SCHEM_SELECTOR_FILTER_OWNER_SEARCH=§7Besitzer: §e{0} +SCHEM_SELECTOR_FILTER_SEL_TYPE=Typ wählen... +SCHEM_SELECTOR_FILTER_TYPE=§7Nach Typ Filtern... +SCHEM_SELECTOR_FILTER_TYPE_SEARCH=§7Typ: §e{0} +SCHEM_SELECTOR_FILTER_MAT=§7Nach Item Filtern... +SCHEM_SELECTOR_FILTER_MAT_SEARCH=§7Item: §e{0} +SCHEM_SELECTOR_CANCEL=§eAbbrechen +SCHEM_SELECTOR_GO=§eSuchen... + +SCHEM_SELECTOR_SCHEMATIC=Schematic +SCHEM_SELECTOR_DIRECTORY=Ordner +SCHEM_SELECTOR_SCHEMATIC_NODE=Schematic/Ordner + +MATERIAL_SELECTOR_TITLE=Material auswählen \ No newline at end of file diff --git a/SpigotCore_Main/src/SpigotCore_en.properties b/SpigotCore_Main/src/SpigotCore_en.properties new file mode 100644 index 0000000..e69de29 diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index 4c28502..b0fc18f 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -25,6 +25,7 @@ import de.steamwar.core.events.ChattingEvent; import de.steamwar.core.events.ChunkListener; import de.steamwar.core.events.PlayerJoinedEvent; import de.steamwar.core.events.WorldLoadEvent; +import de.steamwar.message.Message; import de.steamwar.sql.SQL; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; @@ -39,6 +40,7 @@ public class Core extends JavaPlugin{ private static Core instance; private static final int version; + public static Message MESSAGE; static{ String packageName = Bukkit.getServer().getClass().getPackage().getName(); @@ -79,6 +81,8 @@ public class Core extends JavaPlugin{ } catch (IOException e) { throw new SecurityException("Could not load Hostname", e); } + + MESSAGE = new Message("SpigotCore", getClassLoader()); } @Override diff --git a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java index 74059bc..a8c417d 100644 --- a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java +++ b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java @@ -19,10 +19,12 @@ package de.steamwar.util; +import de.steamwar.core.Core; import de.steamwar.inventory.*; import de.steamwar.sql.SchematicNode; import de.steamwar.sql.SchematicType; import de.steamwar.sql.SteamwarUser; +import jdk.nashorn.internal.runtime.doubleconv.CachedPowers; import lombok.*; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -33,9 +35,12 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.function.Consumer; +import java.util.function.Function; public class SchematicSelector { + public static final String DEFAULT_TITLE = "SCHEM_SELECTOR_TITLE"; + @Getter private final Player player; @Getter @@ -53,10 +58,11 @@ public class SchematicSelector { private PublicMode publicMode = PublicMode.ALL; @Setter @Getter - private String title = "{0} auswahl: {1}"; + private boolean singleDirOpen; @Setter @Getter - private boolean singleDirOpen; + private Function title; + private boolean sdoTrigger = false; public SchematicSelector(Player player, SelectorTarget target, Consumer callback) { this.player = player; @@ -80,112 +86,42 @@ public class SchematicSelector { } private void openList(SchematicNode parent, boolean publics) { - List nodes = new ArrayList<>(); + List nodes = filter.isFilter()?getFilteredSchematics(publics):getSchematicList(publics, parent); - if(filter.isFilter()) { - nodes.addAll(SchematicNode.getAllSchematicsAccessibleByUser(publics?0:user.getId())); - nodes.removeIf(node -> { - if(useHooks) { - injectable.onNodeFilter(this, node); - } - return !filter.matches(node); - }); - if(target.target == SelectorTarget.Target.DIRECTORY) { - nodes.removeIf(node -> !node.isDir()); - } - if(target.target == SelectorTarget.Target.SCHEMATIC_TYPE) { - nodes.removeIf(node -> node.isDir() || !node.getType().equals(target.type.toDB())); - } - } else { - switch (target.target) { - case DIRECTORY: - if(parent == null) { - nodes.addAll(SchematicNode.getSchematicsAccessibleByUser(publics?0:user.getId(), null)); - nodes.removeIf(node -> !node.isDir()); - } else { - nodes.addAll(SchematicNode.getSchematicDirectoryInNode(parent.getId())); - } - break; - case SCHEMATIC_TYPE: - nodes.addAll(SchematicNode.getAccessibleSchematicsOfTypeInParent(publics?0:user.getId(), target.type.toDB(), parent==null?0:parent.getId())); - if(target.rank >= 0) { - nodes.removeIf(node -> node.getRank() > target.rank); - } - break; - default: - nodes.addAll(SchematicNode.getSchematicsAccessibleByUser(publics?0:user.getId(), parent == null?0:parent.getId())); - } - - if(singleDirOpen && nodes.size() == 1 && nodes.get(0).isDir()) { - openList(nodes.get(0), publics); - return; - } + if(sdoTrigger) { + sdoTrigger = false; + return; } List> list = new ArrayList<>(); if(parent != null) { - list.add(new SWListInv.SWListEntry<>(new SWItem(Material.ARROW, "§eZurück", clickType -> {}), null)); + list.add(new SWListInv.SWListEntry<>(new SWItem(Material.ARROW, Core.MESSAGE.parse("SCHEM_SELECTOR_BACK", player), clickType -> {}), null)); } for (SchematicNode node : nodes) { if(node.getName().equals("//copy")) continue; - Material m; - if (node.getItem().isEmpty()) - m = node.isDir()?SWItem.getMaterial("CHEST"):SWItem.getMaterial("CAULDRON_ITEM"); - else - m = SWItem.getMaterial(node.getItem()); - - String name = "§" + (filter.getName().isEmpty()?"e":"7") + node.getName(); - - if(!filter.getName().isEmpty()) { - name = name.replace(filter.getName(), "§e" + filter.getName() + "§7"); - } - - SWItem item = new SWItem(m, name, Collections.singletonList(node.isDir() ? "§9Ordner" : "§7" + node.getSchemtype().name()), !node.isDir() && !node.getSchemtype().writeable(), click -> { - }); - if(!node.isDir()) { - if(node.getRank() > 0) - item.setLore(Arrays.asList("§7" + node.getSchemtype().name(), "§8Rang " + node.getRank())); - } - list.add(new SWListInv.SWListEntry<>(item, node)); + list.add(renderItem(node)); } - SWListInv inv = new SWListInv<>(player, MessageFormat.format(title, target.target.getName(), filter.getName().isEmpty()?(parent == null?"/":parent.generateBreadcrumbs(user)):filter.getName()), false, list, (clickType, node) -> { - if(node == null) { - openList(getParent(parent), publics); - return; - } - if(node.isDir()) { - if(filter.isFilter() && target.target.isDirs()) { - player.closeInventory(); - callback.accept(node); - return; - } - filter.reset(); - openList(node, publics); - return; - } - player.closeInventory(); - callback.accept(node); - }); + SWListInv inv = new SWListInv<>(player, MessageFormat.format(title==null?DEFAULT_TITLE:title.apply(player), target.target.getName(player), filter.getName().isEmpty()?(parent == null?"/":parent.generateBreadcrumbs(user)):filter.getName()), false, list, (clickType, node) -> handleClick(publics, node, parent)); if(publicMode == PublicMode.ALL) { if(publics) { - inv.setItem(48, Material.BUCKET, "§7Eigene Schematics", clickType -> openList(null, false)); + inv.setItem(48, Material.BUCKET, Core.MESSAGE.parse("SCHEM_SELECTOR_OWN", player), clickType -> openList(null, false)); } else { - inv.setItem(48, Material.GLASS, "§7Public Schematics", clickType -> openList(null, true)); + inv.setItem(48, Material.GLASS, Core.MESSAGE.parse("SCHEM_SELECTOR_PUB", player), clickType -> openList(null, true)); } } if(target.target.isDirs()) { - inv.setItem(49, SWItem.getDye(10), "§7Ordner auswählen", clickType -> { + inv.setItem(49, SWItem.getDye(10), Core.MESSAGE.parse("SCHEM_SELECTOR_SEL_DIR", player), clickType -> { player.closeInventory(); callback.accept(parent); }); } if(!publics) { - inv.setItem(50, Material.CHEST, "§7Neuer Ordner", clickType -> createFolderIn(parent)); + inv.setItem(50, Material.CHEST, Core.MESSAGE.parse("SCHEM_SELECTOR_NEW_DIR", player), clickType -> createFolderIn(parent)); } - inv.setItem(51, Material.NAME_TAG, "§7Filter", clickType -> openFilter(publics)); + inv.setItem(51, Material.NAME_TAG, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER", player), clickType -> openFilter(publics)); if(useHooks) { injectable.onListRender(this, inv, parent); @@ -193,8 +129,48 @@ public class SchematicSelector { inv.open(); } + private void handleClick(boolean publics, SchematicNode node, SchematicNode parent) { + if(node == null) { + openList(getParent(parent), publics); + return; + } + if(node.isDir()) { + if(filter.isFilter() && target.target.isDirs()) { + player.closeInventory(); + callback.accept(node); + return; + } + filter.reset(); + openList(node, publics); + return; + } + player.closeInventory(); + callback.accept(node); + } + + private SWListInv.SWListEntry renderItem(SchematicNode node) { + Material m; + if (node.getItem().isEmpty()) + m = node.isDir()?SWItem.getMaterial("CHEST"):SWItem.getMaterial("CAULDRON_ITEM"); + else + m = SWItem.getMaterial(node.getItem()); + + String name = "§" + (filter.getName().isEmpty()?"e":"7") + node.getName(); + + if(!filter.getName().isEmpty()) { + name = name.replace(filter.getName(), "§e" + filter.getName() + "§7"); + } + + SWItem item = new SWItem(m, name, Collections.singletonList(node.isDir() ? ("§9" + Core.MESSAGE.parse("SCHEM_SELECTOR_DIR", player)) : "§7" + node.getSchemtype().name()), !node.isDir() && !node.getSchemtype().writeable(), click -> { + }); + if(!node.isDir() && node.getRank() > 0) { + item.setLore(Arrays.asList("§7" + node.getSchemtype().name(), Core.MESSAGE.parse("SCHEM_SELECTOR_RANK", player))); + } + return new SWListInv.SWListEntry<>(item, node); + } + private void createFolderIn(SchematicNode parent) { - SWAnvilInv inv = new SWAnvilInv(player, "Ordner Erstellen"); + SWAnvilInv inv = new SWAnvilInv(player, Core.MESSAGE.parse("SCHEM_SELECTOR_CREATE_DIR_TITLE", player)); inv.setItem(Material.CHEST); inv.setCallback(s -> { if(useHooks) { @@ -211,13 +187,13 @@ public class SchematicSelector { } private void openFilter(boolean publics) { - SWInventory inv = new SWInventory(player, 9, "Filter"); + SWInventory inv = new SWInventory(player, 9, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_TITLE", player)); InvCallback nameCallback = clickType -> { if(clickType.isRightClick()) { filter.setName(""); openFilter(publics); } else { - SWAnvilInv swAnvilInv = new SWAnvilInv(player, "Name eingeben"); + SWAnvilInv swAnvilInv = new SWAnvilInv(player, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_ENTER_NAME", player)); swAnvilInv.setItem(Material.NAME_TAG); swAnvilInv.setCallback(s -> { filter.setName(s); @@ -227,9 +203,9 @@ public class SchematicSelector { } }; if(filter.getName().isEmpty()) { - inv.setItem(0, Material.NAME_TAG, "§7Nach namen suchen...", nameCallback); + inv.setItem(0, Material.NAME_TAG, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_NAME", player), nameCallback); } else { - inv.setItem(0, Material.NAME_TAG, "§7Nach namen suchen...", Collections.singletonList("§7Suchwort: §e" + filter.getName()), true, nameCallback); + inv.setItem(0, Material.NAME_TAG, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_NAME", player), Collections.singletonList(Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_NAME_SEARCH", player, filter.getName())), true, nameCallback); } InvCallback ownerCallback = clickType -> { @@ -237,7 +213,7 @@ public class SchematicSelector { filter.setOwner(null); openFilter(publics); } else { - SWAnvilInv swAnvilInv = new SWAnvilInv(player, "Besitzer eingeben"); + SWAnvilInv swAnvilInv = new SWAnvilInv(player, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_ENTER_OWNER", player)); swAnvilInv.setItem(Material.PLAYER_HEAD); swAnvilInv.setCallback(s -> { filter.setOwner(SteamwarUser.get(s).getId()); @@ -247,13 +223,13 @@ public class SchematicSelector { } }; if(filter.getOwner() == null) { - inv.setItem(1, Material.PLAYER_HEAD, "§7Nach Besitzer suchen...", ownerCallback); + inv.setItem(1, Material.PLAYER_HEAD, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_OWNER", player), ownerCallback); } else { - SteamwarUser user = SteamwarUser.get(filter.getOwner()); + SteamwarUser tUser = SteamwarUser.get(filter.getOwner()); SWItem item = SWItem.getPlayerSkull(user.getUserName()); - item.setName("§7Nach Besitzer suchen..."); + item.setName(Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_OWNER", player)); item.setEnchanted(true); - item.setLore(Collections.singletonList("§7Besitzer: §e" + user.getUserName())); + item.setLore(Collections.singletonList(Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_OWNER_SEARCH", player, tUser.getUserName()))); item.setCallback(ownerCallback); inv.setItem(1, item); } @@ -268,7 +244,7 @@ public class SchematicSelector { SchematicType.values().forEach(schematicType -> { types.add(new SWListInv.SWListEntry<>(new SWItem(SWItem.getMaterial("STONE_BUTTON"), "§e" + schematicType.name(), Collections.emptyList(), schematicType.fightType(), n -> {}), schematicType)); }); - SWListInv listInv = new SWListInv<>(player, "Typ wählen...", types, (clickType1, schematicType) -> { + SWListInv listInv = new SWListInv<>(player, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_SEL_TYPE", player), types, (clickType1, schematicType) -> { filter.setType(schematicType); openFilter(publics); }); @@ -277,9 +253,9 @@ public class SchematicSelector { }; if(filter.getType() == null) { - inv.setItem(2, SWItem.getMaterial("STONE_BUTTON"), "§7Nach Typ Filtern...", schemTypeCallback); + inv.setItem(2, SWItem.getMaterial("STONE_BUTTON"), Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_TYPE", player), schemTypeCallback); } else { - inv.setItem(2, SWItem.getMaterial("STONE_BUTTON"), "§7Nach Typ Filtern...", Collections.singletonList("§7Typ: §e" + filter.getType().name()), true, schemTypeCallback); + inv.setItem(2, SWItem.getMaterial("STONE_BUTTON"), Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_TYPE", player), Collections.singletonList(Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_TYPE_SEARCH", player, filter.getType().name())), true, schemTypeCallback); } } @@ -298,16 +274,16 @@ public class SchematicSelector { final int iSlot = target.target== SelectorTarget.Target.SCHEMATIC_TYPE?2:3; if(filter.getItem() == null) { - inv.setItem(iSlot, Material.STONE, "§7Nach Item Filtern...", materialCallback); + inv.setItem(iSlot, Material.STONE, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_MAT", player), materialCallback); } else { - inv.setItem(iSlot, filter.getItem(), "§7Nach Item Filtern...", Collections.singletonList("§7Item: §e" + filter.getItem().name()), true, materialCallback); + inv.setItem(iSlot, filter.getItem(), Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_MAT", player), Collections.singletonList(Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_MAT_SEARCH", player, filter.getItem().name())), true, materialCallback); } - inv.setItem(7, SWItem.getDye(1), "§eAbbrechen", clickType -> { + inv.setItem(7, SWItem.getDye(1), Core.MESSAGE.parse("SCHEM_SELECTOR_CANCEL", player), clickType -> { filter.reset(); openList(null, publics); }); - inv.setItem(8, SWItem.getDye(10), "§eSuchen...", clickType -> { + inv.setItem(8, SWItem.getDye(10), Core.MESSAGE.parse("SCHEM_SELECTOR_GO", player), clickType -> { filter.setFilter(true); if(useHooks) { injectable.onFilterApply(this); @@ -322,6 +298,51 @@ public class SchematicSelector { inv.open(); } + private List getFilteredSchematics(boolean publics) { + List nodes = new ArrayList<>(SchematicNode.getAllSchematicsAccessibleByUser(publics ? 0 : user.getId())); + nodes.removeIf(node -> { + if(useHooks) { + injectable.onNodeFilter(this, node); + } + return !filter.matches(node); + }); + if(target.target == SelectorTarget.Target.DIRECTORY) { + nodes.removeIf(node -> !node.isDir()); + } + if(target.target == SelectorTarget.Target.SCHEMATIC_TYPE) { + nodes.removeIf(node -> node.isDir() || !node.getType().equals(target.type.toDB())); + } + return nodes; + } + + private List getSchematicList(boolean publics, SchematicNode parent) { + List nodes = new ArrayList<>(); + switch (target.target) { + case DIRECTORY: + if(parent == null) { + nodes.addAll(SchematicNode.getSchematicsAccessibleByUser(publics?0:user.getId(), null)); + nodes.removeIf(node -> !node.isDir()); + } else { + nodes.addAll(SchematicNode.getSchematicDirectoryInNode(parent.getId())); + } + break; + case SCHEMATIC_TYPE: + nodes.addAll(SchematicNode.getAccessibleSchematicsOfTypeInParent(publics?0:user.getId(), target.type.toDB(), parent==null?0:parent.getId())); + if(target.rank >= 0) { + nodes.removeIf(node -> node.getRank() > target.rank); + } + break; + default: + nodes.addAll(SchematicNode.getSchematicsAccessibleByUser(publics?0:user.getId(), parent == null?0:parent.getId())); + } + + if(singleDirOpen && nodes.size() == 1 && nodes.get(0).isDir()) { + openList(nodes.get(0), publics); + sdoTrigger = true; + } + return nodes; + } + private static SchematicNode getParent(SchematicNode node) { if(node.getParent() == null) { return null; @@ -363,15 +384,19 @@ public class SchematicSelector { @AllArgsConstructor private enum Target { - SCHEMATIC("Schematic", false), - DIRECTORY("Ordner", true), - SCHEMATIC_NODE("Schematic/Ordner", true), - SCHEMATIC_TYPE("Schematic", false); + SCHEMATIC("SCHEM_SELECTOR_SCHEMATIC", false), + DIRECTORY("SCHEM_SELECTOR_DIRECTORY", true), + SCHEMATIC_NODE("SCHEM_SELECTOR_SCHEMATIC_NODE", true), + SCHEMATIC_TYPE("SCHEM_SELECTOR_SCHEMATIC", false); @Getter - private String name; + private String rawName; @Getter private boolean dirs; + + public String getName(Player player) { + return Core.MESSAGE.parse(rawName, player); + } } } diff --git a/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java b/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java index 35629d0..fabc33f 100644 --- a/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java +++ b/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java @@ -25,15 +25,15 @@ import de.steamwar.sql.SchematicNode; public interface SchematicSelectorInjectable { - void onSelectorCreate(SchematicSelector selector); + default void onSelectorCreate(SchematicSelector selector) {} - void onListRender(SchematicSelector selector, SWListInv inv, SchematicNode parent); + default void onListRender(SchematicSelector selector, SWListInv inv, SchematicNode parent) {} - void onFilterRender(SchematicSelector selector, SWInventory inventory); + default void onFilterRender(SchematicSelector selector, SWInventory inventory) {} - void onFilterApply(SchematicSelector selector); + default void onFilterApply(SchematicSelector selector) {} - boolean onFolderCreate(SchematicSelector selector, String name); + default boolean onFolderCreate(SchematicSelector selector, String name) {return true;} - void onNodeFilter(SchematicSelector selector, SchematicNode node); + default void onNodeFilter(SchematicSelector selector, SchematicNode node) {} } diff --git a/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectableAdapter.java b/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectableAdapter.java deleted file mode 100644 index 883ac84..0000000 --- a/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectableAdapter.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2021 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.util; - -import de.steamwar.inventory.SWInventory; -import de.steamwar.inventory.SWListInv; -import de.steamwar.sql.SchematicNode; -import org.bukkit.entity.Player; - -public abstract class SchematicSelectorInjectableAdapter implements SchematicSelectorInjectable { - @Override - public void onSelectorCreate(SchematicSelector selector) {} - - @Override - public void onListRender(SchematicSelector selector, SWListInv inv, SchematicNode parent) {} - - @Override - public void onFilterRender(SchematicSelector selector, SWInventory inventory) {} - - @Override - public void onFilterApply(SchematicSelector selector) {} - - @Override - public boolean onFolderCreate(SchematicSelector selector, String name) { - return true; - } - - @Override - public void onNodeFilter(SchematicSelector selector, SchematicNode node) {} -} diff --git a/SpigotCore_Main/src/de/steamwar/util/UtilGui.java b/SpigotCore_Main/src/de/steamwar/util/UtilGui.java index cfb9012..c653610 100644 --- a/SpigotCore_Main/src/de/steamwar/util/UtilGui.java +++ b/SpigotCore_Main/src/de/steamwar/util/UtilGui.java @@ -19,6 +19,7 @@ package de.steamwar.util; +import de.steamwar.core.Core; import de.steamwar.inventory.SWItem; import de.steamwar.inventory.SWListInv; import lombok.experimental.UtilityClass; @@ -33,7 +34,7 @@ import java.util.function.Consumer; public class UtilGui { public static void openMaterialSelector(Player player, Consumer callback) { - openMaterialSelector(player, "Material auswählen", callback); + openMaterialSelector(player, Core.MESSAGE.parse("MATERIAL_SELECTOR_TITLE", player), callback); } public static void openMaterialSelector(Player player, String title, Consumer callback) { From 74a4dba9f3b21b7bf66e99a6d7d2367db110b4cd Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 23 Nov 2021 19:02:23 +0100 Subject: [PATCH 63/75] Public Signed-off-by: Chaoscaot --- SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java index a8c417d..0a4840c 100644 --- a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java +++ b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java @@ -370,7 +370,7 @@ public class SchematicSelector { return new SelectorTarget(SelectorTarget.Target.SCHEMATIC_TYPE, type, rank); } - private static class SelectorTarget { + public static class SelectorTarget { private final Target target; private final SchematicType type; From 256e76f9bb7db8c70b79aa46a2e8702a364df28d Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 23 Nov 2021 19:04:15 +0100 Subject: [PATCH 64/75] Enum in a class in a class Signed-off-by: Chaoscaot --- .../de/steamwar/util/SchematicSelector.java | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java index 0a4840c..dbde6ed 100644 --- a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java +++ b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java @@ -234,7 +234,7 @@ public class SchematicSelector { inv.setItem(1, item); } - if(target.target != SelectorTarget.Target.SCHEMATIC_TYPE) { + if(target.target != Target.SCHEMATIC_TYPE) { InvCallback schemTypeCallback = clickType -> { if(clickType.isRightClick()) { filter.setType(null); @@ -271,7 +271,7 @@ public class SchematicSelector { } }; - final int iSlot = target.target== SelectorTarget.Target.SCHEMATIC_TYPE?2:3; + final int iSlot = target.target == Target.SCHEMATIC_TYPE?2:3; if(filter.getItem() == null) { inv.setItem(iSlot, Material.STONE, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_MAT", player), materialCallback); @@ -306,10 +306,10 @@ public class SchematicSelector { } return !filter.matches(node); }); - if(target.target == SelectorTarget.Target.DIRECTORY) { + if(target.target == Target.DIRECTORY) { nodes.removeIf(node -> !node.isDir()); } - if(target.target == SelectorTarget.Target.SCHEMATIC_TYPE) { + if(target.target == Target.SCHEMATIC_TYPE) { nodes.removeIf(node -> node.isDir() || !node.getType().equals(target.type.toDB())); } return nodes; @@ -351,23 +351,23 @@ public class SchematicSelector { } public static SelectorTarget selectSchematic() { - return new SelectorTarget(SelectorTarget.Target.SCHEMATIC, null, -1); + return new SelectorTarget(Target.SCHEMATIC, null, -1); } public static SelectorTarget selectDirectory() { - return new SelectorTarget(SelectorTarget.Target.DIRECTORY, null, -1); + return new SelectorTarget(Target.DIRECTORY, null, -1); } public static SelectorTarget selectSchematicNode() { - return new SelectorTarget(SelectorTarget.Target.SCHEMATIC_NODE, null, -1); + return new SelectorTarget(Target.SCHEMATIC_NODE, null, -1); } public static SelectorTarget selectSchematicType(SchematicType type) { - return new SelectorTarget(SelectorTarget.Target.SCHEMATIC_TYPE, type, -1); + return new SelectorTarget(Target.SCHEMATIC_TYPE, type, -1); } public static SelectorTarget selectSchematicTypeWithRank(SchematicType type, int rank) { - return new SelectorTarget(SelectorTarget.Target.SCHEMATIC_TYPE, type, rank); + return new SelectorTarget(Target.SCHEMATIC_TYPE, type, rank); } public static class SelectorTarget { @@ -381,22 +381,22 @@ public class SchematicSelector { this.type = type; this.rank = rank; } + } - @AllArgsConstructor - private enum Target { - SCHEMATIC("SCHEM_SELECTOR_SCHEMATIC", false), - DIRECTORY("SCHEM_SELECTOR_DIRECTORY", true), - SCHEMATIC_NODE("SCHEM_SELECTOR_SCHEMATIC_NODE", true), - SCHEMATIC_TYPE("SCHEM_SELECTOR_SCHEMATIC", false); + @AllArgsConstructor + private enum Target { + SCHEMATIC("SCHEM_SELECTOR_SCHEMATIC", false), + DIRECTORY("SCHEM_SELECTOR_DIRECTORY", true), + SCHEMATIC_NODE("SCHEM_SELECTOR_SCHEMATIC_NODE", true), + SCHEMATIC_TYPE("SCHEM_SELECTOR_SCHEMATIC", false); - @Getter - private String rawName; - @Getter - private boolean dirs; + @Getter + private String rawName; + @Getter + private boolean dirs; - public String getName(Player player) { - return Core.MESSAGE.parse(rawName, player); - } + public String getName(Player player) { + return Core.MESSAGE.parse(rawName, player); } } From fd420778c12d8c2ebb93df4a5cc0e9f7b8d2389d Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 23 Nov 2021 19:05:14 +0100 Subject: [PATCH 65/75] Fix Build Signed-off-by: Chaoscaot --- SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java | 1 - 1 file changed, 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java index dbde6ed..f8a34db 100644 --- a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java +++ b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java @@ -24,7 +24,6 @@ import de.steamwar.inventory.*; import de.steamwar.sql.SchematicNode; import de.steamwar.sql.SchematicType; import de.steamwar.sql.SteamwarUser; -import jdk.nashorn.internal.runtime.doubleconv.CachedPowers; import lombok.*; import org.bukkit.Material; import org.bukkit.entity.Player; From edee75a522ddf22693d0300e8b556d124c24950e Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 24 Nov 2021 17:32:09 +0100 Subject: [PATCH 66/75] Add SWCommand description hover and suggest part command on click --- .../src/de/steamwar/command/SWCommand.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index bcf15da..258ab5c 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -21,6 +21,7 @@ package de.steamwar.command; import de.steamwar.message.Message; import lombok.Setter; +import net.md_5.bungee.api.chat.ClickEvent; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -249,13 +250,7 @@ public abstract class SWCommand { boolean hasTabCompletes = tabCompletes.stream() .anyMatch(s -> s.toLowerCase().startsWith(args[args.length - 1].toLowerCase())); if (hasTabCompletes) { - try { - for (String s : subCommand.description) { - message.sendPrefixless(s, p); - } - } catch (Exception e) { - Bukkit.getLogger().log(Level.WARNING, "Failed to send description of registered method '" + subCommand.method + "' with description '" + subCommand.description + "'", e); - } + send(p, subCommand); } else { atomicInteger.incrementAndGet(); } @@ -264,18 +259,24 @@ public abstract class SWCommand { if (args.length == 0 || atomicInteger.get() == commandList.size()) { commandList.forEach(subCommand -> { if (subCommand.guardChecker == null || subCommand.guardChecker.guard(p, GuardCheckType.TAB_COMPLETE, new String[0], null) == GuardResult.ALLOWED) { - try { - for (String s : subCommand.description) { - message.sendPrefixless(s, p); - } - } catch (Exception e) { - Bukkit.getLogger().log(Level.WARNING, "Failed to send description of registered method '" + subCommand.method + "' with description '" + subCommand.description + "'", e); - } + send(p, subCommand); } }); } } + private void send(Player p, SubCommand subCommand) { + try { + for (String s : subCommand.description) { + String hover = "§8/§e" + command.getName() + " " + String.join(" ", subCommand.subCommand); + String suggest = "/" + command.getName() + " " + String.join(" ", subCommand.subCommand); + message.sendPrefixless(s, p, hover, new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, suggest)); + } + } catch (Exception e) { + Bukkit.getLogger().log(Level.WARNING, "Failed to send description of registered method '" + subCommand.method + "' with description '" + subCommand.description + "'", e); + } + } + @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) @Repeatable(Register.Registeres.class) From 200a1a4718c5d02d04876298dcbffada6d43072f Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 24 Nov 2021 17:40:49 +0100 Subject: [PATCH 67/75] Fix SubCommand.getGuardChecker --- SpigotCore_Main/src/de/steamwar/command/SubCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index 05cba2d..e18b5f8 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -101,7 +101,7 @@ class SubCommand { return localGuardChecker.getOrDefault(s, GUARD_FUNCTIONS.getOrDefault(s, null)); } GuardChecker current = localGuardChecker.getOrDefault(guard.value(), GUARD_FUNCTIONS.getOrDefault(guard.value(), null)); - if (guardChecker == null) { + if (current == null) { Bukkit.getLogger().log(Level.WARNING, () -> "The guard checker with name '" + guard.value() + "' is neither a local guard checker nor a global one"); } return current; From cf9f2a2375ac924b589244dbc6003e483bf5606c Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 27 Nov 2021 10:13:19 +0100 Subject: [PATCH 68/75] Fix Language & more PR Stuff Signed-off-by: Chaoscaot --- SpigotCore_Main/src/SpigotCore.properties | 9 +- .../src/de/steamwar/sql/SchematicNode.java | 20 +-- .../de/steamwar/util/SchematicSelector.java | 146 +++++++----------- .../util/SchematicSelectorInjectable.java | 10 ++ 4 files changed, 84 insertions(+), 101 deletions(-) diff --git a/SpigotCore_Main/src/SpigotCore.properties b/SpigotCore_Main/src/SpigotCore.properties index 53227e0..8f75226 100644 --- a/SpigotCore_Main/src/SpigotCore.properties +++ b/SpigotCore_Main/src/SpigotCore.properties @@ -27,23 +27,22 @@ SCHEM_SELECTOR_SEL_DIR= SCHEM_SELECTOR_NEW_DIR=§7Neuer Ordner SCHEM_SELECTOR_FILTER=§7Filter -SCHEM_SELECTOR_CREATE_DIR_TITLE=Ordner Erstellen +SCHEM_SELECTOR_CREATE_DIR_TITLE=Ordner erstellen SCHEM_SELECTOR_FILTER_TITLE=Filter SCHEM_SELECTOR_FILTER_ENTER_NAME=Name eingeben SCHEM_SELECTOR_FILTER_NAME=§7Nach Namen suchen... -SCHEM_SELECTOR_FILTER_NAME_SEARCH=§7Suchwort: §e{0} +SCHEM_SELECTOR_FILTER_NAME_SEARCH=§7Suchbegriff: §e{0} SCHEM_SELECTOR_FILTER_ENTER_OWNER=Besitzer eingeben SCHEM_SELECTOR_FILTER_OWNER=§7Nach Besitzer suchen... SCHEM_SELECTOR_FILTER_OWNER_SEARCH=§7Besitzer: §e{0} SCHEM_SELECTOR_FILTER_SEL_TYPE=Typ wählen... -SCHEM_SELECTOR_FILTER_TYPE=§7Nach Typ Filtern... +SCHEM_SELECTOR_FILTER_TYPE=§7Nach Typ filtern... SCHEM_SELECTOR_FILTER_TYPE_SEARCH=§7Typ: §e{0} -SCHEM_SELECTOR_FILTER_MAT=§7Nach Item Filtern... +SCHEM_SELECTOR_FILTER_MAT=§7Nach Item filtern... SCHEM_SELECTOR_FILTER_MAT_SEARCH=§7Item: §e{0} SCHEM_SELECTOR_CANCEL=§eAbbrechen SCHEM_SELECTOR_GO=§eSuchen... - SCHEM_SELECTOR_SCHEMATIC=Schematic SCHEM_SELECTOR_DIRECTORY=Ordner SCHEM_SELECTOR_SCHEMATIC_NODE=Schematic/Ordner diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 009920c..f850a03 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -57,7 +57,7 @@ public class SchematicNode { private static final SQL.Statement getAccessibleByUserByTypeInNode_Null = new SQL.Statement("WITH RECURSIVE RSNB AS (WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSNB WHERE SN.NodeId = RSNB.ParentNode)SELECT * FROM RSNB WHERE ParentNode is null ORDER BY NodeName"); private static final SQL.Statement getAccessibleByUserByType = new SQL.Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? ORDER BY NodeName"); private static final SQL.Statement getAllSchematicsAccessibleByUser = new SQL.Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN ORDER BY NodeName"); - private static final SQL.Statement isSchematicAccessibleForUser = new SQL.Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT COUNT(RSN.NodeId) AS `Accessible` FROM RSN LEFT Join NodeMember NM On NM.NodeId = RSN.NodeId WHERE NodeOwner = ? OR UserId = ? LIMIT 1 ORDER BY NodeName"); + private static final SQL.Statement isSchematicAccessibleForUser = new SQL.Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT COUNT(RSN.NodeId) AS `Accessible` FROM RSN LEFT Join NodeMember NM On NM.NodeId = RSN.NodeId WHERE NodeOwner = ? OR UserId = ? LIMIT 1"); private static final SQL.Statement getAllParentsOfNode = new SQL.Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? UNION SELECT SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT * FROM RSN ORDER BY NodeName"); private static final SQL.Statement countNodes = new SQL.Statement("SELECT COUNT(NodeId) AS 'count' FROM SchematicNode"); private static final SQL.Statement updateDB = new SQL.Statement("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?"); @@ -80,8 +80,6 @@ public class SchematicNode { return getSchematicNode(owner, name, parent); } - private Timestamp lastUpdate; - public static SchematicNode getSchematicNode(int owner, String name, SchematicNode parent) { return getSchematicNode(owner, name, parent.getId()); } @@ -119,9 +117,8 @@ public class SchematicNode { parent = null; } SQL.Statement.ResultSetUser user = rs -> { - while (rs.next()) { - SchematicNode node = new SchematicNode(rs); - return node; + if (rs.next()) { + return new SchematicNode(rs); } return null; }; @@ -137,8 +134,9 @@ public class SchematicNode { parent = null; SQL.Statement.ResultSetUser> user = rs -> { List nodes = new ArrayList<>(); - while (rs.next()) + while (rs.next()) { nodes.add(new SchematicNode(rs)); + } return nodes; }; if(parent == null) { @@ -266,11 +264,12 @@ public class SchematicNode { public static List getSchematicsAccessibleByUser(int user, Integer parent) { if (parent != null && parent != 0) { - if(isSchematicAccessibleForUser.select(rs -> { + if(Boolean.TRUE.equals(isSchematicAccessibleForUser.select(rs -> { rs.next(); return rs.getInt("Accessible") > 0; - }, parent, user, user)) + }, parent, user, user))) { return getSchematicNodeInNode(parent); + } } else { return getAccessibleByUser.select(rs -> { List nodes = new ArrayList<>(); @@ -322,7 +321,7 @@ public class SchematicNode { if (currentNode == null) { node = SchematicNode.getSchematicsAccessibleByUser(user.getId(), 0).stream().filter(node1 -> node1.getName().equals(layers[finalI])).findAny(); } else { - node = SchematicNode.getSchematicNodeInNode(currentNode).stream().filter(node1 -> node1.getName().equals(layers[finalI])).findAny(); + node = Optional.of(SchematicNode.getSchematicNode(layers[i], currentNode.getId())); } if (!node.isPresent()) { return null; @@ -348,6 +347,7 @@ public class SchematicNode { private String type; private boolean schemFormat; private int rank; + private Timestamp lastUpdate; private final boolean isDir; private Map brCache = new HashMap<>(); diff --git a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java index f8a34db..f992c53 100644 --- a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java +++ b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java @@ -34,33 +34,27 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.function.Consumer; -import java.util.function.Function; public class SchematicSelector { - public static final String DEFAULT_TITLE = "SCHEM_SELECTOR_TITLE"; + protected static final String DEFAULT_TITLE = "SCHEM_SELECTOR_TITLE"; @Getter private final Player player; @Getter - private final SteamwarUser user; + private SteamwarUser user; @Getter private final Consumer callback; - @Getter private final SelectorTarget target; @Getter private final SelectorFilter filter = new SelectorFilter(); - private SchematicSelectorInjectable injectable; - private boolean useHooks; + private SchematicSelectorInjectable injectable = SchematicSelectorInjectable.DEFAULT; @Setter @Getter private PublicMode publicMode = PublicMode.ALL; @Setter @Getter private boolean singleDirOpen; - @Setter - @Getter - private Function title; private boolean sdoTrigger = false; public SchematicSelector(Player player, SelectorTarget target, Consumer callback) { @@ -68,24 +62,24 @@ public class SchematicSelector { this.user = SteamwarUser.get(player.getUniqueId()); this.target = target; this.callback = callback; - this.singleDirOpen = !target.target.isDirs(); + this.singleDirOpen = !target.target.dirs; } public SchematicSelector(Player player, SelectorTarget target, SchematicSelectorInjectable injectable, Consumer callback) { this(player, target, callback); - this.useHooks = true; this.injectable = injectable; } public void open() { - if(useHooks) { - injectable.onSelectorCreate(this); + injectable.onSelectorCreate(this); + if(publicMode == PublicMode.PUBLIC_ONLY) { + this.user = SteamwarUser.get(0); } - openList(null, publicMode == PublicMode.PUBLIC_ONLY); + openList(null); } - private void openList(SchematicNode parent, boolean publics) { - List nodes = filter.isFilter()?getFilteredSchematics(publics):getSchematicList(publics, parent); + private void openList(SchematicNode parent) { + List nodes = filter.isFilter()?getFilteredSchematics():getSchematicList(parent); if(sdoTrigger) { sdoTrigger = false; @@ -103,44 +97,48 @@ public class SchematicSelector { list.add(renderItem(node)); } - SWListInv inv = new SWListInv<>(player, MessageFormat.format(title==null?DEFAULT_TITLE:title.apply(player), target.target.getName(player), filter.getName().isEmpty()?(parent == null?"/":parent.generateBreadcrumbs(user)):filter.getName()), false, list, (clickType, node) -> handleClick(publics, node, parent)); + SWListInv inv = new SWListInv<>(player, MessageFormat.format(injectable.createTitle(player), target.target.getName(player), filter.getName().isEmpty()?(parent == null?"/":parent.generateBreadcrumbs(user)):filter.getName()), false, list, (clickType, node) -> handleClick(node, parent)); if(publicMode == PublicMode.ALL) { - if(publics) { - inv.setItem(48, Material.BUCKET, Core.MESSAGE.parse("SCHEM_SELECTOR_OWN", player), clickType -> openList(null, false)); + if(user.getId() == 0) { + inv.setItem(48, Material.BUCKET, Core.MESSAGE.parse("SCHEM_SELECTOR_OWN", player), clickType -> { + this.user = SteamwarUser.get(player.getUniqueId()); + openList(null); + }); } else { - inv.setItem(48, Material.GLASS, Core.MESSAGE.parse("SCHEM_SELECTOR_PUB", player), clickType -> openList(null, true)); + inv.setItem(48, Material.GLASS, Core.MESSAGE.parse("SCHEM_SELECTOR_PUB", player), clickType -> { + this.user = SteamwarUser.get(0); + openList(null); + }); } } - if(target.target.isDirs()) { + if(target.target.dirs) { inv.setItem(49, SWItem.getDye(10), Core.MESSAGE.parse("SCHEM_SELECTOR_SEL_DIR", player), clickType -> { player.closeInventory(); callback.accept(parent); }); } - if(!publics) { + if(user.getId() != 0) { inv.setItem(50, Material.CHEST, Core.MESSAGE.parse("SCHEM_SELECTOR_NEW_DIR", player), clickType -> createFolderIn(parent)); } - inv.setItem(51, Material.NAME_TAG, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER", player), clickType -> openFilter(publics)); + inv.setItem(51, Material.NAME_TAG, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER", player), clickType -> openFilter()); - if(useHooks) { - injectable.onListRender(this, inv, parent); - } + injectable.onListRender(this, inv, parent); inv.open(); } - private void handleClick(boolean publics, SchematicNode node, SchematicNode parent) { + private void handleClick(SchematicNode node, SchematicNode parent) { if(node == null) { - openList(getParent(parent), publics); + openList(getParent(parent)); return; } if(node.isDir()) { - if(filter.isFilter() && target.target.isDirs()) { + if(filter.isFilter() && target.target.dirs) { player.closeInventory(); callback.accept(node); return; } filter.reset(); - openList(node, publics); + openList(node); return; } player.closeInventory(); @@ -172,31 +170,26 @@ public class SchematicSelector { SWAnvilInv inv = new SWAnvilInv(player, Core.MESSAGE.parse("SCHEM_SELECTOR_CREATE_DIR_TITLE", player)); inv.setItem(Material.CHEST); inv.setCallback(s -> { - if(useHooks) { - if(injectable.onFolderCreate(this, s)) { - SchematicNode.createSchematicDirectory(user.getId(), s, parent==null?0:parent.getId()); - openList(parent, false); - } - } else { + if(injectable.onFolderCreate(this, s)) { SchematicNode.createSchematicDirectory(user.getId(), s, parent==null?0:parent.getId()); - openList(parent, false); + openList(parent); } }); inv.open(); } - private void openFilter(boolean publics) { + private void openFilter() { SWInventory inv = new SWInventory(player, 9, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_TITLE", player)); InvCallback nameCallback = clickType -> { if(clickType.isRightClick()) { filter.setName(""); - openFilter(publics); + openFilter(); } else { SWAnvilInv swAnvilInv = new SWAnvilInv(player, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_ENTER_NAME", player)); swAnvilInv.setItem(Material.NAME_TAG); swAnvilInv.setCallback(s -> { filter.setName(s); - openFilter(publics); + openFilter(); }); swAnvilInv.open(); } @@ -210,13 +203,13 @@ public class SchematicSelector { InvCallback ownerCallback = clickType -> { if(clickType.isRightClick()) { filter.setOwner(null); - openFilter(publics); + openFilter(); } else { SWAnvilInv swAnvilInv = new SWAnvilInv(player, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_ENTER_OWNER", player)); swAnvilInv.setItem(Material.PLAYER_HEAD); swAnvilInv.setCallback(s -> { filter.setOwner(SteamwarUser.get(s).getId()); - openFilter(publics); + openFilter(); }); swAnvilInv.open(); } @@ -237,7 +230,7 @@ public class SchematicSelector { InvCallback schemTypeCallback = clickType -> { if(clickType.isRightClick()) { filter.setType(null); - openFilter(publics); + openFilter(); } else { List> types = new ArrayList<>(); SchematicType.values().forEach(schematicType -> { @@ -245,7 +238,7 @@ public class SchematicSelector { }); SWListInv listInv = new SWListInv<>(player, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_SEL_TYPE", player), types, (clickType1, schematicType) -> { filter.setType(schematicType); - openFilter(publics); + openFilter(); }); listInv.open(); } @@ -261,11 +254,11 @@ public class SchematicSelector { InvCallback materialCallback = clickType -> { if(clickType.isRightClick()) { filter.setItem(null); - openFilter(publics); + openFilter(); } else { UtilGui.openMaterialSelector(player, material -> { filter.setItem(material); - openFilter(publics); + openFilter(); }); } }; @@ -280,29 +273,23 @@ public class SchematicSelector { inv.setItem(7, SWItem.getDye(1), Core.MESSAGE.parse("SCHEM_SELECTOR_CANCEL", player), clickType -> { filter.reset(); - openList(null, publics); + openList(null); }); inv.setItem(8, SWItem.getDye(10), Core.MESSAGE.parse("SCHEM_SELECTOR_GO", player), clickType -> { filter.setFilter(true); - if(useHooks) { - injectable.onFilterApply(this); - } - openList(null, publics); + injectable.onFilterApply(this); + openList(null); }); - if(useHooks) { - injectable.onFilterRender(this, inv); - } + injectable.onFilterRender(this, inv); inv.open(); } - private List getFilteredSchematics(boolean publics) { - List nodes = new ArrayList<>(SchematicNode.getAllSchematicsAccessibleByUser(publics ? 0 : user.getId())); + private List getFilteredSchematics() { + List nodes = new ArrayList<>(SchematicNode.getAllSchematicsAccessibleByUser(user.getId())); nodes.removeIf(node -> { - if(useHooks) { - injectable.onNodeFilter(this, node); - } + injectable.onNodeFilter(this, node); return !filter.matches(node); }); if(target.target == Target.DIRECTORY) { @@ -314,29 +301,29 @@ public class SchematicSelector { return nodes; } - private List getSchematicList(boolean publics, SchematicNode parent) { + private List getSchematicList(SchematicNode parent) { List nodes = new ArrayList<>(); switch (target.target) { case DIRECTORY: if(parent == null) { - nodes.addAll(SchematicNode.getSchematicsAccessibleByUser(publics?0:user.getId(), null)); + nodes.addAll(SchematicNode.getSchematicsAccessibleByUser(user.getId(), null)); nodes.removeIf(node -> !node.isDir()); } else { nodes.addAll(SchematicNode.getSchematicDirectoryInNode(parent.getId())); } break; case SCHEMATIC_TYPE: - nodes.addAll(SchematicNode.getAccessibleSchematicsOfTypeInParent(publics?0:user.getId(), target.type.toDB(), parent==null?0:parent.getId())); + nodes.addAll(SchematicNode.getAccessibleSchematicsOfTypeInParent(user.getId(), target.type.toDB(), parent==null?0:parent.getId())); if(target.rank >= 0) { nodes.removeIf(node -> node.getRank() > target.rank); } break; default: - nodes.addAll(SchematicNode.getSchematicsAccessibleByUser(publics?0:user.getId(), parent == null?0:parent.getId())); + nodes.addAll(SchematicNode.getSchematicsAccessibleByUser(user.getId(), parent == null?0:parent.getId())); } if(singleDirOpen && nodes.size() == 1 && nodes.get(0).isDir()) { - openList(nodes.get(0), publics); + openList(nodes.get(0)); sdoTrigger = true; } return nodes; @@ -369,17 +356,12 @@ public class SchematicSelector { return new SelectorTarget(Target.SCHEMATIC_TYPE, type, rank); } - public static class SelectorTarget { + @AllArgsConstructor + static class SelectorTarget { private final Target target; private final SchematicType type; private final int rank; - - private SelectorTarget(Target target, SchematicType type, int rank) { - this.target = target; - this.type = type; - this.rank = rank; - } } @AllArgsConstructor @@ -389,12 +371,10 @@ public class SchematicSelector { SCHEMATIC_NODE("SCHEM_SELECTOR_SCHEMATIC_NODE", true), SCHEMATIC_TYPE("SCHEM_SELECTOR_SCHEMATIC", false); - @Getter private String rawName; - @Getter private boolean dirs; - public String getName(Player player) { + private String getName(Player player) { return Core.MESSAGE.parse(rawName, player); } } @@ -421,22 +401,16 @@ public class SchematicSelector { public boolean matches(SchematicNode node) { boolean matches = true; - if(!name.isEmpty()) { - if(!node.getName().contains(name)) { - matches = false; - } + if(!name.isEmpty() && !node.getName().contains(name)) { + matches = false; } - if(owner != null) { - if(node.getOwner() != owner) { - matches = false; - } + if(owner != null && node.getOwner() != owner) { + matches = false; } - if(type != null) { - if(node.isDir() || !node.getType().equals(type.toDB())) { - matches = false; - } + if(type != null && (node.isDir() || !node.getSchemtype().equals(type))) { + matches = false; } if(item != null) { diff --git a/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java b/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java index fabc33f..d8a6193 100644 --- a/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java +++ b/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java @@ -19,12 +19,20 @@ package de.steamwar.util; +import de.steamwar.core.Core; import de.steamwar.inventory.SWInventory; import de.steamwar.inventory.SWListInv; import de.steamwar.sql.SchematicNode; +import org.bukkit.entity.Player; public interface SchematicSelectorInjectable { + SchematicSelectorInjectable DEFAULT = new DefaultSchematicSelectorInjectable(); + + default String createTitle(Player player) { + return Core.MESSAGE.parse("DEFAULT_TITLE", player); + } + default void onSelectorCreate(SchematicSelector selector) {} default void onListRender(SchematicSelector selector, SWListInv inv, SchematicNode parent) {} @@ -36,4 +44,6 @@ public interface SchematicSelectorInjectable { default boolean onFolderCreate(SchematicSelector selector, String name) {return true;} default void onNodeFilter(SchematicSelector selector, SchematicNode node) {} + + class DefaultSchematicSelectorInjectable implements SchematicSelectorInjectable {} } From 82421d95441b050a2a65573652962509933f4e03 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 27 Nov 2021 10:34:53 +0100 Subject: [PATCH 69/75] Fixing... Signed-off-by: Chaoscaot --- SpigotCore_Main/src/SpigotCore.properties | 2 +- .../de/steamwar/util/SchematicSelector.java | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/SpigotCore_Main/src/SpigotCore.properties b/SpigotCore_Main/src/SpigotCore.properties index 8f75226..66ec9a1 100644 --- a/SpigotCore_Main/src/SpigotCore.properties +++ b/SpigotCore_Main/src/SpigotCore.properties @@ -19,7 +19,7 @@ SCHEM_SELECTOR_TITLE={0} auswählen: {1} SCHEM_SELECTOR_BACK=§eZurück -SCHEM_SELECTOR_DIR=Ordner +SCHEM_SELECTOR_DIR=§9Ordner SCHEM_SELECTOR_RANK=§8Rang {0} SCHEM_SELECTOR_OWN=§7Eigene Schematics SCHEM_SELECTOR_PUB=§7Public Schematics diff --git a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java index f992c53..ba3ff0c 100644 --- a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java +++ b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java @@ -97,7 +97,7 @@ public class SchematicSelector { list.add(renderItem(node)); } - SWListInv inv = new SWListInv<>(player, MessageFormat.format(injectable.createTitle(player), target.target.getName(player), filter.getName().isEmpty()?(parent == null?"/":parent.generateBreadcrumbs(user)):filter.getName()), false, list, (clickType, node) -> handleClick(node, parent)); + SWListInv inv = new SWListInv<>(player, MessageFormat.format(injectable.createTitle(player), target.target.getName(player), filter.getName() == null?(parent == null?"/":parent.generateBreadcrumbs(user)):filter.getName()), false, list, (clickType, node) -> handleClick(node, parent)); if(publicMode == PublicMode.ALL) { if(user.getId() == 0) { inv.setItem(48, Material.BUCKET, Core.MESSAGE.parse("SCHEM_SELECTOR_OWN", player), clickType -> { @@ -152,13 +152,13 @@ public class SchematicSelector { else m = SWItem.getMaterial(node.getItem()); - String name = "§" + (filter.getName().isEmpty()?"e":"7") + node.getName(); + String name = "§" + (filter.getName() == null?"e":"7") + node.getName(); - if(!filter.getName().isEmpty()) { + if(filter.getName() != null) { name = name.replace(filter.getName(), "§e" + filter.getName() + "§7"); } - SWItem item = new SWItem(m, name, Collections.singletonList(node.isDir() ? ("§9" + Core.MESSAGE.parse("SCHEM_SELECTOR_DIR", player)) : "§7" + node.getSchemtype().name()), !node.isDir() && !node.getSchemtype().writeable(), click -> { + SWItem item = new SWItem(m, name, Collections.singletonList(node.isDir() ? (Core.MESSAGE.parse("SCHEM_SELECTOR_DIR", player)) : "§7" + node.getSchemtype().name()), !node.isDir() && !node.getSchemtype().writeable(), click -> { }); if(!node.isDir() && node.getRank() > 0) { item.setLore(Arrays.asList("§7" + node.getSchemtype().name(), Core.MESSAGE.parse("SCHEM_SELECTOR_RANK", player))); @@ -182,7 +182,7 @@ public class SchematicSelector { SWInventory inv = new SWInventory(player, 9, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_TITLE", player)); InvCallback nameCallback = clickType -> { if(clickType.isRightClick()) { - filter.setName(""); + filter.setName(null); openFilter(); } else { SWAnvilInv swAnvilInv = new SWAnvilInv(player, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_ENTER_NAME", player)); @@ -194,7 +194,7 @@ public class SchematicSelector { swAnvilInv.open(); } }; - if(filter.getName().isEmpty()) { + if(filter.getName() == null) { inv.setItem(0, Material.NAME_TAG, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_NAME", player), nameCallback); } else { inv.setItem(0, Material.NAME_TAG, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_NAME", player), Collections.singletonList(Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_NAME_SEARCH", player, filter.getName())), true, nameCallback); @@ -386,13 +386,13 @@ public class SchematicSelector { private boolean filter; - private String name = ""; + private String name = null; private Integer owner = null; private SchematicType type = null; private Material item = null; public void reset() { - name = ""; + name = null; owner = null; type = null; item = null; @@ -401,7 +401,7 @@ public class SchematicSelector { public boolean matches(SchematicNode node) { boolean matches = true; - if(!name.isEmpty() && !node.getName().contains(name)) { + if(name != null && !node.getName().contains(name)) { matches = false; } From becee281f66932b6596e39e369548bf0c10ad4ce Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 27 Nov 2021 10:40:54 +0100 Subject: [PATCH 70/75] More Language Signed-off-by: Chaoscaot --- SpigotCore_Main/src/SpigotCore.properties | 5 +++++ .../src/de/steamwar/util/SchematicSelector.java | 8 ++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/SpigotCore_Main/src/SpigotCore.properties b/SpigotCore_Main/src/SpigotCore.properties index 66ec9a1..e36969f 100644 --- a/SpigotCore_Main/src/SpigotCore.properties +++ b/SpigotCore_Main/src/SpigotCore.properties @@ -27,6 +27,11 @@ SCHEM_SELECTOR_SEL_DIR= SCHEM_SELECTOR_NEW_DIR=§7Neuer Ordner SCHEM_SELECTOR_FILTER=§7Filter +SCHEM_SELECTOR_ITEM_NAME=§e{0} +SCHEM_SELECTOR_ITEM_NAME_FILTER=§7{0} +SCHEM_SELECTOR_ITEM_REPLACE=§e{0}§7 +SCHEM_SELECTOR_ITEM_LORE_TYPE=§7{0} + SCHEM_SELECTOR_CREATE_DIR_TITLE=Ordner erstellen SCHEM_SELECTOR_FILTER_TITLE=Filter diff --git a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java index ba3ff0c..d1e3f01 100644 --- a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java +++ b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java @@ -152,16 +152,16 @@ public class SchematicSelector { else m = SWItem.getMaterial(node.getItem()); - String name = "§" + (filter.getName() == null?"e":"7") + node.getName(); + String name = Core.MESSAGE.parse(filter.name == null?"SCHEM_SELECTOR_ITEM_NAME":"SCHEM_SELECTOR_ITEM_NAME_FILTER", player, node.getName()); if(filter.getName() != null) { - name = name.replace(filter.getName(), "§e" + filter.getName() + "§7"); + name = name.replace(filter.getName(), Core.MESSAGE.parse("SCHEM_SELECTOR_ITEM_REPLACE", player, filter.getName())); } - SWItem item = new SWItem(m, name, Collections.singletonList(node.isDir() ? (Core.MESSAGE.parse("SCHEM_SELECTOR_DIR", player)) : "§7" + node.getSchemtype().name()), !node.isDir() && !node.getSchemtype().writeable(), click -> { + SWItem item = new SWItem(m, name, Collections.singletonList(node.isDir() ? (Core.MESSAGE.parse("SCHEM_SELECTOR_DIR", player)) : Core.MESSAGE.parse("SCHEM_SELECTOR_ITEM_LORE_TYPE", player, node.getSchemtype().name())), !node.isDir() && !node.getSchemtype().writeable(), click -> { }); if(!node.isDir() && node.getRank() > 0) { - item.setLore(Arrays.asList("§7" + node.getSchemtype().name(), Core.MESSAGE.parse("SCHEM_SELECTOR_RANK", player))); + item.setLore(Arrays.asList(Core.MESSAGE.parse("SCHEM_SELECTOR_ITEM_LORE_TYPE", player, node.getSchemtype().name()), Core.MESSAGE.parse("SCHEM_SELECTOR_RANK", player))); } return new SWListInv.SWListEntry<>(item, node); } From f15e051d50a7a65a71e3878cc11cf544fbd34c39 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 27 Nov 2021 11:16:16 +0100 Subject: [PATCH 71/75] Fix Default Signed-off-by: Chaoscaot --- .../src/de/steamwar/util/SchematicSelectorInjectable.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java b/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java index d8a6193..fd8f4ce 100644 --- a/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java +++ b/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java @@ -27,7 +27,7 @@ import org.bukkit.entity.Player; public interface SchematicSelectorInjectable { - SchematicSelectorInjectable DEFAULT = new DefaultSchematicSelectorInjectable(); + SchematicSelectorInjectable DEFAULT = new SchematicSelectorInjectable() {}; default String createTitle(Player player) { return Core.MESSAGE.parse("DEFAULT_TITLE", player); @@ -44,6 +44,4 @@ public interface SchematicSelectorInjectable { default boolean onFolderCreate(SchematicSelector selector, String name) {return true;} default void onNodeFilter(SchematicSelector selector, SchematicNode node) {} - - class DefaultSchematicSelectorInjectable implements SchematicSelectorInjectable {} } From 117277c926992f2c3dd8b748deafc43b4ccb76a8 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 27 Nov 2021 11:16:56 +0100 Subject: [PATCH 72/75] Fix Default Signed-off-by: Chaoscaot --- SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java | 2 -- .../src/de/steamwar/util/SchematicSelectorInjectable.java | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java index d1e3f01..9518707 100644 --- a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java +++ b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java @@ -37,8 +37,6 @@ import java.util.function.Consumer; public class SchematicSelector { - protected static final String DEFAULT_TITLE = "SCHEM_SELECTOR_TITLE"; - @Getter private final Player player; @Getter diff --git a/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java b/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java index fd8f4ce..4f2f63e 100644 --- a/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java +++ b/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java @@ -30,7 +30,7 @@ public interface SchematicSelectorInjectable { SchematicSelectorInjectable DEFAULT = new SchematicSelectorInjectable() {}; default String createTitle(Player player) { - return Core.MESSAGE.parse("DEFAULT_TITLE", player); + return Core.MESSAGE.parse("SCHEM_SELECTOR_TITLE", player); } default void onSelectorCreate(SchematicSelector selector) {} From e73ae9210ce9c59513796770257c8e90cda8314a Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 27 Nov 2021 15:12:54 +0100 Subject: [PATCH 73/75] Fix Default Signed-off-by: Chaoscaot --- SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java | 6 +++++- SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java | 6 +----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index f850a03..4e959a3 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -22,6 +22,7 @@ package de.steamwar.sql; import com.sk89q.worldedit.extent.clipboard.Clipboard; import de.steamwar.core.Core; import de.steamwar.core.WorldEditWrapper; +import de.steamwar.inventory.SWItem; import org.bukkit.entity.Player; import java.io.ByteArrayInputStream; @@ -321,7 +322,7 @@ public class SchematicNode { if (currentNode == null) { node = SchematicNode.getSchematicsAccessibleByUser(user.getId(), 0).stream().filter(node1 -> node1.getName().equals(layers[finalI])).findAny(); } else { - node = Optional.of(SchematicNode.getSchematicNode(layers[i], currentNode.getId())); + node = Optional.ofNullable(SchematicNode.getSchematicNode(layers[i], currentNode.getId())); } if (!node.isPresent()) { return null; @@ -401,6 +402,9 @@ public class SchematicNode { } public String getItem() { + if (item.isEmpty()) { + return isDir ? "CHEST" : "CAULDRON_ITEM"; + } return item; } diff --git a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java index 9518707..90f62c7 100644 --- a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java +++ b/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java @@ -144,11 +144,7 @@ public class SchematicSelector { } private SWListInv.SWListEntry renderItem(SchematicNode node) { - Material m; - if (node.getItem().isEmpty()) - m = node.isDir()?SWItem.getMaterial("CHEST"):SWItem.getMaterial("CAULDRON_ITEM"); - else - m = SWItem.getMaterial(node.getItem()); + Material m = SWItem.getMaterial(node.getItem()); String name = Core.MESSAGE.parse(filter.name == null?"SCHEM_SELECTOR_ITEM_NAME":"SCHEM_SELECTOR_ITEM_NAME_FILTER", player, node.getName()); From 4d40d1aa466adb2bc53ec91cb62c0f859f2485eb Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 30 Nov 2021 18:01:10 +0100 Subject: [PATCH 74/75] Move Files Signed-off-by: Chaoscaot --- .../de/steamwar/{util => inventory}/SchematicSelector.java | 3 +-- .../{util => inventory}/SchematicSelectorInjectable.java | 4 +--- .../src/de/steamwar/{util => inventory}/UtilGui.java | 2 +- 3 files changed, 3 insertions(+), 6 deletions(-) rename SpigotCore_Main/src/de/steamwar/{util => inventory}/SchematicSelector.java (99%) rename SpigotCore_Main/src/de/steamwar/{util => inventory}/SchematicSelectorInjectable.java (93%) rename SpigotCore_Main/src/de/steamwar/{util => inventory}/UtilGui.java (98%) diff --git a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java b/SpigotCore_Main/src/de/steamwar/inventory/SchematicSelector.java similarity index 99% rename from SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java rename to SpigotCore_Main/src/de/steamwar/inventory/SchematicSelector.java index 90f62c7..83e8ef8 100644 --- a/SpigotCore_Main/src/de/steamwar/util/SchematicSelector.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SchematicSelector.java @@ -17,10 +17,9 @@ * along with this program. If not, see . */ -package de.steamwar.util; +package de.steamwar.inventory; import de.steamwar.core.Core; -import de.steamwar.inventory.*; import de.steamwar.sql.SchematicNode; import de.steamwar.sql.SchematicType; import de.steamwar.sql.SteamwarUser; diff --git a/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java b/SpigotCore_Main/src/de/steamwar/inventory/SchematicSelectorInjectable.java similarity index 93% rename from SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java rename to SpigotCore_Main/src/de/steamwar/inventory/SchematicSelectorInjectable.java index 4f2f63e..7c4d718 100644 --- a/SpigotCore_Main/src/de/steamwar/util/SchematicSelectorInjectable.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SchematicSelectorInjectable.java @@ -17,11 +17,9 @@ * along with this program. If not, see . */ -package de.steamwar.util; +package de.steamwar.inventory; import de.steamwar.core.Core; -import de.steamwar.inventory.SWInventory; -import de.steamwar.inventory.SWListInv; import de.steamwar.sql.SchematicNode; import org.bukkit.entity.Player; diff --git a/SpigotCore_Main/src/de/steamwar/util/UtilGui.java b/SpigotCore_Main/src/de/steamwar/inventory/UtilGui.java similarity index 98% rename from SpigotCore_Main/src/de/steamwar/util/UtilGui.java rename to SpigotCore_Main/src/de/steamwar/inventory/UtilGui.java index c653610..298d0c0 100644 --- a/SpigotCore_Main/src/de/steamwar/util/UtilGui.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/UtilGui.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package de.steamwar.util; +package de.steamwar.inventory; import de.steamwar.core.Core; import de.steamwar.inventory.SWItem; From 646d9f7dab9c261cd93b6c8aff800a61072311a5 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 30 Nov 2021 18:09:20 +0100 Subject: [PATCH 75/75] Add Command Mapper and some more Functions Signed-off-by: Chaoscaot --- .../de/steamwar/command/SWCommandUtils.java | 12 +++++++++++ .../src/de/steamwar/sql/SchematicNode.java | 20 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java index 52e362e..1d7110f 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java @@ -19,6 +19,7 @@ package de.steamwar.command; +import de.steamwar.sql.SchematicNode; import de.steamwar.sql.SteamwarUser; import org.bukkit.Bukkit; import org.bukkit.GameMode; @@ -72,6 +73,17 @@ public class SWCommandUtils { return null; }, s -> Arrays.asList("s", "survival", "0", "c", "creative", "1", "sp", "spectator", "3", "a", "adventure", "2"))); MAPPER_FUNCTIONS.put(SteamwarUser.class.getTypeName(), createMapper(SteamwarUser::get, s -> Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList()))); + MAPPER_FUNCTIONS.put(SchematicNode.class.getTypeName(), new TypeMapper() { + @Override + public List tabCompletes(CommandSender commandSender, String[] strings, String s) { + return SchematicNode.getNodeTabcomplete(SteamwarUser.get(((Player) commandSender).getUniqueId()), s); + } + + @Override + public SchematicNode map(CommandSender commandSender, String[] previousArguments, String s) { + return SchematicNode.getNodeFromPath(SteamwarUser.get(((Player) commandSender).getUniqueId()), s); + } + }); } private static void addMapper(Class clazz, Class alternativeClazz, TypeMapper mapper) { diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 4e959a3..faea608 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -582,4 +582,24 @@ public class SchematicNode { SchematicNode node = (SchematicNode) obj; return node.getId() == id; } + + public static List getNodeTabcomplete(SteamwarUser user, String s) { + List list = new ArrayList<>(); + boolean sws = s.startsWith("/"); + if (sws) { + s = s.substring(1); + } + if (s.contains("/")) { + String preTab = s.substring(0, s.lastIndexOf("/") + 1); + SchematicNode pa = SchematicNode.getNodeFromPath(user, preTab); + if (pa == null) return Collections.emptyList(); + List nodes = SchematicNode.getSchematicNodeInNode(pa); + nodes.forEach(node -> list.add((sws ? "/" : "") + node.generateBreadcrumbs(user))); + } else { + List nodes = SchematicNode.getSchematicsAccessibleByUser(user.getId(), 0); + nodes.forEach(node -> list.add((sws ? "/" : "") + node.getName() + (node.isDir() ? "/" : ""))); + } + list.remove("//copy"); + return list; + } }