Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dqu.coupons;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.Material;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.player.PlayerInteractEvent;
- import org.bukkit.event.player.PlayerJoinEvent;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.meta.ItemMeta;
- import org.bukkit.plugin.java.JavaPlugin;
- public class Main extends JavaPlugin implements Listener
- {
- public int total = 1;
- public FileConfiguration config = getConfig();
- public File configfile = new File(getDataFolder(), "config.yml");
- @Override
- public void onEnable()
- {
- this.getConfig().options().copyDefaults(false);
- this.saveDefaultConfig();
- this.getServer().getPluginManager().registerEvents(this, this);
- this.getLogger().info("Plugin has been enabled!");
- countTotal();
- }
- @Override
- public void onDisable()
- {
- this.getLogger().info("Plugin has been disabled!");
- }
- public void AddToQueue(String playername, String value)
- {
- String config_data=(config.getString("Queue"));
- if(!config_data.contains(playername))
- {
- config_data += playername + ":" + value + ";";
- config.set("Queue", config_data);
- this.saveConfig();
- };
- return;
- };
- public void countTotal()
- {
- String yaml = "Setup";
- String yaml_path = "Setup";
- while ( yaml != null )
- {
- yaml_path = ("Coupons." + total + ".name");
- yaml = config.getString(yaml_path);
- total++;
- }
- total -= 2;
- }
- public void RemoveFromQueue(String playername)
- {
- ArrayList<String> config_data = new ArrayList<>();
- config_data.addAll(Arrays.asList(config.getString("Queue").split(";")));
- ArrayList<String> players = new ArrayList<>();
- for(int config_index = 0; config_index < config_data.size(); config_index += 1)
- players.add(config_data.get(config_index).split(":")[0]);
- if(players.contains(playername))
- {
- int index_of_player = (players.indexOf(playername));
- if(index_of_player > -1)
- {
- config_data.remove(index_of_player);
- String config_reformatted = "";
- for(int part = 0; part < config_data.size(); part += 1)
- config_reformatted += (config_data.get(part) + ";");
- config.set("Queue", config_reformatted);
- this.saveConfig();
- };
- };
- return;
- };
- @Override
- public boolean onCommand(CommandSender sender, Command command, String alias, String[] args)
- {
- if (alias.equalsIgnoreCase("coupons"))
- {
- if (!sender.hasPermission("dqucoupons.coupons"))
- {
- sender.sendMessage(ChatColor.DARK_RED + "You dont have permission to run this command.");
- return true;
- }
- if (args.length == 0)
- {
- sender.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Coupons" + ChatColor.DARK_GRAY + "]" + ChatColor.LIGHT_PURPLE + " Help for the coupons:");
- sender.sendMessage(ChatColor.AQUA + "/coupons give <id> [player]" + ChatColor.YELLOW + " Give someone a coupon");
- sender.sendMessage(ChatColor.AQUA + "/coupons queue" + ChatColor.YELLOW + " List of players in the queue");
- sender.sendMessage(ChatColor.AQUA + "/coupons queue remove <player>" + ChatColor.YELLOW + " Delete player from the queue");
- sender.sendMessage(ChatColor.AQUA + "/coupons reload" + ChatColor.YELLOW + " Reload the config file");
- return true;
- }
- if (args[0].equalsIgnoreCase("give"))
- {
- if (args.length < 2)
- {
- sender.sendMessage(ChatColor.DARK_RED + "Invalid command. Type '/coupons' for help.");
- return true;
- }
- String name_path = ("Coupons." + args[1].toString() + ".name");
- String name = config.getString(name_path);
- if (name != null && !name.isEmpty())
- {
- } else
- {
- sender.sendMessage(ChatColor.DARK_RED + "Incorrect ID!");
- System.out.println("Incorrect ID!");
- return true;
- }
- name = ChatColor.translateAlternateColorCodes('&', name);
- if (args.length > 2)
- {
- Player player = Bukkit.getPlayerExact(args[2]);
- if (player == null)
- {
- AddToQueue(args[2], args[1]);
- sender.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Coupons" + ChatColor.DARK_GRAY + "]" + ChatColor.LIGHT_PURPLE + " This player is not online, added to the queue!");
- System.out.println("This player is not online, added to the queue.");
- return true;
- }
- Material material = Material.PAPER;
- ItemStack item = new ItemStack(material);
- ItemMeta meta = item.getItemMeta();
- meta.setDisplayName(name);
- item.setItemMeta(meta);
- player.getInventory().addItem(item);
- sender.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Coupons" + ChatColor.DARK_GRAY + "]" + ChatColor.LIGHT_PURPLE + " Gave this player a coupon!");
- System.out.println("Gave this player an item!");
- return true;
- } else
- {
- if ( sender instanceof Player )
- {
- Player player = (Player) sender;
- Material material = Material.PAPER;
- ItemStack item = new ItemStack(material);
- ItemMeta meta = item.getItemMeta();
- meta.setDisplayName(name);
- item.setItemMeta(meta);
- player.getInventory().addItem(item);
- sender.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Coupons" + ChatColor.DARK_GRAY + "]" + ChatColor.LIGHT_PURPLE + " Gave you an coupon!");
- return true;
- } else
- {
- System.out.println("Invalid command. Type /coupons for help.");
- return true;
- }
- }
- }
- if (args[0].equalsIgnoreCase("queue"))
- {
- if (args.length == 1)
- {
- List<String> raw_buffer = Arrays.asList (config.getString("Queue").split(";"));
- ArrayList<String> players = new ArrayList<>();
- ArrayList<String> values = new ArrayList<>();
- for(String player : raw_buffer)
- {
- if(player.contains(":"))
- {
- players.add(player.split(":")[0]);
- values.add(player.split(":")[1]);
- }
- }
- sender.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Coupons" + ChatColor.DARK_GRAY + "]" + ChatColor.LIGHT_PURPLE + " List of the queue: \n");
- for(int player_id = 0; player_id < players.size(); player_id += 1)
- {
- String playername = players.get(player_id);
- String coupon_value = values.get(player_id);
- String player_stats = (ChatColor.LIGHT_PURPLE + playername + ": " + coupon_value + "\n");
- sender.sendMessage(player_stats);
- System.out.println(player_stats);
- };
- return true;
- }
- if (args[1].equalsIgnoreCase("remove"))
- {
- if (args.length < 2)
- {
- sender.sendMessage(ChatColor.DARK_RED + "Invalid command. Type /coupons for help.");
- System.out.println("Invalid command. Type /coupons for help.");
- return true;
- }
- RemoveFromQueue(args[2]);
- sender.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Coupons" + ChatColor.DARK_GRAY + "]" + ChatColor.LIGHT_PURPLE + " Removed player " + args[2] + " from the queue!");
- return true;
- }
- sender.sendMessage(ChatColor.DARK_RED + "Invalid command. Type /coupons for help.");
- System.out.println("Invalid command. Type /coupons for help.");
- return true;
- }
- if (args[0].equalsIgnoreCase("reload"))
- {
- config = YamlConfiguration.loadConfiguration(configfile);
- countTotal();
- sender.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Coupons" + ChatColor.DARK_GRAY + "]" + ChatColor.LIGHT_PURPLE + "Reloaded config!");
- System.out.println("Config reloaded");
- return true;
- }
- sender.sendMessage(ChatColor.DARK_RED + "Invalid command. Type '/coupons' for help.");
- System.out.println("Invalid command. Type /coupons for help.");
- return true;
- }
- return true;
- }
- @EventHandler
- public void onPlayerInteraction(PlayerInteractEvent e)
- {
- ItemStack item = e.getItem();
- if((item == null) || (item.getType() == Material.AIR))
- return;
- if(!item.hasItemMeta())
- return;
- ItemMeta item_meta = item.getItemMeta();
- if(!item_meta.hasDisplayName())
- return;
- String item_display_name = item_meta.getDisplayName();
- if(item.getType() == Material.PAPER)
- {
- for ( int i = 1 ; i <= total; i++ )
- {
- String path = ("Coupons." + i + ".name");
- String name = config.getString(path);
- name = ChatColor.translateAlternateColorCodes('&', name);
- if(item_display_name.equalsIgnoreCase(name))
- {
- String cmd_path = ("Coupons." + i + ".cmd");
- String acmd = config.getString(cmd_path);
- if (acmd != null)
- {
- String cmd = acmd.replace("{player}", e.getPlayer().getName());
- e.getPlayer().getInventory().remove(item);
- Bukkit.getServer().dispatchCommand((CommandSender)Bukkit.getConsoleSender(), cmd);
- }
- break;
- }
- }
- return;
- }
- return;
- }
- @EventHandler
- public void onJoin(PlayerJoinEvent e)
- {
- String name = e.getPlayer().getName();
- List<String> raw_buffer = Arrays.asList (config.getString("Queue").split(";"));
- ArrayList<String> players = new ArrayList<>();
- ArrayList<String> values = new ArrayList<>();
- for(String player : raw_buffer)
- {
- if(player.contains(":"))
- {
- players.add(player.split(":")[0]);
- values.add(player.split(":")[1]);
- }
- }
- for(int player_id = 0; player_id < players.size(); player_id += 1)
- {
- String playername = players.get(player_id);
- String coupon_value = values.get(player_id);
- if (playername.equals(name))
- {
- System.out.println("This player is in the queue!");
- String name_path = ("Coupons." + coupon_value + ".name");
- String iname = config.getString(name_path);
- Material material = Material.PAPER;
- ItemStack item = new ItemStack(material);
- ItemMeta meta = item.getItemMeta();
- meta.setDisplayName(iname);
- item.setItemMeta(meta);
- e.getPlayer().getInventory().addItem(item);
- e.getPlayer().sendMessage(ChatColor.GRAY + "[" + ChatColor.YELLOW + "Coupons" + ChatColor.DARK_GRAY + "]" + ChatColor.LIGHT_PURPLE + "Gave you an coupon!");
- RemoveFromQueue(playername);
- return;
- }
- }
- System.out.println("This player isnt in the queue!");
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement