Finish HeroChat support - handle directing web messages to selected
channel (versus spamming everyone)
This commit is contained in:
parent
e372c64a37
commit
01b337d3ff
2 changed files with 118 additions and 37 deletions
|
|
@ -407,6 +407,8 @@ public class DynmapPlugin extends JavaPlugin {
|
||||||
public void webChat(String name, String message) {
|
public void webChat(String name, String message) {
|
||||||
mapManager.pushUpdate(new Client.ChatMessage("web", name, message));
|
mapManager.pushUpdate(new Client.ChatMessage("web", name, message));
|
||||||
log.info("[WEB]" + name + ": " + message);
|
log.info("[WEB]" + name + ": " + message);
|
||||||
getServer().broadcastMessage("[WEB]" + name + ": " + message);
|
/* Let HeroChat take a look - only broadcast to players if it doesn't handle it */
|
||||||
|
if(hchand.sendWebMessageToHeroChat(name, message) == false)
|
||||||
|
getServer().broadcastMessage("[WEB]" + name + ": " + message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ public class HeroChatHandler {
|
||||||
|
|
||||||
private List<String> hcchannels;
|
private List<String> hcchannels;
|
||||||
private String hcwebinputchannel;
|
private String hcwebinputchannel;
|
||||||
|
private HeroChatChannel hcwebinputchan;
|
||||||
private DynmapPlugin plugin;
|
private DynmapPlugin plugin;
|
||||||
|
|
||||||
private class OurPluginListener extends ServerListener {
|
private class OurPluginListener extends ServerListener {
|
||||||
|
|
@ -39,10 +40,8 @@ public class HeroChatHandler {
|
||||||
/* Reflection-based access wrapper for ChannelChatEvent from HeroChat */
|
/* Reflection-based access wrapper for ChannelChatEvent from HeroChat */
|
||||||
private static class HeroChatChannelChatEvent {
|
private static class HeroChatChannelChatEvent {
|
||||||
private static Class channelchatevent;
|
private static Class channelchatevent;
|
||||||
private static Method getchannel;
|
|
||||||
private static Method getsource;
|
private static Method getsource;
|
||||||
private static Method getmessage;
|
private static Method getmessage;
|
||||||
private static Method iscancelled;
|
|
||||||
private static boolean isgood = false;
|
private static boolean isgood = false;
|
||||||
private Event evt;
|
private Event evt;
|
||||||
|
|
||||||
|
|
@ -51,14 +50,8 @@ public class HeroChatHandler {
|
||||||
try {
|
try {
|
||||||
channelchatevent = Class
|
channelchatevent = Class
|
||||||
.forName("com.herocraftonline.dthielke.herochat.event.ChannelChatEvent");
|
.forName("com.herocraftonline.dthielke.herochat.event.ChannelChatEvent");
|
||||||
getchannel = channelchatevent.getMethod("getChannel",
|
getsource = channelchatevent.getMethod("getSource", new Class[0]);
|
||||||
new Class[0]);
|
getmessage = channelchatevent.getMethod("getMessage", new Class[0]);
|
||||||
getsource = channelchatevent.getMethod("getSource",
|
|
||||||
new Class[0]);
|
|
||||||
getmessage = channelchatevent.getMethod("getMessage",
|
|
||||||
new Class[0]);
|
|
||||||
iscancelled = channelchatevent.getMethod("isCancelled",
|
|
||||||
new Class[0]);
|
|
||||||
isgood = true;
|
isgood = true;
|
||||||
} catch (ClassNotFoundException cnfx) {
|
} catch (ClassNotFoundException cnfx) {
|
||||||
} catch (NoSuchMethodException nsmx) {
|
} catch (NoSuchMethodException nsmx) {
|
||||||
|
|
@ -74,18 +67,6 @@ public class HeroChatHandler {
|
||||||
return channelchatevent.isInstance(evt);
|
return channelchatevent.isInstance(evt);
|
||||||
}
|
}
|
||||||
|
|
||||||
public HeroChatChannel getChannel() {
|
|
||||||
try {
|
|
||||||
Object o;
|
|
||||||
o = getchannel.invoke(evt);
|
|
||||||
if (o != null) {
|
|
||||||
return new HeroChatChannel(o);
|
|
||||||
}
|
|
||||||
} catch (Exception x) {
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSource() {
|
public String getSource() {
|
||||||
try {
|
try {
|
||||||
return (String) getsource.invoke(evt);
|
return (String) getsource.invoke(evt);
|
||||||
|
|
@ -101,6 +82,49 @@ public class HeroChatHandler {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Reflection-based access wrapper for ChannelEvent from HeroChat */
|
||||||
|
private static class HeroChatChannelEvent {
|
||||||
|
private static Class channelevent;
|
||||||
|
private static Method getchannel;
|
||||||
|
private static Method iscancelled;
|
||||||
|
private static boolean isgood = false;
|
||||||
|
private Event evt;
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static boolean initialize() {
|
||||||
|
try {
|
||||||
|
channelevent = Class
|
||||||
|
.forName("com.herocraftonline.dthielke.herochat.event.ChannelEvent");
|
||||||
|
getchannel = channelevent.getMethod("getChannel", new Class[0]);
|
||||||
|
iscancelled = channelevent.getMethod("isCancelled", new Class[0]);
|
||||||
|
isgood = true;
|
||||||
|
} catch (ClassNotFoundException cnfx) {
|
||||||
|
} catch (NoSuchMethodException nsmx) {
|
||||||
|
}
|
||||||
|
return isgood;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HeroChatChannelEvent(Event evt) {
|
||||||
|
this.evt = evt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isInstance(Event evt) {
|
||||||
|
return channelevent.isInstance(evt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public HeroChatChannel getChannel() {
|
||||||
|
try {
|
||||||
|
Object o;
|
||||||
|
o = getchannel.invoke(evt);
|
||||||
|
if (o != null) {
|
||||||
|
return new HeroChatChannel(o);
|
||||||
|
}
|
||||||
|
} catch (Exception x) {
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isCancelled() {
|
public boolean isCancelled() {
|
||||||
try {
|
try {
|
||||||
|
|
@ -115,6 +139,8 @@ public class HeroChatHandler {
|
||||||
private static class HeroChatChannel {
|
private static class HeroChatChannel {
|
||||||
private static Class channel;
|
private static Class channel;
|
||||||
private static Method getname;
|
private static Method getname;
|
||||||
|
private static Method getnick;
|
||||||
|
private static Method sendmessage;
|
||||||
private static boolean isgood = false;
|
private static boolean isgood = false;
|
||||||
private Object chan;
|
private Object chan;
|
||||||
|
|
||||||
|
|
@ -123,10 +149,14 @@ public class HeroChatHandler {
|
||||||
try {
|
try {
|
||||||
channel = Class
|
channel = Class
|
||||||
.forName("com.herocraftonline.dthielke.herochat.channels.Channel");
|
.forName("com.herocraftonline.dthielke.herochat.channels.Channel");
|
||||||
getname = channel.getMethod("getName", new Class[0]);
|
getname = channel.getMethod("getName");
|
||||||
|
getnick = channel.getMethod("getNick", new Class[0]);
|
||||||
|
sendmessage = channel.getMethod("sendMessage", new Class[] {
|
||||||
|
String.class, String.class, String.class, boolean.class } );
|
||||||
isgood = true;
|
isgood = true;
|
||||||
} catch (ClassNotFoundException cnfx) {
|
} catch (ClassNotFoundException cnfx) {
|
||||||
} catch (NoSuchMethodException nsmx) {
|
} catch (NoSuchMethodException nsmx) {
|
||||||
|
System.out.println(nsmx);
|
||||||
}
|
}
|
||||||
return isgood;
|
return isgood;
|
||||||
}
|
}
|
||||||
|
|
@ -135,10 +165,6 @@ public class HeroChatHandler {
|
||||||
this.chan = chan;
|
this.chan = chan;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isInstance(Object obj) {
|
|
||||||
return channel.isInstance(obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
try {
|
try {
|
||||||
return (String) getname.invoke(chan);
|
return (String) getname.invoke(chan);
|
||||||
|
|
@ -146,6 +172,21 @@ public class HeroChatHandler {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getNick() {
|
||||||
|
try {
|
||||||
|
return (String) getnick.invoke(chan);
|
||||||
|
} catch (Exception x) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendMessage(String source, String msg, String format, boolean sentByPlayer) {
|
||||||
|
try {
|
||||||
|
sendmessage.invoke(chan, source, msg, format, sentByPlayer);
|
||||||
|
} catch (Exception x) {
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class OurEventListener extends CustomEventListener {
|
private class OurEventListener extends CustomEventListener {
|
||||||
|
|
@ -154,16 +195,32 @@ public class HeroChatHandler {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onCustomEvent(Event event) {
|
public void onCustomEvent(Event event) {
|
||||||
if (HeroChatChannelChatEvent.isInstance(event)) {
|
if (HeroChatChannelEvent.isInstance(event)) {
|
||||||
HeroChatChannelChatEvent cce = new HeroChatChannelChatEvent(
|
HeroChatChannelEvent ce = new HeroChatChannelEvent(event);
|
||||||
event);
|
/* Snoop for our web channel - we'll need it, and we'll see it before it matters,
|
||||||
if (cce.isCancelled())
|
* since anyone that joins the channel will give us an event (and reflection on
|
||||||
|
* the plugin class to get the manager didn't work, due to a dependency on the IRC
|
||||||
|
* plugin that may not be present....)
|
||||||
|
*/
|
||||||
|
HeroChatChannel c = ce.getChannel();
|
||||||
|
/* If channel name or nickname matches out web channel, remember it */
|
||||||
|
if((c != null) && (hcwebinputchannel != null) &&
|
||||||
|
((c.getName().equals(hcwebinputchannel)) ||
|
||||||
|
c.getNick().equals(hcwebinputchannel))) {
|
||||||
|
hcwebinputchan = c;
|
||||||
|
}
|
||||||
|
if (ce.isCancelled())
|
||||||
return;
|
return;
|
||||||
HeroChatChannel c = cce.getChannel();
|
if (HeroChatChannelChatEvent.isInstance(event)) {
|
||||||
if (hcchannels.contains(c.getName())) {
|
HeroChatChannelChatEvent cce = new HeroChatChannelChatEvent(
|
||||||
plugin.mapManager.pushUpdate(new Client.ChatMessage(
|
event);
|
||||||
"player", "[" + c.getName() + "] "
|
/* Match on name or nickname of channel */
|
||||||
|
if (hcchannels.contains(c.getName()) ||
|
||||||
|
hcchannels.contains(c.getNick())) {
|
||||||
|
plugin.mapManager.pushUpdate(new Client.ChatMessage(
|
||||||
|
"player", "[" + c.getNick() + "] "
|
||||||
+ cce.getSource(), cce.getMessage()));
|
+ cce.getSource(), cce.getMessage()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -188,16 +245,38 @@ public class HeroChatHandler {
|
||||||
|
|
||||||
private void activateHeroChat(Plugin herochat) {
|
private void activateHeroChat(Plugin herochat) {
|
||||||
if (HeroChatChannelChatEvent.initialize() == false) {
|
if (HeroChatChannelChatEvent.initialize() == false) {
|
||||||
log.severe("[dynmap] Cannot load HeroChat event class!");
|
log.severe("[dynmap] Cannot load HeroChat chat event class!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (HeroChatChannel.initialize() == false) {
|
if (HeroChatChannel.initialize() == false) {
|
||||||
log.severe("[dynmap] Cannot load HeroChat channel class!");
|
log.severe("[dynmap] Cannot load HeroChat channel class!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (HeroChatChannelEvent.initialize() == false) {
|
||||||
|
log.severe("[dynmap] Cannot load HeroChat channel event class!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/* Register event handler */
|
/* Register event handler */
|
||||||
plugin.getServer().getPluginManager().registerEvent(Event.Type.CUSTOM_EVENT,
|
plugin.getServer().getPluginManager().registerEvent(Event.Type.CUSTOM_EVENT,
|
||||||
new OurEventListener(), Event.Priority.Monitor, plugin);
|
new OurEventListener(), Event.Priority.Monitor, plugin);
|
||||||
log.info("[dynmap] HeroChat integration active");
|
log.info("[dynmap] HeroChat integration active");
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Send message from web to appropriate HeroChat channel
|
||||||
|
* @param sender - sender ID
|
||||||
|
* @param message - message
|
||||||
|
* @return true if herochat is handling this, false if not
|
||||||
|
*/
|
||||||
|
public boolean sendWebMessageToHeroChat(String sender, String message) {
|
||||||
|
if(hcwebinputchannel != null) { /* Are we handling them? */
|
||||||
|
if(hcwebinputchan != null) { /* Have we seen it yet? Maybe no if nobody has logged on or
|
||||||
|
* joined it, but then who would see it anyway?
|
||||||
|
*/
|
||||||
|
hcwebinputchan.sendMessage(sender, message, "{default}", false);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue