Guest User

Untitled

a guest
Feb 7th, 2014
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.15 KB | None | 0 0
  1. package com.core.mayodwarf.pokeballers.shop;
  2.  
  3. import net.milkbowl.vault.economy.Economy;
  4. import org.bukkit.ChatColor;
  5. import org.bukkit.Material;
  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 javax.sound.midi.SysexMessage;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24.  
  25. /**
  26.  * Created with IntelliJ IDEA.
  27.  * User: MayoDwarf
  28.  * Date: 1/31/14
  29.  * Time: 5:27 PM
  30.  * To change this template use File | Settings | File Templates.
  31.  */
  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(args).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.                     if(economy.getBalance(p.getName()) >= price) {
  171.                         if(evt.getAction() == InventoryAction.PICKUP_ALL) {
  172.                             //Give them 1 of item
  173.                             for(ItemStack items : (List<ItemStack>) getConfig().getList("Outputs")) {
  174.                             if(!(items.getType() == item.getType()) && !(items.getDurability() == item.getDurability())) {
  175.                             p.getInventory().addItem(new ItemStack(item.getType(), 1, item.getDurability()));
  176.                             economy.withdrawPlayer(p.getName(), price);
  177.                             evt.setCancelled(true);
  178.                             break;
  179.                             } else if(items.getType() == item.getType() && items.getDurability() == item.getDurability()) {
  180.                                 //Output adding 1
  181.                                 List<ItemStack> outputs = (List<ItemStack>) getConfig().get("Outputs");
  182.                                 for(ItemStack itemss : outputs) {
  183.                                     if(itemss.getType() == item.getType() && itemss.getDurability() == item.getDurability()) {
  184.                                         //Do stuff
  185.                                         p.getInventory().addItem(itemss);
  186.                                         evt.setCancelled(true);
  187.                                         economy.withdrawPlayer(p.getName(), price);
  188.                                         }
  189.                                     }
  190.                                 }
  191.                             }
  192.                         } else if(evt.getAction() == InventoryAction.PICKUP_HALF) {
  193.                             //Give them 16 of item
  194.                             for(ItemStack items : (List<ItemStack>) getConfig().getList("Outputs")) {
  195.                             if(!(items.getType() == item.getType()) && !(items.getDurability() == item.getDurability())) {
  196.                                 for(int j=0;j<16;j++) {
  197.                                     p.getInventory().addItem(item);
  198.                                 }
  199.                             economy.withdrawPlayer(p.getName(), price*16);
  200.                             evt.setCancelled(true);
  201.                             break;
  202.                             } else if(item.getType() == items.getType() && item.getDurability() == items.getDurability()) {
  203.                                 //Output adding 16
  204.                                 List<ItemStack> outputs = (List<ItemStack>) getConfig().get("Outputs");
  205.                                 for(ItemStack itemss : outputs) {
  206.                                     if(itemss.getType() == item.getType() && itemss.getDurability() == item.getDurability()) {
  207.                                         //Do stuff
  208.                                         for(int i=0;i<16;i++) {
  209.                                         p.getInventory().addItem(itemss);
  210.                                         }
  211.                                         evt.setCancelled(true);
  212.                                         economy.withdrawPlayer(p.getName(), price*16);
  213.                                         }
  214.                                     }
  215.                                 }
  216.                             }
  217.                         }
  218.                     } else {
  219.                         p.sendMessage(ChatColor.RED+"You do not have enough cash for this item!"); p.closeInventory(); evt.setCancelled(true);
  220.                     }
  221.                 } else
  222.                     if (typeC.equalsIgnoreCase("sell")) {
  223.                         if (p.getInventory().contains(new ItemStack(item.getType(), item.getDurability()))) {
  224.                             if(evt.getAction() == InventoryAction.PICKUP_ALL) {
  225.                                 //Take 1
  226.                                     p.getInventory().removeItem(item);
  227.                                     evt.setCancelled(true);
  228.                                     economy.depositPlayer(p.getName(), price);
  229.                             } else if(evt.getAction() == InventoryAction.PICKUP_HALF) {
  230.                                 //Take 16 of them
  231.                                 for(int d=0;d<16;d++) {
  232.                                 p.getInventory().removeItem(item);
  233.                                 }
  234.                                 evt.setCancelled(true);
  235.                                 economy.depositPlayer(p.getName(), price*16);
  236.                             }
  237.                                 } else {
  238.                             p.sendMessage(ChatColor.RED+"You do not have enough of this item to sell!"); p.closeInventory();
  239.                         }
  240.                             }
  241.                         }
  242.                     }
  243.             }
  244.         }
  245.     }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment