Skip to content

Commit ddf32bd

Browse files
committed
Owners from Config and Block commands
1 parent a1131a4 commit ddf32bd

File tree

5 files changed

+154
-9
lines changed

5 files changed

+154
-9
lines changed

src/xyz/jadonfowler/phasebot/Bot.java

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void swing() {
9191
}
9292

9393
public void centerPosition() {
94-
//return;
94+
// return;
9595
double dx = pos.x > 0 ? Math.floor(pos.x) + 0.5d : Math.round(pos.x) - 0.5d;
9696
double dy = Math.floor(pos.y);
9797
double dz = pos.z > 0 ? Math.floor(pos.z) + 0.5d : Math.round(pos.z) - 0.5d;
@@ -248,7 +248,12 @@ private boolean isDiagonal(double x, double y, double z) {
248248
return false;
249249
}
250250

251-
public void breakBlock(int rx, int ry, int rz) {
251+
public void breakBlockAbsolute(Vector3d a) {
252+
Vector3d r = absoluteToRelative(a);
253+
breakBlock(r.x, r.y, r.z);
254+
}
255+
256+
public void breakBlock(double rx, double ry, double rz) {
252257
final Position p = new Position((int) Math.floor(pos.x + rx), (int) Math.floor(pos.y + ry),
253258
(int) Math.floor(pos.z + rz));
254259
// PhaseBot.getConsole().println("Digging at: " + p.getX() + " " +
@@ -266,6 +271,10 @@ public Vector3d relativeToAbsolute(Vector3d d) {
266271
return new Vector3d(pos.x + d.x, pos.y + d.y, pos.z + d.z);
267272
}
268273

274+
public Vector3d absoluteToRelative(Vector3d a) {
275+
return new Vector3d(pos.x - a.x, pos.y - a.y, pos.z - a.z);
276+
}
277+
269278
public Face getPlaceFace(Vector3d d) {
270279
for (int x = -1; x < 2; x++) {
271280
for (int y = -1; y < 2; y++) {
@@ -285,6 +294,10 @@ public Face getPlaceFace(Vector3d d) {
285294
return Face.INVALID;
286295
}
287296

297+
public void placeBlockAbsolute(Vector3d location) {
298+
placeBlock(absoluteToRelative(location));
299+
}
300+
288301
public void placeBlock(Vector3d location) {
289302
Face face = Face.INVALID;
290303
Vector3d blockLocation = null;
@@ -398,7 +411,82 @@ public void setSlot(int i) {
398411
look(yaw, pitch);
399412
}
400413

414+
public void getCloseToAbsolute(Vector3d a) {
415+
getCloseTo(absoluteToRelative(a));
416+
}
417+
418+
public void getCloseTo(Vector3d r) {
419+
double tx = pos.x + r.x;
420+
double ty = pos.y + r.y;
421+
double tz = pos.z + r.z;
422+
double distance = Math.sqrt(Math.pow(pos.x - tx, 2) + Math.pow(pos.y - ty, 2) + Math.pow(pos.z - tz, 2));
423+
if (distance > 4) {
424+
move(r.x, r.y, r.z);
425+
/*
426+
* @formatter:off
427+
* try {
428+
System.out.println(tx + " " + ty + " " + tz);
429+
AStar path = new AStar(new Vector3d(pos.x, pos.y, pos.z), new Vector3d(tx, ty, tz), 100);
430+
ArrayList<Tile> route = path.iterate();
431+
PathingResult result = path.getPathingResult();
432+
switch (result) {
433+
case SUCCESS:
434+
// Path was successful. Do something here.
435+
PhaseBot.getBot().moveAlong(route);
436+
break;
437+
case NO_PATH:
438+
default:
439+
break;
440+
}
441+
}
442+
catch (Exception e) {
443+
e.printStackTrace();
444+
}
445+
@formatter:on
446+
*/
447+
}
448+
}
449+
401450
public void openChest() {
402451
// client.getSession().send(new Client);
403452
}
404-
}
453+
454+
public double getRelativeDistanceAway(Vector3d r) {
455+
return getDistanceAway(relativeToAbsolute(r));
456+
}
457+
458+
public double getDistanceAway(Vector3d l) {
459+
return Math.sqrt(Math.pow(pos.x - l.x, 2) + Math.pow(pos.y - l.y, 2) + Math.pow(pos.z - l.z, 2));
460+
}
461+
462+
public Block getClosestBlock(Material material, int range) {
463+
ArrayList<Block> matches = new ArrayList<Block>();
464+
for (int x = 0; x < range; x++) {
465+
for (int y = 0; y < range; y++) {
466+
for (int z = 0; z < range; z++) {
467+
Block b; // cache
468+
if ((b = Block.getBlock(pos.x + x, pos.y + x, pos.z + z)).getMaterial() == material) {
469+
matches.add(b);
470+
}
471+
}
472+
}
473+
}
474+
double shortestDistance = range;
475+
Block best = null;
476+
for (Block g : matches) {
477+
if (shortestDistance == range) {
478+
best = g;
479+
shortestDistance = getDistanceAway(g.getPos());
480+
}
481+
else {
482+
double d; // cache
483+
if ((d = getDistanceAway(g.getPos())) < shortestDistance) {
484+
best = g;
485+
shortestDistance = d;
486+
}
487+
}
488+
}
489+
System.out.println(best.getPos());
490+
return best;
491+
}
492+
}

src/xyz/jadonfowler/phasebot/PacketHandler.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,7 @@ else if (event.getPacket() instanceof ServerChatPacket) {
143143
try {
144144
ChatMessage m = new ChatMessage(message.getFullText());
145145
if (!m.isCommand()) return;
146-
ArrayList<String> owners = new ArrayList<String>();
147-
owners.add("Phase");
148-
owners.add("VoltzLive");
149-
if (!owners.contains(m.getSender())) {
146+
if (!Arrays.asList(PhaseBot.getOwners()).contains(m.getSender())) {
150147
event.getSession().send(new ClientChatPacket("/msg " + m.getSender() + " You are not my master!"));
151148
return;
152149
}

src/xyz/jadonfowler/phasebot/PhaseBot.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import java.io.*;
55
import java.net.*;
66
import java.util.*;
7-
import javax.swing.*;
8-
import javax.swing.text.*;
97
import org.reflections.*;
108
import org.spacehq.mc.auth.*;
119
import org.spacehq.mc.auth.exception.*;
@@ -22,6 +20,7 @@
2220
public class PhaseBot {
2321

2422
@Getter @Setter private static String prefix = "owk, ";
23+
@Getter private static String[] owners = { "Phase", "Voltz" };
2524
private static String USERNAME = "username";
2625
private static String PASSWORD = "password";
2726
// Build Server
@@ -109,6 +108,10 @@ else if (line.startsWith("Proxy")) { // Proxy:123.456.789:860
109108
PROXY = new Proxy(Proxy.Type.HTTP,
110109
new InetSocketAddress(line.split(":")[1], Integer.parseInt(line.split(":")[2])));
111110
}
111+
else if (line.startsWith("Owners")) {
112+
String o = line.split(": ")[1];
113+
owners = o.contains(",") ? o.split(",") : new String[] { o };
114+
}
112115
line = br.readLine();
113116
}
114117
br.close();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package xyz.jadonfowler.phasebot.cmd.position;
2+
3+
import org.spacehq.packetlib.*;
4+
import xyz.jadonfowler.phasebot.*;
5+
import xyz.jadonfowler.phasebot.cmd.*;
6+
import xyz.jadonfowler.phasebot.world.*;
7+
8+
9+
public class ClosestBlockCommand extends Command {
10+
11+
@Override public void exec(String in, String[] args, Session s) {
12+
try{
13+
Material m = Material.getMaterial(args[1]);
14+
PhaseBot.getBot().getClosestBlock(m, 20);
15+
}catch(Exception e){
16+
e.printStackTrace();
17+
}
18+
}
19+
20+
@Override public String getCommand() {
21+
return "closest";
22+
}
23+
24+
@Override public String getDescription() {
25+
// TODO Auto-generated method stub
26+
return null;
27+
}
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package xyz.jadonfowler.phasebot.cmd.position;
2+
3+
import org.spacehq.packetlib.*;
4+
import xyz.jadonfowler.phasebot.*;
5+
import xyz.jadonfowler.phasebot.cmd.*;
6+
import xyz.jadonfowler.phasebot.util.*;
7+
8+
public class GetCloseToCommand extends Command {
9+
10+
@Override public void exec(String in, String[] args, Session s) {
11+
double rx, ry, rz;
12+
if (args[1].contains("*")) rx = Double.parseDouble(args[1].replace("*", "")) - PhaseBot.getBot().pos.x;
13+
else rx = Double.parseDouble(args[1]);
14+
if (args[2].contains("*")) ry = Double.parseDouble(args[2].replace("*", "")) - PhaseBot.getBot().pos.y;
15+
else ry = Double.parseDouble(args[2]);
16+
if (args[3].contains("*")) rz = Double.parseDouble(args[3].replace("*", "")) - PhaseBot.getBot().pos.z;
17+
else rz = Double.parseDouble(args[3]);
18+
PhaseBot.getBot().getCloseTo(new Vector3d(rx, ry, rz));
19+
}
20+
21+
@Override public String getCommand() {
22+
return "getcloseto";
23+
}
24+
25+
@Override public String getDescription() {
26+
// TODO Auto-generated method stub
27+
return null;
28+
}
29+
}

0 commit comments

Comments
 (0)