Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ShopItem
- public class ShopItem {
- private final ItemStack item;
- private ItemStack[] price;
- private final int slot;
- public ShopItem(ItemStack item, ItemStack[] price, int slot) {
- this.item = item;
- this.price = price;
- this.slot = slot;
- }
- public ShopItem(NBTTagCompound nbtTagCompound) {
- net.minecraft.server.v1_12_R1.ItemStack originalItem = new net.minecraft.server.v1_12_R1.ItemStack(nbtTagCompound.getCompound("ShopItem"));
- this.item = CraftItemStack.asBukkitCopy(originalItem);
- NBTTagList nbtPrice = nbtTagCompound.getList("ShopPrice", 10);
- this.price = new ItemStack[9];
- for (int i = 0; i < this.price.length; i++) {
- NBTTagCompound part = nbtPrice.get(i);
- if (part.isEmpty()) {
- price[i] = new ItemStack(Material.AIR);
- } else {
- originalItem = new net.minecraft.server.v1_12_R1.ItemStack(part);
- price[i] = CraftItemStack.asBukkitCopy(originalItem);
- }
- }
- this.slot = nbtTagCompound.getInt("ShopSlot");
- }
- public void saveShopItem(NBTTagCompound nbtTagCompound) {
- NBTTagCompound nbtItem = new NBTTagCompound();
- CraftItemStack craft = ShopNBT.getCraftVersion(this.item);
- CraftItemStack.asNMSCopy(craft).save(nbtItem);
- NBTTagList nbtPrice = new NBTTagList();
- for (ItemStack item : this.price) {
- NBTTagCompound part = new NBTTagCompound();
- if (item != null) {
- if (item.getType() != Material.AIR) {
- craft = ShopNBT.getCraftVersion(item);
- CraftItemStack.asNMSCopy(craft).save(part);
- }
- }
- nbtPrice.add(part);
- }
- nbtTagCompound.set("ShopItem", nbtItem);
- nbtTagCompound.set("ShopPrice", nbtPrice);
- nbtTagCompound.setInt("ShopSlot", this.slot);
- }
- public ItemStack getItem() {
- return this.item;
- }
- public ItemStack[] getPrice() {
- return this.price;
- }
- public void setPrice(ItemStack[] price) {
- this.price = price;
- }
- public int getSlot() {
- return this.slot;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- final ShopItem other = (ShopItem) obj;
- return Objects.equals(this.item, other.item) && Arrays.deepEquals(this.price, other.price);
- }
- @Override
- public int hashCode() {
- int hash = 7;
- hash = 11 * hash + Objects.hashCode(this.item);
- hash = 11 * hash + Arrays.deepHashCode(this.price);
- return hash;
- }
- }
- //Это восстановление ShopItem
- public static Inventory loadShop() {
- File shopFile = new File(folder + File.separator + "shop.dat");
- if (!shopFile.exists()) {
- LOGGER.log(Level.SEVERE, CONSOLE_PREFIX + "Could not load shop from file. File does not exist.");
- return null;
- }
- NBTTagCompound nbtShop = loadNBTTagFromFile(shopFile);
- if (nbtShop == null) {
- return null;
- }
- NBTTagList nbtItems = nbtShop.getList("Shop", 10);
- Inventory shop = new CraftInventoryCustom(null, QDShop.inst().getSettings().getSlots(), "Магазин");
- ShopItem shopItem;
- for (int i = 0; i < shop.getSize(); i++) {
- if (i < nbtItems.size()) {
- shopItem = new ShopItem(nbtItems.get(i));
- QDShop.inst().getShopManager().getShopItems().add(shopItem);
- shop.setItem(shopItem.getSlot(), shopItem.getItem());
- } else {
- break;
- }
- }
- return shop;
- }
- //Это методы установки цены для ShopItem. Я включаю режим "установки цены" и клацаю по предмету в магазе, после чего этот метод ставит цену для указанного предмета с нотбара.
- public void setPrice(Player p, ItemStack stack, int slot) {
- ItemStack[] contents = p.getInventory().getContents();
- boolean b = false;
- for (int i = 0; i < 9; i++) {
- if (contents[i] != null) {
- if (contents[i].getType() != Material.AIR) {
- b = true;
- }
- }
- }
- if (b) {
- ItemStack[] price = new ItemStack[9];
- System.arraycopy(contents, 0, price, 0, 9);
- ShopItem shopItem = getShopItem(stack);
- if (shopItem != null) {
- shopItem.setPrice(price);
- p.sendMessage(CHAT_PREFIX + ChatColor.GREEN + "Обновлён старый ShopItem.");
- } else {
- shopItem = new ShopItem(stack, price, slot);
- shopItems.add(shopItem);
- p.sendMessage(CHAT_PREFIX + ChatColor.GREEN + "Добавлен новый ShopItem.");
- }
- } else {
- p.sendMessage(CHAT_PREFIX + ChatColor.DARK_RED + "Предметов для установки цены нет.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement