Commit 9bec49a4751b4f39adaa59c8f8efa6b970cf0dd8

Authored by Mumfrey
1 parent a954d09e

Remove erroneous exceptions

java/common/com/mumfrey/liteloader/core/ClientPluginChannels.java
1 -package com.mumfrey.liteloader.core;  
2 -  
3 -import net.minecraft.network.INetHandler;  
4 -import net.minecraft.network.PacketBuffer;  
5 -import net.minecraft.network.play.server.S3FPacketCustomPayload;  
6 -  
7 -import com.mumfrey.liteloader.PluginChannelListener;  
8 -import com.mumfrey.liteloader.api.Listener;  
9 -import com.mumfrey.liteloader.core.event.HandlerList;  
10 -import com.mumfrey.liteloader.interfaces.FastIterableDeque;  
11 -import com.mumfrey.liteloader.permissions.PermissionsManagerClient;  
12 -import com.mumfrey.liteloader.util.log.LiteLoaderLogger;  
13 -  
14 -/**  
15 - * Handler for client plugin channels  
16 - *  
17 - * @author Adam Mummery-Smith  
18 - */  
19 -public abstract class ClientPluginChannels extends PluginChannels<PluginChannelListener>  
20 -{  
21 - private static ClientPluginChannels instance;  
22 -  
23 - protected ClientPluginChannels()  
24 - {  
25 - if (ClientPluginChannels.instance != null) throw new RuntimeException("Plugin Channels Startup Error", new InstantiationException("Only a single instance of ClientPluginChannels is allowed"));  
26 - ClientPluginChannels.instance = this;  
27 - }  
28 -  
29 - @Override  
30 - protected FastIterableDeque<PluginChannelListener> createHandlerList()  
31 - {  
32 - return new HandlerList<PluginChannelListener>(PluginChannelListener.class);  
33 - }  
34 -  
35 - protected static ClientPluginChannels getInstance()  
36 - {  
37 - return ClientPluginChannels.instance;  
38 - }  
39 -  
40 - /* (non-Javadoc)  
41 - * @see com.mumfrey.liteloader.api.InterfaceProvider#initProvider()  
42 - */  
43 - @Override  
44 - public void initProvider()  
45 - {  
46 - }  
47 -  
48 - /* (non-Javadoc)  
49 - * @see com.mumfrey.liteloader.api.InterfaceProvider#getListenerBaseType()  
50 - */  
51 - @Override  
52 - public Class<? extends Listener> getListenerBaseType()  
53 - {  
54 - return Listener.class;  
55 - }  
56 -  
57 - /* (non-Javadoc)  
58 - * @see com.mumfrey.liteloader.api.InterfaceProvider#registerInterfaces(com.mumfrey.liteloader.core.InterfaceRegistrationDelegate)  
59 - */  
60 - @Override  
61 - public void registerInterfaces(InterfaceRegistrationDelegate delegate)  
62 - {  
63 - delegate.registerInterface(PluginChannelListener.class);  
64 - }  
65 -  
66 - void addClientPluginChannelListener(PluginChannelListener pluginChannelListener)  
67 - {  
68 - super.addPluginChannelListener(pluginChannelListener);  
69 - }  
70 -  
71 - /**  
72 - * Callback for the plugin channel hook  
73 - *  
74 - * @param customPayload  
75 - */  
76 - public abstract void onPluginChannelMessage(S3FPacketCustomPayload customPayload);  
77 -  
78 - /**  
79 - * @param channel  
80 - * @param data  
81 - */  
82 - protected void onPluginChannelMessage(String channel, PacketBuffer data)  
83 - {  
84 - if (PluginChannels.CHANNEL_REGISTER.equals(channel))  
85 - {  
86 - this.onRegisterPacketReceived(data);  
87 - }  
88 - else if (this.pluginChannels.containsKey(channel))  
89 - {  
90 - try  
91 - {  
92 - PermissionsManagerClient permissionsManager = LiteLoader.getClientPermissionsManager();  
93 - if (permissionsManager != null)  
94 - {  
95 - permissionsManager.onCustomPayload(channel, data);  
96 - }  
97 - }  
98 - catch (Exception ex) {}  
99 -  
100 - this.onModPacketReceived(channel, data);  
101 - }  
102 - }  
103 -  
104 - /**  
105 - * @param channel  
106 - * @param data  
107 - */  
108 - protected void onModPacketReceived(String channel, PacketBuffer data)  
109 - {  
110 - for (PluginChannelListener pluginChannelListener : this.pluginChannels.get(channel))  
111 - {  
112 - try  
113 - {  
114 - pluginChannelListener.onCustomPayload(channel, data);  
115 - throw new RuntimeException();  
116 - }  
117 - catch (Exception ex)  
118 - {  
119 - int failCount = 1;  
120 - if (this.faultingPluginChannelListeners.containsKey(pluginChannelListener))  
121 - failCount = this.faultingPluginChannelListeners.get(pluginChannelListener).intValue() + 1;  
122 -  
123 - if (failCount >= PluginChannels.WARN_FAULT_THRESHOLD)  
124 - {  
125 - LiteLoaderLogger.warning("Plugin channel listener %s exceeded fault threshold on channel %s with %s", pluginChannelListener.getName(), channel, ex.getClass().getSimpleName());  
126 - this.faultingPluginChannelListeners.remove(pluginChannelListener);  
127 - }  
128 - else  
129 - {  
130 - this.faultingPluginChannelListeners.put(pluginChannelListener, Integer.valueOf(failCount));  
131 - }  
132 - }  
133 - }  
134 - }  
135 -  
136 - protected void sendRegisteredPluginChannels(INetHandler netHandler)  
137 - {  
138 - // Add the permissions manager channels  
139 - this.addPluginChannelsFor(LiteLoader.getClientPermissionsManager());  
140 -  
141 - try  
142 - {  
143 - // Enumerate mods for plugin channels  
144 - for (PluginChannelListener pluginChannelListener : this.pluginChannelListeners)  
145 - {  
146 - this.addPluginChannelsFor(pluginChannelListener);  
147 - }  
148 -  
149 - PacketBuffer registrationData = this.getRegistrationData();  
150 - if (registrationData != null)  
151 - {  
152 - this.sendRegistrationData(netHandler, registrationData);  
153 - }  
154 - }  
155 - catch (Exception ex)  
156 - {  
157 - LiteLoaderLogger.warning(ex, "Error dispatching REGISTER packet to server %s", ex.getClass().getSimpleName());  
158 - }  
159 - }  
160 -  
161 - /**  
162 - * @param netHandler  
163 - * @param registrationData  
164 - */  
165 - protected abstract void sendRegistrationData(INetHandler netHandler, PacketBuffer registrationData);  
166 -  
167 - /**  
168 - * Send a message to the server on a plugin channel  
169 - *  
170 - * @param channel Channel to send, must not be a reserved channel name  
171 - * @param data  
172 - */  
173 - public static boolean sendMessage(String channel, PacketBuffer data, ChannelPolicy policy)  
174 - {  
175 - if (ClientPluginChannels.instance != null)  
176 - {  
177 - return ClientPluginChannels.instance.send(channel, data, policy);  
178 - }  
179 -  
180 - return false;  
181 - }  
182 -  
183 - /**  
184 - * Send a message to the server on a plugin channel  
185 - *  
186 - * @param channel Channel to send, must not be a reserved channel name  
187 - * @param data  
188 - */  
189 - protected abstract boolean send(String channel, PacketBuffer data, ChannelPolicy policy);  
190 -} 1 +package com.mumfrey.liteloader.core;
  2 +
  3 +import net.minecraft.network.INetHandler;
  4 +import net.minecraft.network.PacketBuffer;
  5 +import net.minecraft.network.play.server.S3FPacketCustomPayload;
  6 +
  7 +import com.mumfrey.liteloader.PluginChannelListener;
  8 +import com.mumfrey.liteloader.api.Listener;
  9 +import com.mumfrey.liteloader.core.event.HandlerList;
  10 +import com.mumfrey.liteloader.interfaces.FastIterableDeque;
  11 +import com.mumfrey.liteloader.permissions.PermissionsManagerClient;
  12 +import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
  13 +
  14 +/**
  15 + * Handler for client plugin channels
  16 + *
  17 + * @author Adam Mummery-Smith
  18 + */
  19 +public abstract class ClientPluginChannels extends PluginChannels<PluginChannelListener>
  20 +{
  21 + private static ClientPluginChannels instance;
  22 +
  23 + protected ClientPluginChannels()
  24 + {
  25 + if (ClientPluginChannels.instance != null) throw new RuntimeException("Plugin Channels Startup Error", new InstantiationException("Only a single instance of ClientPluginChannels is allowed"));
  26 + ClientPluginChannels.instance = this;
  27 + }
  28 +
  29 + @Override
  30 + protected FastIterableDeque<PluginChannelListener> createHandlerList()
  31 + {
  32 + return new HandlerList<PluginChannelListener>(PluginChannelListener.class);
  33 + }
  34 +
  35 + protected static ClientPluginChannels getInstance()
  36 + {
  37 + return ClientPluginChannels.instance;
  38 + }
  39 +
  40 + /* (non-Javadoc)
  41 + * @see com.mumfrey.liteloader.api.InterfaceProvider#initProvider()
  42 + */
  43 + @Override
  44 + public void initProvider()
  45 + {
  46 + }
  47 +
  48 + /* (non-Javadoc)
  49 + * @see com.mumfrey.liteloader.api.InterfaceProvider#getListenerBaseType()
  50 + */
  51 + @Override
  52 + public Class<? extends Listener> getListenerBaseType()
  53 + {
  54 + return Listener.class;
  55 + }
  56 +
  57 + /* (non-Javadoc)
  58 + * @see com.mumfrey.liteloader.api.InterfaceProvider#registerInterfaces(com.mumfrey.liteloader.core.InterfaceRegistrationDelegate)
  59 + */
  60 + @Override
  61 + public void registerInterfaces(InterfaceRegistrationDelegate delegate)
  62 + {
  63 + delegate.registerInterface(PluginChannelListener.class);
  64 + }
  65 +
  66 + void addClientPluginChannelListener(PluginChannelListener pluginChannelListener)
  67 + {
  68 + super.addPluginChannelListener(pluginChannelListener);
  69 + }
  70 +
  71 + /**
  72 + * Callback for the plugin channel hook
  73 + *
  74 + * @param customPayload
  75 + */
  76 + public abstract void onPluginChannelMessage(S3FPacketCustomPayload customPayload);
  77 +
  78 + /**
  79 + * @param channel
  80 + * @param data
  81 + */
  82 + protected void onPluginChannelMessage(String channel, PacketBuffer data)
  83 + {
  84 + if (PluginChannels.CHANNEL_REGISTER.equals(channel))
  85 + {
  86 + this.onRegisterPacketReceived(data);
  87 + }
  88 + else if (this.pluginChannels.containsKey(channel))
  89 + {
  90 + try
  91 + {
  92 + PermissionsManagerClient permissionsManager = LiteLoader.getClientPermissionsManager();
  93 + if (permissionsManager != null)
  94 + {
  95 + permissionsManager.onCustomPayload(channel, data);
  96 + }
  97 + }
  98 + catch (Exception ex) {}
  99 +
  100 + this.onModPacketReceived(channel, data);
  101 + }
  102 + }
  103 +
  104 + /**
  105 + * @param channel
  106 + * @param data
  107 + */
  108 + protected void onModPacketReceived(String channel, PacketBuffer data)
  109 + {
  110 + for (PluginChannelListener pluginChannelListener : this.pluginChannels.get(channel))
  111 + {
  112 + try
  113 + {
  114 + pluginChannelListener.onCustomPayload(channel, data);
  115 + }
  116 + catch (Exception ex)
  117 + {
  118 + int failCount = 1;
  119 + if (this.faultingPluginChannelListeners.containsKey(pluginChannelListener))
  120 + failCount = this.faultingPluginChannelListeners.get(pluginChannelListener).intValue() + 1;
  121 +
  122 + if (failCount >= PluginChannels.WARN_FAULT_THRESHOLD)
  123 + {
  124 + LiteLoaderLogger.warning("Plugin channel listener %s exceeded fault threshold on channel %s with %s", pluginChannelListener.getName(), channel, ex.getClass().getSimpleName());
  125 + this.faultingPluginChannelListeners.remove(pluginChannelListener);
  126 + }
  127 + else
  128 + {
  129 + this.faultingPluginChannelListeners.put(pluginChannelListener, Integer.valueOf(failCount));
  130 + }
  131 + }
  132 + }
  133 + }
  134 +
  135 + protected void sendRegisteredPluginChannels(INetHandler netHandler)
  136 + {
  137 + // Add the permissions manager channels
  138 + this.addPluginChannelsFor(LiteLoader.getClientPermissionsManager());
  139 +
  140 + try
  141 + {
  142 + // Enumerate mods for plugin channels
  143 + for (PluginChannelListener pluginChannelListener : this.pluginChannelListeners)
  144 + {
  145 + this.addPluginChannelsFor(pluginChannelListener);
  146 + }
  147 +
  148 + PacketBuffer registrationData = this.getRegistrationData();
  149 + if (registrationData != null)
  150 + {
  151 + this.sendRegistrationData(netHandler, registrationData);
  152 + }
  153 + }
  154 + catch (Exception ex)
  155 + {
  156 + LiteLoaderLogger.warning(ex, "Error dispatching REGISTER packet to server %s", ex.getClass().getSimpleName());
  157 + }
  158 + }
  159 +
  160 + /**
  161 + * @param netHandler
  162 + * @param registrationData
  163 + */
  164 + protected abstract void sendRegistrationData(INetHandler netHandler, PacketBuffer registrationData);
  165 +
  166 + /**
  167 + * Send a message to the server on a plugin channel
  168 + *
  169 + * @param channel Channel to send, must not be a reserved channel name
  170 + * @param data
  171 + */
  172 + public static boolean sendMessage(String channel, PacketBuffer data, ChannelPolicy policy)
  173 + {
  174 + if (ClientPluginChannels.instance != null)
  175 + {
  176 + return ClientPluginChannels.instance.send(channel, data, policy);
  177 + }
  178 +
  179 + return false;
  180 + }
  181 +
  182 + /**
  183 + * Send a message to the server on a plugin channel
  184 + *
  185 + * @param channel Channel to send, must not be a reserved channel name
  186 + * @param data
  187 + */
  188 + protected abstract boolean send(String channel, PacketBuffer data, ChannelPolicy policy);
  189 +}
