Skip to content

Commit 712e2e8

Browse files
committed
it doesn't crash but misses args???
1 parent 5f1ba4e commit 712e2e8

3 files changed

Lines changed: 178 additions & 57 deletions

File tree

src/main/java/dev/dfonline/codeclient/Commands.java

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -176,23 +176,77 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
176176
// return 0;
177177
// }));
178178

179-
dispatcher.register(literal("scanplot").executes(context -> {
180-
if(CodeClient.location instanceof Dev) {
181-
var scan = new ArrayList<Template>();
182-
CodeClient.currentAction = new ScanPlot(() -> {
183-
CodeClient.currentAction = new None();
184-
Utility.sendMessage("Done!", ChatType.SUCCESS);
185-
Utility.sendMessage("Results:", ChatType.INFO);
186-
for (Template template : scan) {
187-
Utility.sendMessage(String.valueOf(template.blocks.size()));
179+
dispatcher.register(literal("scanplot")//.executes(context -> {
180+
// if(CodeClient.location instanceof Dev) {
181+
// var scan = new ArrayList<Template>();
182+
// CodeClient.currentAction = new ScanPlot(() -> {
183+
// CodeClient.currentAction = new None();
184+
// Utility.sendMessage("Done!", ChatType.SUCCESS);
185+
// Utility.sendMessage("Results:", ChatType.INFO);
186+
// for (Template template : scan) {
187+
// Utility.sendMessage(String.valueOf(template.blocks.size()));
188+
// }
189+
//
190+
// },scan);
191+
// CodeClient.currentAction.init();
192+
// return 0;
193+
// }
194+
// else {
195+
// Utility.sendMessage("You must be in dev mode!", ChatType.FAIL);
196+
// return 1;
197+
// }
198+
// })
199+
.then(argument("folder",StringArgumentType.greedyString()).suggests(Commands::suggestTemplates).executes(context -> {
200+
if(CodeClient.location instanceof Dev) {
201+
String arg = context.getArgument("folder",String.class);
202+
String[] path = arg.split("/");
203+
204+
Path currentPath = FileManager.templatesPath();
205+
for (String dir: path) {
206+
currentPath = currentPath.resolve(dir);
207+
if(Files.notExists(currentPath)) {
208+
try {
209+
Files.createDirectory(currentPath);
210+
}
211+
catch (Exception ignored) {
212+
Utility.sendMessage("Could not make a folder at " + currentPath);
213+
}
214+
}
215+
else if(!Files.isDirectory(currentPath)) {
216+
Utility.sendMessage(currentPath + " isn't a directory, can't put anything inside it!", ChatType.FAIL);
217+
return -1;
218+
}
188219
}
189220

190-
},scan);
191-
CodeClient.currentAction.init();
192-
return 0;
193-
}
194-
else return 1;
195-
}));
221+
Utility.sendMessage("Scanning plot. Use /abort to abort.",ChatType.INFO);
222+
var scan = new ArrayList<Template>();
223+
Path finalCurrentPath = currentPath;
224+
CodeClient.currentAction = new ScanPlot(() -> {
225+
CodeClient.currentAction = new None();
226+
Utility.sendMessage("Done!", ChatType.SUCCESS);
227+
for (Template template : scan) {
228+
var first = template.blocks.get(0);
229+
String name = Objects.requireNonNullElse(first.action != null ? first.action : first.data,"unknown");
230+
var filePath = finalCurrentPath.resolve(name + ".dft");
231+
try {
232+
Files.write(filePath,template.compress());
233+
}
234+
catch (Exception ignored) {
235+
Utility.sendMessage("Couldn't save " + filePath + "...", ChatType.FAIL);
236+
}
237+
Utility.sendMessage(String.valueOf(template.blocks.size()));
238+
}
239+
240+
},scan);
241+
CodeClient.currentAction.init();
242+
return 0;
243+
}
244+
else {
245+
Utility.sendMessage("You must be in dev mode!", ChatType.FAIL);
246+
return 1;
247+
}
248+
}))
249+
);
196250
dispatcher.register(literal("scanfor").then(argument("name", StringArgumentType.greedyString()).executes(context -> {
197251
if(CodeClient.location instanceof Dev dev) {
198252
Pattern pattern = Pattern.compile(context.getArgument("name", String.class), Pattern.CASE_INSENSITIVE);
@@ -282,6 +336,7 @@ else if(!Files.isDirectory(currentPath)) {
282336
}
283337
}
284338
currentPath = currentPath.resolve(path[path.length - 1] + ".dft");
339+
// Write file
285340
try {
286341
Files.write(currentPath.resolve(currentPath), Base64.getDecoder().decode(data));
287342
Utility.sendMessage("Saved " + currentPath);
@@ -293,6 +348,7 @@ else if(!Files.isDirectory(currentPath)) {
293348
return 0;
294349
})));
295350
dispatcher.register(literal("load").then(argument("path", StringArgumentType.greedyString()).suggests(Commands::suggestTemplates).executes(context -> {
351+
if(CodeClient.MC.player.isCreative()) {
296352
String arg = context.getArgument("path",String.class);
297353
Path path = FileManager.templatesPath().resolve(arg + ".dft");
298354
if(Files.notExists(path)) {
@@ -311,6 +367,9 @@ else if(!Files.isDirectory(currentPath)) {
311367
return -2;
312368
}
313369
return 0;
370+
}
371+
Utility.sendMessage("You must be in creative mode!",ChatType.FAIL);
372+
return -1;
314373
})));
315374
dispatcher.register(literal("swap").then(argument("path", StringArgumentType.greedyString()).suggests(Commands::suggestTemplates).executes(context -> {
316375
try {

src/main/java/dev/dfonline/codeclient/action/impl/ScanPlot.java

Lines changed: 92 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,11 @@
55
import dev.dfonline.codeclient.Utility;
66
import dev.dfonline.codeclient.action.Action;
77
import dev.dfonline.codeclient.hypercube.template.Template;
8-
import dev.dfonline.codeclient.hypercube.template.TemplateBlock;
98
import dev.dfonline.codeclient.location.Dev;
109
import net.minecraft.item.ItemStack;
1110
import net.minecraft.network.packet.Packet;
1211
import net.minecraft.network.packet.c2s.play.ClientCommandC2SPacket;
1312
import net.minecraft.network.packet.c2s.play.CreativeInventoryActionC2SPacket;
14-
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket;
15-
import net.minecraft.network.packet.c2s.play.PlayerInteractBlockC2SPacket;
16-
import net.minecraft.network.packet.s2c.play.InventoryS2CPacket;
1713
import net.minecraft.network.packet.s2c.play.ScreenHandlerSlotUpdateS2CPacket;
1814
import net.minecraft.util.Hand;
1915
import net.minecraft.util.hit.BlockHitResult;
@@ -23,14 +19,14 @@
2319

2420
import java.util.ArrayList;
2521
import java.util.List;
22+
import java.util.concurrent.atomic.AtomicInteger;
2623
import java.util.regex.Pattern;
2724

2825
public class ScanPlot extends Action {
2926
private List<BlockPos> blocks = null;
30-
private GoTo goTo = null;
31-
private int progress = 0;
32-
private static final Vec3d goToOffset = new Vec3d(0,1.5,0);
33-
private boolean waitForResponse = false;
27+
private Action step = null;
28+
private static final Vec3d goToOffset = new Vec3d(0, 1.5, 0);
29+
private Integer progress = 0;
3430
private final ArrayList<Template> scanList;
3531

3632
public ScanPlot(Callback callback, ArrayList<Template> scanList) {
@@ -43,53 +39,108 @@ public ScanPlot(Callback callback, ArrayList<Template> scanList) {
4339

4440
@Override
4541
public void init() {
46-
if(CodeClient.location instanceof Dev plot) {
47-
blocks = plot.scanForSigns(Pattern.compile("(PLAYER|ENTITY) EVENT|FUNCTION|PROCESS"),Pattern.compile(".*")).keySet().stream().toList();
42+
if (CodeClient.location instanceof Dev plot) {
43+
blocks = plot.scanForSigns(Pattern.compile("(PLAYER|ENTITY) EVENT|FUNCTION|PROCESS"), Pattern.compile(".*")).keySet().stream().toList();
4844
}
4945
}
5046

5147
@Override
5248
public boolean onReceivePacket(Packet<?> packet) {
53-
var net = CodeClient.MC.getNetworkHandler();
54-
if(waitForResponse && net != null && packet instanceof ScreenHandlerSlotUpdateS2CPacket slot) {
55-
var data = Utility.templateDataItem(slot.getStack());
56-
var template = Template.parse64(data);
57-
if(template == null) return false;
58-
scanList.add(Template.parse64(data));
59-
waitForResponse = false;
60-
progress += 1;
61-
net.sendPacket(new CreativeInventoryActionC2SPacket(slot.getSlot(), ItemStack.EMPTY));
62-
return true;
63-
}
49+
if (step != null) return step.onReceivePacket(packet);
6450
return false;
51+
// var net = CodeClient.MC.getNetworkHandler();
52+
// if(waitForResponse && net != null && packet instanceof ScreenHandlerSlotUpdateS2CPacket slot) {
53+
// var data = Utility.templateDataItem(slot.getStack());
54+
// var template = Template.parse64(data);
55+
// if(template == null) return false;
56+
// scanList.add(Template.parse64(data));
57+
// waitForResponse = false;
58+
// progress += 1;
59+
// net.sendPacket(new CreativeInventoryActionC2SPacket(slot.getSlot(), ItemStack.EMPTY));
60+
// return true;
61+
// }
6562
}
6663

6764
@Override
6865
public void onTick() {
69-
if(progress == blocks.size()) {
70-
if(!waitForResponse) {
66+
if (step != null) step.onTick();
67+
else {
68+
if(progress >= blocks.size()) {
7169
callback();
70+
return;
7271
}
73-
return;
72+
step = new GoTo(blocks.get(progress).toCenterPos().add(goToOffset), () -> {
73+
this.step = new pickUpBlock(blocks.get(progress),() -> {
74+
CodeClient.LOGGER.info("picked up block");
75+
step = null;
76+
progress+=1;
77+
});
78+
this.step.init();
79+
});
80+
step.init();
7481
}
75-
var net = CodeClient.MC.getNetworkHandler();
76-
var player = CodeClient.MC.player;
77-
var inter = CodeClient.MC.interactionManager;
78-
if(CodeClient.location instanceof Dev && this.blocks != null && net != null && player != null && inter != null) {
79-
if(goTo == null || goTo.complete) {
80-
if(goTo != null && !waitForResponse) {
81-
var current = blocks.get(progress);
82-
boolean sneaky = !player.isSneaking();
83-
if(sneaky) net.sendPacket(new ClientCommandC2SPacket(player, ClientCommandC2SPacket.Mode.PRESS_SHIFT_KEY));
84-
inter.interactBlock(player, Hand.MAIN_HAND, new BlockHitResult(current.toCenterPos(), Direction.UP, current,false));
85-
if(sneaky) net.sendPacket(new ClientCommandC2SPacket(player, ClientCommandC2SPacket.Mode.RELEASE_SHIFT_KEY));
86-
waitForResponse = true;
87-
goTo = null;
88-
}
89-
goTo = new GoTo(blocks.get(progress).toCenterPos().add(goToOffset), () -> {});
90-
goTo.init();
82+
}
83+
84+
85+
private class pickUpBlock extends Action {
86+
private BlockPos pos;
87+
88+
public pickUpBlock(BlockPos pos, Callback callback) {
89+
super(callback);
90+
this.pos = pos;
91+
}
92+
93+
@Override
94+
public void init() {
95+
var net = CodeClient.MC.getNetworkHandler();
96+
var player = CodeClient.MC.player;
97+
var inter = CodeClient.MC.interactionManager;
98+
boolean sneaky = !player.isSneaking();
99+
if (sneaky) net.sendPacket(new ClientCommandC2SPacket(player, ClientCommandC2SPacket.Mode.PRESS_SHIFT_KEY));
100+
inter.interactBlock(player, Hand.MAIN_HAND, new BlockHitResult(this.pos.toCenterPos(), Direction.UP, this.pos, false));
101+
if (sneaky)
102+
net.sendPacket(new ClientCommandC2SPacket(player, ClientCommandC2SPacket.Mode.RELEASE_SHIFT_KEY));
103+
}
104+
105+
@Override
106+
public boolean onReceivePacket(Packet<?> packet) {
107+
var net = CodeClient.MC.getNetworkHandler();
108+
if (net != null && packet instanceof ScreenHandlerSlotUpdateS2CPacket slot) {
109+
var data = Utility.templateDataItem(slot.getStack());
110+
var template = Template.parse64(data);
111+
if (template == null) return false;
112+
scanList.add(Template.parse64(data));
113+
net.sendPacket(new CreativeInventoryActionC2SPacket(slot.getSlot(), ItemStack.EMPTY));
114+
this.callback();
115+
return true;
91116
}
92-
goTo.onTick();
117+
return super.onReceivePacket(packet);
93118
}
119+
// if(progress == blocks.size()) {
120+
// if(!waitForResponse) {
121+
// callback();
122+
// }
123+
// return;
124+
// }
125+
// var net = CodeClient.MC.getNetworkHandler();
126+
// var player = CodeClient.MC.player;
127+
// var inter = CodeClient.MC.interactionManager;
128+
// if(CodeClient.location instanceof Dev && this.blocks != null && net != null && player != null && inter != null) {
129+
// if(goTo == null || goTo.complete) {
130+
// if(goTo != null && !waitForResponse) {
131+
// var current = blocks.get(progress);
132+
// boolean sneaky = !player.isSneaking();
133+
// if(sneaky) net.sendPacket(new ClientCommandC2SPacket(player, ClientCommandC2SPacket.Mode.PRESS_SHIFT_KEY));
134+
// inter.interactBlock(player, Hand.MAIN_HAND, new BlockHitResult(current.toCenterPos(), Direction.UP, current,false));
135+
// if(sneaky) net.sendPacket(new ClientCommandC2SPacket(player, ClientCommandC2SPacket.Mode.RELEASE_SHIFT_KEY));
136+
// waitForResponse = true;
137+
// goTo = null;
138+
// }
139+
// goTo = new GoTo(blocks.get(progress).toCenterPos().add(goToOffset), () -> {});
140+
// goTo.init();
141+
// }
142+
// goTo.onTick();
143+
// }
144+
// }
94145
}
95146
}

src/main/java/dev/dfonline/codeclient/hypercube/template/Template.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import java.io.ByteArrayInputStream;
66
import java.io.ByteArrayOutputStream;
77
import java.io.IOException;
8-
import java.nio.charset.StandardCharsets;
98
import java.util.ArrayList;
109
import java.util.Base64;
1110
import java.util.zip.GZIPInputStream;
11+
import java.util.zip.GZIPOutputStream;
1212

1313
public class Template {
1414
public ArrayList<TemplateBlock> blocks;
@@ -19,6 +19,10 @@ public int getLength() {
1919
return length;
2020
}
2121

22+
public byte[] compress() throws IOException {
23+
return compress(CodeClient.gson.toJson(this));
24+
}
25+
2226
/**
2327
* Parse base64+gzip data
2428
*/
@@ -58,4 +62,11 @@ private static byte[] decompress(byte[] compressedData) throws IOException {
5862
return bos.toByteArray();
5963
}
6064
}
65+
private static byte[] compress(String uncompressedString) throws IOException {
66+
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
67+
try (GZIPOutputStream gzipStream = new GZIPOutputStream(byteStream)) {
68+
gzipStream.write(uncompressedString.getBytes());
69+
}
70+
return byteStream.toByteArray();
71+
}
6172
}

0 commit comments

Comments
 (0)