PageRenderTime 40ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/TTT/src/net/amigocraft/TTT/TTT.java

https://bitbucket.org/DreTaX/ttt
Java | 1180 lines | 1116 code | 49 blank | 15 comment | 394 complexity | 6ec509d99b79bbeaefa5ed85fa22ff82 MD5 | raw file
  1. package net.amigocraft.TTT;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Random;
  8. import java.util.logging.Logger;
  9. import net.amigocraft.TTT.AutoUpdate;
  10. import net.amigocraft.TTT.Metrics;
  11. import net.amigocraft.TTT.localization.Localization;
  12. import net.amigocraft.TTT.utils.NumUtils;
  13. import net.amigocraft.TTT.utils.WorldUtils;
  14. import org.apache.commons.io.FileUtils;
  15. import org.bukkit.Bukkit;
  16. import org.bukkit.ChatColor;
  17. import org.bukkit.Material;
  18. import org.bukkit.World;
  19. import org.bukkit.WorldCreator;
  20. import org.bukkit.block.Block;
  21. import org.bukkit.block.Chest;
  22. import org.bukkit.command.Command;
  23. import org.bukkit.command.CommandSender;
  24. import org.bukkit.configuration.file.YamlConfiguration;
  25. import org.bukkit.entity.Arrow;
  26. import org.bukkit.entity.EntityType;
  27. import org.bukkit.entity.HumanEntity;
  28. import org.bukkit.entity.Player;
  29. import org.bukkit.event.EventHandler;
  30. import org.bukkit.event.EventPriority;
  31. import org.bukkit.event.Listener;
  32. import org.bukkit.event.block.Action;
  33. import org.bukkit.event.block.BlockBreakEvent;
  34. import org.bukkit.event.block.BlockPlaceEvent;
  35. import org.bukkit.event.entity.EntityDamageByEntityEvent;
  36. import org.bukkit.event.entity.EntityDamageEvent;
  37. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
  38. import org.bukkit.event.entity.EntityRegainHealthEvent;
  39. import org.bukkit.event.entity.FoodLevelChangeEvent;
  40. import org.bukkit.event.inventory.InventoryClickEvent;
  41. import org.bukkit.event.inventory.InventoryCloseEvent;
  42. import org.bukkit.event.inventory.InventoryType;
  43. import org.bukkit.event.player.AsyncPlayerChatEvent;
  44. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
  45. import org.bukkit.event.player.PlayerDropItemEvent;
  46. import org.bukkit.event.player.PlayerInteractEvent;
  47. import org.bukkit.event.player.PlayerPickupItemEvent;
  48. import org.bukkit.event.player.PlayerQuitEvent;
  49. import org.bukkit.event.player.PlayerTeleportEvent;
  50. import org.bukkit.inventory.Inventory;
  51. import org.bukkit.inventory.ItemStack;
  52. import org.bukkit.inventory.PlayerInventory;
  53. import org.bukkit.inventory.meta.ItemMeta;
  54. import org.bukkit.plugin.java.JavaPlugin;
  55. public class TTT extends JavaPlugin implements Listener {
  56. public static Logger log = Logger.getLogger("Minecraft");
  57. public static TTT plugin = new TTT();
  58. public static Localization local = new Localization();
  59. public static String lang;
  60. public HashMap<String, String> joinedPlayers = new HashMap<String, String>();
  61. public HashMap<String, Integer> playerRoles = new HashMap<String, Integer>();
  62. public HashMap<String, Integer> time = new HashMap<String, Integer>();
  63. public HashMap<String, Integer> tasks = new HashMap<String, Integer>();
  64. public HashMap<String, Integer> gameTime = new HashMap<String, Integer>();
  65. public HashMap<String, String> deadPlayers = new HashMap<String, String>();
  66. public List<Body> bodies = new ArrayList<Body>();
  67. public List<Body> foundBodies = new ArrayList<Body>();
  68. public HashMap<String, String> killers = new HashMap<String, String>();
  69. public HashMap<String, String> tracking = new HashMap<String, String>();
  70. public List<String> discreet = new ArrayList<String>();
  71. @Override
  72. public void onEnable(){
  73. // check if server is offline
  74. if (!getServer().getOnlineMode()){
  75. log.info("[TTT] This plugin does not support offline servers! Disabling...");
  76. getServer().getPluginManager().disablePlugin(this);
  77. }
  78. // register events and the plugin variable
  79. getServer().getPluginManager().registerEvents(this, this);
  80. TTT.plugin = this;
  81. // check if config should be overwritten
  82. saveDefaultConfig();
  83. if (getConfig().getString("config-version") != this.getDescription().getVersion()){
  84. File config = new File(this.getDataFolder(), "config.yml");
  85. config.delete();
  86. }
  87. // create the default config
  88. saveDefaultConfig();
  89. TTT.lang = getConfig().getString("localization");
  90. // autoupdate
  91. if (getConfig().getBoolean("enable-auto-update")){
  92. try {new AutoUpdate(this);}
  93. catch (Exception e){e.printStackTrace();}
  94. }
  95. // submit metrics
  96. if (getConfig().getBoolean("enable-metrics")){
  97. try {
  98. Metrics metrics = new Metrics(this);
  99. metrics.start();
  100. }
  101. catch (IOException e) {log.warning("[TTT] " + local.getMessage("metrics-fail"));}
  102. }
  103. File invDir = new File(this.getDataFolder() + File.separator + "inventories");
  104. invDir.mkdir();
  105. log.info(this + " " + local.getMessage("enabled"));
  106. }
  107. @Override
  108. public void onDisable(){
  109. log.info(this + " " + local.getMessage("disabled"));
  110. }
  111. @SuppressWarnings("deprecation")
  112. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
  113. if (commandLabel.equalsIgnoreCase("ttt")){
  114. if (args.length > 0){
  115. if (args[0].equalsIgnoreCase("import")){
  116. if (sender.hasPermission("ttt.import")){
  117. if (args.length > 1){
  118. File folder = new File(args[1]);
  119. if (folder.exists()){
  120. if (!args[1].substring(0, 3).equalsIgnoreCase("TTT_")){
  121. if (WorldUtils.isWorld(folder)){
  122. File newFolder = new File("TTT_" + args[1]);
  123. if (!newFolder.exists()){
  124. try {
  125. File sessionLock = new File(folder + File.separator + "session.lock");
  126. File uidDat = new File(folder + File.separator + "uid.dat");
  127. sessionLock.delete();
  128. uidDat.delete();
  129. FileUtils.copyDirectory(folder, newFolder);
  130. sender.sendMessage(ChatColor.GREEN + "[TTT] " + local.getMessage("import-success"));
  131. }
  132. catch (IOException e){
  133. sender.sendMessage(ChatColor.RED + "[TTT] " + local.getMessage("folder-error"));
  134. e.printStackTrace();
  135. }
  136. }
  137. else
  138. sender.sendMessage(ChatColor.RED + "[TTT] " + local.getMessage("already-imported"));
  139. }
  140. else
  141. sender.sendMessage(ChatColor.RED + "[TTT] " + local.getMessage("cannot-load-world"));
  142. }
  143. else
  144. sender.sendMessage(ChatColor.RED + "[TTT] " + local.getMessage("start-error"));
  145. }
  146. else
  147. sender.sendMessage(ChatColor.RED + "[TTT] " + local.getMessage("folder-not-found"));
  148. }
  149. else {
  150. sender.sendMessage(ChatColor.RED + "[TTT] " + local.getMessage("invalid-args-1"));
  151. sender.sendMessage(ChatColor.RED + "[TTT] " + local.getMessage("usage-import"));
  152. }
  153. }
  154. else
  155. sender.sendMessage(ChatColor.RED + local.getMessage("no-permission-import"));
  156. }
  157. else if (args[0].equalsIgnoreCase("join")){
  158. if (sender instanceof Player){
  159. if (sender.hasPermission("ttt.join")){
  160. if (args.length > 1){
  161. if (gameTime.get(args[1]) == null){
  162. File folder = new File(args[1]);
  163. File tttFolder = new File("TTT_" + args[1]);
  164. if (folder.exists() && tttFolder.exists()){
  165. boolean loaded = false;
  166. for (World w : Bukkit.getServer().getWorlds()){
  167. if(w.getName().equals("TTT_" + args[1])){
  168. loaded = true;
  169. break;
  170. }
  171. }
  172. final String worldName = args[1];
  173. if (!loaded){
  174. getServer().createWorld(new WorldCreator("TTT_" + worldName));
  175. }
  176. ((Player)sender).teleport(getServer().getWorld("TTT_" + worldName).getSpawnLocation());
  177. joinedPlayers.put(((Player)sender).getName(), worldName);
  178. File invF = new File(getDataFolder() + File.separator + "inventories" + File.separator + sender.getName() + ".inv");
  179. Inventory inv = ((Player)sender).getInventory();
  180. PlayerInventory pInv = (PlayerInventory)inv;
  181. try {
  182. if (!invF.exists())
  183. invF.createNewFile();
  184. YamlConfiguration invY = new YamlConfiguration();
  185. invY.load(invF);
  186. for (int i = 0; i < inv.getContents().length; i++)
  187. invY.set(Integer.toString(i), inv.getContents()[i]);
  188. if (pInv.getHelmet() != null)
  189. invY.set("h", pInv.getHelmet());
  190. if (pInv.getChestplate() != null)
  191. invY.set("c", pInv.getChestplate());
  192. if (pInv.getLeggings() != null)
  193. invY.set("l", pInv.getLeggings());
  194. if (pInv.getBoots() != null)
  195. invY.set("b", pInv.getBoots());
  196. invY.save(invF);
  197. }
  198. catch (Exception ex){
  199. ex.printStackTrace();
  200. sender.sendMessage(ChatColor.RED + "[TTT] " + local.getMessage("inv-save-error"));
  201. }
  202. inv.clear();
  203. pInv.setArmorContents(new ItemStack[]{null, null, null, null});
  204. sender.sendMessage(ChatColor.GREEN + local.getMessage("success-join") + " " + worldName);
  205. List<String> testers = new ArrayList<String>();
  206. testers.add("ZerosAce00000");
  207. testers.add("momhipie");
  208. testers.add("xJHA929x");
  209. testers.add("jmm1999");
  210. testers.add("jon674");
  211. testers.add("HardcoreBukkit");
  212. testers.add("shiny3");
  213. testers.add("jpf6368");
  214. String addition = "";
  215. if (sender.getName().equals("AngryNerd1"))
  216. addition = ", " + ChatColor.DARK_RED + local.getMessage("creator") + ", " + ChatColor.DARK_PURPLE;
  217. else if (testers.contains(sender.getName())){
  218. addition = ", " + ChatColor.DARK_RED + local.getMessage("tester") + ", " + ChatColor.DARK_PURPLE;
  219. }
  220. Bukkit.broadcastMessage(ChatColor.DARK_PURPLE + "[TTT] " + sender.getName() + addition + " " + local.getMessage("joined-map") + " \"" + worldName + "\"");
  221. if (joinedPlayers.size() >= getConfig().getInt("minimum-players") && !time.containsKey(worldName)){
  222. for (Player p : getServer().getWorld("TTT_" + worldName).getPlayers())
  223. p.sendMessage(ChatColor.DARK_PURPLE + local.getMessage("round-starting"));
  224. time.put(worldName, getConfig().getInt("setup-time"));
  225. tasks.put(worldName, setupTimer(worldName));
  226. }
  227. else {
  228. ((Player)sender).sendMessage(ChatColor.DARK_PURPLE + local.getMessage("waiting"));
  229. }
  230. }
  231. else
  232. sender.sendMessage(ChatColor.RED + local.getMessage("map-invalid"));
  233. folder = null;
  234. tttFolder = null;
  235. }
  236. else
  237. sender.sendMessage(ChatColor.RED + "[TTT] " + local.getMessage("in-progress"));
  238. }
  239. else {
  240. sender.sendMessage(ChatColor.RED + local.getMessage("invalid-args-1"));
  241. sender.sendMessage(ChatColor.RED + local.getMessage("usage-join"));
  242. }
  243. }
  244. else
  245. sender.sendMessage(ChatColor.RED + local.getMessage("no-permission-join"));
  246. }
  247. else
  248. sender.sendMessage(ChatColor.RED + local.getMessage("must-be-ingame"));
  249. }
  250. else if (args[0].equalsIgnoreCase("quit")){
  251. if (sender instanceof Player){
  252. if (sender.hasPermission("ttt.quit")){
  253. if (joinedPlayers.containsKey(sender.getName()) || deadPlayers.containsKey(sender.getName())){
  254. WorldUtils.teleportPlayer((Player)sender);
  255. String worldName = "";
  256. if (joinedPlayers.containsKey(((Player)sender).getName()))
  257. worldName = joinedPlayers.get(((Player)sender).getName());
  258. if (deadPlayers.containsKey(((Player)sender).getName()))
  259. worldName = deadPlayers.get(((Player)sender).getName());
  260. joinedPlayers.remove(sender.getName());
  261. deadPlayers.remove(sender.getName());
  262. playerRoles.remove(sender.getName());
  263. if (getServer().getWorld("TTT_" + worldName) != null)
  264. for (Player pl : getServer().getWorld("TTT_" + worldName).getPlayers())
  265. pl.sendMessage(ChatColor.DARK_PURPLE + "[TTT] " + ((Player)sender).getName() + local.getMessage("left-game").replace("%", worldName));
  266. Player p = (Player)sender;
  267. p.getInventory().clear();
  268. File invF = new File(getDataFolder() + File.separator + "inventories" + File.separator + p.getName() + ".inv");
  269. if (invF.exists()){
  270. try {
  271. YamlConfiguration invY = new YamlConfiguration();
  272. invY.load(invF);
  273. ItemStack[] invI = new ItemStack[p.getInventory().getSize()];
  274. for (String k : invY.getKeys(false)){
  275. if (NumUtils.isInt(k))
  276. invI[Integer.parseInt(k)] = invY.getItemStack(k);
  277. else if (k.equalsIgnoreCase("h"))
  278. p.getInventory().setHelmet(invY.getItemStack(k));
  279. else if (k.equalsIgnoreCase("c"))
  280. p.getInventory().setChestplate(invY.getItemStack(k));
  281. else if (k.equalsIgnoreCase("l"))
  282. p.getInventory().setLeggings(invY.getItemStack(k));
  283. else if (k.equalsIgnoreCase("b"))
  284. p.getInventory().setBoots(invY.getItemStack(k));
  285. }
  286. p.getInventory().setContents(invI);
  287. p.updateInventory();
  288. invF.delete();
  289. }
  290. catch (Exception ex){
  291. ex.printStackTrace();
  292. p.sendMessage(ChatColor.RED + "[TTT] " + local.getMessage("inv-load-error"));
  293. }
  294. }
  295. }
  296. else
  297. sender.sendMessage(ChatColor.RED + "[TTT] " + local.getMessage("not-in-game"));
  298. }
  299. else
  300. sender.sendMessage(ChatColor.RED + "[TTT] " + local.getMessage("no-permission-quit"));
  301. }
  302. else
  303. sender.sendMessage(ChatColor.RED + "[TTT] " + local.getMessage("must-be-ingame"));
  304. }
  305. else if (args[0].equalsIgnoreCase("setspawn")){
  306. if (sender.hasPermission("ttt.setspawn")){
  307. if (sender instanceof Player){
  308. try {
  309. File spawnFile = new File(getDataFolder() + File.separator + "spawn.yml");
  310. if (!spawnFile.exists()){
  311. log.info("No spawn.yml found, creating...");
  312. spawnFile.createNewFile();
  313. }
  314. YamlConfiguration spawnYaml = new YamlConfiguration();
  315. spawnYaml.load(spawnFile);
  316. spawnYaml.set("world", ((Player)sender).getLocation().getWorld().getName());
  317. spawnYaml.set("x", ((Player)sender).getLocation().getX());
  318. spawnYaml.set("y", ((Player)sender).getLocation().getY());
  319. spawnYaml.set("z", ((Player)sender).getLocation().getZ());
  320. spawnYaml.set("pitch", ((Player)sender).getLocation().getPitch());
  321. spawnYaml.set("yaw", ((Player)sender).getLocation().getYaw());
  322. spawnYaml.save(spawnFile);
  323. }
  324. catch (Exception ex){
  325. ex.printStackTrace();
  326. }
  327. }
  328. else
  329. sender.sendMessage(ChatColor.RED + local.getMessage("must-be-ingame"));
  330. }
  331. else
  332. sender.sendMessage(ChatColor.RED + local.getMessage("no-permission"));
  333. }
  334. else {
  335. sender.sendMessage(ChatColor.RED + "[TTT] " + local.getMessage("invalid-args-2"));
  336. sender.sendMessage(ChatColor.RED + local.getMessage("usage-1"));
  337. }
  338. }
  339. else {
  340. sender.sendMessage(ChatColor.RED + local.getMessage("invalid-args-1"));
  341. sender.sendMessage(ChatColor.RED + local.getMessage("usage-1"));
  342. }
  343. return true;
  344. }
  345. return false;
  346. }
  347. @EventHandler
  348. public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e){
  349. if (e.getMessage().startsWith("kit")){
  350. if (joinedPlayers.containsKey(e.getPlayer().getName()) || deadPlayers.containsKey(e.getPlayer().getName())){
  351. e.setCancelled(true);
  352. e.getPlayer().sendMessage(ChatColor.RED + "[TTT] " + local.getMessage("no-kits"));
  353. }
  354. }
  355. else if (e.getMessage().startsWith("msg") || e.getMessage().startsWith("tell") || e.getMessage().startsWith("r") || e.getMessage().startsWith("msg") || e.getMessage().startsWith("me")){
  356. String p = e.getPlayer().getName();
  357. if (joinedPlayers.containsKey(p) || deadPlayers.containsKey(p)){
  358. e.setCancelled(true);
  359. e.getPlayer().sendMessage(ChatColor.RED + "[TTT] " + local.getMessage("no-pm"));
  360. }
  361. }
  362. }
  363. @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
  364. public void onEntityDamage(EntityDamageEvent e){
  365. if (e.getEntityType() == EntityType.PLAYER){
  366. Player p = (Player)e.getEntity();
  367. int armor = 0;
  368. if (e.getCause() == DamageCause.ENTITY_ATTACK ||
  369. e.getCause() == DamageCause.PROJECTILE ||
  370. e.getCause() == DamageCause.FIRE ||
  371. e.getCause() == DamageCause.FIRE_TICK ||
  372. e.getCause() == DamageCause.BLOCK_EXPLOSION ||
  373. e.getCause() == DamageCause.CONTACT ||
  374. e.getCause() == DamageCause.LAVA ||
  375. e.getCause() == DamageCause.ENTITY_EXPLOSION){
  376. HashMap<Material, Integer> protection = new HashMap<Material, Integer>();
  377. protection.put(Material.LEATHER_HELMET, 1);
  378. protection.put(Material.LEATHER_CHESTPLATE, 3);
  379. protection.put(Material.LEATHER_LEGGINGS, 2);
  380. protection.put(Material.LEATHER_BOOTS, 1);
  381. protection.put(Material.IRON_HELMET, 2);
  382. protection.put(Material.IRON_CHESTPLATE, 5);
  383. protection.put(Material.IRON_LEGGINGS, 3);
  384. protection.put(Material.IRON_BOOTS, 1);
  385. protection.put(Material.CHAINMAIL_HELMET, 2);
  386. protection.put(Material.CHAINMAIL_CHESTPLATE, 5);
  387. protection.put(Material.CHAINMAIL_LEGGINGS, 3);
  388. protection.put(Material.CHAINMAIL_BOOTS, 1);
  389. protection.put(Material.GOLD_HELMET, 2);
  390. protection.put(Material.GOLD_CHESTPLATE, 6);
  391. protection.put(Material.GOLD_LEGGINGS, 5);
  392. protection.put(Material.GOLD_BOOTS, 2);
  393. protection.put(Material.DIAMOND_HELMET, 3);
  394. protection.put(Material.DIAMOND_CHESTPLATE, 8);
  395. protection.put(Material.DIAMOND_LEGGINGS, 6);
  396. protection.put(Material.DIAMOND_BOOTS, 3);
  397. if (p.getInventory().getArmorContents()[0] != null)
  398. if (protection.containsKey(p.getInventory().getArmorContents()[0].getType()))
  399. armor += protection.get(p.getInventory().getArmorContents()[0].getType());
  400. if (p.getInventory().getArmorContents()[1] != null)
  401. if (protection.containsKey(p.getInventory().getArmorContents()[1].getType()))
  402. armor += protection.get(p.getInventory().getArmorContents()[1].getType());
  403. if (p.getInventory().getArmorContents()[2] != null)
  404. if (protection.containsKey(p.getInventory().getArmorContents()[2].getType()))
  405. armor += protection.get(p.getInventory().getArmorContents()[2].getType());
  406. if (p.getInventory().getArmorContents()[3] != null)
  407. if (protection.containsKey(p.getInventory().getArmorContents()[3].getType()))
  408. armor += protection.get(p.getInventory().getArmorContents()[3].getType());
  409. }
  410. if (e.getDamage() - ((armor * .04) * e.getDamage()) >= ((Player)e.getEntity()).getHealth()){
  411. if (joinedPlayers.containsKey(p.getName())){
  412. if (playerRoles.containsKey(p.getName())){
  413. e.setCancelled(true);
  414. p.setHealth(20);
  415. p.sendMessage(ChatColor.DARK_PURPLE + local.getMessage("dead"));
  416. String worldName = p.getWorld().getName().replace("TTT_", "");
  417. joinedPlayers.remove(p.getName());
  418. deadPlayers.put(p.getName(), worldName);
  419. Block block = p.getLocation().getBlock();
  420. block.setType(Material.CHEST);
  421. Chest chest = (Chest)block.getState();
  422. // player identifier
  423. ItemStack id = new ItemStack(Material.PAPER, 1);
  424. ItemMeta idMeta = id.getItemMeta();
  425. idMeta.setDisplayName(local.getMessage("id"));
  426. List<String> idLore = new ArrayList<String>();
  427. idLore.add(local.getMessage("body-of"));
  428. idLore.add(((Player)e.getEntity()).getName());
  429. idMeta.setLore(idLore);
  430. id.setItemMeta(idMeta);
  431. // role identifier
  432. ItemStack ti = new ItemStack(Material.WOOL, 1);
  433. ItemMeta tiMeta = ti.getItemMeta();
  434. if (playerRoles.get(p.getName()) == 0){
  435. ti.setDurability((short)5);
  436. tiMeta.setDisplayName("�2" + local.getMessage("innocent"));
  437. List<String> tiLore = new ArrayList<String>();
  438. tiLore.add(local.getMessage("innocent-id"));
  439. tiMeta.setLore(tiLore);
  440. }
  441. else if (playerRoles.get(p.getName()) == 1){
  442. ti.setDurability((short)14);
  443. tiMeta.setDisplayName("�4" + local.getMessage("traitor"));
  444. List<String> lore = new ArrayList<String>();
  445. lore.add(local.getMessage("traitor-id"));
  446. tiMeta.setLore(lore);
  447. }
  448. else if (playerRoles.get(p.getName()) == 1){
  449. ti.setDurability((short)11);
  450. tiMeta.setDisplayName("�1" + local.getMessage("detective"));
  451. List<String> lore = new ArrayList<String>();
  452. lore.add(local.getMessage("detective-id"));
  453. tiMeta.setLore(lore);
  454. }
  455. ti.setItemMeta(tiMeta);
  456. chest.getInventory().addItem(new ItemStack[]{id, ti});
  457. bodies.add(new Body(p.getName(), playerRoles.get(p.getName()), FixedLocation.getFixedLocation(block), System.currentTimeMillis()));
  458. }
  459. else
  460. p.setHealth(20);
  461. }
  462. }
  463. if (deadPlayers.containsKey(p.getName())){
  464. e.setCancelled(true);
  465. }
  466. if (e instanceof EntityDamageByEntityEvent){
  467. EntityDamageByEntityEvent ed = (EntityDamageByEntityEvent)e;
  468. if (ed.getDamager().getType() == EntityType.PLAYER){
  469. if (((Player)ed.getDamager()).getItemInHand() != null)
  470. if (((Player)ed.getDamager()).getItemInHand().getItemMeta() != null)
  471. if (((Player)ed.getDamager()).getItemInHand().getItemMeta().getDisplayName() != null)
  472. if (((Player)ed.getDamager()).getItemInHand().getItemMeta().getDisplayName().equals("�5" + local.getMessage("crowbar")))
  473. e.setDamage(getConfig().getInt("crowbar-damage"));
  474. if (deadPlayers.containsKey(((Player)ed.getDamager()).getName())){
  475. e.setCancelled(true);
  476. }
  477. if (joinedPlayers.containsKey(((Player)ed.getDamager()).getName())){
  478. if (gameTime.get(joinedPlayers.get(((Player)ed.getDamager()).getName())) == null)
  479. e.setCancelled(true);
  480. }
  481. }
  482. }
  483. }
  484. }
  485. public int setupTimer(final String worldName){
  486. return getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
  487. public void run(){
  488. // verify that all players are still online
  489. List<String> offlinePlayers = new ArrayList<String>();
  490. for (String pl : joinedPlayers.keySet()){
  491. if (joinedPlayers.get(pl).equals(worldName)){
  492. Player p = getServer().getPlayer(pl);
  493. if (p != null){
  494. if (!getServer().getWorld("TTT_" + worldName).getPlayers().contains(p)){
  495. offlinePlayers.add(pl);
  496. }
  497. }
  498. }
  499. }
  500. for (String pl : deadPlayers.keySet()){
  501. if (deadPlayers.get(pl).equals(worldName)){
  502. Player p = getServer().getPlayer(pl);
  503. if (p != null){
  504. if (!getServer().getWorld("TTT_" + worldName).getPlayers().contains(p)){
  505. offlinePlayers.add(pl);
  506. Bukkit.broadcastMessage("[TTT]" + pl + " " + local.getMessage("left-map") + " \"" + worldName + "\"");
  507. }
  508. }
  509. }
  510. }
  511. for (String p : offlinePlayers){
  512. if (joinedPlayers.containsKey(p)){
  513. joinedPlayers.remove(p);
  514. }
  515. if (deadPlayers.containsKey(p)){
  516. deadPlayers.remove(p);
  517. }
  518. }
  519. int currentTime = time.get(worldName);
  520. int playerCount = 0;
  521. for (String p : joinedPlayers.keySet()){
  522. if (joinedPlayers.get(p).equals(worldName))
  523. playerCount += 1;
  524. }
  525. if (playerCount >= getConfig().getInt("minimum-players")){
  526. if((currentTime % 10) == 0 && currentTime > 0){
  527. for (Player p : getServer().getWorld("TTT_" + worldName).getPlayers()){
  528. p.sendMessage(ChatColor.DARK_PURPLE + local.getMessage("begin") + " " + currentTime + " " + local.getMessage("seconds") + "!");
  529. }
  530. }
  531. else if (currentTime > 0 && currentTime < 10){
  532. for (Player p : getServer().getWorld("TTT_" + worldName).getPlayers()){
  533. p.sendMessage(ChatColor.DARK_PURPLE + local.getMessage("begin") + " " + currentTime + " " + local.getMessage("seconds") + "!");
  534. }
  535. }
  536. else if (currentTime <= 0){
  537. int players = getServer().getWorld("TTT_" + worldName).getPlayers().size();
  538. int traitorNum = 0;
  539. int limit = (int)(players * getConfig().getDouble("traitor-ratio"));
  540. if (limit == 0)
  541. limit = 1;
  542. List<String> innocents = new ArrayList<String>();
  543. List<String> traitors = new ArrayList<String>();
  544. List<String> detectives = new ArrayList<String>();
  545. for (Player p : getServer().getWorld("TTT_" + worldName).getPlayers()){
  546. innocents.add(p.getName());
  547. p.sendMessage(ChatColor.DARK_PURPLE + local.getMessage("begun"));
  548. }
  549. while (traitorNum < limit){
  550. Random randomGenerator = new Random();
  551. int index = randomGenerator.nextInt(players);
  552. String traitor = innocents.get(index);
  553. if (innocents.contains(traitor)){
  554. innocents.remove(traitor);
  555. traitors.add(traitor);
  556. traitorNum += 1;
  557. }
  558. }
  559. int dLimit = (int)(players * getConfig().getDouble("detective-ratio"));
  560. if (players >= getConfig().getInt("minimum-players-for-detective") && dLimit == 0)
  561. dLimit += 1;
  562. int detectiveNum = 0;
  563. while (detectiveNum < dLimit){
  564. Random randomGenerator = new Random();
  565. int index = randomGenerator.nextInt(innocents.size());
  566. String detective = innocents.get(index);
  567. innocents.remove(detective);
  568. detectives.add(detective);
  569. detectiveNum += 1;
  570. }
  571. ItemStack crowbar = new ItemStack(Material.IRON_SWORD, 1);
  572. ItemMeta cbMeta = crowbar.getItemMeta();
  573. cbMeta.setDisplayName("�5" + local.getMessage("crowbar"));
  574. crowbar.setItemMeta(cbMeta);
  575. ItemStack gun = new ItemStack(Material.ANVIL, 1);
  576. ItemMeta gunMeta = crowbar.getItemMeta();
  577. gunMeta.setDisplayName("�5" + local.getMessage("gun"));
  578. gun.setItemMeta(gunMeta);
  579. ItemStack ammo = new ItemStack(Material.ARROW, 28);
  580. ItemStack dnaScanner = new ItemStack(Material.COMPASS, 1);
  581. ItemMeta dnaMeta = dnaScanner.getItemMeta();
  582. dnaMeta.setDisplayName("�1" + local.getMessage("dna-scanner"));
  583. dnaScanner.setItemMeta(dnaMeta);
  584. for (String p : joinedPlayers.keySet()){
  585. Player pl = getServer().getPlayer(p);
  586. if (innocents.contains(p)){
  587. playerRoles.put(p, 0);
  588. pl.sendMessage(ChatColor.DARK_GREEN + local.getMessage("you-are-innocent"));
  589. pl.getInventory().addItem(new ItemStack[]{crowbar, gun, ammo});
  590. }
  591. else if (traitors.contains(p)){
  592. playerRoles.put(p, 1);
  593. pl.sendMessage(ChatColor.DARK_RED + local.getMessage("you-are-traitor"));
  594. if (traitors.size() > 1){
  595. pl.sendMessage(ChatColor.DARK_RED + local.getMessage("allies"));
  596. for (String t : traitors)
  597. pl.sendMessage("- " + t);
  598. }
  599. else
  600. pl.sendMessage(ChatColor.DARK_RED + local.getMessage("alone"));
  601. pl.getInventory().addItem(new ItemStack[]{crowbar, gun, ammo});
  602. }
  603. else if (detectives.contains(p)){
  604. playerRoles.put(p, 2);
  605. pl.sendMessage(ChatColor.BLUE + local.getMessage("you-are-detective"));
  606. pl.getInventory().addItem(new ItemStack[]{crowbar, gun, ammo, dnaScanner});
  607. }
  608. pl.setHealth(20);
  609. pl.setFoodLevel(20);
  610. }
  611. time.remove(worldName);
  612. gameTime.put(worldName, getConfig().getInt("time-limit"));
  613. Bukkit.getScheduler().cancelTask(tasks.get(worldName));
  614. tasks.remove(worldName);
  615. gameTimer(worldName);
  616. }
  617. if (currentTime > 0)
  618. time.put(worldName, currentTime - 1);
  619. }
  620. else {
  621. time.remove(worldName);
  622. Bukkit.getScheduler().cancelTask(tasks.get(worldName));
  623. for (Player p : getServer().getWorld("TTT_" + worldName).getPlayers()){
  624. p.sendMessage(ChatColor.DARK_PURPLE + local.getMessage("waiting"));
  625. }
  626. }
  627. }
  628. }, 0L, 20L);
  629. }
  630. public void gameTimer(final String worldName){
  631. tasks.put(worldName, getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
  632. @SuppressWarnings("deprecation")
  633. public void run(){
  634. // verify that all players are still online
  635. List<String> offlinePlayers = new ArrayList<String>();
  636. for (String pl : joinedPlayers.keySet()){
  637. if (joinedPlayers.get(pl).equals(worldName)){
  638. Player p = getServer().getPlayer(pl);
  639. if (p != null){
  640. if (!getServer().getWorld("TTT_" + worldName).getPlayers().contains(p)){
  641. offlinePlayers.add(pl);
  642. }
  643. }
  644. }
  645. }
  646. for (String pl : deadPlayers.keySet()){
  647. if (deadPlayers.get(pl).equals(worldName)){
  648. Player p = getServer().getPlayer(pl);
  649. if (p != null){
  650. if (!getServer().getWorld("TTT_" + worldName).getPlayers().contains(p)){
  651. offlinePlayers.add(pl);
  652. Bukkit.broadcastMessage("[TTT]" + pl + " " + local.getMessage("left-map") + " \"" + worldName + "\"");
  653. }
  654. }
  655. }
  656. }
  657. for (String p : offlinePlayers){
  658. if (joinedPlayers.containsKey(p)){
  659. joinedPlayers.remove(p);
  660. }
  661. if (deadPlayers.containsKey(p)){
  662. deadPlayers.remove(p);
  663. }
  664. for (Player pl : getServer().getWorld("TTT_" + worldName).getPlayers())
  665. pl.sendMessage(ChatColor.DARK_PURPLE + "[TTT] " + p + local.getMessage("left-game").replace("%", worldName));
  666. }
  667. // set compass targets
  668. for (String p : tracking.keySet()){
  669. Player tracker = getServer().getPlayer(p);
  670. Player killer = getServer().getPlayer(tracking.get(p));
  671. if (tracker != null || killer != null)
  672. if (!offlinePlayers.contains(tracker) && !offlinePlayers.contains(killer))
  673. tracker.setCompassTarget(killer.getLocation());
  674. }
  675. // check if game is over
  676. boolean iLeft = false;
  677. boolean tLeft = false;
  678. for (String p : playerRoles.keySet()){
  679. if (playerRoles.get(p) == 0 && joinedPlayers.containsKey(p)){
  680. if (joinedPlayers.get(p).equals(worldName)){
  681. iLeft = true;
  682. }
  683. }
  684. if (playerRoles.get(p) == 1 && joinedPlayers.containsKey(p)){
  685. if (joinedPlayers.get(p).equals(worldName)){
  686. tLeft = true;
  687. }
  688. }
  689. }
  690. if (!(tLeft && iLeft)){
  691. List<Body> removeBodies = new ArrayList<Body>();
  692. List<Body> removeFoundBodies = new ArrayList<Body>();
  693. for (Body b : bodies){
  694. if (deadPlayers.get(b.getName()) != null){
  695. if (deadPlayers.get(b.getName()).equals(worldName)){
  696. removeBodies.add(b);
  697. if (foundBodies.contains(b))
  698. removeFoundBodies.add(b);
  699. }
  700. }
  701. }
  702. for (Body b : removeBodies)
  703. bodies.remove(b);
  704. for (Body b : removeFoundBodies)
  705. foundBodies.remove(b);
  706. removeBodies.clear();
  707. removeFoundBodies.clear();
  708. if (!tLeft)
  709. Bukkit.broadcastMessage(ChatColor.DARK_GREEN + "[TTT] " + local.getMessage("innocent-win").replace("%", "\"" + worldName + "\"") + "!");
  710. if (!iLeft)
  711. Bukkit.broadcastMessage(ChatColor.DARK_RED + "[TTT] " + local.getMessage("traitor-win").replace("%", "\"" + worldName + "\"") + "!");
  712. for (Player p : getServer().getWorld("TTT_" + worldName).getPlayers()){
  713. joinedPlayers.remove(p.getName());
  714. playerRoles.remove(p.getName());
  715. if (deadPlayers.containsKey(p.getName())){
  716. p.setAllowFlight(false);
  717. for (Player pl : getServer().getOnlinePlayers()){
  718. pl.showPlayer(p);
  719. }
  720. deadPlayers.remove(p.getName());
  721. }
  722. p.getInventory().clear();
  723. File invF = new File(getDataFolder() + File.separator + "inventories" + File.separator + p.getName() + ".inv");
  724. if (invF.exists()){
  725. try {
  726. YamlConfiguration invY = new YamlConfiguration();
  727. invY.load(invF);
  728. ItemStack[] invI = new ItemStack[p.getInventory().getSize()];
  729. for (String k : invY.getKeys(false)){
  730. invI[Integer.parseInt(k)] = invY.getItemStack(k);
  731. }
  732. p.getInventory().setContents(invI);
  733. p.updateInventory();
  734. invF.delete();
  735. }
  736. catch (Exception ex){
  737. ex.printStackTrace();
  738. p.sendMessage(ChatColor.RED + "[TTT] " + local.getMessage("inv-load-error"));
  739. }
  740. }
  741. gameTime.remove(worldName);
  742. WorldUtils.teleportPlayer(p);
  743. }
  744. gameTime.remove(worldName);
  745. getServer().getScheduler().cancelTask(tasks.get(worldName));
  746. tasks.remove(tasks.get(worldName));
  747. getServer().unloadWorld("TTT_" + worldName, false);
  748. rollbackWorld(worldName);
  749. }
  750. else {
  751. int newTime = gameTime.get(worldName) - 1;
  752. gameTime.remove(worldName);
  753. gameTime.put(worldName, newTime);
  754. if (newTime % 60 == 0 && newTime >= 60){
  755. for (Player p : getServer().getWorld("TTT_" + worldName).getPlayers()){
  756. p.sendMessage(ChatColor.DARK_PURPLE + Integer.toString(newTime / 60) + " " + local.getMessage("minutes") + " " + local.getMessage("left"));
  757. }
  758. }
  759. else if (newTime % 10 == 0 && newTime > 10 && newTime < 60){
  760. for (Player p : getServer().getWorld("TTT_" + worldName).getPlayers()){
  761. p.sendMessage(ChatColor.DARK_PURPLE + Integer.toString(newTime) + " " + local.getMessage("seconds") + " " + local.getMessage("left"));
  762. }
  763. }
  764. else if (newTime < 10 && newTime > 0){
  765. for (Player p : getServer().getWorld("TTT_" + worldName).getPlayers()){
  766. p.sendMessage(ChatColor.DARK_PURPLE + Integer.toString(newTime) + " " + local.getMessage("seconds") + " " + local.getMessage("left"));
  767. }
  768. }
  769. else if (newTime <= 0){
  770. List<Body> removeBodies = new ArrayList<Body>();
  771. List<Body> removeFoundBodies = new ArrayList<Body>();
  772. for (Body b : bodies){
  773. if (deadPlayers.get(b.getName()) != null){
  774. if (deadPlayers.get(b.getName()).equals(worldName)){
  775. removeBodies.add(b);
  776. if (foundBodies.contains(b))
  777. removeFoundBodies.add(b);
  778. }
  779. }
  780. }
  781. for (Body b : removeBodies)
  782. bodies.remove(b);
  783. for (Body b : removeFoundBodies)
  784. foundBodies.remove(b);
  785. removeBodies.clear();
  786. removeFoundBodies.clear();
  787. for (Player p : getServer().getWorld("TTT_" + worldName).getPlayers()){
  788. p.sendMessage(ChatColor.DARK_GREEN + "[TTT] " + local.getMessage("innocent-win").replace("%", "\"" + worldName + "\"") + "!");
  789. joinedPlayers.remove(p.getName());
  790. playerRoles.remove(p.getName());
  791. if (deadPlayers.containsKey(p.getName())){
  792. p.setAllowFlight(false);
  793. for (Player pl : getServer().getOnlinePlayers()){
  794. pl.showPlayer(p);
  795. }
  796. deadPlayers.remove(p.getName());
  797. }
  798. p.getInventory().clear();
  799. File invF = new File(getDataFolder() + File.separator + "inventories" + File.separator + p.getName() + ".inv");
  800. if (invF.exists()){
  801. try {
  802. YamlConfiguration invY = new YamlConfiguration();
  803. invY.load(invF);
  804. ItemStack[] invI = new ItemStack[p.getInventory().getSize()];
  805. for (String k : invY.getKeys(false)){
  806. if (NumUtils.isInt(k))
  807. invI[Integer.parseInt(k)] = invY.getItemStack(k);
  808. }
  809. p.getInventory().setContents(invI);
  810. if (invY.getItemStack("h") != null)
  811. p.getInventory().setHelmet(invY.getItemStack("h"));
  812. if (invY.getItemStack("c") != null)
  813. p.getInventory().setChestplate(invY.getItemStack("c"));
  814. if (invY.getItemStack("l") != null)
  815. p.getInventory().setLeggings(invY.getItemStack("l"));
  816. if (invY.getItemStack("b") != null)
  817. p.getInventory().setBoots(invY.getItemStack("b"));
  818. p.updateInventory();
  819. invF.delete();
  820. }
  821. catch (Exception ex){
  822. ex.printStackTrace();
  823. p.sendMessage(ChatColor.RED + "[TTT] " + local.getMessage("inv-load-fail"));
  824. }
  825. }
  826. gameTime.remove(worldName);
  827. WorldUtils.teleportPlayer(p);
  828. }
  829. Bukkit.getScheduler().cancelTask(tasks.get(worldName));
  830. getServer().unloadWorld("TTT_" + worldName, false);
  831. rollbackWorld(worldName);
  832. }
  833. }
  834. // hide dead players
  835. for (String p : deadPlayers.keySet()){
  836. if (getServer().getPlayer(p) != null){
  837. if (getServer().getWorld("TTT_" + worldName).getPlayers().contains(getServer().getPlayer(p))){
  838. getServer().getPlayer(p).setAllowFlight(true);
  839. for (String other : joinedPlayers.keySet()){
  840. if (joinedPlayers.get(other).equals(worldName))
  841. getServer().getPlayer(other).hidePlayer(getServer().getPlayer(p));
  842. }
  843. }
  844. }
  845. }
  846. }
  847. }, 0L, 20L));
  848. }
  849. public void rollbackWorld(final String worldName){
  850. getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
  851. public void run(){
  852. File folder = new File(worldName);
  853. if (folder.exists()){
  854. if (WorldUtils.isWorld(folder)){
  855. File newFolder = new File("TTT_" + worldName);
  856. try {
  857. FileUtils.copyDirectory(folder, newFolder);
  858. log.info("[TTT] " + local.getMessage("rollback") + " \"" + worldName + "\"!");
  859. }
  860. catch (IOException ex){
  861. log.info("[TTT] " + local.getMessage("folder-error") + " " + worldName);
  862. ex.printStackTrace();
  863. }
  864. }
  865. else
  866. log.info("[TTT] " + local.getMessage("cannot-load-world"));
  867. }
  868. }
  869. }, 100L);
  870. }
  871. @EventHandler(priority = EventPriority.HIGHEST)
  872. public void onInventoryClick(InventoryClickEvent e){
  873. for (HumanEntity he : e.getViewers()){
  874. Player p = (Player)he;
  875. if (joinedPlayers.containsKey(p.getName())){
  876. if (e.getInventory().getType() == InventoryType.CHEST){
  877. Block block = ((Chest)e.getInventory().getHolder()).getBlock();
  878. for (Body b : bodies){
  879. if (b.getLocation().equals(FixedLocation.getFixedLocation(block))){
  880. e.setCancelled(true);
  881. break;
  882. }
  883. }
  884. }
  885. }
  886. else if (deadPlayers.containsKey(p.getName()))
  887. e.setCancelled(true);
  888. else if (discreet.contains(p.getName()))
  889. e.setCancelled(true);
  890. }
  891. }
  892. @SuppressWarnings("deprecation")
  893. @EventHandler (priority = EventPriority.HIGH)
  894. public void onPlayerChat(AsyncPlayerChatEvent e){
  895. for (Player p : getServer().getOnlinePlayers()){
  896. // check if sender is in TTT game
  897. if (joinedPlayers.containsKey(e.getPlayer().getName())){
  898. if (joinedPlayers.containsKey(p.getName()) || deadPlayers.containsKey(p.getName())){
  899. if (!p.getWorld().getName().equals(e.getPlayer().getWorld().getName()))
  900. e.getRecipients().remove(p);
  901. }
  902. else
  903. e.getRecipients().remove(p);
  904. }
  905. // check if sender is dead
  906. else if (deadPlayers.containsKey(e.getPlayer().getName())){
  907. if (deadPlayers.containsKey(p.getName())){
  908. if (!p.getWorld().getName().equals("TTT_" + deadPlayers.get(e.getPlayer().getName())))
  909. e.getRecipients().remove(p);
  910. }
  911. else
  912. e.getRecipients().remove(p);
  913. }
  914. }
  915. if (playerRoles.containsKey(e.getPlayer().getName())){
  916. if (playerRoles.get(e.getPlayer().getName()) == 2){
  917. final Player player = e.getPlayer();
  918. e.getPlayer().setDisplayName(ChatColor.BLUE + "[Detective] " + e.getPlayer().getDisplayName());
  919. getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable(){
  920. public void run(){
  921. String name = player.getDisplayName();
  922. name = name.replace(ChatColor.BLUE + "[Detective] ", "");
  923. player.setDisplayName(name);
  924. }
  925. }, 1);
  926. }
  927. }
  928. }
  929. @SuppressWarnings("deprecation")
  930. @EventHandler(priority = EventPriority.MONITOR)
  931. public void onPlayerInteract(PlayerInteractEvent e){
  932. if (!deadPlayers.containsKey(e.getPlayer().getName())){
  933. if (e.getAction() == Action.RIGHT_CLICK_BLOCK){
  934. if (e.getClickedBlock().getType() == Material.CHEST){
  935. int index = -1;
  936. for (int i = 0; i < bodies.size(); i++){
  937. if (bodies.get(i).getLocation().equals(FixedLocation.getFixedLocation(e.getClickedBlock()))){
  938. index = i;
  939. break;
  940. }
  941. }
  942. if (index != -1){
  943. boolean found = false;
  944. for (Body b : foundBodies){
  945. if (b.getLocation().equals(FixedLocation.getFixedLocation(e.getClickedBlock()))){
  946. found = true;
  947. break;
  948. }
  949. }
  950. if (!found){
  951. for (Player p : e.getPlayer().getWorld().getPlayers()){
  952. if (bodies.get(index).getRole() == 0)
  953. p.sendMessage(ChatColor.DARK_GREEN + e.getPlayer().getName() + " " + local.getMessage("found-body").replace("%", bodies.get(index).getName()) + ". " + local.getMessage("was-innocent"));
  954. else if (bodies.get(index).getRole() == 1)
  955. p.sendMessage(ChatColor.DARK_RED + e.getPlayer().getName() + " " + local.getMessage("found-body").replace("%", bodies.get(index).getName()) + ". " + local.getMessage("was-traitor"));
  956. else if (bodies.get(index).getRole() == 2)
  957. p.sendMessage(ChatColor.DARK_BLUE + e.getPlayer().getName() + " " + local.getMessage("found-body").replace("%", bodies.get(index).getName()) + ". " + local.getMessage("was-detective"));
  958. }
  959. foundBodies.add(bodies.get(index));
  960. }
  961. if (playerRoles.get(e.getPlayer().getName()) == 2){
  962. if (e.getPlayer().getItemInHand() != null){
  963. if (e.getPlayer().getItemInHand().getType() == Material.COMPASS){
  964. if (e.getPlayer().getItemInHand().getItemMeta() != null){
  965. if (e.getPlayer().getItemInHand().getItemMeta().getDisplayName() != null){
  966. if (e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equals("�1" + local.getMessage("dna-scanner"))){
  967. e.setCancelled(true);
  968. Player killer = getServer().getPlayer(killers.get(bodies.get(index)));
  969. if (killer != null){
  970. if (joinedPlayers.containsKey(killer.getName())){
  971. tracking.remove(e.getPlayer().getName());
  972. tracking.put(e.getPlayer().getName(), killer.getName());
  973. e.getPlayer().sendMessage(ChatColor.BLUE + local.getMessage("collected-dna").replace("%", bodies.get(index).getName()));
  974. }
  975. else
  976. e.getPlayer().sendMessage(ChatColor.BLUE + local.getMessage("killer-left"));
  977. }
  978. else
  979. e.getPlayer().sendMessage(ChatColor.BLUE + local.getMessage("killer-left"));
  980. }
  981. }
  982. }
  983. }
  984. }
  985. }
  986. }
  987. }
  988. }
  989. if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR){
  990. if (e.getPlayer().getItemInHand() != null){
  991. if (e.getPlayer().getItemInHand().getItemMeta() != null){
  992. if (e.getPlayer().getItemInHand().getItemMeta().getDisplayName() != null){
  993. if (e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equals("�5" + local.getMessage("gun"))){
  994. if ((joinedPlayers.containsKey(e.getPlayer().getName()) || getConfig().getBoolean("guns-outside-arenas")) && !deadPlayers.containsKey(e.getPlayer().getName())){
  995. e.setCancelled(true);
  996. if (e.getPlayer().getInventory().contains(Material.ARROW) || !getConfig().getBoolean("require-ammo-for-guns")){
  997. if (getConfig().getBoolean("require-ammo-for-guns")){
  998. removeArrow(e.getPlayer().getInventory());
  999. e.getPlayer().updateInventory();
  1000. }
  1001. e.getPlayer().launchProjectile(Arrow.class);
  1002. }
  1003. else
  1004. e.getPlayer().sendMessage(ChatColor.RED + local.getMessage("need-ammo"));
  1005. }
  1006. }
  1007. }
  1008. }
  1009. }
  1010. }
  1011. }
  1012. else {
  1013. e.setCancelled(true);
  1014. if (deadPlayers.containsKey(e.getPlayer().getName())){
  1015. if (e.getClickedBlock() != null){
  1016. for (Body b : bodies){
  1017. if (b.getLocation().equals(FixedLocation.getFixedLocation(e.getClickedBlock()))){
  1018. if (e.getClickedBlock().getType() == Material.CHEST){
  1019. Inventory chestinv = ((Chest)e.getClickedBlock().getState()).getInventory();
  1020. Inventory inv = getServer().createInventory(null, chestinv.getSize());
  1021. inv.setContents(chestinv.getContents());
  1022. e.getPlayer().sendMessage(ChatColor.DARK_PURPLE + local.getMessage("discreet"));
  1023. discreet.add(e.getPlayer().getName());
  1024. e.getPlayer().openInventory(inv);
  1025. }
  1026. break;
  1027. }
  1028. }
  1029. }
  1030. }
  1031. }
  1032. }
  1033. @EventHandler
  1034. public void onBlockPlace(BlockPlaceEvent e){
  1035. if (joinedPlayers.containsKey(e.getPlayer().getName()) || deadPlayers.containsKey(e.getPlayer().getName()))
  1036. e.setCancelled(true);
  1037. }
  1038. @EventHandler
  1039. public void onBlockBreak(BlockBreakEvent e){
  1040. if (joinedPlayers.containsKey(e.getPlayer().getName()) || deadPlayers.containsKey(e.getPlayer().getName()))
  1041. e.setCancelled(true);
  1042. }
  1043. public void removeArrow(Inventory inv){
  1044. for (int i = 0; i < inv.getContents().length; i++){
  1045. ItemStack is = inv.getItem(i);
  1046. if (is != null){
  1047. if (is.getType() == Material.ARROW){
  1048. if (is.getAmount() == 1)
  1049. inv.setItem(i, null);
  1050. else if (is.getAmount() > 1)
  1051. is.setAmount(is.getAmount() - 1);
  1052. break;
  1053. }
  1054. }
  1055. }
  1056. }
  1057. @EventHandler
  1058. public void onPlayerPickupItem(PlayerPickupItemEvent e){
  1059. if (deadPlayers.containsKey(e.getPlayer().getName()))
  1060. e.setCancelled(true);
  1061. }
  1062. @EventHandler
  1063. public void onPlayerDropItem(PlayerDropItemEvent e){
  1064. if (joinedPlayers.containsKey(e.getPlayer().getName()) || deadPlayers.containsKey(e.getPlayer().getName())){
  1065. e.setCancelled(true);
  1066. e.getPlayer().sendMessage(ChatColor.RED + "[TTT] " + local.getMessage("no-drop"));
  1067. }
  1068. }
  1069. @EventHandler
  1070. public void onFoodDeplete(FoodLevelChangeEvent e){
  1071. if (e.getEntity().getType() == EntityType.PLAYER){
  1072. Player p = (Player)e.getEntity();
  1073. if (joinedPlayers.containsKey(p.getName()) || deadPlayers.containsKey(p.getName()))
  1074. e.setCancelled(true);
  1075. }
  1076. }
  1077. @EventHandler
  1078. public void onInventoryClose(InventoryCloseEvent e){
  1079. if (discreet.contains(e.getPlayer().getName()))
  1080. discreet.remove(e.getPlayer().getName());
  1081. }
  1082. @EventHandler
  1083. public void onPlayerQuit(PlayerQuitEvent e){
  1084. String p = e.getPlayer().getName();
  1085. if (playerRoles.containsKey(p)){
  1086. String worldName = "";
  1087. if (joinedPlayers.containsKey(p)){
  1088. worldName = joinedPlayers.get(p);
  1089. joinedPlayers.remove(p);
  1090. }
  1091. if (deadPlayers.containsKey(p)){
  1092. worldName = deadPlayers.get(p);
  1093. deadPlayers.remove(p);
  1094. }
  1095. for (Player pl : getServer().getWorld("TTT_" + worldName).getPlayers())
  1096. pl.sendMessage(ChatColor.DARK_PURPLE + "[TTT] " + p + local.getMessage("left-game").replace("%", worldName));
  1097. playerRoles.remove(p);
  1098. for (Player pl : getServer().getWorld("TTT_" + worldName).getPlayers())
  1099. pl.sendMessage(ChatColor.DARK_PURPLE + "[TTT] " + p + local.getMessage("left-game").replace("%", worldName));
  1100. }
  1101. }
  1102. @EventHandler
  1103. public void onPlayerTeleport(PlayerTeleportEvent e){
  1104. String p = e.getPlayer().getName();
  1105. if (joinedPlayers.containsKey(p)){
  1106. if (e.getPlayer().getWorld().getName().replace("TTT_", "") != joinedPlayers.get(p)){
  1107. playerRoles.remove(p);
  1108. for (Player pl : getServer().getWorld("TTT_" + joinedPlayers.get(p)).getPlayers())
  1109. pl.sendMessage(ChatColor.DARK_PURPLE + "[TTT] " + p + local.getMessage("left-game").replace("%", joinedPlayers.get(p)));
  1110. joinedPlayers.remove(p);
  1111. }
  1112. }
  1113. else if (deadPlayers.containsKey(p)){
  1114. if (e.getPlayer().getWorld().getName().replace("TTT_", "") != deadPlayers.get(p)){
  1115. playerRoles.remove(p);
  1116. for (Player pl : getServer().getWorld("TTT_" + joinedPlayers.get(p)).getPlayers())
  1117. pl.sendMessage(ChatColor.DARK_PURPLE + "[TTT] " + p + local.getMessage("left-game").replace("%", deadPlayers.get(p)));
  1118. deadPlayers.remove(p);
  1119. }
  1120. }
  1121. }
  1122. public void onHealthRegenerate(EntityRegainHealthEvent e){
  1123. if (e.getEntity() instanceof Player){
  1124. Player p = (Player)e.getEntity();
  1125. if (joinedPlayers.containsKey(p.getName())){
  1126. if (gameTime.get(joinedPlayers.get(p.getName())) != null)
  1127. e.setCancelled(true);
  1128. }
  1129. }
  1130. }
  1131. }