Timtower

Untitled

Feb 7th, 2014
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.core.mayodwarf.pokeballers.shop;
  2.  
  3. import net.milkbowl.vault.economy.Economy;
  4.  
  5. import org.bukkit.ChatColor;
  6. import org.bukkit.block.Sign;
  7. import org.bukkit.command.Command;
  8. import org.bukkit.command.CommandSender;
  9. import org.bukkit.configuration.file.FileConfiguration;
  10. import org.bukkit.event.EventHandler;
  11. import org.bukkit.event.Listener;
  12. import org.bukkit.event.inventory.InventoryAction;
  13. import org.bukkit.event.inventory.InventoryClickEvent;
  14. import org.bukkit.event.player.PlayerInteractEvent;
  15. import org.bukkit.inventory.ItemStack;
  16. import org.bukkit.inventory.meta.ItemMeta;
  17. import org.bukkit.plugin.RegisteredServiceProvider;
  18. import org.bukkit.plugin.java.JavaPlugin;
  19. import org.bukkit.entity.Player;
  20.  
  21. import java.util.List;
  22.  
  23. /**
  24.  * Created with IntelliJ IDEA.
  25.  * User: MayoDwarf
  26.  * Date: 1/31/14
  27.  * Time: 5:27 PM
  28.  * To change this template use File | Settings | File Templates.
  29.  */
  30.  
  31. @SuppressWarnings("unchecked")
  32. public class Main extends JavaPlugin implements Listener {
  33.     public FileConfiguration config;
  34.     public static Economy economy= null;
  35.     private Shop shop;
  36.     public void onEnable() {
  37.         this.saveDefaultConfig();
  38.         shop = new Shop(this);
  39.         getServer().getPluginManager().registerEvents(this, this);
  40.         setupEconomy();
  41.     }
  42.     public void onDisable() {}
  43.     /* [1/31/14, 7:43:44 PM] [Developer] Jared: You type /shop and it opens up a gui with different shops you guys can make with commands. To add the items you do
  44.      * "/shop <name> add <price> <buy or sell>" and that will add the item in your hand with right amount and enchantments and stuff and
  45.      * if it is buy or sell with the price you specify.
  46.      *
  47.      * [1/31/14, 7:44:56 PM] [Developer] Jared: Add a new shop "/shop new <name>".
  48.      * They wouldn't be able to use a shop without the permission "Shop.use.<name of shop>"
  49.      */
  50.     public boolean setupEconomy()
  51.     {
  52.         RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
  53.         if (economyProvider != null) {
  54.             economy = economyProvider.getProvider();
  55.         }
  56.  
  57.         return (economy != null);
  58.     }
  59.     public boolean onCommand (CommandSender sender, Command cmd, String commandLabel, String[] args) {
  60.         Player p = (Player) sender;
  61.         if(cmd.getName().equalsIgnoreCase("setLore")) {
  62.             if(p.hasPermission("Shops.setlore")) {
  63.                 StringBuilder sb = new StringBuilder("");
  64.                 for(String strings : args) {
  65.                     sb.append(strings).append(" ");
  66.                 }
  67.                 String words = sb.toString();
  68.                 shop.setLore(p.getItemInHand(), words, p);
  69.             }
  70.         }
  71.         if (cmd.getName().equalsIgnoreCase("shop")) {
  72.             if (args.length == 0) {
  73.                 if (p.hasPermission("Shops.Admin")) {
  74.                     shop.openShopMenu(p);
  75.                 }
  76.             }
  77.             if (args.length == 1) {
  78.                 //If the args is a shop name
  79.                 List<String> list = (List<String>) getConfig().getList("Shops");
  80.                 for (String strings : list) {
  81.                     if (args[0].equals(strings)) {
  82.                         //Open up the args[0] shop
  83.                         if (p.hasPermission("Shops.use."+strings)) {
  84.                             shop.openShop(args[0], (Player)sender);
  85.                         }
  86.                     }
  87.                 }
  88.                 if (args[0].equalsIgnoreCase("reload")) {
  89.                     if (p.hasPermission("Shops.Admin")) {
  90.                         shop.reloadCfg();
  91.                         p.sendMessage(ChatColor.RED+"The config has been reloaded for "+ChatColor.GOLD+"PokeballersShop"+ChatColor.RED+"!");
  92.                     }
  93.                 }
  94.                 if (args[0].equalsIgnoreCase("output")) {
  95.                     shop.outPut(p.getItemInHand());
  96.                     p.sendMessage(ChatColor.RED+"The output for "+ChatColor.GOLD+""+p.getItemInHand().getType().toString()+ChatColor.RED+" has been added!");
  97.                 }
  98.             }
  99.             if (args.length == 3) {
  100.                 if (args[0].equalsIgnoreCase("new")) {
  101.                     shop.newShop(args[1], p, args[2], p.getItemInHand());
  102.                 }
  103.             }
  104.             if (args.length == 3 || args.length == 4) {
  105.                 if (args[0].equalsIgnoreCase("add")) {
  106.                     if (p.hasPermission("Shops.Admin")) {
  107.                         p.sendMessage(ChatColor.RED+"Invalid arguments! Correct: ./shop add <shop> <buy/sell> <price> <spot>");
  108.                     }
  109.                 }
  110.             }
  111.             if (args.length == 5) {
  112.                 if (p.hasPermission("Shops.Admin")) {
  113.                     if (args[0].equalsIgnoreCase("add")) {
  114.                         if (args[2].equalsIgnoreCase("buy") || args[2].equalsIgnoreCase("sell")) {
  115.                             if (getConfig().getList("Shops").contains(args[1])) {
  116.                                 shop.add(p.getItemInHand(), p, args[3], args[1], args[2], args[4]);
  117.                                 //shop add <shop> <buy/sell> <price> <spot>
  118.                                 //     0    1      2          3       4
  119.                             }
  120.                         }
  121.                     }
  122.                 }
  123.             }
  124.         }
  125.         return true;
  126.     }
  127.     @EventHandler
  128.     public void onInteract(PlayerInteractEvent evt) {
  129.         if (evt.getClickedBlock().getState() instanceof Sign) {
  130.             Sign sign = (Sign) evt.getClickedBlock().getState();
  131.             if (ChatColor.stripColor(sign.getLine(0)).equals("PokeShop")) {
  132.                 List<String> list = (List<String>) getConfig().get("Shops");
  133.                 for (String strings : list) {
  134.                     if (ChatColor.stripColor(sign.getLine(1)).equalsIgnoreCase(strings)) {
  135.                         if (evt.getPlayer().hasPermission("Shops.use."+ChatColor.stripColor(sign.getLine(1)))) {
  136.                             evt.getPlayer().performCommand("shop "+ChatColor.stripColor(sign.getLine(1)));
  137.                         }
  138.                     }
  139.                 }
  140.             }
  141.         }
  142.     }
  143.  
  144.  
  145.  
  146.  
  147.     @EventHandler
  148.     public void onClick(InventoryClickEvent evt) {
  149.         Player p = (Player) evt.getWhoClicked();
  150.         if (evt.getInventory().getTitle().toString().equalsIgnoreCase(ChatColor.AQUA+"Shops")) {
  151.             //Shop gui
  152.             evt.setCancelled(true);
  153.             p.closeInventory();
  154.             evt.setCancelled(true);
  155.             shop.openShop(evt.getCurrentItem().getItemMeta().getDisplayName().toString(), p);
  156.         }
  157.         List<String> shops = (List<String>) getConfig().get("Shops");
  158.         for(String strings : shops) {
  159.             if (ChatColor.stripColor(evt.getInventory().getTitle().toString()).equalsIgnoreCase(strings)) {
  160.                 ItemStack item = evt.getCurrentItem();
  161.                 ItemMeta im = item.getItemMeta();    evt.setCancelled(true);
  162.                 if(im !=null) {
  163.                     String priceC = ChatColor.stripColor(im.getDisplayName());
  164.                     String priceD = priceC.replace("$", "");
  165.                     Integer price = Integer.parseInt(priceD);
  166.                     if(im.getLore().get(0) !=null) {
  167.                         String typeC = ChatColor.stripColor(im.getLore().get(0).toString());
  168.                         evt.setCancelled(true);
  169.                         if (typeC.equalsIgnoreCase("buy")) {
  170.                             ItemStack returnItem = null;
  171.                             for(ItemStack items: (List<ItemStack>) this.getConfig().getList("Outputs")){
  172.                                 if(items.getType()==item.getType() && items.getDurability()==item.getDurability()){
  173.                                     returnItem = items.clone();
  174.                                     break;
  175.                                 }
  176.                             }
  177.                             if(returnItem==null){       //No item found
  178.                                 return;
  179.                             }
  180.                             int amount;
  181.                             switch(evt.getAction()){
  182.                             case PICKUP_ALL: amount = 1;
  183.                             case PICKUP_HALF: amount = 16;
  184.                             default:
  185.                                 amount = 1;
  186.                             }
  187.                             if(!economy.has(p.getName(), price*amount)){
  188.                                 p.sendMessage("You are a broke man nigga");
  189.                                 return;
  190.                             }
  191.                             returnItem.setAmount(amount);
  192.                             p.getInventory().addItem(returnItem);
  193.  
  194.                             /*if(economy.getBalance(p.getName()) >= price) {
  195.                                 if(evt.getAction() == InventoryAction.PICKUP_ALL) {
  196.                                     //Give them 1 of item
  197.                                     for(ItemStack items : (List<ItemStack>) getConfig().getList("Outputs")) {
  198.                                         if(!(items.getType() == item.getType()) && !(items.getDurability() == item.getDurability())) {
  199.                                             p.getInventory().addItem(new ItemStack(item.getType(), 1, item.getDurability()));
  200.                                             economy.withdrawPlayer(p.getName(), price);
  201.                                             evt.setCancelled(true);
  202.                                             break;
  203.                                         } else if(items.getType() == item.getType() && items.getDurability() == item.getDurability()) {
  204.                                             //Output adding 1
  205.                                             List<ItemStack> outputs = (List<ItemStack>) getConfig().get("Outputs");
  206.                                             for(ItemStack itemss : outputs) {
  207.                                                 if(itemss.getType() == item.getType() && itemss.getDurability() == item.getDurability()) {
  208.                                                     //Do stuff
  209.                                                     p.getInventory().addItem(itemss);
  210.                                                     evt.setCancelled(true);
  211.                                                     economy.withdrawPlayer(p.getName(), price);
  212.                                                 }
  213.                                             }
  214.                                         }
  215.                                     }
  216.                                 } else if(evt.getAction() == InventoryAction.PICKUP_HALF) {
  217.                                     //Give them 16 of item
  218.                                     for(ItemStack items : (List<ItemStack>) getConfig().getList("Outputs")) {
  219.                                         if(!(items.getType() == item.getType()) && !(items.getDurability() == item.getDurability())) {
  220.                                             for(int j=0;j<16;j++) {
  221.                                                 p.getInventory().addItem(item);
  222.                                             }
  223.                                             economy.withdrawPlayer(p.getName(), price*16);
  224.                                             evt.setCancelled(true);
  225.                                             break;
  226.                                         } else if(item.getType() == items.getType() && item.getDurability() == items.getDurability()) {
  227.                                             //Output adding 16
  228.                                             List<ItemStack> outputs = (List<ItemStack>) getConfig().get("Outputs");
  229.                                             for(ItemStack itemss : outputs) {
  230.                                                 if(itemss.getType() == item.getType() && itemss.getDurability() == item.getDurability()) {
  231.                                                     //Do stuff
  232.                                                     for(int i=0;i<16;i++) {
  233.                                                         p.getInventory().addItem(itemss);
  234.                                                     }
  235.                                                     evt.setCancelled(true);
  236.                                                     economy.withdrawPlayer(p.getName(), price*16);
  237.                                                 }
  238.                                             }
  239.                                         }
  240.                                     }
  241.                                 }
  242.                             } else {
  243.                                 p.sendMessage(ChatColor.RED+"You do not have enough cash for this item!"); p.closeInventory(); evt.setCancelled(true);
  244.                             }*/
  245.                         } else
  246.                             if (typeC.equalsIgnoreCase("sell")) {
  247.                                 if (p.getInventory().contains(new ItemStack(item.getType(), item.getDurability()))) {
  248.                                     if(evt.getAction() == InventoryAction.PICKUP_ALL) {
  249.                                         //Take 1
  250.                                         p.getInventory().removeItem(item);
  251.                                         evt.setCancelled(true);
  252.                                         economy.depositPlayer(p.getName(), price);
  253.                                     } else if(evt.getAction() == InventoryAction.PICKUP_HALF) {
  254.                                         //Take 16 of them
  255.                                         for(int d=0;d<16;d++) {
  256.                                             p.getInventory().removeItem(item);
  257.                                         }
  258.                                         evt.setCancelled(true);
  259.                                         economy.depositPlayer(p.getName(), price*16);
  260.                                     }
  261.                                 } else {
  262.                                     p.sendMessage(ChatColor.RED+"You do not have enough of this item to sell!"); p.closeInventory();
  263.                                 }
  264.                             }
  265.                     }
  266.                 }
  267.             }
  268.         }
  269.     }
  270. }
Advertisement
Add Comment
Please, Sign In to add comment