Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.86 KB | None | 0 0
  1. package dqu.coupons;
  2.  
  3. import java.io.File;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.List;
  7.  
  8. import org.bukkit.Bukkit;
  9. import org.bukkit.ChatColor;
  10. import org.bukkit.Material;
  11. import org.bukkit.command.Command;
  12. import org.bukkit.command.CommandSender;
  13. import org.bukkit.configuration.file.FileConfiguration;
  14. import org.bukkit.configuration.file.YamlConfiguration;
  15. import org.bukkit.entity.Player;
  16. import org.bukkit.event.EventHandler;
  17. import org.bukkit.event.Listener;
  18. import org.bukkit.event.player.PlayerInteractEvent;
  19. import org.bukkit.event.player.PlayerJoinEvent;
  20. import org.bukkit.inventory.ItemStack;
  21. import org.bukkit.inventory.meta.ItemMeta;
  22. import org.bukkit.plugin.java.JavaPlugin;
  23.  
  24. public class Main extends JavaPlugin implements Listener
  25. {
  26.    
  27.     public int total = 1;
  28.     public FileConfiguration config = getConfig();
  29.     public File configfile = new File(getDataFolder(), "config.yml");
  30.  
  31.     @Override
  32.     public void onEnable()
  33.     {
  34.         this.getConfig().options().copyDefaults(false);
  35.         this.saveDefaultConfig();
  36.         this.getServer().getPluginManager().registerEvents(this, this);
  37.         this.getLogger().info("Plugin has been enabled!");
  38.         countTotal();
  39.     }
  40.  
  41.     @Override
  42.     public void onDisable()
  43.     {
  44.         this.getLogger().info("Plugin has been disabled!");
  45.     }
  46.    
  47.     public void AddToQueue(String playername, String value)
  48.     {
  49.         String config_data=(config.getString("Queue"));
  50.        
  51.         if(!config_data.contains(playername))
  52.         {
  53.             config_data += playername + ":" + value + ";";
  54.            
  55.             config.set("Queue", config_data);
  56.        
  57.             this.saveConfig();
  58.         };
  59.        
  60.         return;
  61.     };
  62.    
  63.     public void countTotal()
  64.     {
  65.         String yaml = "Setup";
  66.         String yaml_path = "Setup";
  67.         while ( yaml != null )
  68.         {
  69.             yaml_path = ("Coupons." + total + ".name");
  70.             yaml = config.getString(yaml_path);
  71.             total++;
  72.         }
  73.         total -= 2;
  74.     }
  75.    
  76.     public void RemoveFromQueue(String playername)
  77.     {
  78.         ArrayList<String> config_data = new ArrayList<>();
  79.  
  80.         config_data.addAll(Arrays.asList(config.getString("Queue").split(";")));
  81.  
  82.         ArrayList<String> players = new ArrayList<>();
  83.        
  84.         for(int config_index = 0; config_index < config_data.size(); config_index += 1)
  85.             players.add(config_data.get(config_index).split(":")[0]);
  86.  
  87.        
  88.         if(players.contains(playername))
  89.         {
  90.             int index_of_player = (players.indexOf(playername));
  91.            
  92.             if(index_of_player > -1)
  93.             {
  94.                 config_data.remove(index_of_player);
  95.        
  96.                 String config_reformatted = "";
  97.        
  98.                 for(int part = 0; part < config_data.size(); part += 1)
  99.                     config_reformatted += (config_data.get(part) + ";");
  100.            
  101.                 config.set("Queue", config_reformatted);
  102.        
  103.                 this.saveConfig();
  104.             };
  105.         };
  106.        
  107.         return;
  108.     };
  109.    
  110.     @Override
  111.     public boolean onCommand(CommandSender sender, Command command, String alias, String[] args)
  112.     {
  113.         if (alias.equalsIgnoreCase("coupons"))
  114.         {
  115.             if (!sender.hasPermission("dqucoupons.coupons"))
  116.             {
  117.                 sender.sendMessage(ChatColor.DARK_RED + "You dont have permission to run this command.");
  118.                 return true;
  119.             }
  120.             if (args.length == 0)
  121.             {
  122.                 sender.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Coupons" + ChatColor.DARK_GRAY + "]" + ChatColor.LIGHT_PURPLE + " Help for the coupons:");
  123.                 sender.sendMessage(ChatColor.AQUA + "/coupons give <id> [player]" + ChatColor.YELLOW + " Give someone a coupon");
  124.                 sender.sendMessage(ChatColor.AQUA + "/coupons queue" + ChatColor.YELLOW + " List of players in the queue");
  125.                 sender.sendMessage(ChatColor.AQUA + "/coupons queue remove <player>" + ChatColor.YELLOW + " Delete player from the queue");
  126.                 sender.sendMessage(ChatColor.AQUA + "/coupons reload" + ChatColor.YELLOW + " Reload the config file");
  127.                 return true;
  128.             }
  129.             if (args[0].equalsIgnoreCase("give"))
  130.             {
  131.                 if (args.length < 2)
  132.                 {
  133.                     sender.sendMessage(ChatColor.DARK_RED + "Invalid command. Type '/coupons' for help.");
  134.                     return true;
  135.                 }
  136.                 String name_path = ("Coupons." + args[1].toString() + ".name");
  137.                 String name = config.getString(name_path);
  138.                 if (name != null && !name.isEmpty())
  139.                 {
  140.                 } else
  141.                 {
  142.                     sender.sendMessage(ChatColor.DARK_RED + "Incorrect ID!");
  143.                     System.out.println("Incorrect ID!");
  144.                     return true;
  145.                 }
  146.                 name = ChatColor.translateAlternateColorCodes('&', name);
  147.                 if (args.length > 2)
  148.                 {
  149.                     Player player = Bukkit.getPlayerExact(args[2]);
  150.                     if (player == null)
  151.                     {
  152.                         AddToQueue(args[2], args[1]);
  153.                         sender.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Coupons" + ChatColor.DARK_GRAY + "]" + ChatColor.LIGHT_PURPLE + " This player is not online, added to the queue!");
  154.                         System.out.println("This player is not online, added to the queue.");
  155.                         return true;
  156.                     }
  157.                     Material material = Material.PAPER;
  158.                     ItemStack item = new ItemStack(material);
  159.                     ItemMeta meta = item.getItemMeta();
  160.                     meta.setDisplayName(name);
  161.                     item.setItemMeta(meta);
  162.                     player.getInventory().addItem(item);
  163.                     sender.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Coupons" + ChatColor.DARK_GRAY + "]" + ChatColor.LIGHT_PURPLE + " Gave this player a coupon!");
  164.                     System.out.println("Gave this player an item!");
  165.                     return true;
  166.                 } else
  167.                 {
  168.                     if ( sender instanceof Player )
  169.                     {
  170.                         Player player = (Player) sender;
  171.                         Material material = Material.PAPER;
  172.                         ItemStack item = new ItemStack(material);
  173.                         ItemMeta meta = item.getItemMeta();
  174.                         meta.setDisplayName(name);
  175.                         item.setItemMeta(meta);
  176.                         player.getInventory().addItem(item);
  177.                         sender.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Coupons" + ChatColor.DARK_GRAY + "]" + ChatColor.LIGHT_PURPLE + " Gave you an coupon!");
  178.                         return true;
  179.                     } else
  180.                     {
  181.                         System.out.println("Invalid command. Type /coupons for help.");
  182.                         return true;
  183.                     }
  184.                 }
  185.             }
  186.            
  187.             if (args[0].equalsIgnoreCase("queue"))
  188.             {
  189.                 if (args.length == 1)
  190.                 {
  191.                     List<String> raw_buffer = Arrays.asList (config.getString("Queue").split(";"));
  192.  
  193.                     ArrayList<String> players = new ArrayList<>();
  194.                     ArrayList<String> values = new ArrayList<>();
  195.  
  196.                     for(String player : raw_buffer)
  197.                     {
  198.                         if(player.contains(":"))
  199.                         {
  200.                             players.add(player.split(":")[0]);
  201.                             values.add(player.split(":")[1]);
  202.                         }
  203.                     }
  204.                    
  205.                     sender.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Coupons" + ChatColor.DARK_GRAY + "]" + ChatColor.LIGHT_PURPLE + " List of the queue: \n");
  206.  
  207.                     for(int player_id = 0; player_id < players.size(); player_id += 1)
  208.                     {
  209.                         String playername = players.get(player_id);
  210.                         String coupon_value = values.get(player_id);
  211.                        
  212.                         String player_stats = (ChatColor.LIGHT_PURPLE + playername + ": " + coupon_value + "\n");
  213.                         sender.sendMessage(player_stats);
  214.                         System.out.println(player_stats);
  215.                     };
  216.                     return true;
  217.                 }
  218.                 if (args[1].equalsIgnoreCase("remove"))
  219.                 {
  220.                     if (args.length < 2)
  221.                     {
  222.                         sender.sendMessage(ChatColor.DARK_RED + "Invalid command. Type /coupons for help.");
  223.                         System.out.println("Invalid command. Type /coupons for help.");
  224.                         return true;
  225.                     }
  226.                     RemoveFromQueue(args[2]);
  227.                     sender.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Coupons" + ChatColor.DARK_GRAY + "]" + ChatColor.LIGHT_PURPLE + " Removed player " + args[2] + " from the queue!");
  228.                     return true;
  229.                 }
  230.                 sender.sendMessage(ChatColor.DARK_RED + "Invalid command. Type /coupons for help.");
  231.                 System.out.println("Invalid command. Type /coupons for help.");
  232.                 return true;
  233.             }
  234.            
  235.             if (args[0].equalsIgnoreCase("reload"))
  236.             {
  237.                 config = YamlConfiguration.loadConfiguration(configfile);
  238.                 countTotal();
  239.                 sender.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Coupons" + ChatColor.DARK_GRAY + "]" + ChatColor.LIGHT_PURPLE + "Reloaded config!");
  240.                 System.out.println("Config reloaded");
  241.                 return true;
  242.             }
  243.            
  244.             sender.sendMessage(ChatColor.DARK_RED + "Invalid command. Type '/coupons' for help.");
  245.             System.out.println("Invalid command. Type /coupons for help.");
  246.             return true;
  247.         }
  248.         return true;
  249.     }
  250.  
  251.     @EventHandler
  252.     public void onPlayerInteraction(PlayerInteractEvent e)
  253.     {
  254.         ItemStack item = e.getItem();
  255.  
  256.         if((item == null) || (item.getType() == Material.AIR))
  257.             return;
  258.  
  259.         if(!item.hasItemMeta())
  260.             return;
  261.  
  262.         ItemMeta item_meta = item.getItemMeta();
  263.  
  264.         if(!item_meta.hasDisplayName())
  265.             return;
  266.  
  267.         String item_display_name = item_meta.getDisplayName();
  268.        
  269.         if(item.getType() == Material.PAPER)
  270.         {
  271.             for ( int i = 1 ; i <= total; i++ )
  272.             {
  273.                 String path = ("Coupons." + i + ".name");
  274.                 String name = config.getString(path);
  275.                 name = ChatColor.translateAlternateColorCodes('&', name);
  276.                 if(item_display_name.equalsIgnoreCase(name))
  277.                 {
  278.                     String cmd_path = ("Coupons." + i + ".cmd");
  279.                     String acmd = config.getString(cmd_path);
  280.                     if (acmd != null)
  281.                     {
  282.                         String cmd = acmd.replace("{player}", e.getPlayer().getName());
  283.                         e.getPlayer().getInventory().remove(item);
  284.                         Bukkit.getServer().dispatchCommand((CommandSender)Bukkit.getConsoleSender(), cmd);
  285.                     }
  286.                     break;
  287.                 }
  288.             }
  289.             return;
  290.         }
  291.         return;
  292.     }
  293.  
  294.     @EventHandler
  295.     public void onJoin(PlayerJoinEvent e)
  296.     {
  297.         String name = e.getPlayer().getName();
  298.         List<String> raw_buffer = Arrays.asList (config.getString("Queue").split(";"));
  299.  
  300.         ArrayList<String> players = new ArrayList<>();
  301.         ArrayList<String> values = new ArrayList<>();
  302.  
  303.         for(String player : raw_buffer)
  304.         {
  305.             if(player.contains(":"))
  306.             {
  307.                 players.add(player.split(":")[0]);
  308.                 values.add(player.split(":")[1]);
  309.             }
  310.         }
  311.  
  312.         for(int player_id = 0; player_id < players.size(); player_id += 1)
  313.         {
  314.             String playername = players.get(player_id);
  315.             String coupon_value = values.get(player_id);
  316.            
  317.             if (playername.equals(name))
  318.             {
  319.                 System.out.println("This player is in the queue!");
  320.                 String name_path = ("Coupons." + coupon_value + ".name");
  321.                 String iname = config.getString(name_path);
  322.                 Material material = Material.PAPER;
  323.                 ItemStack item = new ItemStack(material);
  324.                 ItemMeta meta = item.getItemMeta();
  325.                 meta.setDisplayName(iname);
  326.                 item.setItemMeta(meta);
  327.                 e.getPlayer().getInventory().addItem(item);
  328.                 e.getPlayer().sendMessage(ChatColor.GRAY + "[" + ChatColor.YELLOW + "Coupons" + ChatColor.DARK_GRAY + "]" + ChatColor.LIGHT_PURPLE + "Gave you an coupon!");
  329.                 RemoveFromQueue(playername);
  330.                 return;
  331.             }
  332.         }
  333.         System.out.println("This player isnt in the queue!");
  334.         return;
  335.     }
  336.    
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement