parent
ea6c248534
commit
b7d0b49ac6
6 changed files with 27 additions and 66 deletions
|
@ -129,7 +129,7 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.virtualbox</groupId>
|
<groupId>org.virtualbox</groupId>
|
||||||
<artifactId>VirtualBox</artifactId>
|
<artifactId>VirtualBox-MSCOM</artifactId>
|
||||||
<version>6.0</version>
|
<version>6.0</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
|
@ -61,11 +61,6 @@
|
||||||
<artifactId>VirtualBox-MSCOM</artifactId>
|
<artifactId>VirtualBox-MSCOM</artifactId>
|
||||||
<version>6.0</version>
|
<version>6.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>com.jacob</groupId>
|
|
||||||
<artifactId>Jacob</artifactId>
|
|
||||||
<version>1.19</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.bukkit</groupId>
|
<groupId>org.bukkit</groupId>
|
||||||
<artifactId>craftbukkit</artifactId>
|
<artifactId>craftbukkit</artifactId>
|
||||||
|
|
|
@ -23,7 +23,6 @@ import java.util.Arrays;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
public class PluginMain extends JavaPlugin {
|
public class PluginMain extends JavaPlugin {
|
||||||
public static final boolean MSCOM = true; //TODO: Implement
|
|
||||||
private static final int MCX = 5;
|
private static final int MCX = 5;
|
||||||
private static final int MCY = 4;
|
private static final int MCY = 4;
|
||||||
private BukkitTask mousetask;
|
private BukkitTask mousetask;
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
package sznp.virtualcomputer.events;
|
package sznp.virtualcomputer.events;
|
||||||
|
|
||||||
|
import lombok.val;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.mozilla.interfaces.IEvent;
|
import org.mozilla.interfaces.IEvent;
|
||||||
import org.mozilla.interfaces.IEventListener;
|
import org.mozilla.interfaces.IEventListener;
|
||||||
import org.virtualbox_6_0.IEventSource;
|
import org.virtualbox_6_0.IEventSource;
|
||||||
import org.virtualbox_6_0.VBoxEventType;
|
import org.virtualbox_6_0.VBoxEventType;
|
||||||
import sznp.virtualcomputer.PluginMain;
|
|
||||||
import sznp.virtualcomputer.util.COMObjectBase;
|
import sznp.virtualcomputer.util.COMObjectBase;
|
||||||
import sznp.virtualcomputer.util.Utils;
|
import sznp.virtualcomputer.util.Utils;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -28,15 +31,29 @@ public abstract class EventHandlerBase extends COMObjectBase implements IEventLi
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void handleEvent(IEvent iEvent) {
|
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)
|
if (!enabled)
|
||||||
return;
|
return;
|
||||||
Utils.handleEvent(this, eventMap, iEvent, false);
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T extends EventHandlerBase> org.virtualbox_6_0.IEventListener registerTo(IEventSource source) {
|
public <T extends EventHandlerBase> org.virtualbox_6_0.IEventListener registerTo(IEventSource source) {
|
||||||
if (PluginMain.MSCOM)
|
|
||||||
return Utils.registerListenerMSCOM(source, this, new ArrayList<>(eventMap.keySet()));
|
|
||||||
else
|
|
||||||
return Utils.registerListener(source, this, new ArrayList<>(eventMap.keySet()));
|
return Utils.registerListener(source, this, new ArrayList<>(eventMap.keySet()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
package sznp.virtualcomputer.events;
|
|
||||||
|
|
||||||
public class EventHandlerBaseMSCOM {
|
|
||||||
}
|
|
|
@ -1,21 +1,16 @@
|
||||||
package sznp.virtualcomputer.util;
|
package sznp.virtualcomputer.util;
|
||||||
|
|
||||||
import com.jacob.com.Variant;
|
|
||||||
import lombok.val;
|
import lombok.val;
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.mozilla.interfaces.IEventListener;
|
import org.mozilla.interfaces.IEventListener;
|
||||||
import org.virtualbox_6_0.IEvent;
|
import org.virtualbox_6_0.IEvent;
|
||||||
import org.virtualbox_6_0.IEventSource;
|
import org.virtualbox_6_0.IEventSource;
|
||||||
import org.virtualbox_6_0.VBoxEventType;
|
import org.virtualbox_6_0.VBoxEventType;
|
||||||
import org.virtualbox_6_0.xpcom.IUnknown;
|
import org.virtualbox_6_0.xpcom.IUnknown;
|
||||||
import sznp.virtualcomputer.events.EventHandlerBase;
|
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class Utils {
|
public class Utils {
|
||||||
/**
|
/**
|
||||||
|
@ -44,20 +39,17 @@ public class Utils {
|
||||||
usrPathsField.set(null, newPaths);
|
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<VBoxEventType> types) {
|
public static org.virtualbox_6_0.IEventListener registerListener(IEventSource source, IEventListener listener, List<VBoxEventType> types) {
|
||||||
val ret = new org.virtualbox_6_0.IEventListener(listener);
|
val ret = new org.virtualbox_6_0.IEventListener(listener);
|
||||||
source.registerListener(ret, types, true);
|
source.registerListener(ret, types, true);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static org.virtualbox_6_0.IEventListener registerListenerMSCOM(IEventSource source, Object listener, List<VBoxEventType> types) {
|
|
||||||
val ret = new org.virtualbox_6_0.IEventListener(new Variant(listener).toDispatch());
|
|
||||||
source.registerListener(ret, types, true);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static <T extends IEvent> T getEvent(org.mozilla.interfaces.IEvent event, Class<T> cl) {
|
public static <T extends IEvent> T getEvent(org.mozilla.interfaces.IEvent event, Class<T> cl) {
|
||||||
|
//if (event.getType() != type.value()) return null;
|
||||||
|
//return (T) T.queryInterface(new IEvent(event)); - Probably won't work
|
||||||
try {
|
try {
|
||||||
val method = cl.getMethod("queryInterface", IUnknown.class);
|
val method = cl.getMethod("queryInterface", IUnknown.class);
|
||||||
return (T) method.invoke(null, new IEvent(event));
|
return (T) method.invoke(null, new IEvent(event));
|
||||||
|
@ -66,42 +58,4 @@ public class Utils {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public static <T extends IEvent> T getEventMSCOM(Object event, Class<T> 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<VBoxEventType, Class<? extends IEvent>> 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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue