diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index ad9ced66..56760a1e 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -20,6 +20,7 @@ package de.steamwar.bungeecore; import de.steamwar.bungeecore.commands.*; +import de.steamwar.bungeecore.comms.SpigotReceiver; import de.steamwar.bungeecore.listeners.*; import de.steamwar.bungeecore.listeners.mods.Fabric; import de.steamwar.bungeecore.listeners.mods.Forge; @@ -69,6 +70,9 @@ public class BungeeCore extends Plugin { @Override public void onEnable(){ + + getProxy().registerChannel("sw:bridge"); + setInstance(this); loadConfig(); @@ -124,6 +128,7 @@ public class BungeeCore extends Plugin { new EventStarter(); new Broadcaster(); new SessionManager(); + new SpigotReceiver(); new TablistManager(); diff --git a/src/de/steamwar/bungeecore/commands/BauCommand.java b/src/de/steamwar/bungeecore/commands/BauCommand.java index 616bed28..043ddfb3 100644 --- a/src/de/steamwar/bungeecore/commands/BauCommand.java +++ b/src/de/steamwar/bungeecore/commands/BauCommand.java @@ -20,6 +20,8 @@ package de.steamwar.bungeecore.commands; import de.steamwar.bungeecore.*; +import de.steamwar.bungeecore.inventory.SWInventory; +import de.steamwar.bungeecore.inventory.SWItem; import de.steamwar.bungeecore.sql.BauweltMember; import de.steamwar.bungeecore.sql.SteamwarUser; import net.md_5.bungee.api.ProxyServer; @@ -226,32 +228,31 @@ public class BauCommand { } private static void delete(ProxiedPlayer p, String[] command){ - if(bau15(p, command, 2)){ - BungeeCore.send(p, BungeeCore.CHAT_PREFIX + "§aDeine Welt wird zurückgesetzt."); - ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { - for (Subserver subserver : Subserver.getServerList()) { - if (subserver.getType() == Servertype.BAUSERVER && ((Bauserver) subserver).getOwner().equals(p.getUniqueId())) { - subserver.stop(); - break; - } - } + SWInventory inventory = new SWInventory(p, 9, "§eWirklich Welt löschen?"); + inventory.addItem(8, new SWItem(8, "§cAbbrechen", 1), click -> inventory.close()); + inventory.addItem(1, new SWItem(1, "§aLöchen", 10), click -> { + if(bau15(p, command, 2)){ SteamwarUser user = SteamwarUser.get(p.getUniqueId()); - File directory = new File(BungeeCore.USERWORLDS15 + user.getId()); - del(directory); - }); - }else{ - BungeeCore.send(p, BungeeCore.CHAT_PREFIX + "§aDeine Welt wird zurückgesetzt."); - ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { - for (Subserver subserver : Subserver.getServerList()) { - if (subserver.getType() == Servertype.BAUSERVER && ((Bauserver) subserver).getOwner().equals(p.getUniqueId())) { - subserver.stop(); - break; - } + deleteWorld(p, BungeeCore.USERWORLDS15 + user.getId()); + }else{ + deleteWorld(p, BungeeCore.WORLD_FOLDER + p.getUniqueId().toString()); + } + }); + inventory.open(); + } + + private static void deleteWorld(ProxiedPlayer player, String world) { + BungeeCore.send(player, BungeeCore.CHAT_PREFIX + "§aDeine Welt wird zurückgesetzt."); + ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { + for (Subserver subserver : Subserver.getServerList()) { + if (subserver.getType() == Servertype.BAUSERVER && ((Bauserver) subserver).getOwner().equals(player.getUniqueId())) { + subserver.stop(); + break; } - File directory = new File(BungeeCore.WORLD_FOLDER + p.getUniqueId().toString()); - del(directory); - }); - } + } + File directory = new File(world); + del(directory); + }); } private static void testarena(ProxiedPlayer p, String[] command){ diff --git a/src/de/steamwar/bungeecore/commands/CheckCommand.java b/src/de/steamwar/bungeecore/commands/CheckCommand.java index a64c3830..d7eaf1a8 100644 --- a/src/de/steamwar/bungeecore/commands/CheckCommand.java +++ b/src/de/steamwar/bungeecore/commands/CheckCommand.java @@ -67,8 +67,8 @@ public class CheckCommand extends BasicCommand { ProxyServer.getInstance().getScheduler().schedule(BungeeCore.get(), () -> { List schematics = getSchemsToCheck(); - if(!schematics.isEmpty()) - ServerTeamchatCommand.sendToTeam("§7Es sind §e" + schematics.size() + " §7Schematics zu prüfen§8!"); + if(schematics.size() != currentCheckers.size()) + ServerTeamchatCommand.sendToTeam("§7Es sind §e" + (schematics.size() - currentCheckers.size()) + " §7Schematics zu prüfen§8!"); }, 10, 10, TimeUnit.MINUTES); } diff --git a/src/de/steamwar/bungeecore/comms/BungeePacket.java b/src/de/steamwar/bungeecore/comms/BungeePacket.java new file mode 100644 index 00000000..b0417d20 --- /dev/null +++ b/src/de/steamwar/bungeecore/comms/BungeePacket.java @@ -0,0 +1,47 @@ +/* + 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.bungeecore.comms; + +import com.google.common.io.ByteArrayDataOutput; +import com.google.common.io.ByteStreams; +import net.md_5.bungee.api.connection.ProxiedPlayer; +import net.md_5.bungee.api.connection.Server; + +import java.io.ByteArrayOutputStream; + +public abstract class BungeePacket { + + public void send(Server server) { + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + out.writeByte(getId()); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + writeVars(out); + out.writeUTF(outputStream.toByteArray().toString()); + server.sendData("sw:bridge", out.toByteArray()); + } + + public void send(ProxiedPlayer player) { + send(player.getServer()); + } + + public abstract int getId(); + + public abstract void writeVars(ByteArrayDataOutput byteArrayDataOutput); +} diff --git a/src/de/steamwar/bungeecore/comms/PacketIdManager.java b/src/de/steamwar/bungeecore/comms/PacketIdManager.java new file mode 100644 index 00000000..32d25e3b --- /dev/null +++ b/src/de/steamwar/bungeecore/comms/PacketIdManager.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.bungeecore.comms; + +public class PacketIdManager { + + //0x0(X) Standalone Packets + public final static byte PING_PACKET = 0x01; + //0x1(X) Bungee Inventory + public final static byte INVENTORY_PACKET = 0x10; + public final static byte INVENTORY_CALLBACK_PACKET = 0x11; + public final static byte INVENTORY_CLOSE_PACKET = 0x12; +} diff --git a/src/de/steamwar/bungeecore/comms/SpigotHandler.java b/src/de/steamwar/bungeecore/comms/SpigotHandler.java new file mode 100644 index 00000000..5c0f118c --- /dev/null +++ b/src/de/steamwar/bungeecore/comms/SpigotHandler.java @@ -0,0 +1,27 @@ +/* + 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.bungeecore.comms; + +import com.google.common.io.ByteArrayDataInput; + +public interface SpigotHandler { + + void handle(ByteArrayDataInput byteArrayDataInput); +} diff --git a/src/de/steamwar/bungeecore/comms/SpigotReceiver.java b/src/de/steamwar/bungeecore/comms/SpigotReceiver.java new file mode 100644 index 00000000..9a8da48a --- /dev/null +++ b/src/de/steamwar/bungeecore/comms/SpigotReceiver.java @@ -0,0 +1,57 @@ +/* + 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.bungeecore.comms; + +import com.google.common.io.ByteArrayDataInput; +import com.google.common.io.ByteStreams; +import de.steamwar.bungeecore.comms.handlers.InventoryCallbackHandler; +import de.steamwar.bungeecore.listeners.BasicListener; +import net.md_5.bungee.api.event.PluginMessageEvent; +import net.md_5.bungee.event.EventHandler; + +import java.util.HashMap; +import java.util.Map; + +public class SpigotReceiver extends BasicListener { + + private static final Map handlerMap = new HashMap<>(); + + @EventHandler + public void onPluginMessage(PluginMessageEvent event) { + if(!event.getTag().equalsIgnoreCase("sw:bridge")) + return; + if(!event.getSender().getAddress().getHostName().equals("localhost")){ + event.setCancelled(true); + return; + } + ByteArrayDataInput in = ByteStreams.newDataInput(event.getData()); + Byte handler = in.readByte(); + handlerMap.get(handler).handle(in); + event.setCancelled(true); + } + + public static void registerHandler(Byte id, SpigotHandler handler) { + handlerMap.put(id, handler); + } + + static { + registerHandler(PacketIdManager.INVENTORY_CALLBACK_PACKET, new InventoryCallbackHandler()); + } +} diff --git a/src/de/steamwar/bungeecore/comms/handlers/InventoryCallbackHandler.java b/src/de/steamwar/bungeecore/comms/handlers/InventoryCallbackHandler.java new file mode 100644 index 00000000..e39b668b --- /dev/null +++ b/src/de/steamwar/bungeecore/comms/handlers/InventoryCallbackHandler.java @@ -0,0 +1,56 @@ +/* + 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.bungeecore.comms.handlers; + +import com.google.common.io.ByteArrayDataInput; +import de.steamwar.bungeecore.comms.SpigotHandler; +import de.steamwar.bungeecore.inventory.InvCallback; +import de.steamwar.bungeecore.inventory.SWInventory; +import de.steamwar.bungeecore.sql.SteamwarUser; + +import java.util.HashMap; +import java.util.Map; + +public class InventoryCallbackHandler implements SpigotHandler { + + public static final Map inventoryHashMap = new HashMap<>(); + + @Override + public void handle(ByteArrayDataInput byteArrayDataInput) { + SteamwarUser owner = SteamwarUser.get(byteArrayDataInput.readInt()); + CallbackType type = CallbackType.valueOf(byteArrayDataInput.readUTF()); + if(type == CallbackType.CLICK) { + int pos = byteArrayDataInput.readInt(); + InvCallback.ClickType clickType = InvCallback.ClickType.valueOf(byteArrayDataInput.readUTF()); + inventoryHashMap.get(owner.getId()).handleCallback(clickType, pos); + }else if(type == CallbackType.CLOSE) { + if(inventoryHashMap.get(owner.getId()).isNext()) { + inventoryHashMap.get(owner.getId()).handleClose(); + return; + } + inventoryHashMap.get(owner.getId()).setNext(true); + } + } + + public enum CallbackType { + CLICK, + CLOSE, + } +} diff --git a/src/de/steamwar/bungeecore/comms/packets/CloseInventoryPacket.java b/src/de/steamwar/bungeecore/comms/packets/CloseInventoryPacket.java new file mode 100644 index 00000000..974661ab --- /dev/null +++ b/src/de/steamwar/bungeecore/comms/packets/CloseInventoryPacket.java @@ -0,0 +1,45 @@ +/* + 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.bungeecore.comms.packets; + +import com.google.common.io.ByteArrayDataOutput; +import de.steamwar.bungeecore.comms.BungeePacket; +import de.steamwar.bungeecore.comms.PacketIdManager; +import de.steamwar.bungeecore.sql.SteamwarUser; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +public class CloseInventoryPacket extends BungeePacket { + + final ProxiedPlayer player; + + @Override + public int getId() { + return PacketIdManager.INVENTORY_CLOSE_PACKET; + } + + public CloseInventoryPacket(ProxiedPlayer player) { + this.player = player; + } + + @Override + public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { + byteArrayDataOutput.writeInt(SteamwarUser.get(player).getId()); + } +} diff --git a/src/de/steamwar/bungeecore/comms/packets/InventoryPacket.java b/src/de/steamwar/bungeecore/comms/packets/InventoryPacket.java new file mode 100644 index 00000000..4a37d1c8 --- /dev/null +++ b/src/de/steamwar/bungeecore/comms/packets/InventoryPacket.java @@ -0,0 +1,80 @@ +/* + 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.bungeecore.comms.packets; + +import com.google.common.io.ByteArrayDataOutput; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import de.steamwar.bungeecore.comms.PacketIdManager; +import de.steamwar.bungeecore.inventory.SWInventory; +import de.steamwar.bungeecore.inventory.SWItem; +import de.steamwar.bungeecore.comms.BungeePacket; +import de.steamwar.bungeecore.sql.SteamwarUser; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +import java.util.HashMap; +import java.util.Map; + +public class InventoryPacket extends BungeePacket { + + final String title; + final int player; + final int size; + final Map items; + + public InventoryPacket(int size, String title, ProxiedPlayer player) { + items = new HashMap<>(); + this.title = title; + this.size = size; + this.player = SteamwarUser.get(player).getId(); + } + + public InventoryPacket(SWInventory inventory) { + items = inventory.getItems(); + this.title = inventory.getTitle(); + this.size = inventory.getSize(); + this.player = SteamwarUser.get(inventory.getPlayer()).getId(); + } + + @Override + public int getId() { + return PacketIdManager.INVENTORY_PACKET; + } + + @Override + public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { + JsonObject object = new JsonObject(); + object.addProperty("id", player); + object.addProperty("title", title); + object.addProperty("size", size); + object.addProperty("itemcount", items.size()); + JsonArray array = new JsonArray(); + for (int i = 0; i < size; i++) { + if(items.get(i) != null) + array.add(items.get(i).writeToString()); + } + object.add("items", array); + byteArrayDataOutput.writeUTF(object.toString()); + } + + public void setItem(int index, SWItem item) { + items.put(index, item); + } +} diff --git a/src/de/steamwar/bungeecore/comms/packets/PingPacket.java b/src/de/steamwar/bungeecore/comms/packets/PingPacket.java new file mode 100644 index 00000000..47f2bd30 --- /dev/null +++ b/src/de/steamwar/bungeecore/comms/packets/PingPacket.java @@ -0,0 +1,43 @@ +/* + 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.bungeecore.comms.packets; + +import com.google.common.io.ByteArrayDataOutput; +import de.steamwar.bungeecore.comms.BungeePacket; +import de.steamwar.bungeecore.comms.PacketIdManager; + +public class PingPacket extends BungeePacket { + + final int id; + + @Override + public int getId() { + return PacketIdManager.PING_PACKET; + } + + public PingPacket(int id) { + this.id = id; + } + + @Override + public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { + byteArrayDataOutput.writeInt(id); + } +} diff --git a/src/de/steamwar/bungeecore/inventory/InvCallback.java b/src/de/steamwar/bungeecore/inventory/InvCallback.java new file mode 100644 index 00000000..1c52bb24 --- /dev/null +++ b/src/de/steamwar/bungeecore/inventory/InvCallback.java @@ -0,0 +1,61 @@ +/* + 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.bungeecore.inventory; + +public interface InvCallback { + + void clicked(ClickType click); + + public static enum ClickType { + LEFT, + SHIFT_LEFT, + RIGHT, + SHIFT_RIGHT, + WINDOW_BORDER_LEFT, + WINDOW_BORDER_RIGHT, + MIDDLE, + NUMBER_KEY, + DOUBLE_CLICK, + DROP, + CONTROL_DROP, + CREATIVE, + UNKNOWN; + + public boolean isKeyboardClick() { + return this == NUMBER_KEY || this == DROP || this == CONTROL_DROP; + } + + public boolean isCreativeAction() { + return this == MIDDLE || this == CREATIVE; + } + + public boolean isRightClick() { + return this == RIGHT || this == SHIFT_RIGHT; + } + + public boolean isLeftClick() { + return this == LEFT || this == SHIFT_LEFT || this == DOUBLE_CLICK || this == CREATIVE; + } + + public boolean isShiftClick() { + return this == SHIFT_LEFT || this == SHIFT_RIGHT || this == CONTROL_DROP; + } + } +} diff --git a/src/de/steamwar/bungeecore/inventory/SWInventory.java b/src/de/steamwar/bungeecore/inventory/SWInventory.java new file mode 100644 index 00000000..6a308cff --- /dev/null +++ b/src/de/steamwar/bungeecore/inventory/SWInventory.java @@ -0,0 +1,137 @@ +/* + 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.bungeecore.inventory; + +import de.steamwar.bungeecore.comms.handlers.InventoryCallbackHandler; +import de.steamwar.bungeecore.comms.packets.CloseInventoryPacket; +import de.steamwar.bungeecore.comms.packets.InventoryPacket; +import de.steamwar.bungeecore.sql.SteamwarUser; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +import java.util.HashMap; +import java.util.Map; + +public class SWInventory { + + private final Map itemMap; + private InvCallback close; + private ProxiedPlayer player; + private int size; + private String title; + private boolean next; + + public SWInventory(ProxiedPlayer proxiedPlayer, int size, String title) { + itemMap = new HashMap<>(); + InventoryCallbackHandler.inventoryHashMap.put(SteamwarUser.get(proxiedPlayer).getId(), this); + this.player = proxiedPlayer; + this.size = size; + this.title = title; + next = false; + } + + public void addItem(SWItem item) { + itemMap.put(item.getPosition(), item); + } + + public void addItem(int position, SWItem item) { + itemMap.put(position, item); + } + + public void addItem(int pos, SWItem item, InvCallback callback) { + item.setCallback(callback); + itemMap.put(pos, item); + } + + public void addItem(int pos, SWItem item, String name, InvCallback callback) { + item.setCallback(callback); + itemMap.put(pos, item); + } + + public void removeItem(SWItem item) { + itemMap.remove(item.getPosition()); + } + + public void removeItem(int position) { + itemMap.remove(position); + } + + public int getSize() { + return size; + } + + public void setSize(int size) { + this.size = size; + } + + public void setTitle(String title) { + this.title = title; + } + + public void setCallback(int pos, InvCallback callback) { + itemMap.get(pos).setCallback(callback); + } + + public void setNext(boolean next) { + this.next = next; + } + + public boolean isNext() { + return next; + } + + public Map getItems() { + return itemMap; + } + + public void handleCallback(InvCallback.ClickType type, int pos) { + itemMap.get(pos).getCallback().clicked(type); + } + + public void handleClose() { + InventoryCallbackHandler.inventoryHashMap.remove(SteamwarUser.get(player).getId(), this); + if(close != null) + close.clicked(null); + } + + public void open() { + InventoryPacket inv = new InventoryPacket(this); + inv.send(player); + } + + public void close() { + new CloseInventoryPacket(player).send(player); + } + + public void setClose(InvCallback close) { + this.close = close; + } + + public ProxiedPlayer getPlayer() { + return player; + } + + public void setPlayer(ProxiedPlayer player) { + this.player = player; + } + + public String getTitle() { + return title; + } +} diff --git a/src/de/steamwar/bungeecore/inventory/SWItem.java b/src/de/steamwar/bungeecore/inventory/SWItem.java new file mode 100644 index 00000000..aadd2d98 --- /dev/null +++ b/src/de/steamwar/bungeecore/inventory/SWItem.java @@ -0,0 +1,159 @@ +/* + 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.bungeecore.inventory; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; + +import java.util.ArrayList; +import java.util.List; + +public class SWItem { + + private String material, title, skullOwner; + private boolean enchanted, hideAttributes; + private int position; + private List lore; + private InvCallback callback; + private int color; + + public SWItem(String material, int position, String title) { + this.material = material.toUpperCase(); + lore = new ArrayList<>(); + this.position = position; + this.title = title; + color = 0; + } + + public SWItem(int position, String title, int color) { + this.material = "DYE"; + lore = new ArrayList<>(); + this.position = position; + this.title = title; + this.color = color; + } + + public static SWItem getSkull(String skullOwner) { + SWItem item = new SWItem("SKULL", 1, skullOwner); + item.setSkullOwner(skullOwner); + return item; + } + + public void setCallback(InvCallback callback) { + this.callback = callback; + } + + public void setColor(int color) { + this.color = color; + } + + public InvCallback getCallback() { + return callback; + } + + public String getMaterial() { + return material; + } + + public void setMaterial(String material) { + this.material = material.toUpperCase(); + } + + public String getSkullOwner() { + return skullOwner; + } + + public void setSkullOwner(String skullOwner) { + this.skullOwner = skullOwner; + } + + public boolean isEnchanted() { + return enchanted; + } + + public void setEnchanted(boolean enchanted) { + this.enchanted = enchanted; + } + + public boolean isHideAttributes() { + return hideAttributes; + } + + public void setHideAttributes(boolean hideAttributes) { + this.hideAttributes = hideAttributes; + } + + public int getPosition() { + return position; + } + + public void setName(String name) { + title = name; + } + + public void addLore(String lore) { + this.lore.add(lore); + } + + public JsonObject writeToString() { + JsonObject object = new JsonObject(); + object.addProperty("material", material); + object.addProperty("position", position); + object.addProperty("title", title); + if(skullOwner != null) + object.addProperty("skullOwner", skullOwner); + if(enchanted) + object.addProperty("enchanted", true); + if(hideAttributes) + object.addProperty("hideAttributes", true); + if(color != 0) + object.addProperty("color", color); + if(lore != null) { + JsonArray array = new JsonArray(); + for (String lores: + lore) { + array.add(lores); + } + object.add("lore", array); + } + + return object; + } + + @Override + public boolean equals(Object o) { + if(!(o instanceof SWItem)) + return false; + SWItem item = (SWItem) o; + if(item.enchanted != enchanted) + return false; + if(item.hideAttributes != hideAttributes) + return false; + if(!item.material.equals(material)) + return false; + if(!item.lore.equals(lore)) + return false; + if(!item.skullOwner.equals(skullOwner)) + return false; + if(item.position != position) + return false; + return true; + } +} diff --git a/src/de/steamwar/bungeecore/inventory/SWListInv.java b/src/de/steamwar/bungeecore/inventory/SWListInv.java new file mode 100644 index 00000000..639e9536 --- /dev/null +++ b/src/de/steamwar/bungeecore/inventory/SWListInv.java @@ -0,0 +1,145 @@ +/* + 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.bungeecore.inventory; + +import de.steamwar.bungeecore.sql.Schematic; +import de.steamwar.bungeecore.sql.SchematicType; +import net.md_5.bungee.BungeeCord; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +import java.util.*; + +public class SWListInv extends SWInventory { + private ListCallback callback; + private List> elements; + private int page; + + public SWListInv(ProxiedPlayer p, String t, List> l, ListCallback c){ + super(p, (l.size()>45) ? 54 : (l.size() + 9-l.size()%9), t); + callback = c; + elements = l; + page = 0; + } + + @Override + public void open(){ + setCallback(-999, (InvCallback.ClickType click) -> close()); + if(elements.size() > 54){ + if(page != 0) + addItem(45, new SWItem(45, "§eSeite zurück", 10), (InvCallback.ClickType click) -> { + page--; + open(); + }); + else + addItem(45, new SWItem(45, "§7Seite zurück", 8), (InvCallback.ClickType click) -> {}); + if(page < elements.size()/45) + addItem(53, new SWItem( 53, "§eSeite vor", 10), "§eSeite vor", (InvCallback.ClickType click) -> { + page++; + open(); + }); + else + addItem(53, new SWItem(53, "§eSeite vor", 8), (InvCallback.ClickType click) -> {}); + } + + int ipageLimit = elements.size() - page*45; + if(ipageLimit > 45 && elements.size() > 54){ + ipageLimit = 45; + } + int i = page*45; + for(int ipage=0; ipage < ipageLimit; ipage++ ){ + SWItem e = elements.get(i).getItem(); + + final int pos = i; + addItem(ipage, e); + setCallback(ipage, (InvCallback.ClickType click) -> callback.clicked(click, elements.get(pos).getObject())); + i++; + } + super.open(); + } + + public void setCallback(ListCallback c){ + callback = c; + } + + public interface ListCallback{ + void clicked(InvCallback.ClickType click, T element); + } + + public static List> createPlayerList(UUID without){ + List> onlinePlayers = new ArrayList<>(); + for(ProxiedPlayer player : BungeeCord.getInstance().getPlayer(without).getServer().getInfo().getPlayers()){ + if(without != null && player.getUniqueId().equals(without)) + continue; + + onlinePlayers.add(new SWListEntry<>(SWItem.getSkull(player.getName()), player.getUniqueId())); + } + return onlinePlayers; + } + + public static List> createGlobalPlayerList(UUID without){ + List> onlinePlayers = new ArrayList<>(); + for(ProxiedPlayer player : BungeeCord.getInstance().getPlayers()){ + if(without != null && player.getUniqueId().equals(without)) + continue; + + onlinePlayers.add(new SWListEntry<>(SWItem.getSkull(player.getName()), player.getUniqueId())); + } + 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){ + String m; + if(s.getSchemItem().isEmpty()) + m = "CAULDRON_ITEM"; + else + m = s.getSchemItem(); + SWItem item = new SWItem(m, 0, "§e" + s.getSchemName()); + schemList.add(new SWListEntry<>(item, s)); + } + return schemList; + } + + public static class SWListEntry{ + final SWItem item; + final T object; + + public SWListEntry(SWItem item, T object){ + this.item = item; + this.object = object; + } + + public SWItem getItem(){ + return item; + } + + public T getObject(){ + return object; + } + } +} diff --git a/src/de/steamwar/bungeecore/listeners/ChatListener.java b/src/de/steamwar/bungeecore/listeners/ChatListener.java index 554db08b..dcb95b95 100644 --- a/src/de/steamwar/bungeecore/listeners/ChatListener.java +++ b/src/de/steamwar/bungeecore/listeners/ChatListener.java @@ -24,6 +24,7 @@ import de.steamwar.bungeecore.Servertype; import de.steamwar.bungeecore.Subserver; import de.steamwar.bungeecore.commands.BauCommand; import de.steamwar.bungeecore.commands.TpCommand; +import de.steamwar.bungeecore.comms.packets.PingPacket; import de.steamwar.bungeecore.sql.SteamwarUser; import de.steamwar.bungeecore.sql.Team; import net.md_5.bungee.api.ChatColor; @@ -188,6 +189,7 @@ public class ChatListener extends BasicListener { for (String curr : msgSplits) { if(curr.equalsIgnoreCase("@" + target.getName())) { builder.append("§e@").append(target.getName()).append(chatcolor).append(" "); + new PingPacket(SteamwarUser.get(target).getId()).send(target); }else { builder.append(curr).append(" "); } diff --git a/src/de/steamwar/bungeecore/sql/Schematic.java b/src/de/steamwar/bungeecore/sql/Schematic.java index c70e21d8..9d25a384 100644 --- a/src/de/steamwar/bungeecore/sql/Schematic.java +++ b/src/de/steamwar/bungeecore/sql/Schematic.java @@ -30,12 +30,14 @@ public class Schematic { private final String schemName; private final int schemOwner; private SchematicType schemType; + private String schemItem; private Schematic(ResultSet rs) throws SQLException { this.schemID = rs.getInt("SchemID"); this.schemName = rs.getString("SchemName"); this.schemOwner = rs.getInt("SchemOwner"); this.schemType = SchematicType.fromDB(rs.getString("SchemType")); + this.schemItem = rs.getString("Item"); } public static Schematic getSchemFromDB(String schemName, UUID schemOwner){ @@ -43,7 +45,7 @@ public class Schematic { } public static Schematic getSchemFromDB(String schemName, int schemOwner){ - ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType FROM Schematic WHERE SchemName = ? AND SchemOwner = ?", schemName, schemOwner); + ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType, Item FROM Schematic WHERE SchemName = ? AND SchemOwner = ?", schemName, schemOwner); try { if(schematic == null || !schematic.next()){ return null; @@ -55,7 +57,7 @@ public class Schematic { } public static Schematic getSchemFromDB(int schemID){ - ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType FROM Schematic WHERE SchemID = ?", schemID); + ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType, Item FROM Schematic WHERE SchemID = ?", schemID); try { if(!schematic.next()) throw new SecurityException("Failed loading schematic " + schemID); @@ -67,7 +69,7 @@ public class Schematic { public static List getAllSchemsOfType(SchematicType schemType){ try{ - ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType FROM Schematic WHERE SchemType = ?", schemType.toDB()); + ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType, Item FROM Schematic WHERE SchemType = ?", schemType.toDB()); List schematics = new ArrayList<>(); while(schematic.next()){ schematics.add(new Schematic(schematic)); @@ -80,7 +82,7 @@ public class Schematic { public static List getSchemsOfType(int userId, SchematicType schemType){ try{ - ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType FROM Schematic WHERE SchemType = ? AND SchemOwner = ?", schemType.toDB(), userId); + ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType, Item FROM Schematic WHERE SchemType = ? AND SchemOwner = ?", schemType.toDB(), userId); List schematics = new ArrayList<>(); while(schematic.next()){ schematics.add(new Schematic(schematic)); @@ -91,6 +93,23 @@ public class Schematic { } } + public static List getSchemsAccessibleByUser(int schemOwner){ + try{ + ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, Item, SchemType, Rank, SchemFormat, Item FROM Schematic WHERE SchemOwner = ? ORDER BY SchemName", schemOwner); + List schematics = new ArrayList<>(); + while(schematic.next()){ + schematics.add(new Schematic(schematic)); + } + List addedSchems = SchematicMember.getAccessibleSchems(schemOwner); + for(SchematicMember schem : addedSchems){ + schematics.add(getSchemFromDB(schem.getSchemName(), schem.getSchemOwner())); + } + return schematics; + }catch(SQLException e){ + throw new SecurityException("Failed listing schematics", e); + } + } + public int getSchemID() { return schemID; } @@ -103,6 +122,10 @@ public class Schematic { return schemOwner; } + public String getSchemItem() { + return schemItem; + } + public SchematicType getSchemType() { return schemType; } diff --git a/src/de/steamwar/bungeecore/sql/SchematicMember.java b/src/de/steamwar/bungeecore/sql/SchematicMember.java new file mode 100644 index 00000000..85b84855 --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/SchematicMember.java @@ -0,0 +1,134 @@ +/* + 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.bungeecore.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); + } +}