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.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<x<6
{
Computer.getInstance().UpdateMouse(sender, Integer.parseInt(args[1]), Integer.parseInt(args[2]),
Integer.parseInt(args[3]), Integer.parseInt(args[4]), "", 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]);
try {
// 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);
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) {
sender.sendMessage("§cUsage: /computer mouse <relx> <rely> <relz> <relw>");
@ -118,10 +126,15 @@ public class Commands implements CommandExecutor {
case "mspeed":
case "mousespeed":
if (args.length < 3) {
sender.sendMessage("§cUsage: /computer input mspeed <integer>");
sender.sendMessage("§cUsage: /computer input mspeed <number>");
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;

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))
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);

View file

@ -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);
}

View file

@ -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 ../

View file

@ -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<mapcx; k++) {
printf("PXC: k=%d\n", k);
for(int l=0; l<mapcy; l++) {
printf("PXC: l=%d\n", l);
mapp = maps + MAPSIZE*k + MAPSIZE*mapcx*MAPSIZE*l; //Go to the start of the map
printf("PXC: mapp=%p imgp=%p - %d\n", mapp, imgp, *mapp);
for(short i=0; i<MAPSIZE; i++) { //We need to jump
for(short j=0; j<MAPSIZE; j++) {
//*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
}
for(int l=y; l<y+h; l++) {
//printf("PXC: l=%d\n", l);
for(int k=x; k<x+w; k++) {
//printf("PXC: k=%d\n", k);
char *mapp=getMapColor(k, l);
char color=matchColor(*getNativeColor(k, l));
//NativeColor x={.red=0, .green=0, .blue=255, .alpha=255};
//*mapp=matchColor(x);
*mapp=color;
//printf("PXC: mapp: %d %d - %x\n", k, l, *mapp); //TODO: The bottom of the image
//printf("PXC: mapcx: %d mapcy: %d\n", k, l); //TODO: Has nothing
}
}
printf("PXC: ret maps=%p\n", maps);
@ -159,7 +142,7 @@ double getDistance(Color c1, Color c2) {
//Code from Bukkit's MapPalette class
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};
char index = 0;

View file

@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* 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;
}