PlotSquared/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitInventoryUtil.java

170 lines
5.9 KiB
Java
Raw Normal View History

/*
* PlotSquared, a land and world management plugin for Minecraft.
* Copyright (C) IntellectualSites <https://intellectualsites.com>
* Copyright (C) IntellectualSites team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-04-11 02:19:18 +02:00
package com.plotsquared.bukkit.util;
2020-07-10 22:12:37 +02:00
import com.google.inject.Singleton;
2020-04-11 02:19:18 +02:00
import com.plotsquared.bukkit.player.BukkitPlayer;
2020-04-30 12:01:52 +02:00
import com.plotsquared.core.player.PlotPlayer;
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.plot.PlotInventory;
import com.plotsquared.core.plot.PlotItemStack;
import com.plotsquared.core.util.InventoryUtil;
2019-11-11 21:39:28 +01:00
import com.sk89q.worldedit.bukkit.BukkitAdapter;
2020-08-17 15:22:41 +02:00
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
2018-12-17 20:57:21 +01:00
import org.bukkit.Material;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
@Singleton
public class BukkitInventoryUtil extends InventoryUtil {
private static @Nullable ItemStack getItem(PlotItemStack item) {
if (item == null) {
return null;
}
Material material = BukkitAdapter.adapt(item.getType());
if (material == null) {
return null;
}
ItemStack stack = new ItemStack(material, item.getAmount());
ItemMeta meta = null;
if (item.getName() != null) {
meta = stack.getItemMeta();
Component nameComponent = BukkitUtil.MINI_MESSAGE.parse(item.getName());
meta.setDisplayName(BukkitUtil.LEGACY_COMPONENT_SERIALIZER.serialize(nameComponent));
}
if (item.getLore() != null) {
if (meta == null) {
meta = stack.getItemMeta();
}
List<String> lore = new ArrayList<>();
for (String entry : item.getLore()) {
lore.add(BukkitUtil.LEGACY_COMPONENT_SERIALIZER.serialize(BukkitUtil.MINI_MESSAGE.deserialize(entry)));
}
meta.setLore(lore);
}
if (meta != null) {
stack.setItemMeta(meta);
}
return stack;
}
@SuppressWarnings("deprecation") // Paper deprecation
@Override
public void open(PlotInventory inv) {
2020-08-17 15:17:40 +02:00
BukkitPlayer bp = (BukkitPlayer) inv.getPlayer();
2020-08-17 15:32:17 +02:00
Inventory inventory = Bukkit.createInventory(null, inv.getLines() * 9,
ChatColor.translateAlternateColorCodes('&', inv.getTitle())
);
2019-12-09 20:43:53 +01:00
PlotItemStack[] items = inv.getItems();
2020-08-17 15:32:17 +02:00
for (int i = 0; i < inv.getLines() * 9; i++) {
2019-12-09 20:43:53 +01:00
PlotItemStack item = items[i];
if (item != null) {
inventory.setItem(i, getItem(item));
}
}
bp.player.openInventory(inventory);
}
@Override
public void close(PlotInventory inv) {
2019-12-09 20:43:53 +01:00
if (!inv.isOpen()) {
return;
}
2020-08-17 15:17:40 +02:00
BukkitPlayer bp = (BukkitPlayer) inv.getPlayer();
2019-12-09 20:43:53 +01:00
bp.player.closeInventory();
}
@Override
public boolean setItemChecked(PlotInventory inv, int index, PlotItemStack item) {
2020-08-17 15:17:40 +02:00
BukkitPlayer bp = (BukkitPlayer) inv.getPlayer();
2019-12-09 20:43:53 +01:00
InventoryView opened = bp.player.getOpenInventory();
ItemStack stack = getItem(item);
if (stack == null) {
return false;
}
2019-12-09 20:43:53 +01:00
if (!inv.isOpen()) {
return true;
2019-12-09 20:43:53 +01:00
}
opened.setItem(index, stack);
2019-12-09 20:43:53 +01:00
bp.player.updateInventory();
return true;
}
@SuppressWarnings("deprecation") // Paper deprecation
2019-12-09 20:43:53 +01:00
public PlotItemStack getItem(ItemStack item) {
if (item == null) {
return null;
}
// int id = item.getTypeId();
Material id = item.getType();
ItemMeta meta = item.getItemMeta();
int amount = item.getAmount();
String name = null;
String[] lore = null;
if (item.hasItemMeta()) {
assert meta != null;
if (meta.hasDisplayName()) {
name = meta.getDisplayName();
}
if (meta.hasLore()) {
List<String> itemLore = meta.getLore();
assert itemLore != null;
lore = itemLore.toArray(new String[0]);
}
}
return new PlotItemStack(id.name(), amount, name, lore);
}
@Override
2021-05-01 18:33:02 +02:00
public PlotItemStack[] getItems(PlotPlayer<?> player) {
2019-12-09 20:43:53 +01:00
BukkitPlayer bp = (BukkitPlayer) player;
PlayerInventory inv = bp.player.getInventory();
return IntStream.range(0, 36).mapToObj(i -> getItem(inv.getItem(i)))
.toArray(PlotItemStack[]::new);
2019-04-24 22:11:24 +02:00
}
2019-12-09 20:43:53 +01:00
@SuppressWarnings("deprecation") // #getTitle is needed for Spigot compatibility
@Override
public boolean isOpen(PlotInventory plotInventory) {
2019-12-09 20:43:53 +01:00
if (!plotInventory.isOpen()) {
return false;
}
2020-08-17 15:17:40 +02:00
BukkitPlayer bp = (BukkitPlayer) plotInventory.getPlayer();
2019-12-09 20:43:53 +01:00
InventoryView opened = bp.player.getOpenInventory();
if (plotInventory.isOpen()) {
if (opened.getType() == InventoryType.CRAFTING) {
opened.getTitle();
}
}
return false;
}
}