From f7c72f2f67c250c364c88a85bfbe1a744a70684c Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 10 Apr 2019 22:18:01 +0200 Subject: [PATCH] Exception handling stuff Java: 2019.04.07. PXC: 2019.03.10. --- .../java/sznp/virtualcomputer/Commands.java | 41 +++++++++----- .../java/sznp/virtualcomputer/Computer.java | 6 +- .../MouseLockerPlayerListener.java | 8 ++- libpxc/makefile | 2 +- libpxc/pxc.c | 55 +++++++------------ libpxc/pxct.c | 8 ++- 6 files changed, 63 insertions(+), 57 deletions(-) diff --git a/VirtualComputer/src/main/java/sznp/virtualcomputer/Commands.java b/VirtualComputer/src/main/java/sznp/virtualcomputer/Commands.java index 8060bb7..a92868b 100644 --- a/VirtualComputer/src/main/java/sznp/virtualcomputer/Commands.java +++ b/VirtualComputer/src/main/java/sznp/virtualcomputer/Commands.java @@ -5,6 +5,7 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.virtualbox_6_0.VBoxException; public class Commands implements CommandExecutor { @@ -65,21 +66,28 @@ public class Commands implements CommandExecutor { case "mouse": boolean showusage = true; if (args.length < 6) { - // Command overloading, because I can :P - if (args.length > 4) // 4 4) // 4 "); @@ -118,10 +126,15 @@ public class Commands implements CommandExecutor { case "mspeed": case "mousespeed": if (args.length < 3) { - sender.sendMessage("§cUsage: /computer input mspeed "); + sender.sendMessage("§cUsage: /computer input mspeed "); return true; } - MouseLockerPlayerListener.LockedSpeed = Float.parseFloat(args[2]); + try { + MouseLockerPlayerListener.LockedSpeed = Float.parseFloat(args[2]); + } catch(NumberFormatException e) { + sender.sendMessage("§cThe speed must be a number."); + break; + } sender.sendMessage("§aMouse speed set to " + MouseLockerPlayerListener.LockedSpeed); } break; diff --git a/VirtualComputer/src/main/java/sznp/virtualcomputer/Computer.java b/VirtualComputer/src/main/java/sznp/virtualcomputer/Computer.java index 40cc76e..71961ef 100644 --- a/VirtualComputer/src/main/java/sznp/virtualcomputer/Computer.java +++ b/VirtualComputer/src/main/java/sznp/virtualcomputer/Computer.java @@ -176,17 +176,17 @@ public final class Computer { } } - public void UpdateMouse(CommandSender sender, int x, int y, int z, int w, String mbs, boolean down) { + public void UpdateMouse(CommandSender sender, int x, int y, int z, int w, String mbs, boolean down) throws Exception { if (checkMachineNotRunning(sender)) return; int state = 0; if (mbs.length() > 0 && down) state = Arrays.stream(MouseButtonState.values()).filter(mousebs -> mousebs.name().equalsIgnoreCase(mbs)) - .findAny().orElseThrow(() -> new RuntimeException("Unknown mouse button")).value(); + .findAny().orElseThrow(() -> new Exception("Unknown mouse button")).value(); session.getConsole().getMouse().putMouseEvent(x, y, z, w, state); } - public void UpdateMouse(CommandSender sender, int x, int y, int z, int w, String mbs) { + public void UpdateMouse(CommandSender sender, int x, int y, int z, int w, String mbs) throws Exception { if (checkMachineNotRunning(sender)) return; UpdateMouse(sender, x, y, z, w, mbs, true); diff --git a/VirtualComputer/src/main/java/sznp/virtualcomputer/MouseLockerPlayerListener.java b/VirtualComputer/src/main/java/sznp/virtualcomputer/MouseLockerPlayerListener.java index 9918815..bc9afd9 100644 --- a/VirtualComputer/src/main/java/sznp/virtualcomputer/MouseLockerPlayerListener.java +++ b/VirtualComputer/src/main/java/sznp/virtualcomputer/MouseLockerPlayerListener.java @@ -25,8 +25,12 @@ public class MouseLockerPlayerListener implements Runnable, Listener { if (yaw2 - yaw1 == 0 || pitch2 - pitch1 == 0) return; - Computer.getInstance().UpdateMouse(null, (int) ((yaw2 - yaw1) * LockedSpeed), - (int) ((pitch2 - pitch1) * LockedSpeed), 0, 0, ""); + try { + Computer.getInstance().UpdateMouse(null, (int) ((yaw2 - yaw1) * LockedSpeed), + (int) ((pitch2 - pitch1) * LockedSpeed), 0, 0, ""); + } catch (Exception e) { //Should not happen + e.printStackTrace(); + } entry.getKey().teleport(entry.getValue(), TeleportCause.PLUGIN); } diff --git a/libpxc/makefile b/libpxc/makefile index b4745c1..a1ccd76 100644 --- a/libpxc/makefile +++ b/libpxc/makefile @@ -5,7 +5,7 @@ pxct: pxct.c pxc $(CC) -Wall pxct.c -Lout -lpxc -Wl,-rpath=out -o out/pxct pxc: pxc.c - $(CC) -Wall -c -fpic pxc.c -o obj/pxc.o + $(CC) -Wall -O4 -c -fpic pxc.c -o obj/pxc.o $(CC) -Wall -shared obj/pxc.o -o out/libpxc.so cp out/libpxc.so ../ diff --git a/libpxc/pxc.c b/libpxc/pxc.c index 44798d2..388b503 100644 --- a/libpxc/pxc.c +++ b/libpxc/pxc.c @@ -14,15 +14,15 @@ typedef long long int addr; typedef struct { //RGB - int red : 8; - int green : 8; - int blue : 8; + unsigned int red : 8; + unsigned int green : 8; + unsigned int blue : 8; } Color; //Used for Bukkit's initializers (for simplicity) typedef struct { //BGRA - int blue : 8; - int green : 8; - int red : 8; - int alpha : 8; + unsigned int blue : 8; + unsigned int green : 8; + unsigned int red : 8; + unsigned int alpha : 8; } NativeColor; //Used for the screen data char matchColor(NativeColor nc); @@ -55,34 +55,17 @@ char* getMapColor(int x, int y) { //May return 0 void* updateAndGetMap(int x, int y, int w, int h, int** out_changed) { //TODO: Support the parameters if(image==NULL || maps==NULL) return 0; - char* mapp = maps; - NativeColor* imgp = image; - printf("PXC: mapp=%p imgp=%p\n", mapp, imgp); - for(int k=0; k MAPSIZE * MAPSIZE * mapcx * mapcy || - (void*)imgp - image > width * height * sizeof(NativeColor)) { - mapp = maps; //Start over - imgp = image; //Start over - } + for(int l=y; l #include +#include /* https://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html gcc -Wall -c -fpic pxc.c @@ -17,8 +18,13 @@ int main() { printf("Setting source..."); void* p = malloc(640*480*4); setSource((addr)p, 640, 480, 5, 4); + printf("Adding random values...\n"); + srand(time(NULL)); + unsigned int* xp=p; + for(int i=0; i<640*480; i++) + *(xp++)=rand(); printf("Updating map..."); - void* x=updateAndGetMap(0, 0, 640, 480, NULL); + void* x=updateAndGetMap(0, 0, 320, 240, NULL); free(p); return 0; }