Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.42 KB | None | 0 0
  1. package com.GlossyPanther.SellAll;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.URL;
  7. import java.text.DecimalFormat;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.logging.Logger;
  11.  
  12. import net.milkbowl.vault.economy.Economy;
  13. import net.milkbowl.vault.permission.Permission;
  14.  
  15. import org.bukkit.Bukkit;
  16. import org.bukkit.ChatColor;
  17. import org.bukkit.Material;
  18. import org.bukkit.Sound;
  19. import org.bukkit.block.Sign;
  20. import org.bukkit.command.Command;
  21. import org.bukkit.command.CommandSender;
  22. import org.bukkit.command.ConsoleCommandSender;
  23. import org.bukkit.entity.Player;
  24. import org.bukkit.event.EventHandler;
  25. import org.bukkit.event.Listener;
  26. import org.bukkit.event.block.SignChangeEvent;
  27. import org.bukkit.event.player.PlayerInteractEvent;
  28. import org.bukkit.event.player.PlayerJoinEvent;
  29. import org.bukkit.inventory.ItemStack;
  30. import org.bukkit.plugin.RegisteredServiceProvider;
  31. import org.bukkit.plugin.java.JavaPlugin;
  32.  
  33. public class Main extends JavaPlugin implements Listener {
  34.  
  35.     public Logger logger = Logger.getLogger("Minecraft");
  36.     public static Economy econ = null;
  37.     public static Permission perms = null;
  38.     public String Gmult = null;
  39.     public String Pmult = null;
  40.     public static List<Player> autopickupPlayers = new ArrayList<Player>();
  41.     public boolean updateAvailable = false;
  42.     DecimalFormat moneyFormat = new DecimalFormat("#.##");
  43.     DecimalFormat multiplierFormat = new DecimalFormat("#.#");
  44.  
  45.     @Override
  46.     public void onDisable() {
  47.         logger.info(String.format("[%s] Disabled Version %s", getDescription().getName(), getDescription().getVersion()));
  48.     }
  49.  
  50.     @Override
  51.     public void onEnable() {
  52.         if (!setupEconomy() ) {
  53.             logger.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
  54.             getServer().getPluginManager().disablePlugin(this);
  55.             return;
  56.         }
  57.         setupPermissions();
  58.         saveDefaultConfig();
  59.         getServer().getPluginManager().registerEvents(this, this);
  60.         if (getServer().getPluginManager().getPlugin("WorldGuard") != null)
  61.         {
  62.             getServer().getPluginManager().registerEvents(new WorldGuardListeners(), this);
  63.             logger.info("Registered WorldGuardListeners");
  64.         }
  65.         else
  66.         {
  67.             getServer().getPluginManager().registerEvents(new NoWorldGuardListeners(), this);
  68.             logger.info("Registered NoWorldGuardListeners");
  69.         }
  70.         try {
  71.             Metrics metrics = new Metrics(this);
  72.             metrics.start();
  73.         } catch (IOException e) {
  74.             logger.warning("Failed to start metrics!");
  75.         }
  76.     }
  77.    
  78.     @Override
  79.     public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
  80.     {
  81.         if (sender instanceof ConsoleCommandSender)
  82.         {
  83.             sender.sendMessage(ChatColor.DARK_RED + "Only players can execute this command!");
  84.             return true;
  85.         }
  86.         Player player = (Player)sender;
  87.         if (cmd.getName().equalsIgnoreCase("sellall"))
  88.         {
  89.             if (args.length == 0 || args.length > 3)
  90.             {
  91.                 player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.GREEN + " Created by GlossyPanther!");
  92.                 player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " View global multiplier" + ChatColor.RED + " /sellall global");
  93.                 player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " View your multiplier" + ChatColor.RED + " /sellall player <player>");
  94.                 if (perms.playerHas(player, "sellall.set"))
  95.                 {
  96.                     player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " Set global multiplier" + ChatColor.RED + " /sellall global <amount>");
  97.                     player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " Set player multiplier" + ChatColor.RED + " /sellall player <player> <amount>");
  98.                     player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " Reload config file" + ChatColor.RED + " /sellall reload");
  99.                     return true;
  100.                 }
  101.             }
  102.             else if (args.length == 1)
  103.             {
  104.                 if (args[0].equalsIgnoreCase("global"))
  105.                 {
  106.                     Double globalMult = getConfig().getDouble("GlobalMultiplier");
  107.                     player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " Global multiplier = " + globalMult);
  108.                     return true;
  109.                 }
  110.                 if (perms.playerHas(player, "sellall.set"))
  111.                 {
  112.                     if (args[0].equalsIgnoreCase("reload"))
  113.                     {
  114.                         try
  115.                         {
  116.                             reloadConfig();
  117.                         }
  118.                         catch (Exception e)
  119.                         {
  120.                             player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_RED + " Failed to reload config!");
  121.                             return true;
  122.                         }
  123.                         player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " Config reloaded!");
  124.                         return true;
  125.                     }
  126.                     else
  127.                     {
  128.                         player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " Set global multiplier" + ChatColor.RED + " /sellall global <amount>");
  129.                         player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " Set player multiplier" + ChatColor.RED + " /sellall player <player> <amount>");
  130.                         player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " Reload config file" + ChatColor.RED + " /sellall reload");
  131.                         return true;
  132.                     }
  133.                 }
  134.                 else
  135.                 {
  136.                     player.sendMessage(ChatColor.DARK_RED + "Tu nevari ta darit!");
  137.                     return true;
  138.                 }
  139.             }
  140.             else if (args.length == 2)
  141.             {
  142.                 if (args[0].equalsIgnoreCase("player"))
  143.                 {
  144.                     if (Bukkit.getPlayerExact(args[1]) != null)
  145.                     {
  146.                         Player playerToFind = Bukkit.getPlayerExact(args[1]);
  147.                         Double playerMult = getConfig().getDouble("PlayerMultipliers." + playerToFind.getUniqueId());
  148.                         player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " " + playerToFind.getDisplayName()+ ChatColor.DARK_BLUE + "'s multiplier = " + playerMult);
  149.                         return true;
  150.                     }
  151.                     else
  152.                     {
  153.                         player.sendMessage("fail");
  154.                         return true;
  155.                     }
  156.                 }
  157.                 if (perms.playerHas(player, "sellall.set"))
  158.                 {
  159.                     if (args[0].equalsIgnoreCase("global"))
  160.                     {
  161.                         Double Gtest = null;
  162.                         try
  163.                         {
  164.                              Gtest = Double.parseDouble(args[1]);
  165.                         }
  166.                         catch (Exception e)
  167.                         {
  168.                             player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " The multiplier you entered is not valid!");
  169.                             return true;
  170.                         }
  171.                         multiplierFormat.setMaximumFractionDigits(1);
  172.                         Gmult = multiplierFormat.format(Gtest);
  173.                         getConfig().set("GlobalMultiplier", Double.parseDouble(Gmult));
  174.                         saveConfig();
  175.                         player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " Global multiplier has been set to " + Gmult + "!");
  176.                         return true;
  177.                     }
  178.                     else
  179.                     {
  180.                         player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " Set global multiplier" + ChatColor.RED + " /sellall global <amount>");
  181.                         player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " Set player multiplier" + ChatColor.RED + " /sellall player <player> <amount>");
  182.                         player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " Reload config file" + ChatColor.RED + " /sellall reload");
  183.                         return true;
  184.                     }
  185.                 }
  186.                 else
  187.                 {
  188.                     player.sendMessage(ChatColor.DARK_RED + "Tu nevari ta darit!");
  189.                     return true;
  190.                 }
  191.             }
  192.             else if (args.length == 3)
  193.             {
  194.                 if (perms.playerHas(player, "sellall.set"))
  195.                 {
  196.                     if (args[0].equalsIgnoreCase("player"))
  197.                     {
  198.                         if (Bukkit.getPlayerExact(args[1]) != null)
  199.                         {
  200.                             Player playerForMult = Bukkit.getPlayerExact(args[1]);
  201.                             Double Ptest = null;
  202.                             try
  203.                             {
  204.                                  Ptest = Double.parseDouble(args[2]);
  205.                             }
  206.                             catch (Exception e)
  207.                             {
  208.                                 player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " The multiplier you entered is not valid!");
  209.                                 return true;
  210.                             }
  211.                             multiplierFormat.setMaximumFractionDigits(1);
  212.                             Pmult = multiplierFormat.format(Ptest);
  213.                             getConfig().set("PlayerMultipliers." + playerForMult.getUniqueId(), Double.parseDouble(Pmult));
  214.                             saveConfig();
  215.                             player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " " + playerForMult.getDisplayName() + ChatColor.DARK_BLUE + "'s multiplier has been set to " + Pmult + "!");
  216.                             return true;
  217.                         }
  218.                         else
  219.                         {
  220.                             player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " That player is not online!");
  221.                             return true;
  222.                         }
  223.                     }
  224.                     else
  225.                     {
  226.                         player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " Set global multiplier" + ChatColor.RED + " /sellall global <amount>");
  227.                         player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " Set player multiplier" + ChatColor.RED + " /sellall player <player> <amount>");
  228.                         player.sendMessage(ChatColor.RED + "[" + ChatColor.DARK_BLUE + "SellAll" + ChatColor.RED + "]" + ChatColor.DARK_BLUE + " Reload config file" + ChatColor.RED + " /sellall reload");
  229.                         return true;
  230.                     }
  231.                 }
  232.                 else
  233.                 {
  234.                     player.sendMessage(ChatColor.DARK_RED + "Tu nevari ta darit!");
  235.                     return true;
  236.                 }
  237.             }
  238.         }
  239.         if (cmd.getName().equalsIgnoreCase("autopickup"))
  240.         {
  241.             if (perms.playerHas(player, "sellall.autopickup"))
  242.             {
  243.                 if (autopickupPlayers.contains(player))
  244.                 {
  245.                     autopickupPlayers.remove(player);
  246.                     player.sendMessage(ChatColor.AQUA + "AutoPickup " + ChatColor.RED + "Disabled!");
  247.                 }
  248.                 else
  249.                 {
  250.                     autopickupPlayers.add(player);
  251.                     player.sendMessage(ChatColor.AQUA + "AutoPickup " + ChatColor.GREEN + "Enabled!");
  252.                 }
  253.                 return true;
  254.             }
  255.             return false;
  256.         }
  257.         return false;
  258.     }
  259.  
  260.     private boolean setupEconomy()
  261.     {
  262.         if (getServer().getPluginManager().getPlugin("Vault") == null)
  263.         {
  264.             return false;
  265.         }
  266.         RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
  267.         if (rsp == null)
  268.         {
  269.             return false;
  270.         }
  271.         econ = rsp.getProvider();
  272.         return econ != null;
  273.     }
  274.    
  275.     private boolean setupPermissions() {
  276.         RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
  277.         perms = rsp.getProvider();
  278.         return perms != null;
  279.     }
  280.    
  281.     @EventHandler
  282.     public void onInteract(PlayerInteractEvent e)
  283.     {
  284.         if (e.getClickedBlock() == null)
  285.             return;
  286.         if (e.getClickedBlock().getType() == Material.SIGN || e.getClickedBlock().getType() == Material.SIGN_POST || e.getClickedBlock().getType() == Material.WALL_SIGN)
  287.         {
  288.             Player player = e.getPlayer();
  289.             Sign sign = (Sign)e.getClickedBlock().getState();
  290.             if ((perms.playerHas(player, "sellall.break") && player.getItemInHand().getType() == Material.TNT) && (sign.getLine(0).equalsIgnoreCase(ChatColor.DARK_BLUE + "[sell all]") || sign.getLine(0).equalsIgnoreCase(ChatColor.RED + "[sell all]")))
  291.             {
  292.                 e.setCancelled(true);
  293.                 e.getClickedBlock().setType(Material.AIR);
  294.                 player.sendMessage(ChatColor.GREEN + "Sign successfully broken!");
  295.                 return;
  296.             }
  297.             if (perms.playerHas(player, "sellall.use") && sign.getLine(0).equalsIgnoreCase(ChatColor.DARK_BLUE + "[sell all]"))
  298.             {
  299.                 e.setCancelled(true);
  300.                 int quantityFound = 0;
  301.                 Material sellingMaterial = Material.getMaterial(sign.getLine(2));
  302.                 for (ItemStack is : player.getInventory())
  303.                 {
  304.                     if (is != null)
  305.                     {
  306.                         if (is.getType() == sellingMaterial)
  307.                             quantityFound += is.getAmount();
  308.                     }
  309.                 }
  310.                 if (quantityFound < 1)
  311.                 {
  312.                     player.sendMessage(ChatColor.RED + "Tev nav neviens " + sellingMaterial.name() + " ko pardot!");
  313.                     return;
  314.                 }
  315.                 else
  316.                 {
  317.                     double pricePer = Double.parseDouble(sign.getLine(3).replace("$","").replace("/par katru", ""));
  318.                     player.getInventory().remove(sellingMaterial);
  319.                     double amountToGive = quantityFound * pricePer;
  320.                     Double globalMult = getConfig().getDouble("GlobalMultiplier");
  321.                     Double playerMult = getConfig().getDouble("PlayerMultipliers." + player.getUniqueId());
  322.                     double newAmountToGive = amountToGive;
  323.                     if (globalMult != 1 && globalMult != 0)
  324.                         newAmountToGive = newAmountToGive * globalMult;
  325.                     if (playerMult != 1 && playerMult != 0)
  326.                         newAmountToGive = newAmountToGive * playerMult;
  327.                     econ.depositPlayer(player, newAmountToGive);
  328.                     player.updateInventory();
  329.                     player.sendMessage(ChatColor.GREEN.toString() + ChatColor.BOLD.toString() + "TU PARDEVI: " + ChatColor.DARK_GREEN.toString() + quantityFound + "x " + sellingMaterial.name() + ChatColor.GREEN.toString() + " par $" + moneyFormat.format(newAmountToGive));
  330.                     return;
  331.                 }
  332.             }
  333.             if (sign.getLine(0).equalsIgnoreCase(ChatColor.RED + "[Sell All]"))
  334.             {
  335.                 e.setCancelled(true);
  336.                 return;
  337.             }
  338.         }
  339.     }
  340.    
  341.     @EventHandler
  342.     public void onSignCreate(SignChangeEvent e)
  343.     {
  344.         Player player = e.getPlayer();
  345.        
  346.         if (e.getLine(0).equalsIgnoreCase("[sell all]"))
  347.         {
  348.             if (perms.playerHas(player, "sellall.create"))
  349.             {
  350.                 Material sellingMaterial = Material.matchMaterial(e.getLine(2));
  351.                 if (sellingMaterial == null)
  352.                 {
  353.                     e.setLine(0, ChatColor.RED + "[Sell All]");
  354.                     player.sendMessage(ChatColor.RED + "Unknown material on line 3!");
  355.                     return;
  356.                 }
  357.                 double sellingAmount = 0;
  358.                 try
  359.                 {
  360.                     sellingAmount = Double.parseDouble(e.getLine(3));
  361.                 }
  362.                 catch (Exception except)
  363.                 {
  364.                     e.setLine(0, ChatColor.RED + "[Sell All]");
  365.                     player.sendMessage(ChatColor.RED + "Unknown price on line 4!");
  366.                     return;
  367.                 }
  368.                 String sellingMaterialLine = sellingMaterial.name();
  369.                 String sellingPriceLine = "$" + moneyFormat.format(sellingAmount) + "/par katru";
  370.                 e.setLine(0, ChatColor.DARK_BLUE + "[Sell All]");
  371.                 e.setLine(1, "pardot visu");
  372.                 e.setLine(2, sellingMaterialLine);
  373.                 e.setLine(3, sellingPriceLine);
  374.                 player.sendMessage(ChatColor.GREEN + "SellAll sign successfully created!");
  375.                 return;
  376.             }
  377.             else
  378.             {
  379.                 player.sendMessage(ChatColor.RED + "Tu nevari ta darit!");
  380.                 e.setCancelled(true);
  381.                 return;
  382.             }
  383.         }
  384.         if (e.getLine(0).equalsIgnoreCase(ChatColor.DARK_BLUE + "[sell all]"))
  385.         {
  386.             player.sendMessage(ChatColor.RED + "This is not the correct way to make a SellAll sign!");
  387.             e.setCancelled(true);
  388.             return;
  389.         }
  390.     }
  391.    
  392.     @EventHandler
  393.     public void onPlayerJoin(PlayerJoinEvent e)
  394.     {
  395.         if (perms.playerHas(e.getPlayer(), "sellall.set"))
  396.         if (!getConfig().isSet("PlayerMultipliers." + e.getPlayer().getUniqueId()))
  397.         {
  398.             getConfig().set("PlayerMultipliers." + e.getPlayer().getUniqueId(), 1.0);
  399.             saveConfig();
  400.         }
  401.     }
  402. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement