Created event framework

It uses a similar system to Bukkit's
Hopefully it works
This commit is contained in:
Norbi Peti 2019-03-21 00:46:54 +01:00
parent e75b841d33
commit 5e98afe66a
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
9 changed files with 137 additions and 49 deletions

View file

@ -75,6 +75,12 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId>
<version>1.12.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View file

@ -5,7 +5,6 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import sznp.virtualcomputer.events.Computer;
public class Commands implements CommandExecutor { public class Commands implements CommandExecutor {

View file

@ -1,4 +1,4 @@
package sznp.virtualcomputer.events; package sznp.virtualcomputer;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import lombok.Getter; import lombok.Getter;
@ -7,10 +7,10 @@ import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.virtualbox_6_0.*; import org.virtualbox_6_0.*;
import sznp.virtualcomputer.PluginMain; import sznp.virtualcomputer.events.MachineEventHandler;
import sznp.virtualcomputer.events.VBoxEventHandler;
import sznp.virtualcomputer.renderer.MCFrameBuffer; import sznp.virtualcomputer.renderer.MCFrameBuffer;
import sznp.virtualcomputer.util.Scancode; import sznp.virtualcomputer.util.Scancode;
import sznp.virtualcomputer.util.Utils;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Arrays; import java.util.Arrays;
@ -25,10 +25,12 @@ public final class Computer {
private IMachine machine; private IMachine machine;
@java.beans.ConstructorProperties({"plugin"}) @java.beans.ConstructorProperties({"plugin"})
public Computer(PluginMain plugin) { public Computer(PluginMain plugin, ISession session, IVirtualBox vbox) {
this.plugin = plugin; this.plugin = plugin;
this.session = session;
this.vbox = vbox;
if(instance!=null) throw new IllegalStateException("A computer already exists!"); if(instance!=null) throw new IllegalStateException("A computer already exists!");
instance=this; //TODO: Move some init stuff here instance = this;
} }
public void Start(CommandSender sender, int index) {// TODO: Add touchscreen support (#2) public void Start(CommandSender sender, int index) {// TODO: Add touchscreen support (#2)
@ -73,9 +75,9 @@ public final class Computer {
machine = session.getMachine(); // This is the Machine object we can work with machine = session.getMachine(); // This is the Machine object we can work with
final IConsole console = session.getConsole(); final IConsole console = session.getConsole();
val handler = new MachineEventHandler(Computer.this); val handler = new MachineEventHandler(Computer.this);
Utils.registerListener(console.getEventSource(), handler, VBoxEventType.MachineEvent); handler.registerTo(console.getEventSource());
IProgress progress = console.powerUp(); // https://marc.info/?l=vbox-dev&m=142780789819967&w=2 IProgress progress = console.powerUp(); // https://marc.info/?l=vbox-dev&m=142780789819967&w=2
Utils.registerListener(progress.getEventSource(), handler, VBoxEventType.OnProgressTaskCompleted); //TODO: Show progress bar some way? handler.registerTo(progress.getEventSource()); //TODO: Show progress bar some way?
console.getDisplay().attachFramebuffer(0L, console.getDisplay().attachFramebuffer(0L,
new IFramebuffer(new MCFrameBuffer(console.getDisplay(), true))); new IFramebuffer(new MCFrameBuffer(console.getDisplay(), true)));
sendMessage(sender, "§eComputer started."); sendMessage(sender, "§eComputer started.");

View file

@ -1,13 +1,13 @@
package sznp.virtualcomputer; package sznp.virtualcomputer;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class MouseLockerPlayerListener implements Runnable { public class MouseLockerPlayerListener implements Runnable {
public static Map<Player, Location> LockedPlayers = new HashMap<>(); public static Map<Player, Location> LockedPlayers = new HashMap<>();
public static float LockedSpeed = 5; public static float LockedSpeed = 5;
@ -22,7 +22,7 @@ public class MouseLockerPlayerListener implements Runnable {
if (yaw2 - yaw1 == 0 || pitch2 - pitch1 == 0) if (yaw2 - yaw1 == 0 || pitch2 - pitch1 == 0)
return; return;
PluginMain.Instance.UpdateMouse(null, (int) ((yaw2 - yaw1) * LockedSpeed), Computer.getInstance().UpdateMouse(null, (int) ((yaw2 - yaw1) * LockedSpeed),
(int) ((pitch2 - pitch1) * LockedSpeed), 0, 0, ""); (int) ((pitch2 - pitch1) * LockedSpeed), 0, 0, "");
entry.getKey().teleport(entry.getValue(), TeleportCause.PLUGIN); entry.getKey().teleport(entry.getValue(), TeleportCause.PLUGIN);

View file

@ -70,8 +70,9 @@ public class PluginMain extends JavaPlugin {
VBoxLib vbl = LibraryLoader.create(VBoxLib.class).load("vboxjxpcom"); VBoxLib vbl = LibraryLoader.create(VBoxLib.class).load("vboxjxpcom");
vbl.RTR3InitExe(0, "", 0); vbl.RTR3InitExe(0, "", 0);
vbox = manager.getVBox(); vbox = manager.getVBox();
Utils.registerListener(vbox.getEventSource(), new VBoxEventHandler(), VBoxEventType.OnMachineStateChanged); new VBoxEventHandler().registerTo(vbox.getEventSource());
session = manager.getSessionObject(); // TODO: Events session = manager.getSessionObject();
new Computer(this, session, vbox); //Saves itself
ccs.sendMessage("§bLoading Screen..."); ccs.sendMessage("§bLoading Screen...");
try { try {
//throw new NoClassDefFoundError("Test error pls ignore"); //throw new NoClassDefFoundError("Test error pls ignore");

View file

@ -0,0 +1,56 @@
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.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;
/**
* A Bukkit-like event system which calls the appropriate methods on an event.
*/
public abstract class EventHandlerBase extends COMObjectBase implements IEventListener {
/**
* The events to listen for. It will only look for these handlers.
*/
private final Map<VBoxEventType, Class<? extends org.virtualbox_6_0.IEvent>> eventMap;
protected EventHandlerBase(Map<VBoxEventType, Class<? extends org.virtualbox_6_0.IEvent>> eventMap) {
//this.eventMap = eventMap.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().value(), Map.Entry::getValue));
this.eventMap = eventMap;
}
@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
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> void registerTo(IEventSource source) {
Utils.registerListener(source, this, new ArrayList<>(eventMap.keySet()));
}
}

View file

@ -1,19 +1,24 @@
package sznp.virtualcomputer.events; package sznp.virtualcomputer.events;
import lombok.RequiredArgsConstructor; import com.google.common.collect.ImmutableMap;
import lombok.val; import org.bukkit.event.EventHandler;
import org.mozilla.interfaces.IEvent; import org.virtualbox_6_0.IMachineStateChangedEvent;
import org.mozilla.interfaces.IEventListener; import org.virtualbox_6_0.IProgressTaskCompletedEvent;
import org.virtualbox_6_0.IStateChangedEvent; import org.virtualbox_6_0.IStateChangedEvent;
import sznp.virtualcomputer.util.COMObjectBase; import org.virtualbox_6_0.VBoxEventType;
import sznp.virtualcomputer.Computer;
@RequiredArgsConstructor public class MachineEventHandler extends EventHandlerBase {
public class MachineEventHandler extends COMObjectBase implements IEventListener {
private final Computer computer; private final Computer computer;
@Override
public void handleEvent(IEvent iEvent) { public MachineEventHandler(Computer computer) {
if(iEvent instanceof IStateChangedEvent) { super(ImmutableMap.of(VBoxEventType.OnMachineStateChanged, IMachineStateChangedEvent.class,
val event=(IStateChangedEvent) iEvent; //https://www.virtualbox.org/sdkref/_virtual_box_8idl.html#a80b08f71210afe16038e904a656ed9eb VBoxEventType.OnProgressTaskCompleted, IProgressTaskCompletedEvent.class));
this.computer = computer;
}
@EventHandler
public void handleStateChange(IStateChangedEvent event) { //https://www.virtualbox.org/sdkref/_virtual_box_8idl.html#a80b08f71210afe16038e904a656ed9eb
switch (event.getState()) { switch (event.getState()) {
case Stuck: case Stuck:
computer.Stop(null); computer.Stop(null);
@ -23,5 +28,4 @@ public class MachineEventHandler extends COMObjectBase implements IEventListener
computer.stopRendering(); computer.stopRendering();
} }
} }
}
} }

View file

@ -1,16 +1,17 @@
package sznp.virtualcomputer.events; package sznp.virtualcomputer.events;
import com.google.common.collect.ImmutableMap;
import lombok.Getter; import lombok.Getter;
import lombok.val;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.mozilla.interfaces.IEvent; import org.bukkit.event.EventHandler;
import org.mozilla.interfaces.IEventListener;
import org.virtualbox_6_0.ISessionStateChangedEvent; import org.virtualbox_6_0.ISessionStateChangedEvent;
import org.virtualbox_6_0.SessionState; import org.virtualbox_6_0.SessionState;
import sznp.virtualcomputer.util.COMObjectBase; import org.virtualbox_6_0.VBoxEventType;
import sznp.virtualcomputer.Computer;
public class VBoxEventHandler extends COMObjectBase implements IEventListener { public class VBoxEventHandler extends EventHandlerBase {
public VBoxEventHandler() { public VBoxEventHandler() {
super(ImmutableMap.of(VBoxEventType.OnSessionStateChanged, ISessionStateChangedEvent.class));
instance = this; instance = this;
} }
@ -19,15 +20,15 @@ public class VBoxEventHandler extends COMObjectBase implements IEventListener {
private String machineID; private String machineID;
private CommandSender sender; private CommandSender sender;
@Override @EventHandler
public void handleEvent(IEvent iEvent) { public void onSessionStateChange(ISessionStateChangedEvent event) {
if (iEvent instanceof ISessionStateChangedEvent) { System.out.println("Session change event: " + event);
val event = ((ISessionStateChangedEvent) iEvent); System.out.println("ID1: " + event.getMachineId() + " - ID2: " + machineID);
if (!event.getMachineId().equals(machineID)) return; if (!event.getMachineId().equals(machineID)) return;
System.out.println("State: " + event.getState());
if (event.getState() == SessionState.Locked) //Need to check here, because we can't access the console yet if (event.getState() == SessionState.Locked) //Need to check here, because we can't access the console yet
Computer.getInstance().onLock(sender); Computer.getInstance().onLock(sender);
} }
}
public void setup(String machineID, CommandSender sender) { public void setup(String machineID, CommandSender sender) {
this.machineID = machineID; this.machineID = machineID;

View file

@ -1,11 +1,16 @@
package sznp.virtualcomputer.util; package sznp.virtualcomputer.util;
import lombok.val;
import org.mozilla.interfaces.IEventListener; import org.mozilla.interfaces.IEventListener;
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 java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
public class Utils { public class Utils {
/** /**
@ -34,7 +39,21 @@ public class Utils {
usrPathsField.set(null, newPaths); usrPathsField.set(null, newPaths);
} }
public static void registerListener(IEventSource source, IEventListener listener, VBoxEventType... types) { //public static void registerListener(IEventSource source, IEventListener listener, VBoxEventType... types) {
source.registerListener(new org.virtualbox_6_0.IEventListener(listener), Arrays.asList(types), true); public static void registerListener(IEventSource source, IEventListener listener, List<VBoxEventType> types) {
source.registerListener(new org.virtualbox_6_0.IEventListener(listener), types, true);
}
@SuppressWarnings("unchecked")
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 {
val method = cl.getMethod("queryInterface", IUnknown.class);
return (T) method.invoke(null, new IEvent(event));
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
return null;
}
} }
} }