java/common/com/mumfrey/liteloader/core/ServerPluginChannels.java
1 -package com.mumfrey.liteloader.core;  
2 -  
3 -import net.minecraft.entity.player.EntityPlayerMP;  
4 -import net.minecraft.network.INetHandler;  
5 -import net.minecraft.network.NetHandlerPlayServer;  
6 -import net.minecraft.network.PacketBuffer;  
7 -import net.minecraft.network.play.client.C17PacketCustomPayload;  
8 -import net.minecraft.network.play.server.S3FPacketCustomPayload;  
9 -  
10 -import com.mumfrey.liteloader.ServerPluginChannelListener;  
11 -import com.mumfrey.liteloader.api.Listener;  
12 -import com.mumfrey.liteloader.core.event.HandlerList;  
13 -import com.mumfrey.liteloader.core.exceptions.UnregisteredChannelException;  
14 -import com.mumfrey.liteloader.interfaces.FastIterableDeque;  
15 -import com.mumfrey.liteloader.permissions.PermissionsManagerServer;  
16 -import com.mumfrey.liteloader.util.log.LiteLoaderLogger;  
17 -  
18 -/**  
19 - * Handler for server plugin channels  
20 - *  
21 - * @author Adam Mummery-Smith  
22 - */  
23 -public class ServerPluginChannels extends PluginChannels<ServerPluginChannelListener>  
24 -{  
25 - private static ServerPluginChannels instance;  
26 -  
27 - public ServerPluginChannels()  
28 - {  
29 - if (ServerPluginChannels.instance != null) throw new RuntimeException("Plugin Channels Startup Error", new InstantiationException("Only a single instance of ServerPluginChannels is allowed"));  
30 - ServerPluginChannels.instance = this;  
31 - }  
32 -  
33 - @Override  
34 - protected FastIterableDeque<ServerPluginChannelListener> createHandlerList()  
35 - {  
36 - return new HandlerList<ServerPluginChannelListener>(ServerPluginChannelListener.class);  
37 - }  
38 -  
39 - public static ServerPluginChannels getInstance()  
40 - {  
41 - return instance;  
42 - }  
43 -  
44 - /* (non-Javadoc)  
45 - * @see com.mumfrey.liteloader.api.InterfaceProvider#initProvider()  
46 - */  
47 - @Override  
48 - public void initProvider()  
49 - {  
50 - }  
51 -  
52 - /* (non-Javadoc)  
53 - * @see com.mumfrey.liteloader.api.InterfaceProvider#getListenerBaseType()  
54 - */  
55 - @Override  
56 - public Class<? extends Listener> getListenerBaseType()  
57 - {  
58 - return Listener.class;  
59 - }  
60 -  
61 - /* (non-Javadoc)  
62 - * @see com.mumfrey.liteloader.api.InterfaceProvider#registerInterfaces(com.mumfrey.liteloader.core.InterfaceRegistrationDelegate)  
63 - */  
64 - @Override  
65 - public void registerInterfaces(InterfaceRegistrationDelegate delegate)  
66 - {  
67 - delegate.registerInterface(ServerPluginChannelListener.class);  
68 - }  
69 -  
70 - void addServerPluginChannelListener(ServerPluginChannelListener pluginChannelListener)  
71 - {  
72 - super.addPluginChannelListener(pluginChannelListener);  
73 - }  
74 -  
75 - void onServerStartup()  
76 - {  
77 - this.clearPluginChannels(null);  
78 -  
79 - // Enumerate mods for plugin channels  
80 - for (ServerPluginChannelListener pluginChannelListener : this.pluginChannelListeners)  
81 - {  
82 - this.addPluginChannelsFor(pluginChannelListener);  
83 - }  
84 - }  
85 -  
86 - void onPlayerJoined(EntityPlayerMP player)  
87 - {  
88 - this.sendRegisteredPluginChannels(player);  
89 - }  
90 -  
91 - /**  
92 - * Callback for the plugin channel hook  
93 - *  
94 - * @param netHandler  
95 - * @param customPayload  
96 - */  
97 - public void onPluginChannelMessage(INetHandler netHandler, C17PacketCustomPayload customPayload)  
98 - {  
99 - if (customPayload != null && customPayload.getChannelName() != null)  
100 - {  
101 - String channel = customPayload.getChannelName();  
102 - PacketBuffer data = customPayload.getBufferData();  
103 -  
104 - EntityPlayerMP sender = ((NetHandlerPlayServer)netHandler).playerEntity;  
105 - this.onPluginChannelMessage(sender, channel, data);  
106 - }  
107 - }  
108 -  
109 - /**  
110 - * @param channel  
111 - * @param data  
112 - */  
113 - private final void onPluginChannelMessage(EntityPlayerMP sender, String channel, PacketBuffer data)  
114 - {  
115 - if (PluginChannels.CHANNEL_REGISTER.equals(channel))  
116 - {  
117 - this.onRegisterPacketReceived(data);  
118 - }  
119 - else if (this.pluginChannels.containsKey(channel))  
120 - {  
121 - try  
122 - {  
123 - PermissionsManagerServer permissionsManager = LiteLoader.getServerPermissionsManager();  
124 - if (permissionsManager != null)  
125 - {  
126 - permissionsManager.onCustomPayload(sender, channel, data);  
127 - }  
128 - }  
129 - catch (Exception ex) {}  
130 -  
131 - this.onModPacketReceived(sender, channel, data);  
132 - }  
133 - }  
134 -  
135 - /**  
136 - * @param sender  
137 - * @param channel  
138 - * @param data  
139 - */  
140 - protected void onModPacketReceived(EntityPlayerMP sender, String channel, PacketBuffer data)  
141 - {  
142 - for (ServerPluginChannelListener pluginChannelListener : this.pluginChannels.get(channel))  
143 - {  
144 - try  
145 - {  
146 - pluginChannelListener.onCustomPayload(sender, channel, data);  
147 - throw new RuntimeException();  
148 - }  
149 - catch (Exception ex)  
150 - {  
151 - int failCount = 1;  
152 - if (this.faultingPluginChannelListeners.containsKey(pluginChannelListener))  
153 - failCount = this.faultingPluginChannelListeners.get(pluginChannelListener).intValue() + 1;  
154 -  
155 - if (failCount >= PluginChannels.WARN_FAULT_THRESHOLD)  
156 - {  
157 - LiteLoaderLogger.warning("Plugin channel listener %s exceeded fault threshold on channel %s with %s", pluginChannelListener.getName(), channel, ex.getClass().getSimpleName());  
158 - this.faultingPluginChannelListeners.remove(pluginChannelListener);  
159 - }  
160 - else  
161 - {  
162 - this.faultingPluginChannelListeners.put(pluginChannelListener, Integer.valueOf(failCount));  
163 - }  
164 - }  
165 - }  
166 - }  
167 -  
168 - protected void sendRegisteredPluginChannels(EntityPlayerMP player)  
169 - {  
170 - try  
171 - {  
172 - PacketBuffer registrationData = this.getRegistrationData();  
173 - if (registrationData != null)  
174 - {  
175 - this.sendRegistrationData(player, registrationData);  
176 - }  
177 - }  
178 - catch (Exception ex)  
179 - {  
180 - LiteLoaderLogger.warning(ex, "Error dispatching REGISTER packet to client %s", player.getDisplayName());  
181 - }  
182 - }  
183 -  
184 - /**  
185 - * @param recipient  
186 - * @param registrationData  
187 - */  
188 - private void sendRegistrationData(EntityPlayerMP recipient, PacketBuffer registrationData)  
189 - {  
190 - ServerPluginChannels.dispatch(recipient, new S3FPacketCustomPayload(CHANNEL_REGISTER, registrationData));  
191 - }  
192 -  
193 - /**  
194 - * Send a message to the specified client on a plugin channel  
195 - *  
196 - * @param recipient  
197 - * @param channel Channel to send, must not be a reserved channel name  
198 - * @param data  
199 - */  
200 - public static boolean sendMessage(EntityPlayerMP recipient, String channel, PacketBuffer data, ChannelPolicy policy)  
201 - {  
202 - if (ServerPluginChannels.instance != null)  
203 - {  
204 - return ServerPluginChannels.instance.send(recipient, channel, data, policy);  
205 - }  
206 -  
207 - return false;  
208 - }  
209 -  
210 - /**  
211 - * Send a message to the specified client on a plugin channel  
212 - *  
213 - * @param recipient Recipient to send to  
214 - * @param channel Channel to send, must not be a reserved channel name  
215 - * @param data  
216 - */  
217 - private boolean send(EntityPlayerMP recipient, String channel, PacketBuffer data, ChannelPolicy policy)  
218 - {  
219 - if (recipient == null) return false;  
220 -  
221 - if (channel == null || channel.length() > 16 || CHANNEL_REGISTER.equals(channel) || CHANNEL_UNREGISTER.equals(channel))  
222 - throw new RuntimeException("Invalid channel name specified");  
223 -  
224 - if (!policy.allows(this, channel))  
225 - {  
226 - if (policy.isSilent()) return false;  
227 - throw new UnregisteredChannelException(channel);  
228 - }  
229 -  
230 - S3FPacketCustomPayload payload = new S3FPacketCustomPayload(channel, data);  
231 - return ServerPluginChannels.dispatch(recipient, payload);  
232 - }  
233 -  
234 - /**  
235 - * @param recipient  
236 - * @param payload  
237 - */  
238 - static boolean dispatch(EntityPlayerMP recipient, S3FPacketCustomPayload payload)  
239 - {  
240 - try  
241 - {  
242 - if (recipient != null && recipient.playerNetServerHandler != null)  
243 - {  
244 - recipient.playerNetServerHandler.sendPacket(payload);  
245 - return true;  
246 - }  
247 - }  
248 - catch (Exception ex) {}  
249 -  
250 - return false;  
251 - }  
252 -} 1 +package com.mumfrey.liteloader.core;
  2 +
  3 +import net.minecraft.entity.player.EntityPlayerMP;
  4 +import net.minecraft.network.INetHandler;
  5 +import net.minecraft.network.NetHandlerPlayServer;
  6 +import net.minecraft.network.PacketBuffer;
  7 +import net.minecraft.network.play.client.C17PacketCustomPayload;
  8 +import net.minecraft.network.play.server.S3FPacketCustomPayload;
  9 +
  10 +import com.mumfrey.liteloader.ServerPluginChannelListener;
  11 +import com.mumfrey.liteloader.api.Listener;
  12 +import com.mumfrey.liteloader.core.event.HandlerList;
  13 +import com.mumfrey.liteloader.core.exceptions.UnregisteredChannelException;
  14 +import com.mumfrey.liteloader.interfaces.FastIterableDeque;
  15 +import com.mumfrey.liteloader.permissions.PermissionsManagerServer;
  16 +import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
  17 +
  18 +/**
  19 + * Handler for server plugin channels
  20 + *
  21 + * @author Adam Mummery-Smith
  22 + */
  23 +public class ServerPluginChannels extends PluginChannels<ServerPluginChannelListener>
  24 +{
  25 + private static ServerPluginChannels instance;
  26 +
  27 + public ServerPluginChannels()
  28 + {
  29 + if (ServerPluginChannels.instance != null) throw new RuntimeException("Plugin Channels Startup Error", new InstantiationException("Only a single instance of ServerPluginChannels is allowed"));
  30 + ServerPluginChannels.instance = this;
  31 + }
  32 +
  33 + @Override
  34 + protected FastIterableDeque<ServerPluginChannelListener> createHandlerList()
  35 + {
  36 + return new HandlerList<ServerPluginChannelListener>(ServerPluginChannelListener.class);
  37 + }
  38 +
  39 + public static ServerPluginChannels getInstance()
  40 + {
  41 + return instance;
  42 + }
  43 +
  44 + /* (non-Javadoc)
  45 + * @see com.mumfrey.liteloader.api.InterfaceProvider#initProvider()
  46 + */
  47 + @Override
  48 + public void initProvider()
  49 + {
  50 + }
  51 +
  52 + /* (non-Javadoc)
  53 + * @see com.mumfrey.liteloader.api.InterfaceProvider#getListenerBaseType()
  54 + */
  55 + @Override
  56 + public Class<? extends Listener> getListenerBaseType()
  57 + {
  58 + return Listener.class;
  59 + }
  60 +
  61 + /* (non-Javadoc)
  62 + * @see com.mumfrey.liteloader.api.InterfaceProvider#registerInterfaces(com.mumfrey.liteloader.core.InterfaceRegistrationDelegate)
  63 + */
  64 + @Override
  65 + public void registerInterfaces(InterfaceRegistrationDelegate delegate)
  66 + {
  67 + delegate.registerInterface(ServerPluginChannelListener.class);
  68 + }
  69 +
  70 + void addServerPluginChannelListener(ServerPluginChannelListener pluginChannelListener)
  71 + {
  72 + super.addPluginChannelListener(pluginChannelListener);
  73 + }
  74 +
  75 + void onServerStartup()
  76 + {
  77 + this.clearPluginChannels(null);
  78 +
  79 + // Enumerate mods for plugin channels
  80 + for (ServerPluginChannelListener pluginChannelListener : this.pluginChannelListeners)
  81 + {
  82 + this.addPluginChannelsFor(pluginChannelListener);
  83 + }
  84 + }
  85 +
  86 + void onPlayerJoined(EntityPlayerMP player)
  87 + {
  88 + this.sendRegisteredPluginChannels(player);
  89 + }
  90 +
  91 + /**
  92 + * Callback for the plugin channel hook
  93 + *
  94 + * @param netHandler
  95 + * @param customPayload
  96 + */
  97 + public void onPluginChannelMessage(INetHandler netHandler, C17PacketCustomPayload customPayload)
  98 + {
  99 + if (customPayload != null && customPayload.getChannelName() != null)
  100 + {
  101 + String channel = customPayload.getChannelName();
  102 + PacketBuffer data = customPayload.getBufferData();
  103 +
  104 + EntityPlayerMP sender = ((NetHandlerPlayServer)netHandler).playerEntity;
  105 + this.onPluginChannelMessage(sender, channel, data);
  106 + }
  107 + }
  108 +
  109 + /**
  110 + * @param channel
  111 + * @param data
  112 + */
  113 + private final void onPluginChannelMessage(EntityPlayerMP sender, String channel, PacketBuffer data)
  114 + {
  115 + if (PluginChannels.CHANNEL_REGISTER.equals(channel))
  116 + {
  117 + this.onRegisterPacketReceived(data);
  118 + }
  119 + else if (this.pluginChannels.containsKey(channel))
  120 + {
  121 + try
  122 + {
  123 + PermissionsManagerServer permissionsManager = LiteLoader.getServerPermissionsManager();
  124 + if (permissionsManager != null)
  125 + {
  126 + permissionsManager.onCustomPayload(sender, channel, data);
  127 + }
  128 + }
  129 + catch (Exception ex) {}
  130 +
  131 + this.onModPacketReceived(sender, channel, data);
  132 + }
  133 + }
  134 +
  135 + /**
  136 + * @param sender
  137 + * @param channel
  138 + * @param data
  139 + */
  140 + protected void onModPacketReceived(EntityPlayerMP sender, String channel, PacketBuffer data)
  141 + {
  142 + for (ServerPluginChannelListener pluginChannelListener : this.pluginChannels.get(channel))
  143 + {
  144 + try
  145 + {
  146 + pluginChannelListener.onCustomPayload(sender, channel, data);
  147 + }
  148 + catch (Exception ex)
  149 + {
  150 + int failCount = 1;
  151 + if (this.faultingPluginChannelListeners.containsKey(pluginChannelListener))
  152 + failCount = this.faultingPluginChannelListeners.get(pluginChannelListener).intValue() + 1;
  153 +
  154 + if (failCount >= PluginChannels.WARN_FAULT_THRESHOLD)
  155 + {
  156 + LiteLoaderLogger.warning("Plugin channel listener %s exceeded fault threshold on channel %s with %s", pluginChannelListener.getName(), channel, ex.getClass().getSimpleName());
  157 + this.faultingPluginChannelListeners.remove(pluginChannelListener);
  158 + }
  159 + else
  160 + {
  161 + this.faultingPluginChannelListeners.put(pluginChannelListener, Integer.valueOf(failCount));
  162 + }
  163 + }
  164 + }
  165 + }
  166 +
  167 + protected void sendRegisteredPluginChannels(EntityPlayerMP player)
  168 + {
  169 + try
  170 + {
  171 + PacketBuffer registrationData = this.getRegistrationData();
  172 + if (registrationData != null)
  173 + {
  174 + this.sendRegistrationData(player, registrationData);
  175 + }
  176 + }
  177 + catch (Exception ex)
  178 + {
  179 + LiteLoaderLogger.warning(ex, "Error dispatching REGISTER packet to client %s", player.getDisplayName());
  180 + }
  181 + }
  182 +
  183 + /**
  184 + * @param recipient
  185 + * @param registrationData
  186 + */
  187 + private void sendRegistrationData(EntityPlayerMP recipient, PacketBuffer registrationData)
  188 + {
  189 + ServerPluginChannels.dispatch(recipient, new S3FPacketCustomPayload(CHANNEL_REGISTER, registrationData));
  190 + }
  191 +
  192 + /**
  193 + * Send a message to the specified client on a plugin channel
  194 + *
  195 + * @param recipient
  196 + * @param channel Channel to send, must not be a reserved channel name
  197 + * @param data
  198 + */
  199 + public static boolean sendMessage(EntityPlayerMP recipient, String channel, PacketBuffer data, ChannelPolicy policy)
  200 + {
  201 + if (ServerPluginChannels.instance != null)
  202 + {
  203 + return ServerPluginChannels.instance.send(recipient, channel, data, policy);
  204 + }
  205 +
  206 + return false;
  207 + }
  208 +
  209 + /**
  210 + * Send a message to the specified client on a plugin channel
  211 + *
  212 + * @param recipient Recipient to send to
  213 + * @param channel Channel to send, must not be a reserved channel name
  214 + * @param data
  215 + */
  216 + private boolean send(EntityPlayerMP recipient, String channel, PacketBuffer data, ChannelPolicy policy)
  217 + {
  218 + if (recipient == null) return false;
  219 +
  220 + if (channel == null || channel.length() > 16 || CHANNEL_REGISTER.equals(channel) || CHANNEL_UNREGISTER.equals(channel))
  221 + throw new RuntimeException("Invalid channel name specified");
  222 +
  223 + if (!policy.allows(this, channel))
  224 + {
  225 + if (policy.isSilent()) return false;
  226 + throw new UnregisteredChannelException(channel);
  227 + }
  228 +
  229 + S3FPacketCustomPayload payload = new S3FPacketCustomPayload(channel, data);
  230 + return ServerPluginChannels.dispatch(recipient, payload);
  231 + }
  232 +
  233 + /**
  234 + * @param recipient
  235 + * @param payload
  236 + */
  237 + static boolean dispatch(EntityPlayerMP recipient, S3FPacketCustomPayload payload)
  238 + {
  239 + try
  240 + {
  241 + if (recipient != null && recipient.playerNetServerHandler != null)
  242 + {
  243 + recipient.playerNetServerHandler.sendPacket(payload);
  244 + return true;
  245 + }
  246 + }
  247 + catch (Exception ex) {}
  248 +
  249 + return false;
  250 + }
  251 +}