196 Zeilen
6.3 KiB
Java
196 Zeilen
6.3 KiB
Java
/*
|
|
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.inventory;
|
|
|
|
import com.google.gson.JsonArray;
|
|
import com.google.gson.JsonObject;
|
|
import de.steamwar.core.FlatteningWrapper;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.OfflinePlayer;
|
|
import org.bukkit.enchantments.Enchantment;
|
|
import org.bukkit.inventory.ItemFlag;
|
|
import org.bukkit.inventory.ItemStack;
|
|
import org.bukkit.inventory.meta.ItemMeta;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class SWItem {
|
|
|
|
private ItemStack itemStack;
|
|
private ItemMeta itemMeta;
|
|
private InvCallback callback;
|
|
|
|
public static SWItem getPlayerSkull(OfflinePlayer player){
|
|
return getPlayerSkull(player.getName());
|
|
}
|
|
|
|
public static SWItem getPlayerSkull(String playerName){
|
|
SWItem p = new SWItem();
|
|
ItemStack head = FlatteningWrapper.impl.setSkullOwner(playerName);
|
|
p.setItemStack(head);
|
|
return p;
|
|
}
|
|
|
|
public static Material getMaterial(String material){
|
|
try{
|
|
return FlatteningWrapper.impl.getMaterial(material);
|
|
}catch(IllegalArgumentException e){
|
|
return Material.STONE;
|
|
}
|
|
}
|
|
|
|
public static Material getDye(int colorCode){
|
|
return FlatteningWrapper.impl.getDye(colorCode);
|
|
}
|
|
|
|
public SWItem() {
|
|
itemStack = new ItemStack(Material.AIR);
|
|
itemMeta = itemStack.getItemMeta();
|
|
hideAttributes();
|
|
}
|
|
|
|
public SWItem(Material material, String name){
|
|
this(material, (byte)0, name, new ArrayList<>(), false, null);
|
|
}
|
|
|
|
public SWItem(Material material, String name, InvCallback c){
|
|
this(material, (byte)0, name, new ArrayList<>(), false, c);
|
|
}
|
|
|
|
public SWItem(Material material, byte meta, String name){
|
|
this(material, meta, name, new ArrayList<>(), false, null);
|
|
}
|
|
|
|
public SWItem(Material material, byte meta, String name, InvCallback c){
|
|
this(material, meta, name, new ArrayList<>(), false, c);
|
|
}
|
|
|
|
public SWItem(Material material, String name, List<String> lore, boolean enchanted, InvCallback c) {
|
|
this(material, (byte)0, name, lore, enchanted, c);
|
|
}
|
|
|
|
@SuppressWarnings("deprecation")
|
|
public SWItem(Material material, byte meta, String name, List<String> lore, boolean enchanted, InvCallback c) {
|
|
try {
|
|
itemStack = new ItemStack(material, 1, (short)0, meta);
|
|
} catch (IllegalArgumentException e) {
|
|
itemStack = new ItemStack(material, 1);
|
|
}
|
|
itemMeta = itemStack.getItemMeta();
|
|
|
|
if (itemMeta != null) {
|
|
hideAttributes();
|
|
|
|
itemMeta.setDisplayName(name);
|
|
if (lore != null && !lore.isEmpty()) itemMeta.setLore(lore);
|
|
if (enchanted) itemMeta.addEnchant(Enchantment.DURABILITY , 10, true);
|
|
itemStack.setItemMeta(itemMeta);
|
|
}
|
|
callback = c;
|
|
}
|
|
|
|
public static SWItem getItemFromJson(JsonObject itemJson) {
|
|
SWItem item = null;
|
|
try {
|
|
if(itemJson.has("color")) {
|
|
item = new SWItem(SWItem.getDye(itemJson.get("color").getAsInt()),
|
|
itemJson.has("color")?itemJson.get("color").getAsByte():0,
|
|
itemJson.get("title").getAsString());
|
|
}else {
|
|
item = new SWItem(SWItem.getMaterial(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("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() {
|
|
if (itemMeta == null) return;
|
|
itemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
|
|
itemMeta.addItemFlags(ItemFlag.HIDE_DESTROYS);
|
|
itemMeta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
|
|
itemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
|
|
itemMeta.addItemFlags(ItemFlag.HIDE_PLACED_ON);
|
|
itemMeta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
|
|
}
|
|
|
|
public ItemStack getItemStack() {
|
|
return itemStack;
|
|
}
|
|
|
|
public void setItemStack(ItemStack itemStack) {
|
|
this.itemStack = itemStack;
|
|
itemMeta = itemStack.getItemMeta();
|
|
hideAttributes();
|
|
}
|
|
|
|
public ItemMeta getItemMeta() {
|
|
return itemMeta;
|
|
}
|
|
|
|
public void setItemMeta(ItemMeta itemMeta) {
|
|
this.itemMeta = itemMeta;
|
|
itemStack.setItemMeta(itemMeta);
|
|
hideAttributes();
|
|
}
|
|
|
|
public InvCallback getCallback() {
|
|
return callback;
|
|
}
|
|
|
|
public void setCallback(InvCallback callback) {
|
|
this.callback = callback;
|
|
}
|
|
|
|
public void setName(String name) {
|
|
itemMeta.setDisplayName(name);
|
|
itemStack.setItemMeta(itemMeta);
|
|
}
|
|
|
|
public void setLore(List<String> lore) {
|
|
itemMeta.setLore(lore);
|
|
itemStack.setItemMeta(itemMeta);
|
|
}
|
|
|
|
public void setEnchanted(boolean enchanted) {
|
|
if (enchanted){
|
|
itemMeta.addEnchant(Enchantment.DURABILITY , 10, true);
|
|
} else {
|
|
itemMeta.removeEnchant(Enchantment.DURABILITY);
|
|
}
|
|
itemStack.setItemMeta(itemMeta);
|
|
}
|
|
}
|