Skip to content

Commit 8d40955

Browse files
committed
Allow APIs to get plot size
1 parent 9b7cd51 commit 8d40955

File tree

2 files changed

+69
-9
lines changed

2 files changed

+69
-9
lines changed

src/main/java/dev/dfonline/codeclient/location/Plot.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public void setSize(Size size) {
2525
this.size = size;
2626
}
2727

28+
public Size getSize() {
29+
return size;
30+
}
31+
2832
public Integer getX() {
2933
return originX;
3034
}

src/main/java/dev/dfonline/codeclient/websocket/SocketHandler.java

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import java.net.InetSocketAddress;
44
import java.util.ArrayList;
55

6+
import com.google.gson.JsonObject;
67
import dev.dfonline.codeclient.CodeClient;
78
import dev.dfonline.codeclient.action.impl.ClearPlot;
9+
import dev.dfonline.codeclient.action.impl.GetPlotSize;
810
import dev.dfonline.codeclient.action.impl.MoveToSpawn;
911
import dev.dfonline.codeclient.action.impl.PlaceTemplates;
12+
import dev.dfonline.codeclient.location.Plot;
1013
import net.minecraft.item.ItemStack;
1114
import net.minecraft.item.Items;
1215
import net.minecraft.nbt.NbtCompound;
@@ -41,28 +44,74 @@ public static void setConnection(WebSocket socket) {
4144
}
4245

4346
public static String onMessage(String message) {
44-
if(!authorised) return "{\"status\":\"error\",\"error\":\"auth\",\"message\":\"This app isn't authorized, check you have run /auth in the game.\"}";
45-
if(action == Action.CLEAR) return "{\"status\":\"error\",\"error\":\"clearing\",\"message\":\"The client is already clearing a plot. If it is stuck, run /abort and /auth.\"}";
46-
if(action == Action.SPAWN) return "{\"status\":\"error\",\"error\":\"spawn\",\"message\":\"The client is trying to go to the plot spawn. If it is stuck, run /abort and /auth.\"}";
47+
JsonObject response = new JsonObject();
48+
if(!authorised) {
49+
response.addProperty("status","error");
50+
response.addProperty("error","auth");
51+
response.addProperty("message","This app isn't authorized, check you have run /auth in the game.");
52+
return response.toString();
53+
}
54+
if(action == Action.CLEAR) {
55+
response.addProperty("status","error");
56+
response.addProperty("error","clear");
57+
response.addProperty("message","The client is already clearing a plot. If it is stuck, run /abort.");
58+
return response.toString();
59+
}
60+
if(action == Action.SPAWN) {
61+
response.addProperty("status","error");
62+
response.addProperty("error","spawn");
63+
response.addProperty("message","The client is trying to go to the plot spawn. If it is stuck, run /abort.");
64+
return response.toString();
65+
}
4766
String[] arguments = message.split(" ");
4867
switch (arguments[0]) {
4968
case "clear" -> {
5069
action = Action.CLEAR;
70+
response.addProperty("status","action");
71+
response.addProperty("action","clear");
5172
CodeClient.currentAction = new ClearPlot(() -> {
52-
connection.send("{\"status\":\"action\",\"action\":\"clear\",\"progress\":\"finish\",\"message\":\"Finished clear plot.\"}");
73+
response.addProperty("progress","finish");
74+
response.addProperty("message","Finished clear plot.");
75+
connection.send(response.toString());
5376
action = Action.NONE;
5477
});
5578
CodeClient.currentAction.init();
56-
return "{\"status\":\"action\",\"action\":\"clear\",\"progress\":\"start\",\"message\":\"Starting clear plot.\"}";
79+
response.addProperty("progress","start");
80+
response.addProperty("message","Starting clear plot.");
81+
return response.toString();
5782
}
5883
case "spawn" -> {
5984
action = Action.SPAWN;
85+
response.addProperty("status","action");
86+
response.addProperty("action","spawn");
6087
CodeClient.currentAction = new MoveToSpawn(() -> {
61-
connection.send("{\"status\":\"action\",\"action\":\"spawn\",\"progress\":\"finish\",\"message\":\"Finished move to spawn.\"}");
88+
response.addProperty("progress","finish");
89+
response.addProperty("message","Finished move to spawn.");
90+
connection.send(response.toString());
91+
action = Action.NONE;
92+
});
93+
CodeClient.currentAction.init();
94+
response.addProperty("progress","start");
95+
response.addProperty("message","Starting move to spawn.");
96+
return response.toString();
97+
}
98+
case "size" -> {
99+
action = Action.SIZE;
100+
response.addProperty("status","action");
101+
response.addProperty("action","size");
102+
CodeClient.currentAction = new GetPlotSize(() -> {
103+
response.addProperty("progress","finish");
104+
response.addProperty("message","Finished get plot size.");
105+
if(CodeClient.location instanceof Plot plot) {
106+
response.addProperty("data",plot.getSize().name());
107+
}
108+
connection.send(response.toString());
62109
action = Action.NONE;
63110
});
64111
CodeClient.currentAction.init();
65-
return "{\"status\":\"action\",\"action\":\"spawn\",\"progress\":\"start\",\"message\":\"Starting move to spawn.\"}";
112+
response.addProperty("progress","start");
113+
response.addProperty("message","Starting get plot size.");
114+
return response.toString();
66115
}
67116
case "place" -> {
68117
if(action != Action.TEMPLATES) {
@@ -93,10 +142,16 @@ public static String onMessage(String message) {
93142
}
94143
}
95144
case "swap" -> {
96-
return "{\"status\":\"error\",\"error\":\"swapping\",\"message\":\"Not implemented.\"}";
145+
response.addProperty("status","error");
146+
response.addProperty("error","swap");
147+
response.addProperty("message","Not implemented.");
148+
return response.toString();
97149
}
98150
default -> {
99-
return "{\"status\":\"error\",\"error\":\"generic\",\"message\":\"Invalid command.\"}";
151+
response.addProperty("status","error");
152+
response.addProperty("error","generic");
153+
response.addProperty("message","Invalid command.");
154+
return response.toString();
100155
}
101156
}
102157
return null;
@@ -106,6 +161,7 @@ private enum Action {
106161
NONE,
107162
CLEAR, // Clearing a plot
108163
SPAWN, // Moving client to plot origin
164+
SIZE, // Getting plot size
109165
TEMPLATES, // Collecting templates for PLACE
110166
PLACE, // Aforementioned place, places collected templates into plot
111167
}

0 commit comments

Comments
 (0)