Skip to content
Permalink
Browse files

More Rust Interfacing

  • Loading branch information...
phase committed Feb 27, 2016
1 parent 4ea6640 commit 3acf47704da8f15381bfbdb6cc6c3ce786d2af45
@@ -2,6 +2,10 @@
name = "phasebot"
version = "1.0.2"
authors = ["phase <ph4sesaber@gmail.com>"]

[lib]
name = "phasebot"
crate-type = ["dylib"]
crate-type = ["dylib"]

[dependencies]
lazy_static = "0.1.*"
BIN +1.08 MB lib/jna.jar
Binary file not shown.
BIN +292 KB (120%) res/lib/phasebot.dll
Binary file not shown.
@@ -0,0 +1,30 @@
#![crate_type = "dylib"]

#[macro_use]
extern crate lazy_static;

use std::sync::Mutex;
use std::collections::HashSet;

#[repr(C)]
pub struct Vec3d {
x: f64,
y: f64,
z: f64,
}

#[derive(Eq, PartialEq, Hash)]
struct Chunk {
x: i32,
y: i32,
z: i32,
blocks: [[[i32; 16]; 16]; 16],
}

lazy_static! {
static ref CHUNKS: Mutex<HashSet<Chunk>> = Mutex::new(HashSet::new());
}

pub extern fn addChunk(cx: i32, cy: i32, cz: i32, c_blocks: [[[i32; 16]; 16]; 16]) {
CHUNKS.lock().unwrap().insert(Chunk {x: cx, y: cy, z: cz, blocks: c_blocks});
}

This file was deleted.

@@ -1,86 +1,85 @@
package xyz.jadonfowler.phasebot.cmd.position;

import java.util.*;
import org.spacehq.packetlib.*;
import xyz.jadonfowler.phasebot.*;
import xyz.jadonfowler.phasebot.cmd.*;
import xyz.jadonfowler.phasebot.entity.*;
import xyz.jadonfowler.phasebot.pathfind.*;
import xyz.jadonfowler.phasebot.pathfind.AStar.InvalidPathException;
import xyz.jadonfowler.phasebot.util.*;

public class PathfindCommand extends Command {

@Override public void exec(String in, String[] args, Session s) {
try {
Vector3d start = PhaseBot.getBot().getPos().clone().addY(-1);
Vector3d end = null;
if (args.length == 2) {
if (args[1].matches("[-+]?\\d*\\.?\\d+")) {
int id = Integer.parseInt(args[1]);
Entity e = Entity.byId(id);
end = new Vector3d(e.getX(), e.getY()-1, e.getZ());
}
else {
try {
UUIDFetcher uf = new UUIDFetcher(Arrays.asList(args[1]));
UUID u = uf.call().get(args[1]);
Player e = Player.byUUID(u);
end = new Vector3d(e.getX(), e.getY()-1, e.getZ());
PhaseBot.getConsole().println("I'm moving towards " + args[1] + " at " + end.clone().floor());
}
catch (Exception e) {
e.printStackTrace();
}
}
}
// get our vars
if (end == null) {
double x = Double.parseDouble(args[1]);
double y = Double.parseDouble(args[2]);
double z = Double.parseDouble(args[3]);
end = PhaseBot.getBot().relativeToAbsolute(new Vector3d(x, y, z)).clone().addY(-1);
}
PhaseBot.getConsole().println("start= " + start + " \nend= " + end);
// create our pathfinder
AStar path = new AStar(start, end, 100);
// get the list of nodes to walk to as a Tile object
ArrayList<Tile> route = path.iterate();
// get the result of the path trace
PathingResult result = path.getPathingResult();
switch (result) {
case SUCCESS:
// Path was successful. Do something here.
PhaseBot.getBot().moveAlong(route);
PhaseBot.getConsole().println("Path was found! :D");
break;
case NO_PATH:
// No path found, throw error.
PhaseBot.getConsole().println("No path found!");
break;
default:
break;
}
}
catch (InvalidPathException e) {
// InvalidPathException will be thrown if start or end block is air
if (e.isEndNotSolid()) {
PhaseBot.getConsole().println("End block is not walkable");
}
if (e.isStartNotSolid()) {
PhaseBot.getConsole().println("Start block is not walkable");
}
} catch(Exception e){
e.printStackTrace();
}
}

@Override public String getCommand() {
return "pathfind";
}

@Override public String getDescription() {
// TODO Auto-generated method stub
return null;
}
}
package xyz.jadonfowler.phasebot.cmd.position;

import java.util.*;
import org.spacehq.packetlib.*;
import xyz.jadonfowler.phasebot.*;
import xyz.jadonfowler.phasebot.cmd.*;
import xyz.jadonfowler.phasebot.entity.*;
import xyz.jadonfowler.phasebot.pathfind.*;
import xyz.jadonfowler.phasebot.pathfind.AStar.InvalidPathException;
import xyz.jadonfowler.phasebot.util.*;

public class PathfindCommand extends Command {

@Override public void exec(String in, String[] args, Session s) {
try {
Vector3d start = PhaseBot.getBot().getPos().clone().addY(-1);
Vector3d end = null;
if (args.length == 2) {
if (args[1].matches("[-+]?\\d*\\.?\\d+")) {
int id = Integer.parseInt(args[1]);
Entity e = Entity.byId(id);
end = new Vector3d(e.getX(), e.getY()-1, e.getZ());
}
else {
try {
UUIDFetcher uf = new UUIDFetcher(Arrays.asList(args[1]));
UUID u = uf.call().get(args[1]);
Player e = Player.byUUID(u);
end = new Vector3d(e.getX(), e.getY()-1, e.getZ());
PhaseBot.getConsole().println("I'm moving towards " + args[1] + " at " + end.clone().floor());
}
catch (Exception e) {
e.printStackTrace();
}
}
}
// get our vars
if (end == null) {
double x = Double.parseDouble(args[1]);
double y = Double.parseDouble(args[2]);
double z = Double.parseDouble(args[3]);
end = PhaseBot.getBot().relativeToAbsolute(new Vector3d(x, y, z)).clone().addY(-1);
}
PhaseBot.getConsole().println("start= " + start + " \nend= " + end);
// create our pathfinder
AStar path = new AStar(start, end, 100);
// get the list of nodes to walk to as a Tile object
ArrayList<Tile> route = path.iterate();
// get the result of the path trace
PathingResult result = path.getPathingResult();
switch (result) {
case SUCCESS:
// Path was successful. Do something here.
PhaseBot.getBot().moveAlong(route);
PhaseBot.getConsole().println("Path was found! :D");
break;
case NO_PATH:
// No path found, throw error.
PhaseBot.getConsole().println("No path found!");
break;
default:
break;
}
}
catch (InvalidPathException e) {
// InvalidPathException will be thrown if start or end block is air
if (e.isEndNotSolid()) {
PhaseBot.getConsole().println("End block is not walkable");
}
if (e.isStartNotSolid()) {
PhaseBot.getConsole().println("Start block is not walkable");
}
} catch(Exception e){
e.printStackTrace();
}
}

@Override public String getCommand() {
return "pathfind";
}

@Override public String getDescription() {
return "Pathfind to a player or entity";
}
}
@@ -0,0 +1,12 @@
package xyz.jadonfowler.phasebot.jna;

import com.sun.jna.*;

public interface RustInterface extends Library {
String JNA_LIBRARY_NAME = "phasebot";
NativeLibrary JNA_NATIVE_LIB = NativeLibrary.getInstance(JNA_LIBRARY_NAME);

RustInterface INSTANCE = (RustInterface) Native.loadLibrary(JNA_LIBRARY_NAME, RustInterface.class);

void addChunk(int x, int y, int z, int[][][] blocks);
}

0 comments on commit 3acf477

Please sign in to comment.
You can’t perform that action at this time.