Exception handling stuff

Java: 2019.04.07.
PXC: 2019.03.10.
This commit is contained in:
Norbi Peti 2019-04-10 22:18:01 +02:00
parent dd9b850da8
commit f7c72f2f67
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
6 changed files with 63 additions and 57 deletions

View file

@ -5,6 +5,7 @@ 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 org.virtualbox_6_0.VBoxException;
public class Commands implements CommandExecutor { public class Commands implements CommandExecutor {
@ -65,21 +66,28 @@ public class Commands implements CommandExecutor {
case "mouse": case "mouse":
boolean showusage = true; boolean showusage = true;
if (args.length < 6) { if (args.length < 6) {
// Command overloading, because I can :P try {
if (args.length > 4) // 4<x<6 // Command overloading, because I can :P
{ if (args.length > 4) // 4<x<6
Computer.getInstance().UpdateMouse(sender, Integer.parseInt(args[1]), Integer.parseInt(args[2]), {
Integer.parseInt(args[3]), Integer.parseInt(args[4]), "", false); Computer.getInstance().UpdateMouse(sender, Integer.parseInt(args[1]), Integer.parseInt(args[2]),
showusage = false; Integer.parseInt(args[3]), Integer.parseInt(args[4]), "", false);
} else {
if (args.length == 3) {
Computer.getInstance().UpdateMouse(sender, 0, 0, 0, 0, args[1], args[2].equals("down"));
showusage = false;
} else if (args.length == 2) {
Computer.getInstance().UpdateMouse(sender, 0, 0, 0, 0, args[1]);
showusage = false; showusage = false;
} else {
if (args.length == 3) {
Computer.getInstance().UpdateMouse(sender, 0, 0, 0, 0, args[1], args[2].equals("down"));
showusage = false;
} else if (args.length == 2) {
Computer.getInstance().UpdateMouse(sender, 0, 0, 0, 0, args[1]);
showusage = false;
}
} }
} }
catch (VBoxException e) {
e.printStackTrace();
}
catch (Exception ignored) { //It will show the usage here
}
} }
if (showusage) { if (showusage) {
sender.sendMessage("§cUsage: /computer mouse <relx> <rely> <relz> <relw>"); sender.sendMessage("§cUsage: /computer mouse <relx> <rely> <relz> <relw>");
@ -118,10 +126,15 @@ public class Commands implements CommandExecutor {
case "mspeed": case "mspeed":
case "mousespeed": case "mousespeed":
if (args.length < 3) { if (args.length < 3) {
sender.sendMessage("§cUsage: /computer input mspeed <integer>"); sender.sendMessage("§cUsage: /computer input mspeed <number>");
return true; 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); sender.sendMessage("§aMouse speed set to " + MouseLockerPlayerListener.LockedSpeed);
} }
break; break;

View file

@ -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)) if (checkMachineNotRunning(sender))
return; return;
int state = 0; int state = 0;
if (mbs.length() > 0 && down) if (mbs.length() > 0 && down)
state = Arrays.stream(MouseButtonState.values()).filter(mousebs -> mousebs.name().equalsIgnoreCase(mbs)) 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); 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)) if (checkMachineNotRunning(sender))
return; return;
UpdateMouse(sender, x, y, z, w, mbs, true); UpdateMouse(sender, x, y, z, w, mbs, true);

View file

@ -25,8 +25,12 @@ public class MouseLockerPlayerListener implements Runnable, Listener {
if (yaw2 - yaw1 == 0 || pitch2 - pitch1 == 0) if (yaw2 - yaw1 == 0 || pitch2 - pitch1 == 0)
return; return;
Computer.getInstance().UpdateMouse(null, (int) ((yaw2 - yaw1) * LockedSpeed), try {
(int) ((pitch2 - pitch1) * LockedSpeed), 0, 0, ""); 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); entry.getKey().teleport(entry.getValue(), TeleportCause.PLUGIN);
} }

View file

@ -5,7 +5,7 @@ pxct: pxct.c pxc
$(CC) -Wall pxct.c -Lout -lpxc -Wl,-rpath=out -o out/pxct $(CC) -Wall pxct.c -Lout -lpxc -Wl,-rpath=out -o out/pxct
pxc: pxc.c 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 $(CC) -Wall -shared obj/pxc.o -o out/libpxc.so
cp out/libpxc.so ../ cp out/libpxc.so ../

View file

@ -14,15 +14,15 @@
typedef long long int addr; typedef long long int addr;
typedef struct { //RGB typedef struct { //RGB
int red : 8; unsigned int red : 8;
int green : 8; unsigned int green : 8;
int blue : 8; unsigned int blue : 8;
} Color; //Used for Bukkit's initializers (for simplicity) } Color; //Used for Bukkit's initializers (for simplicity)
typedef struct { //BGRA typedef struct { //BGRA
int blue : 8; unsigned int blue : 8;
int green : 8; unsigned int green : 8;
int red : 8; unsigned int red : 8;
int alpha : 8; unsigned int alpha : 8;
} NativeColor; //Used for the screen data } NativeColor; //Used for the screen data
char matchColor(NativeColor nc); char matchColor(NativeColor nc);
@ -55,34 +55,17 @@ char* getMapColor(int x, int y) {
//May return 0 //May return 0
void* updateAndGetMap(int x, int y, int w, int h, int** out_changed) { //TODO: Support the parameters void* updateAndGetMap(int x, int y, int w, int h, int** out_changed) { //TODO: Support the parameters
if(image==NULL || maps==NULL) return 0; if(image==NULL || maps==NULL) return 0;
char* mapp = maps; for(int l=y; l<y+h; l++) {
NativeColor* imgp = image; //printf("PXC: l=%d\n", l);
printf("PXC: mapp=%p imgp=%p\n", mapp, imgp); for(int k=x; k<x+w; k++) {
for(int k=0; k<mapcx; k++) { //printf("PXC: k=%d\n", k);
printf("PXC: k=%d\n", k); char *mapp=getMapColor(k, l);
for(int l=0; l<mapcy; l++) { char color=matchColor(*getNativeColor(k, l));
printf("PXC: l=%d\n", l); //NativeColor x={.red=0, .green=0, .blue=255, .alpha=255};
mapp = maps + MAPSIZE*k + MAPSIZE*mapcx*MAPSIZE*l; //Go to the start of the map //*mapp=matchColor(x);
printf("PXC: mapp=%p imgp=%p - %d\n", mapp, imgp, *mapp); *mapp=color;
for(short i=0; i<MAPSIZE; i++) { //We need to jump //printf("PXC: mapp: %d %d - %x\n", k, l, *mapp); //TODO: The bottom of the image
for(short j=0; j<MAPSIZE; j++) { //printf("PXC: mapcx: %d mapcy: %d\n", k, l); //TODO: Has nothing
//*mapp=matchColor(*imgp); - TODO
NativeColor x={.red=0, .green=0, .blue=255};
*mapp=matchColor(x);
printf("PXC: imgp: %ld - %x\n", (void*)imgp-image, *imgp); //TODO: The bottom of the image
printf("PXC: mapcx: %d mapcy: %d\n", k, l); //TODO: Has nothing
imgp++; //Increment by the size of NativeColor so 4
mapp++;
}
imgp+=width-MAPSIZE; //Go back to the first column
//printf("Row done, imgp=%p\n", imgp);
}
imgp+=MAPSIZE; //Go to the next map, kind of - TODO: Use X and Y coords!
if((void*)mapp - maps > MAPSIZE * MAPSIZE * mapcx * mapcy ||
(void*)imgp - image > width * height * sizeof(NativeColor)) {
mapp = maps; //Start over
imgp = image; //Start over
}
} }
} }
printf("PXC: ret maps=%p\n", maps); printf("PXC: ret maps=%p\n", maps);
@ -159,7 +142,7 @@ double getDistance(Color c1, Color c2) {
//Code from Bukkit's MapPalette class //Code from Bukkit's MapPalette class
char matchColor(NativeColor nc) { char matchColor(NativeColor nc) {
if (nc.alpha < 128) return 0; //if (nc.alpha < 128) return 0;
Color color={.red=nc.red, .green=nc.green, .blue=nc.blue}; Color color={.red=nc.red, .green=nc.green, .blue=nc.blue};
char index = 0; char index = 0;

View file

@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h>
/* https://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html /* https://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html
gcc -Wall -c -fpic pxc.c gcc -Wall -c -fpic pxc.c
@ -17,8 +18,13 @@ int main() {
printf("Setting source..."); printf("Setting source...");
void* p = malloc(640*480*4); void* p = malloc(640*480*4);
setSource((addr)p, 640, 480, 5, 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..."); printf("Updating map...");
void* x=updateAndGetMap(0, 0, 640, 480, NULL); void* x=updateAndGetMap(0, 0, 320, 240, NULL);
free(p); free(p);
return 0; return 0;
} }