Cool_boy21

Shop

May 13th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.98 KB | None | 0 0
  1. package ua.Coolboy.QuartzDefenders.Shop;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.ChatColor;
  8. import org.bukkit.Material;
  9. import org.bukkit.configuration.ConfigurationSection;
  10. import org.bukkit.configuration.file.FileConfiguration;
  11. import org.bukkit.enchantments.Enchantment;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.inventory.Inventory;
  14. import org.bukkit.inventory.ItemFlag;
  15. import org.bukkit.inventory.ItemStack;
  16. import org.bukkit.inventory.Merchant;
  17. import org.bukkit.inventory.MerchantRecipe;
  18. import org.bukkit.inventory.meta.EnchantmentStorageMeta;
  19. import org.bukkit.inventory.meta.ItemMeta;
  20. import ua.Endertainment.QuartzDefenders.GameTeam;
  21. import ua.Endertainment.QuartzDefenders.QuartzDefenders;
  22. import ua.Endertainment.QuartzDefenders.Utils.FilesUtil;
  23. import ua.Endertainment.QuartzDefenders.Utils.ItemUtil;
  24.  
  25. public class Shop {
  26.  
  27.     QuartzDefenders plugin;
  28.     Inventory main;
  29.     FilesUtil files;
  30.     ItemUtil item = new ItemUtil();
  31.  
  32.     String name, stuffName, resourcesName, potionsName, enchantName, otherName, foodName, blocksName;
  33.  
  34.     public Shop(QuartzDefenders plugin) {
  35.         this.plugin = plugin;
  36.         this.files = plugin.getConfigs();
  37.         this.name = files.getLang().getString("shop.name").replace('&', '§');
  38.         this.stuffName = files.getLang().getString("shop.stuff").replace('&', '§');
  39.         this.resourcesName = files.getLang().getString("shop.resources").replace('&', '§');
  40.         this.potionsName = files.getLang().getString("shop.potions").replace('&', '§');
  41.         this.enchantName = files.getLang().getString("shop.enchant").replace('&', '§');
  42.         this.otherName = files.getLang().getString("shop.other").replace('&', '§');
  43.         this.foodName = files.getLang().getString("shop.food").replace('&', '§');
  44.         this.blocksName = files.getLang().getString("shop.blocks").replace('&', '§');
  45.  
  46.         this.main = Bukkit.createInventory(null, 27, ChatColor.GOLD + name);
  47.  
  48.         ItemStack enchant = new ItemStack(Material.EXP_BOTTLE);
  49.         enchant = item.setName(enchant, enchantName);
  50.         ItemStack potions = new ItemStack(Material.POTION);
  51.         potions = item.hideAll(item.setName(potions, potionsName));
  52.         ItemStack food = new ItemStack(Material.BREAD);
  53.         food = item.setName(food, foodName);
  54.         ItemStack other = new ItemStack(Material.NAME_TAG);
  55.         other = item.setName(other, otherName);
  56.         ItemStack blocks = new ItemStack(Material.BRICK);
  57.         blocks = item.setName(blocks, blocksName);
  58.         ItemStack stuff = new ItemStack(Material.ARROW);
  59.         stuff = item.setName(stuff, stuffName);
  60.         ItemStack resources = new ItemStack(Material.DIAMOND_PICKAXE);
  61.         resources = item.hideAll(item.setName(resources, resourcesName));
  62.  
  63.         ItemStack frame = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 15);
  64.         frame = item.setName(frame, " ");
  65.  
  66.         main.setItem(10, enchant);
  67.         main.setItem(11, potions);
  68.         main.setItem(12, food);
  69.         main.setItem(13, other);
  70.         main.setItem(14, blocks);
  71.         main.setItem(15, stuff);
  72.         main.setItem(16, resources);
  73.  
  74.         int[] g = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26};
  75.         for (int z : g) {
  76.             main.setItem(z, frame);
  77.         }
  78.     }
  79.  
  80.     public Inventory getInventory() {
  81.         return main;
  82.     }
  83.  
  84.     public Merchant getSection(String sectionName, String merchantName/*, GameTeam team*/) {
  85.         FileConfiguration shop = files.getShopInfo();
  86.         ConfigurationSection value = shop.getConfigurationSection(sectionName);
  87.         List<MerchantRecipe> recipes = new ArrayList();
  88.         for (String strng : value.getKeys(false)) {
  89.             ConfigurationSection section = value.getConfigurationSection(strng);
  90.             ConfigurationSection primary = section.getConfigurationSection("primary");
  91.             ConfigurationSection result = section.getConfigurationSection("result");
  92.  
  93.             Boolean resultTeam = false;
  94.             int resultDamage = 0;
  95.  
  96.             String primaryMaterial = primary.getString("material");
  97.             int primaryAmount = primary.getInt("amount");
  98.             int primaryDamage = primary.getInt("damage");
  99.             ItemStack primaryStack = new ItemStack(Material.valueOf(primaryMaterial), primaryAmount, (short) primaryDamage);
  100.  
  101.             ItemStack optionalStack = null;
  102.             if (section.isConfigurationSection("optional")) {
  103.                 ConfigurationSection optional = section.getConfigurationSection("optional");
  104.                 String optionalMaterial = optional.getString("material");
  105.                 int optionalAmount = optional.getInt("amount");
  106.                 int optionalDamage = optional.getInt("damage");
  107.                 optionalStack = new ItemStack(Material.valueOf(optionalMaterial), optionalAmount, (short) optionalDamage);
  108.             }
  109.  
  110.             String resultMaterial = result.getString("material");
  111.             int resultAmount = result.getInt("amount");
  112.             if (result.isBoolean("team")) {
  113.                 resultTeam = result.getBoolean("team");
  114.                 resultDamage = 5;//getDamageByColor(team.getColor());
  115.             }
  116.             if (!resultTeam) {
  117.                 resultDamage = result.getInt("damage");
  118.             }
  119.             ItemStack resultStack = new ItemStack(Material.valueOf(resultMaterial), resultAmount, (short) resultDamage);
  120.             if (result.isString("name")) {
  121.                 String resultName = result.getString("name");
  122.                 resultStack = item.setName(resultStack, resultName);
  123.             }
  124.             if (result.isList("lore")) {
  125.                 List<String> resultLore = result.getStringList("lore");
  126.                 resultStack = item.setLore(resultStack, resultLore);
  127.             }
  128.  
  129.             if (result.isConfigurationSection("enchantments")) {
  130.                 Map<String, Object> enchant = result.getConfigurationSection("enchantments").getValues(false);
  131.                 for (Map.Entry<String, Object> entry : enchant.entrySet()) {
  132.                     Enchantment ench = Enchantment.getByName(entry.getKey());
  133.                     int enchLvl = (int) entry.getValue();
  134.                     resultStack.addEnchantment(ench, enchLvl);
  135.                 }
  136.             }
  137.             if (resultMaterial.equals("ENCHANTED_BOOK")) {
  138.                 Map<String, Object> bookEnchant = result.getConfigurationSection("bookEnchant").getValues(false);
  139.                 EnchantmentStorageMeta meta = (EnchantmentStorageMeta) resultStack.getItemMeta();
  140.                 for (Map.Entry<String, Object> entry : bookEnchant.entrySet()) {
  141.                     Enchantment ench = Enchantment.getByName(entry.getKey());
  142.                     int enchLvl = (int) entry.getValue();
  143.                     meta.addStoredEnchant(ench, enchLvl, true);
  144.                 }
  145.                 resultStack.setItemMeta(meta);
  146.             }
  147.             MerchantRecipe recipe = new MerchantRecipe(resultStack, 999);
  148.             recipe.addIngredient(primaryStack);
  149.             if (optionalStack != null) {
  150.                 recipe.addIngredient(optionalStack);
  151.             }
  152.             recipes.add(recipe);
  153.         }
  154.         Merchant m = Bukkit.createMerchant(merchantName);
  155.         m.setRecipes(recipes);
  156.         return m;
  157.     }
  158.  
  159.     public Integer getDamageByColor(ChatColor color) {
  160.         switch (color) {
  161.             case RED:
  162.                 return 14;
  163.             case BLUE:
  164.                 return 11;
  165.             case GREEN:
  166.                 return 13;
  167.             case YELLOW:
  168.                 return 4;
  169.             case DARK_PURPLE:
  170.                 return 10;
  171.             case AQUA:
  172.                 return 3;
  173.             case DARK_GRAY:
  174.                 return 7;
  175.             case WHITE:
  176.                 return 0;
  177.         }
  178.         return 0;
  179.     }
  180.     private void bcast(String text) {
  181.         Bukkit.getServer().broadcastMessage(text);
  182.     }
  183. }
Add Comment
Please, Sign In to add comment