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 @SuppressWarnings("deprecation") // Paper deprecation 048 private static @Nullable ItemStack getItem(PlotItemStack item) { 049 if (item == null) { 050 return null; 051 } 052 Material material = BukkitAdapter.adapt(item.getType()); 053 if (material == null) { 054 return null; 055 } 056 ItemStack stack = new ItemStack(material, item.getAmount()); 057 ItemMeta meta = null; 058 if (item.getName() != null) { 059 meta = stack.getItemMeta(); 060 Component nameComponent = BukkitUtil.MINI_MESSAGE.deserialize(item.getName()); 061 meta.setDisplayName(BukkitUtil.LEGACY_COMPONENT_SERIALIZER.serialize(nameComponent)); 062 } 063 if (item.getLore() != null) { 064 if (meta == null) { 065 meta = stack.getItemMeta(); 066 } 067 List<String> lore = new ArrayList<>(); 068 for (String entry : item.getLore()) { 069 lore.add(BukkitUtil.LEGACY_COMPONENT_SERIALIZER.serialize(BukkitUtil.MINI_MESSAGE.deserialize(entry))); 070 } 071 meta.setLore(lore); 072 } 073 if (meta != null) { 074 stack.setItemMeta(meta); 075 } 076 return stack; 077 } 078 079 @SuppressWarnings("deprecation") // Paper deprecation 080 @Override 081 public void open(PlotInventory inv) { 082 BukkitPlayer bp = (BukkitPlayer) inv.getPlayer(); 083 Inventory inventory = Bukkit.createInventory(null, inv.getLines() * 9, 084 ChatColor.translateAlternateColorCodes('&', inv.getTitle()) 085 ); 086 PlotItemStack[] items = inv.getItems(); 087 for (int i = 0; i < inv.getLines() * 9; i++) { 088 PlotItemStack item = items[i]; 089 if (item != null) { 090 inventory.setItem(i, getItem(item)); 091 } 092 } 093 bp.player.openInventory(inventory); 094 } 095 096 @Override 097 public void close(PlotInventory inv) { 098 if (!inv.isOpen()) { 099 return; 100 } 101 BukkitPlayer bp = (BukkitPlayer) inv.getPlayer(); 102 bp.player.closeInventory(); 103 } 104 105 @Override 106 public boolean setItemChecked(PlotInventory inv, int index, PlotItemStack item) { 107 BukkitPlayer bp = (BukkitPlayer) inv.getPlayer(); 108 InventoryView opened = bp.player.getOpenInventory(); 109 ItemStack stack = getItem(item); 110 if (stack == null) { 111 return false; 112 } 113 if (!inv.isOpen()) { 114 return true; 115 } 116 opened.setItem(index, stack); 117 bp.player.updateInventory(); 118 return true; 119 } 120 121 @SuppressWarnings("deprecation") // Paper deprecation 122 public PlotItemStack getItem(ItemStack item) { 123 if (item == null) { 124 return null; 125 } 126 // int id = item.getTypeId(); 127 Material id = item.getType(); 128 ItemMeta meta = item.getItemMeta(); 129 int amount = item.getAmount(); 130 String name = null; 131 String[] lore = null; 132 if (item.hasItemMeta()) { 133 assert meta != null; 134 if (meta.hasDisplayName()) { 135 name = meta.getDisplayName(); 136 } 137 if (meta.hasLore()) { 138 List<String> itemLore = meta.getLore(); 139 assert itemLore != null; 140 lore = itemLore.toArray(new String[0]); 141 } 142 } 143 return new PlotItemStack(id.name(), amount, name, lore); 144 } 145 146 @Override 147 public PlotItemStack[] getItems(PlotPlayer<?> player) { 148 BukkitPlayer bp = (BukkitPlayer) player; 149 PlayerInventory inv = bp.player.getInventory(); 150 return IntStream.range(0, 36).mapToObj(i -> getItem(inv.getItem(i))) 151 .toArray(PlotItemStack[]::new); 152 } 153 154 @SuppressWarnings("deprecation") // #getTitle is needed for Spigot compatibility 155 @Override 156 public boolean isOpen(PlotInventory plotInventory) { 157 if (!plotInventory.isOpen()) { 158 return false; 159 } 160 BukkitPlayer bp = (BukkitPlayer) plotInventory.getPlayer(); 161 InventoryView opened = bp.player.getOpenInventory(); 162 if (plotInventory.isOpen()) { 163 if (opened.getType() == InventoryType.CRAFTING) { 164 opened.getTitle(); 165 } 166 } 167 return false; 168 } 169 170}