SteamWar/SpigotCore
Archiviert
13
0
Dieser Commit ist enthalten in:
Chaoscaot 2020-09-24 14:09:19 +02:00
Ursprung 00b9809cde
Commit ee7e68eaae
4 geänderte Dateien mit 67 neuen und 27 gelöschten Zeilen

Datei anzeigen

@ -1,3 +1,22 @@
/*
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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.comms; package de.steamwar.comms;
public class PacketIdManager { public class PacketIdManager {

Datei anzeigen

@ -27,7 +27,6 @@ import de.steamwar.inventory.SWInventory;
import de.steamwar.inventory.SWItem; import de.steamwar.inventory.SWItem;
import de.steamwar.sql.SteamwarUser; import de.steamwar.sql.SteamwarUser;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material;
import de.steamwar.comms.packets.*; import de.steamwar.comms.packets.*;
import java.util.*; import java.util.*;
@ -45,38 +44,15 @@ public class InventoryHandler implements BungeeHandler {
JsonArray array = object.get("items").getAsJsonArray(); JsonArray array = object.get("items").getAsJsonArray();
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
JsonObject itemJson = array.get(i).getAsJsonObject(); JsonObject itemJson = array.get(i).getAsJsonObject();
SWItem item = null; SWItem item = SWItem.getItemFromJson(itemJson);
try {
item = new SWItem(Material.valueOf(itemJson.get("material").getAsString()), itemJson.get("title").getAsString());
}catch (IllegalArgumentException e) {
item = new SWItem(Material.STONE, itemJson.get("title").getAsString());
}
if(itemJson.has("skullOwner")) {
item = SWItem.getPlayerSkull(itemJson.get("skullOwner").getAsString());
item.setName(itemJson.get("title").getAsString());
}
if(itemJson.has("color")) {
item.getItemStack().setType(SWItem.getDye(itemJson.get("color").getAsInt()));
}
if(itemJson.has("enchanted"))
item.setEnchanted(true);
if(itemJson.has("lore")) {
List<String> lore = new ArrayList<>();
JsonArray loreArray = itemJson.getAsJsonArray("lore");
loreArray.forEach(jsonElement -> lore.add(jsonElement.getAsString()));
item.setLore(lore);
}
item.setCallback(click -> { item.setCallback(click -> {
new InventoryCallbackPacket().setPosition(itemJson.get("position").getAsInt()).setClick(click) new InventoryCallbackPacket(itemJson, click, player).send(Bukkit.getPlayer(player));
.setCallback(InventoryCallbackPacket.CallbackType.CLICK)
.setOwner(SteamwarUser.get(player).getId()).send(Bukkit.getPlayer(player));
}); });
items.put(itemJson.get("position").getAsInt(), item); items.put(itemJson.get("position").getAsInt(), item);
} }
SWInventory inventory = new SWInventory(Bukkit.getPlayer(player), size, title, items); SWInventory inventory = new SWInventory(Bukkit.getPlayer(player), size, title, items);
inventory.addCloseCallback(click -> new InventoryCallbackPacket() inventory.addCloseCallback(click -> new InventoryCallbackPacket(player).send(Bukkit.getPlayer(player)));
.setCallback(InventoryCallbackPacket.CallbackType.CLOSE).setOwner(SteamwarUser.get(player).getId()).send(Bukkit.getPlayer(player)));
inventory.open(); inventory.open();
} }
} }

Datei anzeigen

@ -20,9 +20,13 @@
package de.steamwar.comms.packets; package de.steamwar.comms.packets;
import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteArrayDataOutput;
import com.google.gson.JsonObject;
import de.steamwar.comms.PacketIdManager; import de.steamwar.comms.PacketIdManager;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;
import java.util.UUID;
public class InventoryCallbackPacket extends SpigotPacket { public class InventoryCallbackPacket extends SpigotPacket {
int position = -1; int position = -1;
@ -45,6 +49,20 @@ public class InventoryCallbackPacket extends SpigotPacket {
} }
} }
public InventoryCallbackPacket() {}
public InventoryCallbackPacket(JsonObject itemJson, ClickType click, UUID player) {
setPosition(itemJson.get("position").getAsInt());
setClick(click);
setCallback(InventoryCallbackPacket.CallbackType.CLICK);
setOwner(SteamwarUser.get(player).getId());
}
public InventoryCallbackPacket(UUID player) {
setCallback(CallbackType.CLOSE);
setOwner(SteamwarUser.get(player).getId());
}
public InventoryCallbackPacket setPosition(int position) { public InventoryCallbackPacket setPosition(int position) {
this.position = position; this.position = position;
return this; return this;

Datei anzeigen

@ -19,6 +19,8 @@
package de.steamwar.inventory; package de.steamwar.inventory;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import de.steamwar.core.Core; import de.steamwar.core.Core;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
@ -137,6 +139,31 @@ public class SWItem {
callback = c; callback = c;
} }
public static SWItem getItemFromJson(JsonObject itemJson) {
SWItem item = null;
try {
item = new SWItem(Material.valueOf(itemJson.get("material").getAsString()), itemJson.get("title").getAsString());
}catch (IllegalArgumentException e) {
item = new SWItem(Material.STONE, itemJson.get("title").getAsString());
}
if(itemJson.has("skullOwner")) {
item = SWItem.getPlayerSkull(itemJson.get("skullOwner").getAsString());
item.setName(itemJson.get("title").getAsString());
}
if(itemJson.has("color")) {
item.getItemStack().setType(SWItem.getDye(itemJson.get("color").getAsInt()));
}
if(itemJson.has("enchanted"))
item.setEnchanted(true);
if(itemJson.has("lore")) {
List<String> lore = new ArrayList<>();
JsonArray loreArray = itemJson.getAsJsonArray("lore");
loreArray.forEach(jsonElement -> lore.add(jsonElement.getAsString()));
item.setLore(lore);
}
return item;
}
private void hideAttributes() { private void hideAttributes() {
if (itemMeta == null) return; if (itemMeta == null) return;
itemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES); itemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);