From ea6c248534403ac007fd5520dcf4f6af0b42c965 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 21 Apr 2019 23:26:40 +0200 Subject: [PATCH] Attempt to use MSCOM It starts to get messy --- VirtualComputer/dependency-reduced-pom.xml | 2 +- VirtualComputer/pom.xml | 5 ++ .../java/sznp/virtualcomputer/PluginMain.java | 1 + .../events/EventHandlerBase.java | 29 +++-------- .../events/EventHandlerBaseMSCOM.java | 4 ++ .../java/sznp/virtualcomputer/util/Utils.java | 52 +++++++++++++++++-- 6 files changed, 66 insertions(+), 27 deletions(-) create mode 100644 VirtualComputer/src/main/java/sznp/virtualcomputer/events/EventHandlerBaseMSCOM.java diff --git a/VirtualComputer/dependency-reduced-pom.xml b/VirtualComputer/dependency-reduced-pom.xml index 3e0d028..61136e3 100644 --- a/VirtualComputer/dependency-reduced-pom.xml +++ b/VirtualComputer/dependency-reduced-pom.xml @@ -129,7 +129,7 @@ org.virtualbox - VirtualBox-MSCOM + VirtualBox 6.0 compile diff --git a/VirtualComputer/pom.xml b/VirtualComputer/pom.xml index fc2f807..e8a19ae 100644 --- a/VirtualComputer/pom.xml +++ b/VirtualComputer/pom.xml @@ -61,6 +61,11 @@ VirtualBox-MSCOM 6.0 + + com.jacob + Jacob + 1.19 + org.bukkit craftbukkit diff --git a/VirtualComputer/src/main/java/sznp/virtualcomputer/PluginMain.java b/VirtualComputer/src/main/java/sznp/virtualcomputer/PluginMain.java index f38ab70..6d9767d 100644 --- a/VirtualComputer/src/main/java/sznp/virtualcomputer/PluginMain.java +++ b/VirtualComputer/src/main/java/sznp/virtualcomputer/PluginMain.java @@ -23,6 +23,7 @@ import java.util.Arrays; import java.util.function.Predicate; public class PluginMain extends JavaPlugin { + public static final boolean MSCOM = true; //TODO: Implement private static final int MCX = 5; private static final int MCY = 4; private BukkitTask mousetask; diff --git a/VirtualComputer/src/main/java/sznp/virtualcomputer/events/EventHandlerBase.java b/VirtualComputer/src/main/java/sznp/virtualcomputer/events/EventHandlerBase.java index 2aafd3d..2a24975 100644 --- a/VirtualComputer/src/main/java/sznp/virtualcomputer/events/EventHandlerBase.java +++ b/VirtualComputer/src/main/java/sznp/virtualcomputer/events/EventHandlerBase.java @@ -1,16 +1,13 @@ package sznp.virtualcomputer.events; -import lombok.val; -import org.bukkit.Bukkit; import org.mozilla.interfaces.IEvent; import org.mozilla.interfaces.IEventListener; import org.virtualbox_6_0.IEventSource; import org.virtualbox_6_0.VBoxEventType; +import sznp.virtualcomputer.PluginMain; import sznp.virtualcomputer.util.COMObjectBase; import sznp.virtualcomputer.util.Utils; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Map; @@ -31,30 +28,16 @@ public abstract class EventHandlerBase extends COMObjectBase implements IEventLi @Override public final void handleEvent(IEvent iEvent) { - //val cl=eventMap.get((int)iEvent.getType()); - We can afford to search through the events for this handler if (!enabled) return; - val kv = eventMap.entrySet().stream().filter(e -> e.getKey().value() == iEvent.getType()).findAny(); - if (!kv.isPresent()) return; //Event not supported - val cl = kv.get().getValue(); - for (Method method : getClass().getMethods()) { - if (method.isAnnotationPresent(org.bukkit.event.EventHandler.class) - && method.getParameterCount() == 1 && method.getParameterTypes()[0] == cl) { - try { - method.invoke(this, Utils.getEvent(iEvent, cl)); - return; - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - Bukkit.getLogger().warning("Error while handling VirtualBox event!"); - e.getCause().printStackTrace(); - } - } - } + Utils.handleEvent(this, eventMap, iEvent, false); } public org.virtualbox_6_0.IEventListener registerTo(IEventSource source) { - return Utils.registerListener(source, this, new ArrayList<>(eventMap.keySet())); + if (PluginMain.MSCOM) + return Utils.registerListenerMSCOM(source, this, new ArrayList<>(eventMap.keySet())); + else + return Utils.registerListener(source, this, new ArrayList<>(eventMap.keySet())); } public void disable() { diff --git a/VirtualComputer/src/main/java/sznp/virtualcomputer/events/EventHandlerBaseMSCOM.java b/VirtualComputer/src/main/java/sznp/virtualcomputer/events/EventHandlerBaseMSCOM.java new file mode 100644 index 0000000..611d113 --- /dev/null +++ b/VirtualComputer/src/main/java/sznp/virtualcomputer/events/EventHandlerBaseMSCOM.java @@ -0,0 +1,4 @@ +package sznp.virtualcomputer.events; + +public class EventHandlerBaseMSCOM { +} diff --git a/VirtualComputer/src/main/java/sznp/virtualcomputer/util/Utils.java b/VirtualComputer/src/main/java/sznp/virtualcomputer/util/Utils.java index dfe919e..4210670 100644 --- a/VirtualComputer/src/main/java/sznp/virtualcomputer/util/Utils.java +++ b/VirtualComputer/src/main/java/sznp/virtualcomputer/util/Utils.java @@ -1,16 +1,21 @@ package sznp.virtualcomputer.util; +import com.jacob.com.Variant; import lombok.val; +import org.bukkit.Bukkit; import org.mozilla.interfaces.IEventListener; import org.virtualbox_6_0.IEvent; import org.virtualbox_6_0.IEventSource; import org.virtualbox_6_0.VBoxEventType; import org.virtualbox_6_0.xpcom.IUnknown; +import sznp.virtualcomputer.events.EventHandlerBase; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; +import java.util.Map; public class Utils { /** @@ -39,17 +44,20 @@ public class Utils { usrPathsField.set(null, newPaths); } - //public static void registerListener(IEventSource source, IEventListener listener, VBoxEventType... types) { public static org.virtualbox_6_0.IEventListener registerListener(IEventSource source, IEventListener listener, List types) { val ret = new org.virtualbox_6_0.IEventListener(listener); source.registerListener(ret, types, true); return ret; } + public static org.virtualbox_6_0.IEventListener registerListenerMSCOM(IEventSource source, Object listener, List types) { + val ret = new org.virtualbox_6_0.IEventListener(new Variant(listener).toDispatch()); + source.registerListener(ret, types, true); + return ret; + } + @SuppressWarnings("unchecked") public static T getEvent(org.mozilla.interfaces.IEvent event, Class cl) { - //if (event.getType() != type.value()) return null; - //return (T) T.queryInterface(new IEvent(event)); - Probably won't work try { val method = cl.getMethod("queryInterface", IUnknown.class); return (T) method.invoke(null, new IEvent(event)); @@ -58,4 +66,42 @@ public class Utils { return null; } } + + @SuppressWarnings("unchecked") + public static T getEventMSCOM(Object event, Class cl) { + try { + val method = cl.getMethod("queryInterface", org.virtualbox_6_0.mscom.IUnknown.class); + return (T) method.invoke(null, new IEvent(new Variant(event).toDispatch())); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + return null; + } + } + + public static void handleEvent(EventHandlerBase handler, Map> eventMap, Object iEvent, boolean mscom) { + Class cl; + if (mscom) { + val kv = eventMap.entrySet().stream().filter(e -> e.getKey().value() == iEvent.getType().value()).findAny(); + if (!kv.isPresent()) return; //Event not supported + cl = kv.get().getValue(); + } else { + val kv = eventMap.entrySet().stream().filter(e -> e.getKey().value() == iEvent.getType()).findAny(); + if (!kv.isPresent()) return; //Event not supported + cl = kv.get().getValue(); + } + for (Method method : handler.getClass().getMethods()) { + if (method.isAnnotationPresent(org.bukkit.event.EventHandler.class) + && method.getParameterCount() == 1 && method.getParameterTypes()[0] == cl) { + try { + method.invoke(handler, Utils.getEvent(iEvent, cl)); + return; + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + Bukkit.getLogger().warning("Error while handling VirtualBox event!"); + e.getCause().printStackTrace(); + } + } + } + } }