Guest User

Kae's Tag Source Code

a guest
May 28th, 2020
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.62 KB | None | 0 0
  1. package me.KaeFiverr.Tag;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Random;
  9.  
  10. import org.bukkit.Bukkit;
  11. import org.bukkit.ChatColor;
  12. import org.bukkit.GameMode;
  13. import org.bukkit.Material;
  14. import org.bukkit.World;
  15. import org.bukkit.boss.BarColor;
  16. import org.bukkit.boss.BarStyle;
  17. import org.bukkit.boss.BossBar;
  18. import org.bukkit.command.Command;
  19. import org.bukkit.command.CommandSender;
  20. import org.bukkit.configuration.file.FileConfiguration;
  21. import org.bukkit.entity.Player;
  22. import org.bukkit.event.EventHandler;
  23. import org.bukkit.event.EventPriority;
  24. import org.bukkit.event.Listener;
  25. import org.bukkit.event.entity.EntityDamageByEntityEvent;
  26. import org.bukkit.event.inventory.InventoryClickEvent;
  27. import org.bukkit.event.player.PlayerChangedWorldEvent;
  28. import org.bukkit.event.player.PlayerGameModeChangeEvent;
  29. import org.bukkit.event.player.PlayerInteractEvent;
  30. import org.bukkit.event.player.PlayerQuitEvent;
  31. import org.bukkit.inventory.Inventory;
  32. import org.bukkit.inventory.ItemStack;
  33. import org.bukkit.inventory.meta.ItemMeta;
  34. import org.bukkit.plugin.java.JavaPlugin;
  35. import org.bukkit.potion.PotionEffect;
  36. import org.bukkit.potion.PotionEffectType;
  37.  
  38. public class main extends JavaPlugin implements Listener {
  39.  
  40.     Map<String, Boolean> tagged = new HashMap<String, Boolean>();
  41.     Map<String, Boolean> worlds = new HashMap<String, Boolean>();
  42.     BossBar bar = Bukkit.createBossBar("", BarColor.BLUE, BarStyle.SOLID);
  43.     public Inventory inv1;
  44.     public Inventory inv2;
  45.     public String tagworld;
  46.     public String[] worldNames = new String[Bukkit.getServer().getWorlds().size()];
  47.     FileConfiguration config = getConfig();
  48.     public String prefix = (ChatColor.AQUA + config.getString("commandprefix"));
  49.    
  50.     @Override
  51.     public void onEnable() { //Run when server starts
  52.         this.getServer().getPluginManager().registerEvents(this, this); //Required for listeners
  53.         this.saveDefaultConfig();
  54.         createInv();
  55.         config.addDefault("world", "''");
  56.         config.addDefault("griefDisabled", true);
  57.         config.addDefault("changegamemode", true);
  58.         config.addDefault("speedboost", false);
  59.         config.addDefault("commandprefix", "[Kaes Tag]");
  60.         config.addDefault("taggedtitle", "You have been tagged!");
  61.         config.addDefault("taggedsubtitle", "Run and tag someone else!");
  62.         config.addDefault("jointagtitle", "You have joined tag!");
  63.         config.options().copyDefaults(true);
  64.     }
  65.    
  66.     @Override
  67.     public void onDisable() {
  68.  
  69.     }
  70.    
  71.     public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { //Listen for commands
  72.  
  73.         if (label.equalsIgnoreCase("tagsetup") && (sender.hasPermission("KaePlace.tag.admin"))) { //Tag Setup Command
  74.             if(!(sender instanceof Player)) {  
  75.                 sender.sendMessage("You cannot do this!");
  76.                 return true;
  77.             }
  78.            
  79.             Player target = (Player) sender;
  80.             // open the GUI
  81.             target.openInventory(inv1);
  82.             return true;
  83.         }
  84.         if (label.equalsIgnoreCase("tagreload") && (sender.hasPermission("KaePlace.tag.admin"))) { //Tag Reload Command
  85.             this.reloadConfig();
  86.             config = getConfig(); //this is necessary if you're like me and don't want to write "this.getConfig();" all the damn time.
  87.             this.saveDefaultConfig();
  88.             prefix = (ChatColor.AQUA + config.getString("commandprefix"));
  89.             sender.sendMessage(prefix + ChatColor.WHITE + " Config reloaded!");
  90.         }
  91.         return false;
  92.     }
  93.    
  94.     @EventHandler //Tag logic
  95.     public void onHit(EntityDamageByEntityEvent event) {
  96.         if (event.getEntity() instanceof Player && event.getDamager() instanceof Player && event.getEntity().getWorld().getName().equalsIgnoreCase((config.getString("world")))) {
  97.             Player whoWasHit = (Player) event.getEntity();
  98.             Player whoHit = (Player) event.getDamager();
  99.             event.setCancelled(true);
  100.             if (tagged.get(whoHit.getName()) == true) {
  101.                 TagPlayer(whoWasHit);
  102.                 if (config.getBoolean("speedboost")) {
  103.                     whoHit.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 200, 3));
  104.                 }
  105.             }
  106.         }  
  107.     }
  108.    
  109.     @EventHandler(priority = EventPriority.HIGHEST) //Grief Prevention
  110.     public void onInteract(PlayerInteractEvent event) {
  111.         Player target = (Player) event.getPlayer();
  112.         String worldname = target.getWorld().getName();
  113.         List<String> allowedItems = Arrays.asList("OAK_DOOR", "SPRUCE_DOOR", "BIRCH_DOOR", "JUNGLE_DOOR", "ACACIA_DOOR", "DARK_OAK_DOOR");
  114.        
  115.         if (worldname.equalsIgnoreCase( (config.getString("world"))) && (!(allowedItems.contains(event.getClickedBlock().getBlockData().getMaterial().toString())))) {
  116.             if (config.getBoolean("griefdisabled")) {
  117.                 event.setCancelled(true);
  118.                 return;
  119.             }
  120.         }
  121.         return;
  122.     }
  123.    
  124.    
  125.     @EventHandler // Prevents all game mode changes from Multiverse or otherwise.
  126.     public void player(PlayerGameModeChangeEvent event) {
  127.        
  128.         Player target = (Player) event.getPlayer();
  129.         String worldname = target.getWorld().getName();
  130.         GameMode gamemode = target.getGameMode();
  131.        
  132.         if (gamemode == GameMode.SURVIVAL && worldname.equalsIgnoreCase((config.getString("world"))) && config.getBoolean("changegamemode")) {
  133.             event.setCancelled(true);
  134.             return;
  135.         }
  136.     }
  137.    
  138.    
  139.     @EventHandler () // Whenever players change their current worlds
  140.     public void player(PlayerChangedWorldEvent event) {
  141.        
  142.         Player target = (Player) event.getPlayer();
  143.         String worldname = target.getWorld().getName();
  144.        
  145.         if (worldname.equalsIgnoreCase((config.getString("world")))) {
  146.             PlayerJoins(target);
  147.         } else {
  148.             PlayerLeaves(target);
  149.         }  
  150.     }
  151.    
  152.     @EventHandler () // Removes players from game on Server Disconnect
  153.     public void player(PlayerQuitEvent event) {
  154.         Player target = (Player) event.getPlayer();
  155.         PlayerLeaves(target);
  156.     }
  157.  
  158.    
  159.     @EventHandler() //Menus! Making it look a bit better. Java isnt my thing though
  160.     public void onClick(InventoryClickEvent event) {
  161.         if (event.getInventory().equals(inv1)) {
  162.             if (event.getCurrentItem() == null) return;
  163.             if (event.getCurrentItem().getItemMeta() == null) return;
  164.             if (event.getCurrentItem().getItemMeta().getDisplayName() == null) return;
  165.        
  166.             event.setCancelled(true);
  167.        
  168.             Player target = (Player) event.getWhoClicked();
  169.        
  170.             config.set("world", (String.valueOf(worldNames[event.getSlot()])));
  171.             this.saveConfig();
  172.             target.closeInventory();
  173.             target.openInventory(inv2);
  174.         }
  175.        
  176.         if (event.getInventory().equals(inv2)) {
  177.             if (event.getCurrentItem() == null) return;
  178.             if (event.getCurrentItem().getItemMeta() == null) return;
  179.             if (event.getCurrentItem().getItemMeta().getDisplayName() == null) return;
  180.             event.setCancelled(true);
  181.    
  182.             Player target = (Player) event.getWhoClicked();
  183.             switch (event.getSlot()) {
  184.                 case 0:
  185.                     config.set("griefdisabled", (!(config.getBoolean("griefdisabled"))));
  186.                     this.saveConfig();
  187.                     target.sendMessage("Grief prevention is now set to: " + (String.valueOf((config.getBoolean("griefdisabled")))));
  188.                     break;
  189.                 case 1:
  190.                     config.set("changegamemode", (!(config.getBoolean("changegamemode"))));
  191.                     this.saveConfig();
  192.                     target.sendMessage("Change Gamemode is now set to: " + (String.valueOf((config.getBoolean("changegamemode")))));
  193.                     break;
  194.                 case 2:
  195.                     config.set("speedboost", (!(config.getBoolean("speedboost"))));
  196.                     this.saveConfig();
  197.                     target.sendMessage("Speed boost is now set to: " + (String.valueOf((config.getBoolean("speedboost")))));
  198.                     break;
  199.                 case 8:
  200.                     target.closeInventory();
  201.                     this.saveConfig();
  202.                     target.sendMessage("Success! Configuration saved." + ChatColor.DARK_RED + " Have all players leave the world to prevent issues.");
  203.             }
  204.            
  205.         }
  206.  
  207.     }
  208.    
  209.    
  210.     public void createInv() { //Creates the GUI menus!
  211.    
  212.         inv1 = Bukkit.createInventory(null, 54, "Choose your world!"); //multiples of 9
  213.        
  214.         ItemStack item = new ItemStack(Material.BLUE_CONCRETE);
  215.         ItemMeta meta = item.getItemMeta();
  216.        
  217.         int count = 0;
  218.        
  219.         for(World w : Bukkit.getServer().getWorlds()) {
  220.             worldNames[count] = w.getName();
  221.             count++;
  222.         }  
  223.        
  224.         count = 0;
  225.         for(String s : worldNames){
  226.             List<Material> givenList = Arrays.asList(Material.RED_WOOL, Material.ORANGE_WOOL, Material.YELLOW_WOOL, Material.GREEN_WOOL, Material.BLUE_WOOL, Material.PURPLE_WOOL);
  227.             Random rand = new Random();
  228.             Material randomMaterial = givenList.get(rand.nextInt(givenList.size()));
  229.             getInventory(inv1, meta, randomMaterial, org.bukkit.ChatColor.AQUA, s, "Click to set tag on this world!", count);
  230.             count++;
  231.         }
  232.        
  233.         inv2 = Bukkit.createInventory(null, 9, "Final Settings"); //multiples of 9
  234.        
  235.         count = 0;
  236.         for(int i = 0; i < 9; ++i) {
  237.             List<Material> materialList = Arrays.asList(Material.DIRT, Material.PLAYER_HEAD, Material.POTION, Material.WHITE_STAINED_GLASS, Material.WHITE_STAINED_GLASS, Material.WHITE_STAINED_GLASS, Material.WHITE_STAINED_GLASS, Material.WHITE_STAINED_GLASS, Material.BARRIER);
  238.             List<String> nameList = Arrays.asList("Toggle Grief Prevention", "Lock Gamemode To Survival", "Toggle Speed Boost", "", "", "", "", "", "Close Menu");
  239.             List<String> loreList = Arrays.asList("This option will disable/enable the ability to break blocks.", "Prevents commands/plugins from changing gamemode", "Toggle speed boost when tagging someone", "", "", "", "", "", "Closes Menu");
  240.             getInventory(inv2, meta, materialList.get(count), org.bukkit.ChatColor.AQUA, nameList.get(count), loreList.get(count), count);
  241.             count++;
  242.         }
  243.        
  244.        
  245.     }
  246.    
  247.    
  248.     public void PlayerJoins(Player target) {
  249.         target.setGameMode(GameMode.SURVIVAL);
  250.         target.getInventory().clear();
  251.         bar.addPlayer(target);
  252.         target.sendTitle(ChatColor.AQUA + config.getString("jointagtitle"), ChatColor.WHITE + "Current players: " + target.getWorld().getPlayers().size(), 10, 40, 10);
  253.         tagged.put(target.getName(), false);
  254.         if (!(tagged.isEmpty()) && ((!(tagged.containsValue(true))))) {
  255.             AssignRandom();
  256.         }
  257.         return;
  258.     }
  259.    
  260.     public void PlayerLeaves(Player target) {
  261.        
  262.         tagged.remove(target.getName());
  263.         bar.removePlayer(target);
  264.        
  265.         if (!(tagged.isEmpty()) && ((!(tagged.containsValue(true))))) {
  266.             AssignRandom();
  267.         }
  268.         return;
  269.     }
  270.    
  271.     public void AssignRandom() {
  272.        
  273.         String[] keys = tagged.keySet().toArray(new String[tagged.size()]);
  274.         String key = keys[new Random().nextInt(keys.length)];
  275.         TagPlayer(getServer().getPlayer(key));
  276.         return;
  277.    
  278.     }
  279.    
  280.     public void TagPlayer(Player target) {
  281.        
  282.         tagged.replaceAll((key, value) -> false);
  283.         tagged.put(target.getName(), true);
  284.         MessageTagWorld(target.getName() + " has been tagged!");
  285.         bar.setTitle(target.getName() + " has been tagged!");
  286.         bar.removeAll();
  287.         for (Player on : Bukkit.getOnlinePlayers()) {
  288.             if (on.getWorld().getName().equalsIgnoreCase((config.getString("world")))) {
  289.                 bar.addPlayer(on);
  290.             }
  291.         }
  292.        
  293.         target.sendTitle(ChatColor.DARK_RED + config.getString("taggedtitle"), ChatColor.WHITE + config.getString("taggedsubtitle"), 10, 40, 10);
  294.         return;
  295.        
  296.     }
  297.    
  298.     public void MessageTagWorld(String message) {
  299.         for (Player on : Bukkit.getOnlinePlayers()) {
  300.             if (on.getWorld().getName().equalsIgnoreCase((config.getString("world")))) {
  301.                 on.sendMessage(prefix + " " + ChatColor.WHITE + message);
  302.             }
  303.         }
  304.         return;
  305.     }
  306.    
  307.     public static ItemStack getInventory(Inventory inventory, ItemMeta meta, Material material, org.bukkit.ChatColor cc, String string, String string2, int count) {
  308.         ItemStack TempItem = new ItemStack(material);
  309.         meta.setDisplayName(cc + string);
  310.         List<String> lore = new ArrayList<String>();
  311.         lore.add(string2);
  312.         meta.setLore(lore);
  313.         TempItem.setItemMeta(meta);
  314.         inventory.setItem(count, TempItem);
  315.         return TempItem;
  316.     }
  317.    
  318. }
Advertisement
Add Comment
Please, Sign In to add comment