001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.bukkit.util; 020 021import com.google.inject.Singleton; 022import com.plotsquared.bukkit.player.BukkitPlayer; 023import com.plotsquared.core.player.PlotPlayer; 024import com.plotsquared.core.plot.PlotInventory; 025import com.plotsquared.core.plot.PlotItemStack; 026import com.plotsquared.core.util.InventoryUtil; 027import com.sk89q.worldedit.bukkit.BukkitAdapter; 028import net.kyori.adventure.text.Component; 029import org.bukkit.Bukkit; 030import org.bukkit.ChatColor; 031import org.bukkit.Material; 032import org.bukkit.event.inventory.InventoryType; 033import org.bukkit.inventory.Inventory; 034import org.bukkit.inventory.InventoryView; 035import org.bukkit.inventory.ItemStack; 036import org.bukkit.inventory.PlayerInventory; 037import org.bukkit.inventory.meta.ItemMeta; 038import org.checkerframework.checker.nullness.qual.Nullable; 039 040import java.util.ArrayList; 041import java.util.List; 042import java.util.stream.IntStream; 043 044@Singleton 045public class BukkitInventoryUtil extends InventoryUtil { 046 047 private static @Nullable ItemStack getItem(PlotItemStack item) { 048 if (item == null) { 049 return null; 050 } 051 Material material = BukkitAdapter.adapt(item.getType()); 052 if (material == null) { 053 return null; 054 } 055 ItemStack stack = new ItemStack(material, item.getAmount()); 056 ItemMeta meta = null; 057 if (item.getName() != null) { 058 meta = stack.getItemMeta(); 059 Component nameComponent = BukkitUtil.MINI_MESSAGE.parse(item.getName()); 060 meta.setDisplayName(BukkitUtil.LEGACY_COMPONENT_SERIALIZER.serialize(nameComponent)); 061 } 062 if (item.getLore() != null) { 063 if (meta == null) { 064 meta = stack.getItemMeta(); 065 } 066 List<String> lore = new ArrayList<>(); 067 for (String entry : item.getLore()) { 068 lore.add(BukkitUtil.LEGACY_COMPONENT_SERIALIZER.serialize(BukkitUtil.MINI_MESSAGE.deserialize(entry))); 069 } 070 meta.setLore(lore); 071 } 072 if (meta != null) { 073 stack.setItemMeta(meta); 074 } 075 return stack; 076 } 077 078 @SuppressWarnings("deprecation") // Paper deprecation 079 @Override 080 public void open(PlotInventory inv) { 081 BukkitPlayer bp = (BukkitPlayer) inv.getPlayer(); 082 Inventory inventory = Bukkit.createInventory(null, inv.getLines() * 9, 083 ChatColor.translateAlternateColorCodes('&', inv.getTitle()) 084 ); 085 PlotItemStack[] items = inv.getItems(); 086 for (int i = 0; i < inv.getLines() * 9; i++) { 087 PlotItemStack item = items[i]; 088 if (item != null) { 089 inventory.setItem(i, getItem(item)); 090 } 091 } 092 bp.player.openInventory(inventory); 093 } 094 095 @Override 096 public void close(PlotInventory inv) { 097 if (!inv.isOpen()) { 098 return; 099 } 100 BukkitPlayer bp = (BukkitPlayer) inv.getPlayer(); 101 bp.player.closeInventory(); 102 } 103 104 @Override 105 public boolean setItemChecked(PlotInventory inv, int index, PlotItemStack item) { 106 BukkitPlayer bp = (BukkitPlayer) inv.getPlayer(); 107 InventoryView opened = bp.player.getOpenInventory(); 108 ItemStack stack = getItem(item); 109 if (stack == null) { 110 return false; 111 } 112 if (!inv.isOpen()) { 113 return true; 114 } 115 opened.setItem(index, stack); 116 bp.player.updateInventory(); 117 return true; 118 } 119 120 @SuppressWarnings("deprecation") // Paper deprecation 121 public PlotItemStack getItem(ItemStack item) { 122 if (item == null) { 123 return null; 124 } 125 // int id = item.getTypeId(); 126 Material id = item.getType(); 127 ItemMeta meta = item.getItemMeta(); 128 int amount = item.getAmount(); 129 String name = null; 130 String[] lore = null; 131 if (item.hasItemMeta()) { 132 assert meta != null; 133 if (meta.hasDisplayName()) { 134 name = meta.getDisplayName(); 135 } 136 if (meta.hasLore()) { 137 List<String> itemLore = meta.getLore(); 138 assert itemLore != null; 139 lore = itemLore.toArray(new String[0]); 140 } 141 } 142 return new PlotItemStack(id.name(), amount, name, lore); 143 } 144 145 @Override 146 public PlotItemStack[] getItems(PlotPlayer<?> player) { 147 BukkitPlayer bp = (BukkitPlayer) player; 148 PlayerInventory inv = bp.player.getInventory(); 149 return IntStream.range(0, 36).mapToObj(i -> getItem(inv.getItem(i))) 150 .toArray(PlotItemStack[]::new); 151 } 152 153 @SuppressWarnings("deprecation") // #getTitle is needed for Spigot compatibility 154 @Override 155 public boolean isOpen(PlotInventory plotInventory) { 156 if (!plotInventory.isOpen()) { 157 return false; 158 } 159 BukkitPlayer bp = (BukkitPlayer) plotInventory.getPlayer(); 160 InventoryView opened = bp.player.getOpenInventory(); 161 if (plotInventory.isOpen()) { 162 if (opened.getType() == InventoryType.CRAFTING) { 163 opened.getTitle(); 164 } 165 } 166 return false; 167 } 168 169}