Made world-change update execute next tick
[disguisecraft:disguisecraft.git] / src / pgDev / bukkit / DisguiseCraft / listeners / DCMainListener.java
1 package pgDev.bukkit.DisguiseCraft.listeners;\r
2 \r
3 import java.util.concurrent.ExecutorService;\r
4 import java.util.concurrent.Executors;\r
5 \r
6 import org.bukkit.ChatColor;\r
7 import org.bukkit.entity.Player;\r
8 import org.bukkit.event.*;\r
9 import org.bukkit.event.entity.EntityTargetEvent;\r
10 import org.bukkit.event.entity.EntityTargetEvent.TargetReason;\r
11 import org.bukkit.event.player.*;\r
12 \r
13 import pgDev.bukkit.DisguiseCraft.DisguiseCraft;\r
14 import pgDev.bukkit.DisguiseCraft.disguise.*;\r
15 import pgDev.bukkit.DisguiseCraft.listeners.attack.InvalidInteractHandler;\r
16 import pgDev.bukkit.DisguiseCraft.threading.NamedThreadFactory;\r
17 import pgDev.bukkit.DisguiseCraft.update.DCUpdateNotifier;\r
18 \r
19 public class DCMainListener implements Listener {\r
20         final DisguiseCraft plugin;\r
21         \r
22         public ExecutorService invalidInteractExecutor = Executors.newFixedThreadPool(\r
23                         DisguiseCraft.pluginSettings.pvpThreads, new NamedThreadFactory("DCInvalidInteractHandler"));\r
24         \r
25         public DCMainListener(final DisguiseCraft plugin) {\r
26                 this.plugin = plugin;\r
27         }\r
28         \r
29         @EventHandler(priority = EventPriority.LOW)\r
30         public void onPlayerJoin(PlayerJoinEvent event) {\r
31                 Player player = event.getPlayer();\r
32                 \r
33                 // Show disguises to newly joined players\r
34                 plugin.showWorldDisguises(player);\r
35                 \r
36                 // If he was a disguise-quitter, tell him\r
37                 if (plugin.disguiseQuitters.contains(player.getName())) {\r
38                         event.getPlayer().sendMessage(ChatColor.RED + "You were undisguised because you left the server.");\r
39                         plugin.disguiseQuitters.remove(player.getName());\r
40                 }\r
41                 \r
42                 // Has a disguise?\r
43                 if (plugin.disguiseDB.containsKey(player.getName())) {\r
44                         Disguise disguise = plugin.disguiseDB.get(player.getName());\r
45                         if (disguise.hasPermission(player)) {\r
46                                 plugin.disguiseIDs.put(disguise.entityID, player);\r
47                                 plugin.sendDisguise(player, null);\r
48                                 if (disguise.type.isPlayer()) {\r
49                                         player.sendMessage(ChatColor.GOLD + "You were redisguised as player: " + disguise.data.getFirst());\r
50                                 } else {\r
51                                         player.sendMessage(ChatColor.GOLD + "You were redisguised as a " + ChatColor.DARK_GREEN + disguise.type.name());\r
52                                 }\r
53                                 \r
54                                 // Start position updater\r
55                                 plugin.setPositionUpdater(player, disguise);\r
56                         } else {\r
57                                 plugin.disguiseDB.remove(player.getName());\r
58                                 player.sendMessage(ChatColor.RED + "You do not have the permissions required to wear your disguise in this world.");\r
59                         }\r
60                 }\r
61                 \r
62                 // Updates?\r
63                 if (DisguiseCraft.pluginSettings.updateNotification && plugin.hasPermissions(player, "disguisecraft.update")) {\r
64                         // Check for new DisguiseCraft version\r
65                         plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new DCUpdateNotifier(plugin, player));\r
66                         \r
67                         // Bad configuration?\r
68                         if (DisguiseCraft.protocolManager == null) {\r
69                                 if (DisguiseCraft.pluginSettings.disguisePVP) {\r
70                                         player.sendMessage(ChatColor.RED + "DisguiseCraft's configuration has " + ChatColor.GOLD + "\"disguisePVP\" " +\r
71                                                         ChatColor.RED + "set to " + ChatColor.GOLD + "true " + ChatColor.RED + "but ProtocolLib is not installed!");\r
72                                 }\r
73                                 if (DisguiseCraft.pluginSettings.noTabHide) {\r
74                                         player.sendMessage(ChatColor.RED + "DisguiseCraft's configuration has " + ChatColor.GOLD + "\"noTabHide\" " +\r
75                                                         ChatColor.RED + "set to " + ChatColor.GOLD + "true " + ChatColor.RED + "but ProtocolLib is not installed!");\r
76                                 }\r
77                         }\r
78                 }\r
79         }\r
80         \r
81         @EventHandler\r
82         public void onDisguiseHit(PlayerInvalidInteractEvent event) {\r
83                 invalidInteractExecutor.execute(new InvalidInteractHandler(event, plugin));\r
84         }\r
85         \r
86         @EventHandler\r
87         public void onPlayerQuit(PlayerQuitEvent event) {\r
88                 Player player = event.getPlayer();\r
89                 \r
90                 // Undisguise them because they left\r
91                 if (plugin.disguiseDB.containsKey(player.getName())) {\r
92                         plugin.disguiseIDs.remove(plugin.disguiseDB.get(player.getName()).entityID);\r
93                         if (DisguiseCraft.pluginSettings.quitUndisguise) {\r
94                                 plugin.unDisguisePlayer(player);\r
95                                 plugin.disguiseQuitters.add(player.getName());\r
96                         } else {\r
97                                 plugin.sendUnDisguise(player, null);\r
98                         }\r
99                 }\r
100                 \r
101                 // Undisguise others\r
102                 plugin.halfUndisguiseAllToPlayer(player);\r
103                 \r
104                 // Stop position updater\r
105                 plugin.removePositionUpdater(player);\r
106         }\r
107         \r
108         @EventHandler\r
109         public void onPlayerWorldChange(PlayerChangedWorldEvent event) {\r
110                 // Handle this next tick\r
111                 plugin.getServer().getScheduler().runTask(plugin, new WorldChangeUpdater(plugin, event));\r
112         }\r
113         \r
114         @EventHandler\r
115         public void onTarget(EntityTargetEvent event) {\r
116                 if (!event.isCancelled()) {\r
117                         if (event.getTarget() instanceof Player) {\r
118                                 Player player = (Player) event.getTarget();\r
119                                 if (plugin.disguiseDB.containsKey(player.getName())) {\r
120                                         if (plugin.hasPermissions(player, "disguisecraft.notarget")) {\r
121                                                 if (plugin.hasPermissions(player, "disguisecraft.notarget.strict")) {\r
122                                                         event.setCancelled(true);\r
123                                                 } else {\r
124                                                         if (!plugin.disguiseDB.get(player.getName()).type.isPlayer() && (event.getReason() == TargetReason.CLOSEST_PLAYER || event.getReason() == TargetReason.RANDOM_TARGET)) {\r
125                                                                 event.setCancelled(true);\r
126                                                         }\r
127                                                 }\r
128                                         }\r
129                                 }\r
130                         }\r
131                 }\r
132         }\r
133         \r
134         @EventHandler\r
135         public void onPickup(PlayerPickupItemEvent event) {\r
136                 if (!event.isCancelled()) {\r
137                         if (plugin.disguiseDB.containsKey(event.getPlayer().getName())) {\r
138                                 Disguise disguise = plugin.disguiseDB.get(event.getPlayer().getName());\r
139                                 if (disguise.data != null && disguise.data.contains("nopickup")) {\r
140                                         event.setCancelled(true);\r
141                                 }\r
142                         }\r
143                 }\r
144         }\r
145 }\r