Advertisement
momo5502

Paintball plugin source

Aug 7th, 2013
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 30.72 KB | None | 0 0
  1. // --------------------------------------------------------------------------------------------------------------+
  2. //
  3. // Name: Paintball
  4. //
  5. // Description: Paintball plugin for Bukkit (1.6.2 RC 0.2 )
  6. //
  7. // Initial author: momo5502 <[email protected]>
  8. //
  9. // Version: 1.0.0.0
  10. //
  11. // Credits: redsgreens <[email protected]> for the Snowball spawn and vector to location part
  12. //          Ajaks (Aleksandar Jovanov) for helping and giving advices :D
  13. //          dasg1972 (Yannis Huhle) mental help (beer and chilling ;) ).
  14. //
  15. // --------------------------------------------------------------------------------------------------------------+
  16.  
  17. package us.blackpulse.momo.painball;
  18.  
  19. import java.util.HashMap;
  20. import java.util.Map;
  21.  
  22. import org.bukkit.Bukkit;
  23. import org.bukkit.ChatColor;
  24. import org.bukkit.GameMode;
  25. import org.bukkit.Location;
  26. import org.bukkit.Material;
  27. import org.bukkit.OfflinePlayer;
  28. import org.bukkit.Server;
  29. import org.bukkit.World;
  30. import org.bukkit.command.Command;
  31. import org.bukkit.command.CommandSender;
  32. import org.bukkit.configuration.file.FileConfiguration;
  33. import org.bukkit.entity.Damageable;
  34. import org.bukkit.entity.Entity;
  35. import org.bukkit.entity.Player;
  36. import org.bukkit.entity.Snowball;
  37. import org.bukkit.event.EventHandler;
  38. import org.bukkit.event.EventPriority;
  39. import org.bukkit.event.Listener;
  40. import org.bukkit.event.block.Action;
  41. import org.bukkit.event.entity.EntityDamageByEntityEvent;
  42. import org.bukkit.event.entity.EntityDamageEvent;
  43. import org.bukkit.event.player.PlayerGameModeChangeEvent;
  44. import org.bukkit.event.player.PlayerInteractEvent;
  45. import org.bukkit.inventory.ItemStack;
  46. import org.bukkit.inventory.PlayerInventory;
  47. import org.bukkit.plugin.java.JavaPlugin;
  48. import org.bukkit.scoreboard.DisplaySlot;
  49. import org.bukkit.scoreboard.Objective;
  50. import org.bukkit.scoreboard.Score;
  51. import org.bukkit.scoreboard.Scoreboard;
  52. import org.bukkit.scoreboard.ScoreboardManager;
  53. import org.bukkit.scoreboard.Team;
  54. import org.bukkit.util.Vector;
  55.  
  56. public class main extends JavaPlugin implements Listener
  57. {
  58.  
  59.     // ------------------------------------------------------------------------------------------------------+
  60.     // Global variables
  61.  
  62.     boolean paintballting = false;
  63.     Map<Integer, Player> playerMap = new HashMap<Integer, Player>();
  64.  
  65.     Map<Integer, Player> axisMap = new HashMap<Integer, Player>();
  66.     Map<Integer, Player> alliesMap = new HashMap<Integer, Player>();
  67.  
  68.     Map<Integer, ItemStack[]> invContentMap = new HashMap<Integer, ItemStack[]>();
  69.     Map<Integer, ItemStack[]> invArmorMap = new HashMap<Integer, ItemStack[]>();
  70.  
  71.     Map<Integer, Location> locationMap = new HashMap<Integer, Location>();
  72.  
  73.     int gold_spawns = 0;
  74.     int white_spawns = 0;
  75.  
  76.     Location[] spawnsAxis = new Location[15];
  77.     Location[] spawnsAllies = new Location[15];
  78.  
  79.     ScoreboardManager manager;
  80.  
  81.     Scoreboard axisBoard;
  82.     Scoreboard alliesBoard;
  83.  
  84.     Team axisTeam;
  85.     Team alliesTeam;
  86.  
  87.     Objective axisObj;
  88.     Objective alliesObj;
  89.  
  90.     OfflinePlayer axisSelf;
  91.     OfflinePlayer axisEnemy;
  92.  
  93.     OfflinePlayer alliesSelf;
  94.     OfflinePlayer alliesEnemy;
  95.  
  96.     int maxScore = 50;
  97.     int maxPlayers = 8; // Per team, so in total 2 * maxPlayers
  98.  
  99.     FileConfiguration config = null;
  100.  
  101.     // ------------------------------------------------------------------------------------------------------+
  102.     // Events
  103.  
  104.     @Override
  105.     public void onEnable()
  106.     {
  107.         setupFromConfig();
  108.         setupScoreBoard();
  109.         getServer().getPluginManager().registerEvents(this, this);
  110.     }
  111.  
  112.     @Override
  113.     public void onDisable()
  114.     {
  115.         saveConfig();
  116.         // saveCustomConfig();
  117.     }
  118.  
  119.     @EventHandler(priority = EventPriority.HIGHEST)
  120.     public void onPlayerInteractBlock(PlayerInteractEvent event)
  121.     {
  122.         if (!paintballting || !isPaintBallPlayer(event.getPlayer()))
  123.             return;
  124.  
  125.         Action action = event.getAction();
  126.         if (action != Action.LEFT_CLICK_AIR
  127.                 && action != Action.LEFT_CLICK_BLOCK)
  128.             return;
  129.  
  130.         Player player = event.getPlayer();
  131.         // int id = 280; // Stick id
  132.  
  133.         int playerweapon = getWeapon(isAxis(player)).getId();
  134.  
  135.         if (player.getItemInHand().getTypeId() == playerweapon)
  136.         {
  137.             throwProjectile(player);
  138.         }
  139.     }
  140.  
  141.     @EventHandler(priority = EventPriority.HIGH)
  142.     public void onGamemodeChange(PlayerGameModeChangeEvent event)
  143.     {
  144.         Player player = event.getPlayer();
  145.  
  146.         if (isPaintBallPlayer(player))
  147.         {
  148.             if (event.getNewGameMode() == GameMode.CREATIVE
  149.                     || event.getNewGameMode() == GameMode.ADVENTURE)
  150.             {
  151.  
  152.                 player.sendMessage(ChatColor.DARK_RED
  153.                         + "Please don't cheat and stay in survival!");
  154.  
  155.                 event.setCancelled(true);
  156.  
  157.                 player.setGameMode(GameMode.SURVIVAL);
  158.             }
  159.         }
  160.     }
  161.  
  162.     public boolean onCommand(CommandSender sender, Command cmd, String label,
  163.             String[] args)
  164.     {
  165.         if (cmd.getName().equalsIgnoreCase("paintball")
  166.                 || cmd.getName().equalsIgnoreCase("pb"))
  167.         {
  168.  
  169.             if (!(sender instanceof Player))
  170.             {
  171.                 sender.sendMessage(ChatColor.RED
  172.                         + "Only players can use this command!");
  173.                 return true;
  174.             }
  175.  
  176.             if (args.length < 1)
  177.                 return false;
  178.  
  179.             return subCommand((Player) sender, args);
  180.         }
  181.         return true;
  182.     }
  183.  
  184.     @EventHandler
  185.     public void onEntityDamage(final EntityDamageEvent event)
  186.     {
  187.         if (event instanceof EntityDamageByEntityEvent)
  188.         {
  189.             EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) event;
  190.  
  191.             if (e.getDamager() instanceof Snowball)
  192.             {
  193.                 Snowball b = (Snowball) e.getDamager();
  194.                 Player attacker = (Player) b.getShooter();
  195.                 Entity v = event.getEntity();
  196.  
  197.                 if (!isPaintBallPlayer(attacker))
  198.                     return;
  199.  
  200.                 String end = "a " + v.getType().toString().toLowerCase();
  201.                 ChatColor prefix = ChatColor.GOLD;
  202.  
  203.                 if (v instanceof Player)
  204.                 {
  205.                     Player victim = (Player) v;
  206.                     end = victim.getName();
  207.                     prefix = ChatColor.GREEN;
  208.  
  209.                     if (!isPaintBallPlayer(victim))
  210.                     {
  211.                         attacker.sendMessage(ChatColor.YELLOW + end
  212.                                 + " is not a paintball player!");
  213.                         return;
  214.                     }
  215.  
  216.                     else if (areDifferentTeams(attacker, victim))
  217.                     {
  218.                         moveToSpawn(victim, isAxis(victim));
  219.  
  220.                         victim.sendMessage(ChatColor.RED
  221.                                 + "You were killed by " + attacker.getName());
  222.  
  223.                         Score score = getPlayerScore(attacker);
  224.                         score.setScore(score.getScore() + 1);
  225.                         updateTeamScores(isAxis(attacker));
  226.                     }
  227.                     else
  228.                     {
  229.                         attacker.sendMessage(ChatColor.RED
  230.                                 + "Friendly Fire! You hit " + end);
  231.                         victim.setHealth(victim.getMaxHealth());
  232.                         victim.sendMessage(ChatColor.AQUA
  233.                                 + "Friendly Fire! You were hit by "
  234.                                 + attacker.getName());
  235.                         return;
  236.                     }
  237.                 }
  238.                 else
  239.                 {
  240.                     ((Damageable) v).setHealth(0);
  241.  
  242.                 }
  243.                 attacker.setFoodLevel(20);
  244.                 attacker.sendMessage(prefix + "You killed " + end);
  245.             }
  246.         }
  247.     }
  248.  
  249.     // ------------------------------------------------------------------------------------------------------+
  250.     // Functions
  251.  
  252.     public void setupScoreBoard()
  253.     {
  254.         manager = Bukkit.getScoreboardManager();
  255.         axisBoard = manager.getNewScoreboard();
  256.         alliesBoard = manager.getNewScoreboard();
  257.         axisTeam = axisBoard.registerNewTeam("axis");
  258.         alliesTeam = alliesBoard.registerNewTeam("allies");
  259.  
  260.         alliesTeam.setDisplayName(ChatColor.GOLD + "GOLD");
  261.         axisTeam.setDisplayName(ChatColor.WHITE + "WHITE");
  262.  
  263.         axisObj = axisBoard.registerNewObjective("WHITE", "dummy");
  264.         axisObj.setDisplaySlot(DisplaySlot.SIDEBAR);
  265.         axisObj.setDisplayName(ChatColor.WHITE + "WHITE" + ChatColor.AQUA
  266.                 + " Team");
  267.  
  268.         alliesObj = alliesBoard.registerNewObjective("GOLD", "dummy");
  269.         alliesObj.setDisplaySlot(DisplaySlot.SIDEBAR);
  270.         alliesObj.setDisplayName(ChatColor.GOLD + "GOLD" + ChatColor.AQUA
  271.                 + " Team");
  272.  
  273.         alliesTeam.setCanSeeFriendlyInvisibles(true);
  274.         axisTeam.setCanSeeFriendlyInvisibles(true);
  275.         alliesTeam.setAllowFriendlyFire(false);
  276.         axisTeam.setAllowFriendlyFire(false);
  277.  
  278.         axisSelf = Bukkit.getOfflinePlayer(ChatColor.GREEN + "Team kills:");
  279.         alliesSelf = Bukkit.getOfflinePlayer(ChatColor.GREEN + "Team kills:");
  280.  
  281.         axisEnemy = Bukkit.getOfflinePlayer(ChatColor.RED + "Enemy kills:");
  282.         alliesEnemy = Bukkit.getOfflinePlayer(ChatColor.RED + "Enemy kills:");
  283.  
  284.         axisTeam.addPlayer(axisEnemy);
  285.         axisTeam.addPlayer(axisSelf);
  286.  
  287.         alliesTeam.addPlayer(alliesEnemy);
  288.         alliesTeam.addPlayer(alliesSelf);
  289.  
  290.         axisObj.getScore(axisSelf).setScore(0);
  291.         axisObj.getScore(axisEnemy).setScore(0);
  292.  
  293.         alliesObj.getScore(alliesSelf).setScore(0);
  294.         alliesObj.getScore(alliesEnemy).setScore(0);
  295.     }
  296.  
  297.     public void winAndRestart()
  298.     {
  299.         if (axisObj.getScore(axisSelf).getScore() >= maxScore)
  300.         {
  301.             for (Player p : axisMap.values())
  302.             {
  303.                 p.sendMessage(ChatColor.WHITE + "WHITE" + ChatColor.DARK_GREEN
  304.                         + " team has won!");
  305.                 moveToSpawn(p, true);
  306.             }
  307.  
  308.             for (Player p : alliesMap.values())
  309.             {
  310.                 p.sendMessage(ChatColor.GOLD + "GOLD" + ChatColor.DARK_RED
  311.                         + " team has lost!");
  312.                 moveToSpawn(p, false);
  313.             }
  314.             resetScores();
  315.         }
  316.         else if (alliesObj.getScore(alliesSelf).getScore() >= maxScore)
  317.         {
  318.             for (Player p : alliesMap.values())
  319.             {
  320.                 p.sendMessage(ChatColor.GOLD + "GOLD" + ChatColor.DARK_GREEN
  321.                         + " team has won!");
  322.                 moveToSpawn(p, false);
  323.             }
  324.  
  325.             for (Player p : axisMap.values())
  326.             {
  327.                 p.sendMessage(ChatColor.WHITE + "WHITE" + ChatColor.DARK_RED
  328.                         + " team has lost!");
  329.                 moveToSpawn(p, true);
  330.             }
  331.             resetScores();
  332.         }
  333.     }
  334.  
  335.     public void resetScores()
  336.     {
  337.         axisObj.getScore(axisSelf).setScore(0);
  338.         axisObj.getScore(axisEnemy).setScore(0);
  339.         alliesObj.getScore(alliesSelf).setScore(0);
  340.         alliesObj.getScore(alliesEnemy).setScore(0);
  341.  
  342.         for (Player p : playerMap.values())
  343.         {
  344.             try
  345.             {
  346.                 getPlayerScore(p).setScore(0);
  347.             }
  348.             catch (Exception e)
  349.             {
  350.             }
  351.         }
  352.  
  353.         updateBoards();
  354.     }
  355.  
  356.     public void updateBoards()
  357.     {
  358.         for (Player p : axisMap.values())
  359.         {
  360.             try
  361.             {
  362.                 p.setScoreboard(axisBoard);
  363.             }
  364.             catch (Exception e)
  365.             {
  366.             }
  367.         }
  368.  
  369.         for (Player p : alliesMap.values())
  370.         {
  371.             try
  372.             {
  373.                 p.setScoreboard(alliesBoard);
  374.             }
  375.             catch (Exception e)
  376.             {
  377.             }
  378.  
  379.         }
  380.  
  381.     }
  382.  
  383.     public void updateTeamScores(boolean axis)
  384.     {
  385.         if (axis)
  386.         {
  387.             axisObj.getScore(axisSelf).setScore(
  388.                     axisObj.getScore(axisSelf).getScore() + 1);
  389.             alliesObj.getScore(alliesEnemy).setScore(
  390.                     alliesObj.getScore(alliesEnemy).getScore() + 1);
  391.         }
  392.  
  393.         else
  394.         {
  395.             axisObj.getScore(axisEnemy).setScore(
  396.                     axisObj.getScore(axisEnemy).getScore() + 1);
  397.             alliesObj.getScore(alliesSelf).setScore(
  398.                     alliesObj.getScore(alliesSelf).getScore() + 1);
  399.         }
  400.  
  401.         updateBoards();
  402.         winAndRestart();
  403.     }
  404.  
  405.     public void moveToSpawn(Player player, boolean axis)
  406.     {
  407.         Location loc = getBestSpawn(axis);
  408.  
  409.         if (axis)
  410.         {
  411.             if (white_spawns > 0)
  412.                 player.teleport(loc);
  413.             else
  414.                 player.sendMessage(ChatColor.DARK_RED
  415.                         + "Teleporting failed, axis spawn is not set!");
  416.         }
  417.         else
  418.         {
  419.             if (gold_spawns > 0)
  420.                 player.teleport(loc);
  421.             else
  422.                 player.sendMessage(ChatColor.DARK_RED
  423.                         + "Teleporting failed, axis spawn is not set!");
  424.         }
  425.     }
  426.  
  427.     public void addPlayer(Player player)
  428.     {
  429.         if (playerMap.containsValue(player))
  430.         {
  431.             player.sendMessage(ChatColor.AQUA + "You are already in a team!");
  432.             return;
  433.         }
  434.         boolean axis = false;
  435.  
  436.         // Allies = GOLD
  437.         // Axis = WHITE
  438.  
  439.         if (axisMap.size() < alliesMap.size())
  440.             axis = true;
  441.  
  442.         else if (axisMap.size() > alliesMap.size())
  443.             axis = false;
  444.  
  445.         else
  446.         {
  447.             int rand = (int) (Math.random()) % 2;
  448.             if (rand == 1)
  449.                 axis = true;
  450.             else
  451.                 axis = false;
  452.         }
  453.  
  454.         paintballting = true;
  455.  
  456.         player.sendMessage(ChatColor.AQUA + "You have joined the "
  457.                 + (axis ? ChatColor.WHITE + "WHITE" : ChatColor.GOLD + "GOLD")
  458.                 + ChatColor.AQUA + " team.");
  459.  
  460.         storeInv(player);
  461.         clearInv(player);
  462.  
  463.         player.setGameMode(GameMode.SURVIVAL);
  464.  
  465.         if (axis)
  466.         {
  467.             if (alliesMap.containsValue(player))
  468.                 alliesMap.remove(player.getEntityId());
  469.  
  470.             for (Player p : axisMap.values())
  471.                 p.sendMessage(ChatColor.WHITE + player.getName()
  472.                         + ChatColor.AQUA + " joined your team!");
  473.  
  474.             for (Player p : alliesMap.values())
  475.                 p.sendMessage(ChatColor.WHITE + player.getName()
  476.                         + ChatColor.AQUA + " joined the enemy team!");
  477.  
  478.             axisMap.put(player.getEntityId(), player);
  479.             axisTeam.addPlayer(player);
  480.             getPlayerScore(player).setScore(0);
  481.             player.setScoreboard(axisBoard);
  482.         }
  483.         else
  484.         {
  485.             if (axisMap.containsValue(player))
  486.                 axisMap.remove(player.getName());
  487.  
  488.             for (Player p : alliesMap.values())
  489.                 p.sendMessage(ChatColor.GOLD + player.getName()
  490.                         + ChatColor.AQUA + " joined your team!");
  491.  
  492.             for (Player p : axisMap.values())
  493.                 p.sendMessage(ChatColor.GOLD + player.getName()
  494.                         + ChatColor.AQUA + " joined the enemy team!");
  495.  
  496.             alliesMap.put(player.getEntityId(), player);
  497.             alliesTeam.addPlayer(player);
  498.             getPlayerScore(player).setScore(0);
  499.             player.setScoreboard(alliesBoard);
  500.         }
  501.  
  502.         player.setHealth(player.getMaxHealth());
  503.  
  504.         getPlayerScore(player).setScore(0);
  505.         equipPlayer(player, axis);
  506.         playerMap.put(player.getEntityId(), player);
  507.         locationMap.put(player.getEntityId(), player.getLocation());
  508.         moveToSpawn(player, axis);
  509.         updateBoards();
  510.         player.sendMessage(ChatColor.DARK_RED
  511.                 + "Be aware, if you disconnect from the server (or it crashes) during a paintball match, you will lose all your items! It's highly recommended to leave the match and safely store the important items. We are not responsable if you lose your items, nor will we replace them!.");
  512.     }
  513.  
  514.     public void clearInv(Player player)
  515.     {
  516.         PlayerInventory inv = player.getInventory();
  517.         inv.clear();
  518.  
  519.         inv.setHelmet(new ItemStack(Material.AIR));
  520.         inv.setChestplate(new ItemStack(Material.AIR));
  521.         inv.setLeggings(new ItemStack(Material.AIR));
  522.         inv.setBoots(new ItemStack(Material.AIR));
  523.     }
  524.  
  525.     public void storeInv(Player player)
  526.     {
  527.         invContentMap.put(player.getEntityId(), player.getInventory()
  528.                 .getContents());
  529.         invArmorMap.put(player.getEntityId(), player.getInventory()
  530.                 .getArmorContents());
  531.     }
  532.  
  533.     public void restoreInv(Player player)
  534.     {
  535.         boolean invcontent = false;
  536.         boolean invarmor = false;
  537.  
  538.         if (invContentMap.containsKey(player.getEntityId()))
  539.         {
  540.             ItemStack[] content = invContentMap.get(player.getEntityId());
  541.             player.getInventory().setContents(content);
  542.             invcontent = true;
  543.         }
  544.         else
  545.             player.sendMessage("An error occured. Your inventory couldn't get restored!");
  546.  
  547.         if (invArmorMap.containsKey(player.getEntityId()))
  548.         {
  549.             ItemStack[] armor = invArmorMap.get(player.getEntityId());
  550.             player.getInventory().setArmorContents(armor);
  551.             invarmor = true;
  552.         }
  553.  
  554.         if (invarmor && invcontent)
  555.             player.sendMessage(ChatColor.AQUA
  556.                     + "Your inventory has been restored!");
  557.         else if (invarmor && !invcontent)
  558.             player.sendMessage(ChatColor.YELLOW
  559.                     + "Your armor was restored, but your inventory couldn't be restored!");
  560.         else if (!invarmor && invcontent)
  561.             player.sendMessage(ChatColor.YELLOW
  562.                     + "Your inventory was restored, but your armor couldn't be restored!");
  563.         else
  564.             player.sendMessage(ChatColor.DARK_RED
  565.                     + "Your inventory couldn't be restored!");
  566.  
  567.     }
  568.  
  569.     public void equipPlayer(Player player, boolean axis)
  570.     {
  571.  
  572.         PlayerInventory inv = player.getInventory();
  573.  
  574.         inv.addItem(new ItemStack(getWeapon(axis)));
  575.  
  576.         if (axis)
  577.         {
  578.             inv.setHelmet(new ItemStack(Material.IRON_HELMET));
  579.             inv.setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
  580.             inv.setLeggings(new ItemStack(Material.IRON_LEGGINGS));
  581.             inv.setBoots(new ItemStack(Material.IRON_BOOTS));
  582.         }
  583.         else
  584.         {
  585.             inv.setHelmet(new ItemStack(Material.GOLD_HELMET));
  586.             inv.setChestplate(new ItemStack(Material.GOLD_CHESTPLATE));
  587.             inv.setLeggings(new ItemStack(Material.GOLD_LEGGINGS));
  588.             inv.setBoots(new ItemStack(Material.GOLD_BOOTS));
  589.         }
  590.     }
  591.  
  592.     public void removePlayer(Player player)
  593.     {
  594.         if (playerMap.containsValue(player))
  595.         {
  596.             playerMap.remove(player.getEntityId());
  597.         }
  598.  
  599.         player.setScoreboard(manager.getNewScoreboard());
  600.  
  601.         if (locationMap.containsKey(player.getEntityId()))
  602.         {
  603.             player.teleport(locationMap.get(player.getEntityId()));
  604.             locationMap.remove(player.getEntityId());
  605.         }
  606.  
  607.         try
  608.         {
  609.             axisTeam.removePlayer(player);
  610.         }
  611.         catch (Exception e)
  612.         {
  613.         }
  614.  
  615.         try
  616.         {
  617.             alliesTeam.removePlayer(player);
  618.         }
  619.         catch (Exception e)
  620.         {
  621.         }
  622.  
  623.         if (axisMap.containsValue(player))
  624.         {
  625.             axisMap.remove(player.getEntityId());
  626.             player.sendMessage(ChatColor.AQUA + "You have left the "
  627.                     + ChatColor.WHITE + "WHITE" + ChatColor.AQUA + " team!");
  628.             clearInv(player);
  629.             restoreInv(player);
  630.  
  631.             for (Player p : alliesMap.values())
  632.                 p.sendMessage(ChatColor.WHITE + player.getName()
  633.                         + ChatColor.AQUA + " left the enemy team!");
  634.  
  635.             for (Player p : axisMap.values())
  636.                 p.sendMessage(ChatColor.WHITE + player.getName()
  637.                         + ChatColor.AQUA + " left your team!");
  638.         }
  639.  
  640.         else if (alliesMap.containsValue(player))
  641.         {
  642.             alliesMap.remove(player.getEntityId());
  643.             player.sendMessage(ChatColor.AQUA + "You have left the "
  644.                     + ChatColor.GOLD + "GOLD" + ChatColor.AQUA + " team!");
  645.             clearInv(player);
  646.             restoreInv(player);
  647.  
  648.             for (Player p : alliesMap.values())
  649.                 p.sendMessage(ChatColor.GOLD + player.getName()
  650.                         + ChatColor.AQUA + " left your team!");
  651.  
  652.             for (Player p : axisMap.values())
  653.                 p.sendMessage(ChatColor.GOLD + player.getName()
  654.                         + ChatColor.AQUA + " left the enemy team!");
  655.         }
  656.  
  657.         else
  658.             player.sendMessage(ChatColor.AQUA
  659.                     + "You are not participating in a paintball match!");
  660.  
  661.         updateBoards();
  662.  
  663.         if (playerMap.size() == 0)
  664.         {
  665.             paintballting = false;
  666.             resetScores();
  667.         }
  668.  
  669.     }
  670.  
  671.     public void forceRemove()
  672.     {
  673.         for (Player p : playerMap.values())
  674.         {
  675.             try
  676.             {
  677.                 removePlayer(p);
  678.             }
  679.             catch (Exception e)
  680.             {
  681.             }
  682.         }
  683.  
  684.         playerMap = new HashMap<Integer, Player>();
  685.         axisMap = new HashMap<Integer, Player>();
  686.         alliesMap = new HashMap<Integer, Player>();
  687.         invContentMap = new HashMap<Integer, ItemStack[]>();
  688.         invArmorMap = new HashMap<Integer, ItemStack[]>();
  689.         locationMap = new HashMap<Integer, Location>();
  690.  
  691.         setupScoreBoard();
  692.     }
  693.  
  694.     public void throwProjectile(Player player)
  695.     {
  696.  
  697.         Location playerLoc = player.getLocation();
  698.         Location loc = playerLoc.add(
  699.                 playerLoc
  700.                         .getDirection()
  701.                         .normalize()
  702.                         .multiply(3)
  703.                         .toLocation(player.getWorld(), playerLoc.getYaw(),
  704.                                 playerLoc.getPitch())).add(0, 1D, 0);
  705.         Snowball b = player.getWorld().spawn(loc, Snowball.class);
  706.  
  707.         Vector velocity = playerLoc.getDirection().normalize().multiply(2);
  708.         b.setShooter(player);
  709.         b.setVelocity(velocity);
  710.  
  711.         if (isAllies(player))
  712.         {
  713.             b.setFireTicks(1000);
  714.         }
  715.     }
  716.  
  717.     public void showHelp(Player player)
  718.     {
  719.         String prefix = ChatColor.GOLD + "/paintball ";
  720.  
  721.         // Join
  722.         player.sendMessage(prefix + "join " + ChatColor.AQUA
  723.                 + "Autoassings you either to the " + ChatColor.GOLD + "GOLD"
  724.                 + ChatColor.AQUA + " or the " + ChatColor.WHITE + "WHITE"
  725.                 + ChatColor.AQUA + " team!");
  726.  
  727.         // Leave
  728.         player.sendMessage(prefix
  729.                 + "leave "
  730.                 + ChatColor.AQUA
  731.                 + "Leaves current paintball match and brings you to your last location.");
  732.  
  733.         // Help
  734.         player.sendMessage(prefix + "help " + ChatColor.AQUA
  735.                 + "Displays this help page.");
  736.  
  737.         // Addspawn
  738.         if (player.hasPermission("us.blackpulse.momo.paintball.addSpawn"))
  739.             player.sendMessage(prefix + "addspawn [GOLD/WHITE] "
  740.                     + ChatColor.AQUA
  741.                     + "Adds your location as a spawn to the team.");
  742.  
  743.         // Removespawn
  744.         if (player.hasPermission("us.blackpulse.momo.paintball.removeSpawn"))
  745.             player.sendMessage(prefix + "removespawn [GOLD/WHITE] "
  746.                     + ChatColor.AQUA + "Removes nearest spawn from the team.");
  747.  
  748.         // Resetall
  749.         if (player.hasPermission("us.blackpulse.momo.paintball.resetAll"))
  750.             player.sendMessage(prefix + "resetall " + ChatColor.AQUA
  751.                     + "Resets the whole paintball plugin (for debugging).");
  752.     }
  753.  
  754.     public void removeSpawn(Player player, String team)
  755.     {
  756.         boolean axis = false;
  757.         if (team.equalsIgnoreCase("white"))
  758.         {
  759.             axis = true;
  760.             if (white_spawns <= 0)
  761.             {
  762.                 player.sendMessage(ChatColor.DARK_RED + "No spawns set!");
  763.                 return;
  764.             }
  765.         }
  766.         else if (team.equalsIgnoreCase("gold"))
  767.         {
  768.             axis = false;
  769.             if (gold_spawns <= 0)
  770.             {
  771.                 player.sendMessage(ChatColor.DARK_RED + "No spawns set!");
  772.                 return;
  773.             }
  774.         }
  775.         else
  776.         {
  777.             player.sendMessage(ChatColor.DARK_RED + "Wrong teamname!");
  778.             return;
  779.         }
  780.  
  781.         Location[] array = (axis) ? spawnsAxis : spawnsAllies;
  782.         Location[] newarray = new Location[15];
  783.  
  784.         Location loc = player.getLocation();
  785.  
  786.         int spawns = (axis) ? white_spawns : gold_spawns;
  787.         int spawn = 0;
  788.  
  789.         if (spawns > 1)
  790.         {
  791.             double dist = loc.distanceSquared(array[0]);
  792.  
  793.             for (int i = 0; i < spawns; i++)
  794.             {
  795.                 double newdist = loc.distanceSquared(array[i]);
  796.  
  797.                 if (newdist < dist)
  798.                 {
  799.                     dist = newdist;
  800.                     spawn = i;
  801.                 }
  802.             }
  803.  
  804.             int j = 0;
  805.             for (int i = 0; i < spawns; i++)
  806.             {
  807.                 if (i != spawn)
  808.                 {
  809.                     newarray[j] = array[i];
  810.                     j++;
  811.                 }
  812.             }
  813.         }
  814.         if (axis)
  815.         {
  816.             spawnsAxis = newarray;
  817.             for (int i = 0; i < spawns; i++)
  818.             {
  819.                 config.set("spawns.white." + i + ".x", null);
  820.                 config.set("spawns.white." + i + ".y", null);
  821.                 config.set("spawns.white." + i + ".z", null);
  822.                 config.set("spawns.white." + i + ".yaw", null);
  823.                 config.set("spawns.white." + i + ".pitch", null);
  824.                 config.set("spawns.white." + i + ".world", null);
  825.                 config.set("spawns.white." + i, null);
  826.  
  827.             }
  828.  
  829.             white_spawns--;
  830.  
  831.             for (int i = 0; i < white_spawns; i++)
  832.             {
  833.                 config.set("spawns.white." + i + ".x", spawnsAxis[i].getX());
  834.                 config.set("spawns.white." + i + ".y", spawnsAxis[i].getY());
  835.                 config.set("spawns.white." + i + ".z", spawnsAxis[i].getZ());
  836.                 config.set("spawns.white." + i + ".yaw", spawnsAxis[i].getYaw());
  837.                 config.set("spawns.white." + i + ".pitch",
  838.                         spawnsAxis[i].getPitch());
  839.                 config.set("spawns.white." + i + ".world", spawnsAxis[i]
  840.                         .getWorld().getName());
  841.             }
  842.  
  843.             config.set("spawns.white.amount", white_spawns);
  844.         }
  845.         else
  846.         {
  847.             spawnsAllies = newarray;
  848.  
  849.             for (int i = 0; i < spawns; i++)
  850.             {
  851.                 config.set("spawns.gold." + i + ".x", null);
  852.                 config.set("spawns.gold." + i + ".y", null);
  853.                 config.set("spawns.gold." + i + ".z", null);
  854.                 config.set("spawns.gold." + i + ".yaw", null);
  855.                 config.set("spawns.gold." + i + ".pitch", null);
  856.                 config.set("spawns.gold." + i + ".world", null);
  857.                 config.set("spawns.gold." + i, null);
  858.             }
  859.  
  860.             gold_spawns--;
  861.  
  862.             for (int i = 0; i < gold_spawns; i++)
  863.             {
  864.                 config.set("spawns.gold." + i + ".x", spawnsAllies[i].getX());
  865.                 config.set("spawns.gold." + i + ".y", spawnsAllies[i].getY());
  866.                 config.set("spawns.gold." + i + ".z", spawnsAllies[i].getZ());
  867.                 config.set("spawns.gold." + i + ".yaw",
  868.                         spawnsAllies[i].getYaw());
  869.                 config.set("spawns.gold." + i + ".pitch",
  870.                         spawnsAllies[i].getPitch());
  871.                 config.set("spawns.gold." + i + ".world", spawnsAllies[i]
  872.                         .getWorld().getName());
  873.             }
  874.  
  875.             config.set("spawns.gold.amount", gold_spawns);
  876.         }
  877.  
  878.         this.saveConfig();
  879.  
  880.         String teamn = (axis) ? ChatColor.WHITE + "WHITE" : ChatColor.GOLD
  881.                 + "GOLD";
  882.         player.sendMessage(ChatColor.AQUA + "Nearest " + teamn + ChatColor.AQUA
  883.                 + " spawn removed!");
  884.     }
  885.  
  886.     public void setSpawn(Player player, String team)
  887.     {
  888.         Location loc = player.getLocation();
  889.  
  890.         config = getConfig();
  891.  
  892.         if (team.equalsIgnoreCase("white"))
  893.         {
  894.             if (white_spawns >= 15)
  895.             {
  896.                 player.sendMessage(ChatColor.DARK_RED
  897.                         + "Couldn't add"
  898.                         + ChatColor.WHITE
  899.                         + "WHITE"
  900.                         + ChatColor.DARK_RED
  901.                         + " spawn! Max amount of spawns (15) for this team reached!");
  902.                 return;
  903.             }
  904.             spawnsAxis[white_spawns] = loc;
  905.  
  906.             config.set("spawns.white." + white_spawns + ".x", loc.getX());
  907.             config.set("spawns.white." + white_spawns + ".y", loc.getY());
  908.             config.set("spawns.white." + white_spawns + ".z", loc.getZ());
  909.             config.set("spawns.white." + white_spawns + ".yaw", loc.getYaw());
  910.             config.set("spawns.white." + white_spawns + ".pitch",
  911.                     loc.getPitch());
  912.             config.set("spawns.white." + white_spawns + ".world", loc
  913.                     .getWorld().getName());
  914.  
  915.             white_spawns++;
  916.  
  917.             config.set("spawns.white.amount", white_spawns);
  918.  
  919.             player.sendMessage(ChatColor.WHITE + "WHITE" + ChatColor.AQUA
  920.                     + " spawn added!");
  921.         }
  922.         else if (team.equalsIgnoreCase("gold"))
  923.         {
  924.             if (gold_spawns >= 15)
  925.             {
  926.                 player.sendMessage(ChatColor.DARK_RED
  927.                         + "Couldn't add"
  928.                         + ChatColor.GOLD
  929.                         + "GOLD"
  930.                         + ChatColor.DARK_RED
  931.                         + " spawn! Max amount of spawns (15) for this team reached!");
  932.                 return;
  933.             }
  934.             spawnsAllies[gold_spawns] = loc;
  935.  
  936.             config.set("spawns.gold." + gold_spawns + ".x", loc.getX());
  937.             config.set("spawns.gold." + gold_spawns + ".y", loc.getY());
  938.             config.set("spawns.gold." + gold_spawns + ".z", loc.getZ());
  939.             config.set("spawns.gold." + gold_spawns + ".yaw", loc.getYaw());
  940.             config.set("spawns.gold." + gold_spawns + ".pitch", loc.getPitch());
  941.             config.set("spawns.gold." + gold_spawns + ".world", loc.getWorld()
  942.                     .getName());
  943.  
  944.             gold_spawns++;
  945.  
  946.             config.set("spawns.gold.amount", gold_spawns);
  947.  
  948.             player.sendMessage(ChatColor.GOLD + "GOLD" + ChatColor.AQUA
  949.                     + " spawn added!");
  950.         }
  951.  
  952.         this.saveConfig();
  953.  
  954.     }
  955.  
  956.     public void setupFromConfig()
  957.     {
  958.         config = getConfig();
  959.         config.options().copyDefaults(true);
  960.         this.saveConfig();
  961.  
  962.         maxPlayers = config.getInt("max-players");
  963.         maxScore = config.getInt("max-score");
  964.  
  965.         gold_spawns = config.getInt("spawns.gold.amount");
  966.         white_spawns = config.getInt("spawns.white.amount");
  967.  
  968.         Server s = Bukkit.getServer();
  969.         CommandSender cs = s.getConsoleSender();
  970.  
  971.         for (int i = 0; i < gold_spawns; i++)
  972.         {
  973.             double x = config.getDouble("spawns.gold." + i + ".x");
  974.             double y = config.getDouble("spawns.gold." + i + ".y");
  975.             double z = config.getDouble("spawns.gold." + i + ".z");
  976.             double yaw = config.getDouble("spawns.gold." + i + ".yaw");
  977.             double pitch = config.getDouble("spawns.gold." + i + ".pitch");
  978.             World world = s.getWorld(config.getString("spawns.gold." + i
  979.                     + ".world"));
  980.  
  981.             Location spawn = new Location(world, x, y, z);
  982.             spawn.setPitch((float) pitch);
  983.             spawn.setYaw((float) yaw);
  984.             spawnsAllies[i] = spawn;
  985.         }
  986.  
  987.         for (int i = 0; i < white_spawns; i++)
  988.         {
  989.             double x = config.getDouble("spawns.white." + i + ".x");
  990.             double y = config.getDouble("spawns.white." + i + ".y");
  991.             double z = config.getDouble("spawns.white." + i + ".z");
  992.             double yaw = config.getDouble("spawns.white." + i + ".yaw");
  993.             double pitch = config.getDouble("spawns.white." + i + ".pitch");
  994.             World world = s.getWorld(config.getString("spawns.white." + i
  995.                     + ".world"));
  996.  
  997.             Location spawn = new Location(world, x, y, z);
  998.             spawn.setPitch((float) pitch);
  999.             spawn.setYaw((float) yaw);
  1000.  
  1001.             spawnsAxis[i] = spawn;
  1002.         }
  1003.         cs.sendMessage("[Paintball] " + ChatColor.AQUA + "Max Score set to "
  1004.                 + maxScore);
  1005.         cs.sendMessage("[Paintball] " + ChatColor.AQUA + "Max Players set to "
  1006.                 + maxPlayers);
  1007.         cs.sendMessage("[Paintball] " + ChatColor.AQUA + "Axis ("
  1008.                 + ChatColor.WHITE + "WHITE" + ChatColor.AQUA
  1009.                 + ") spawns loaded: " + white_spawns);
  1010.         cs.sendMessage("[Paintball] " + ChatColor.AQUA + "Allies ("
  1011.                 + ChatColor.GOLD + "GOLD" + ChatColor.AQUA
  1012.                 + ") spawns loaded: " + gold_spawns);
  1013.     }
  1014.  
  1015.     // ------------------------------------------------------------------------------------------------------+
  1016.     // Return stuff
  1017.  
  1018.     public Location getBestSpawn(boolean axis)
  1019.     {
  1020.         Location best = null;
  1021.         double dist = 0;
  1022.         double newdist = 0;
  1023.         if (axis)
  1024.         {
  1025.             best = spawnsAxis[0];
  1026.             for (int i = 0; i < white_spawns; i++)
  1027.             {
  1028.                 for (Player p : alliesMap.values())
  1029.                 {
  1030.                     newdist = spawnsAxis[i].distanceSquared(p.getLocation());
  1031.                     if (newdist > dist)
  1032.                     {
  1033.                         dist = newdist;
  1034.                         best = spawnsAxis[i];
  1035.                     }
  1036.                 }
  1037.             }
  1038.         }
  1039.         else
  1040.         {
  1041.             best = spawnsAllies[0];
  1042.             for (int i = 0; i < gold_spawns; i++)
  1043.             {
  1044.                 for (Player p : axisMap.values())
  1045.                 {
  1046.                     newdist = spawnsAllies[i].distanceSquared(p.getLocation());
  1047.                     if (newdist > dist)
  1048.                     {
  1049.                         dist = newdist;
  1050.                         best = spawnsAllies[i];
  1051.                     }
  1052.                 }
  1053.             }
  1054.         }
  1055.  
  1056.         return best;
  1057.     }
  1058.  
  1059.     public Score getPlayerScore(Player player)
  1060.     {
  1061.         if (isAxis(player))
  1062.             return axisObj.getScore(player);
  1063.         else
  1064.             return alliesObj.getScore(player);
  1065.     }
  1066.  
  1067.     public Material getWeapon(boolean axis)
  1068.     {
  1069.         return axis ? Material.IRON_SPADE : Material.GOLD_SPADE;
  1070.     }
  1071.  
  1072.     public boolean subCommand(Player player, String[] args)
  1073.     {
  1074.         String cmd = args[0];
  1075.  
  1076.         // CMD: add --------------------+
  1077.         if (cmd.equalsIgnoreCase("join"))
  1078.         {
  1079.             addPlayer(player);
  1080.             return true;
  1081.         }
  1082.  
  1083.         // CMD: stop -------------------+
  1084.         if (cmd.equalsIgnoreCase("leave"))
  1085.         {
  1086.             removePlayer(player);
  1087.             return true;
  1088.         }
  1089.  
  1090.         // CMD: addspawn ---------------+
  1091.         if (cmd.equalsIgnoreCase("addspawn"))
  1092.         {
  1093.             if (player.hasPermission("us.blackpulse.momo.paintball.addSpawn"))
  1094.                 setSpawn(player, args[1]);
  1095.             else
  1096.                 player.sendMessage(ChatColor.DARK_RED
  1097.                         + "You don't have the permission to use this command!");
  1098.  
  1099.             return true;
  1100.         }
  1101.  
  1102.         // CMD: removespawn ------------+
  1103.         if (cmd.equalsIgnoreCase("removespawn"))
  1104.         {
  1105.             if (player
  1106.                     .hasPermission("us.blackpulse.momo.paintball.removeSpawn"))
  1107.                 removeSpawn(player, args[1]);
  1108.             else
  1109.                 player.sendMessage(ChatColor.DARK_RED
  1110.                         + "You don't have the permission to use this command!");
  1111.  
  1112.             return true;
  1113.         }
  1114.  
  1115.         // CMD: resetall ---------------+
  1116.         if (cmd.equalsIgnoreCase("resetall"))
  1117.         {
  1118.             if (player.hasPermission("us.blackpulse.momo.paintball.resetAll"))
  1119.             {
  1120.                 forceRemove();
  1121.                 player.sendMessage(ChatColor.DARK_PURPLE
  1122.                         + "Paintball has been restetted!");
  1123.             }
  1124.             else
  1125.                 player.sendMessage(ChatColor.DARK_RED
  1126.                         + "You don't have the permission to use this command!");
  1127.  
  1128.             return true;
  1129.         }
  1130.  
  1131.         // CMD: help -------------------+
  1132.         if (cmd.equalsIgnoreCase("help"))
  1133.         {
  1134.             showHelp(player);
  1135.             return true;
  1136.         }
  1137.  
  1138.         player.sendMessage(ChatColor.DARK_RED
  1139.                 + "Error, wrong subcommand. For help: " + ChatColor.AQUA
  1140.                 + "/paintball help");
  1141.         return false;
  1142.     }
  1143.  
  1144.     public boolean areDifferentTeams(Player player1, Player player2)
  1145.     {
  1146.         if (!isPaintBallPlayer(player1) || !isPaintBallPlayer(player2))
  1147.             return false;
  1148.  
  1149.         if (isAxis(player1) && isAllies(player2))
  1150.             return true;
  1151.  
  1152.         else if (isAllies(player1) && isAxis(player2))
  1153.             return true;
  1154.  
  1155.         return false;
  1156.     }
  1157.  
  1158.     public boolean isAxis(Player player)
  1159.     {
  1160.         return axisMap.containsValue(player);
  1161.     }
  1162.  
  1163.     public boolean isAllies(Player player)
  1164.     {
  1165.         return alliesMap.containsValue(player);
  1166.     }
  1167.  
  1168.     public boolean isPaintBallPlayer(Player player)
  1169.     {
  1170.         return playerMap.containsValue(player);
  1171.     }
  1172.  
  1173.     // ------------------------------------------------------------------------------------------------------+
  1174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement