Advertisement
Exception_Prototype

Untitled

Aug 3rd, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.36 KB | None | 0 0
  1. //ShopItem
  2. public class ShopItem {
  3.  
  4.     private final ItemStack item;
  5.     private ItemStack[] price;
  6.     private final int slot;
  7.  
  8.     public ShopItem(ItemStack item, ItemStack[] price, int slot) {
  9.         this.item = item;
  10.         this.price = price;
  11.         this.slot = slot;
  12.     }
  13.  
  14.     public ShopItem(NBTTagCompound nbtTagCompound) {
  15.         net.minecraft.server.v1_12_R1.ItemStack originalItem = new net.minecraft.server.v1_12_R1.ItemStack(nbtTagCompound.getCompound("ShopItem"));
  16.         this.item = CraftItemStack.asBukkitCopy(originalItem);
  17.  
  18.         NBTTagList nbtPrice = nbtTagCompound.getList("ShopPrice", 10);
  19.         this.price = new ItemStack[9];
  20.         for (int i = 0; i < this.price.length; i++) {
  21.             NBTTagCompound part = nbtPrice.get(i);
  22.             if (part.isEmpty()) {
  23.                 price[i] = new ItemStack(Material.AIR);
  24.             } else {
  25.                 originalItem = new net.minecraft.server.v1_12_R1.ItemStack(part);
  26.                 price[i] = CraftItemStack.asBukkitCopy(originalItem);
  27.             }
  28.         }
  29.  
  30.         this.slot = nbtTagCompound.getInt("ShopSlot");
  31.     }
  32.  
  33.     public void saveShopItem(NBTTagCompound nbtTagCompound) {
  34.         NBTTagCompound nbtItem = new NBTTagCompound();
  35.         CraftItemStack craft = ShopNBT.getCraftVersion(this.item);
  36.         CraftItemStack.asNMSCopy(craft).save(nbtItem);
  37.  
  38.         NBTTagList nbtPrice = new NBTTagList();
  39.         for (ItemStack item : this.price) {
  40.             NBTTagCompound part = new NBTTagCompound();
  41.             if (item != null) {
  42.                 if (item.getType() != Material.AIR) {
  43.                     craft = ShopNBT.getCraftVersion(item);
  44.                     CraftItemStack.asNMSCopy(craft).save(part);
  45.                 }
  46.             }
  47.             nbtPrice.add(part);
  48.         }
  49.  
  50.         nbtTagCompound.set("ShopItem", nbtItem);
  51.         nbtTagCompound.set("ShopPrice", nbtPrice);
  52.         nbtTagCompound.setInt("ShopSlot", this.slot);
  53.     }
  54.  
  55.     public ItemStack getItem() {
  56.         return this.item;
  57.     }
  58.  
  59.     public ItemStack[] getPrice() {
  60.         return this.price;
  61.     }
  62.  
  63.     public void setPrice(ItemStack[] price) {
  64.         this.price = price;
  65.     }
  66.  
  67.     public int getSlot() {
  68.         return this.slot;
  69.     }
  70.  
  71.     @Override
  72.     public boolean equals(Object obj) {
  73.         if (this == obj) {
  74.             return true;
  75.         }
  76.         if (obj == null) {
  77.             return false;
  78.         }
  79.         if (getClass() != obj.getClass()) {
  80.             return false;
  81.         }
  82.         final ShopItem other = (ShopItem) obj;
  83.         return Objects.equals(this.item, other.item) && Arrays.deepEquals(this.price, other.price);
  84.     }
  85.  
  86.     @Override
  87.     public int hashCode() {
  88.         int hash = 7;
  89.         hash = 11 * hash + Objects.hashCode(this.item);
  90.         hash = 11 * hash + Arrays.deepHashCode(this.price);
  91.         return hash;
  92.     }
  93.  
  94. }
  95.  
  96. //Это восстановление ShopItem
  97.     public static Inventory loadShop() {
  98.         File shopFile = new File(folder + File.separator + "shop.dat");
  99.         if (!shopFile.exists()) {
  100.             LOGGER.log(Level.SEVERE, CONSOLE_PREFIX + "Could not load shop from file. File does not exist.");
  101.             return null;
  102.         }
  103.         NBTTagCompound nbtShop = loadNBTTagFromFile(shopFile);
  104.         if (nbtShop == null) {
  105.             return null;
  106.         }
  107.  
  108.         NBTTagList nbtItems = nbtShop.getList("Shop", 10);
  109.         Inventory shop = new CraftInventoryCustom(null, QDShop.inst().getSettings().getSlots(), "Магазин");
  110.         ShopItem shopItem;
  111.         for (int i = 0; i < shop.getSize(); i++) {
  112.             if (i < nbtItems.size()) {
  113.                 shopItem = new ShopItem(nbtItems.get(i));
  114.                 QDShop.inst().getShopManager().getShopItems().add(shopItem);
  115.                 shop.setItem(shopItem.getSlot(), shopItem.getItem());
  116.             } else {
  117.                 break;
  118.             }
  119.         }
  120.  
  121.         return shop;
  122.     }
  123.  
  124. //Это методы установки цены для ShopItem. Я включаю режим "установки цены" и клацаю по предмету в магазе, после чего этот метод ставит цену для указанного предмета с нотбара.
  125.     public void setPrice(Player p, ItemStack stack, int slot) {
  126.         ItemStack[] contents = p.getInventory().getContents();
  127.         boolean b = false;
  128.         for (int i = 0; i < 9; i++) {
  129.             if (contents[i] != null) {
  130.                 if (contents[i].getType() != Material.AIR) {
  131.                     b = true;
  132.                 }
  133.             }
  134.         }
  135.  
  136.         if (b) {
  137.             ItemStack[] price = new ItemStack[9];
  138.             System.arraycopy(contents, 0, price, 0, 9);
  139.             ShopItem shopItem = getShopItem(stack);
  140.             if (shopItem != null) {
  141.                 shopItem.setPrice(price);
  142.                 p.sendMessage(CHAT_PREFIX + ChatColor.GREEN + "Обновлён старый ShopItem.");
  143.             } else {
  144.                 shopItem = new ShopItem(stack, price, slot);
  145.                 shopItems.add(shopItem);
  146.                 p.sendMessage(CHAT_PREFIX + ChatColor.GREEN + "Добавлен новый ShopItem.");
  147.             }
  148.         } else {
  149.             p.sendMessage(CHAT_PREFIX + ChatColor.DARK_RED + "Предметов для установки цены нет.");
  150.         }
  151.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement