Skip to content

Commit

Permalink
it doesn't crash but misses args???
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeRNG committed Mar 30, 2024
1 parent 5f1ba4e commit 712e2e8
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 57 deletions.
89 changes: 74 additions & 15 deletions src/main/java/dev/dfonline/codeclient/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,23 +176,77 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
// return 0;
// }));

dispatcher.register(literal("scanplot").executes(context -> {
if(CodeClient.location instanceof Dev) {
var scan = new ArrayList<Template>();
CodeClient.currentAction = new ScanPlot(() -> {
CodeClient.currentAction = new None();
Utility.sendMessage("Done!", ChatType.SUCCESS);
Utility.sendMessage("Results:", ChatType.INFO);
for (Template template : scan) {
Utility.sendMessage(String.valueOf(template.blocks.size()));
dispatcher.register(literal("scanplot")//.executes(context -> {
// if(CodeClient.location instanceof Dev) {
// var scan = new ArrayList<Template>();
// CodeClient.currentAction = new ScanPlot(() -> {
// CodeClient.currentAction = new None();
// Utility.sendMessage("Done!", ChatType.SUCCESS);
// Utility.sendMessage("Results:", ChatType.INFO);
// for (Template template : scan) {
// Utility.sendMessage(String.valueOf(template.blocks.size()));
// }
//
// },scan);
// CodeClient.currentAction.init();
// return 0;
// }
// else {
// Utility.sendMessage("You must be in dev mode!", ChatType.FAIL);
// return 1;
// }
// })
.then(argument("folder",StringArgumentType.greedyString()).suggests(Commands::suggestTemplates).executes(context -> {
if(CodeClient.location instanceof Dev) {
String arg = context.getArgument("folder",String.class);
String[] path = arg.split("/");

Path currentPath = FileManager.templatesPath();
for (String dir: path) {
currentPath = currentPath.resolve(dir);
if(Files.notExists(currentPath)) {
try {
Files.createDirectory(currentPath);
}
catch (Exception ignored) {
Utility.sendMessage("Could not make a folder at " + currentPath);
}
}
else if(!Files.isDirectory(currentPath)) {
Utility.sendMessage(currentPath + " isn't a directory, can't put anything inside it!", ChatType.FAIL);
return -1;
}
}

},scan);
CodeClient.currentAction.init();
return 0;
}
else return 1;
}));
Utility.sendMessage("Scanning plot. Use /abort to abort.",ChatType.INFO);
var scan = new ArrayList<Template>();
Path finalCurrentPath = currentPath;
CodeClient.currentAction = new ScanPlot(() -> {
CodeClient.currentAction = new None();
Utility.sendMessage("Done!", ChatType.SUCCESS);
for (Template template : scan) {
var first = template.blocks.get(0);
String name = Objects.requireNonNullElse(first.action != null ? first.action : first.data,"unknown");
var filePath = finalCurrentPath.resolve(name + ".dft");
try {
Files.write(filePath,template.compress());
}
catch (Exception ignored) {
Utility.sendMessage("Couldn't save " + filePath + "...", ChatType.FAIL);
}
Utility.sendMessage(String.valueOf(template.blocks.size()));
}

},scan);
CodeClient.currentAction.init();
return 0;
}
else {
Utility.sendMessage("You must be in dev mode!", ChatType.FAIL);
return 1;
}
}))
);
dispatcher.register(literal("scanfor").then(argument("name", StringArgumentType.greedyString()).executes(context -> {
if(CodeClient.location instanceof Dev dev) {
Pattern pattern = Pattern.compile(context.getArgument("name", String.class), Pattern.CASE_INSENSITIVE);
Expand Down Expand Up @@ -282,6 +336,7 @@ else if(!Files.isDirectory(currentPath)) {
}
}
currentPath = currentPath.resolve(path[path.length - 1] + ".dft");
// Write file
try {
Files.write(currentPath.resolve(currentPath), Base64.getDecoder().decode(data));
Utility.sendMessage("Saved " + currentPath);
Expand All @@ -293,6 +348,7 @@ else if(!Files.isDirectory(currentPath)) {
return 0;
})));
dispatcher.register(literal("load").then(argument("path", StringArgumentType.greedyString()).suggests(Commands::suggestTemplates).executes(context -> {
if(CodeClient.MC.player.isCreative()) {
String arg = context.getArgument("path",String.class);
Path path = FileManager.templatesPath().resolve(arg + ".dft");
if(Files.notExists(path)) {
Expand All @@ -311,6 +367,9 @@ else if(!Files.isDirectory(currentPath)) {
return -2;
}
return 0;
}
Utility.sendMessage("You must be in creative mode!",ChatType.FAIL);
return -1;
})));
dispatcher.register(literal("swap").then(argument("path", StringArgumentType.greedyString()).suggests(Commands::suggestTemplates).executes(context -> {
try {
Expand Down
133 changes: 92 additions & 41 deletions src/main/java/dev/dfonline/codeclient/action/impl/ScanPlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
import dev.dfonline.codeclient.Utility;
import dev.dfonline.codeclient.action.Action;
import dev.dfonline.codeclient.hypercube.template.Template;
import dev.dfonline.codeclient.hypercube.template.TemplateBlock;
import dev.dfonline.codeclient.location.Dev;
import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.c2s.play.ClientCommandC2SPacket;
import net.minecraft.network.packet.c2s.play.CreativeInventoryActionC2SPacket;
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket;
import net.minecraft.network.packet.c2s.play.PlayerInteractBlockC2SPacket;
import net.minecraft.network.packet.s2c.play.InventoryS2CPacket;
import net.minecraft.network.packet.s2c.play.ScreenHandlerSlotUpdateS2CPacket;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
Expand All @@ -23,14 +19,14 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

public class ScanPlot extends Action {
private List<BlockPos> blocks = null;
private GoTo goTo = null;
private int progress = 0;
private static final Vec3d goToOffset = new Vec3d(0,1.5,0);
private boolean waitForResponse = false;
private Action step = null;
private static final Vec3d goToOffset = new Vec3d(0, 1.5, 0);
private Integer progress = 0;
private final ArrayList<Template> scanList;

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

@Override
public void init() {
if(CodeClient.location instanceof Dev plot) {
blocks = plot.scanForSigns(Pattern.compile("(PLAYER|ENTITY) EVENT|FUNCTION|PROCESS"),Pattern.compile(".*")).keySet().stream().toList();
if (CodeClient.location instanceof Dev plot) {
blocks = plot.scanForSigns(Pattern.compile("(PLAYER|ENTITY) EVENT|FUNCTION|PROCESS"), Pattern.compile(".*")).keySet().stream().toList();
}
}

@Override
public boolean onReceivePacket(Packet<?> packet) {
var net = CodeClient.MC.getNetworkHandler();
if(waitForResponse && net != null && packet instanceof ScreenHandlerSlotUpdateS2CPacket slot) {
var data = Utility.templateDataItem(slot.getStack());
var template = Template.parse64(data);
if(template == null) return false;
scanList.add(Template.parse64(data));
waitForResponse = false;
progress += 1;
net.sendPacket(new CreativeInventoryActionC2SPacket(slot.getSlot(), ItemStack.EMPTY));
return true;
}
if (step != null) return step.onReceivePacket(packet);
return false;
// var net = CodeClient.MC.getNetworkHandler();
// if(waitForResponse && net != null && packet instanceof ScreenHandlerSlotUpdateS2CPacket slot) {
// var data = Utility.templateDataItem(slot.getStack());
// var template = Template.parse64(data);
// if(template == null) return false;
// scanList.add(Template.parse64(data));
// waitForResponse = false;
// progress += 1;
// net.sendPacket(new CreativeInventoryActionC2SPacket(slot.getSlot(), ItemStack.EMPTY));
// return true;
// }
}

@Override
public void onTick() {
if(progress == blocks.size()) {
if(!waitForResponse) {
if (step != null) step.onTick();
else {
if(progress >= blocks.size()) {
callback();
return;
}
return;
step = new GoTo(blocks.get(progress).toCenterPos().add(goToOffset), () -> {
this.step = new pickUpBlock(blocks.get(progress),() -> {
CodeClient.LOGGER.info("picked up block");
step = null;
progress+=1;
});
this.step.init();
});
step.init();
}
var net = CodeClient.MC.getNetworkHandler();
var player = CodeClient.MC.player;
var inter = CodeClient.MC.interactionManager;
if(CodeClient.location instanceof Dev && this.blocks != null && net != null && player != null && inter != null) {
if(goTo == null || goTo.complete) {
if(goTo != null && !waitForResponse) {
var current = blocks.get(progress);
boolean sneaky = !player.isSneaking();
if(sneaky) net.sendPacket(new ClientCommandC2SPacket(player, ClientCommandC2SPacket.Mode.PRESS_SHIFT_KEY));
inter.interactBlock(player, Hand.MAIN_HAND, new BlockHitResult(current.toCenterPos(), Direction.UP, current,false));
if(sneaky) net.sendPacket(new ClientCommandC2SPacket(player, ClientCommandC2SPacket.Mode.RELEASE_SHIFT_KEY));
waitForResponse = true;
goTo = null;
}
goTo = new GoTo(blocks.get(progress).toCenterPos().add(goToOffset), () -> {});
goTo.init();
}


private class pickUpBlock extends Action {
private BlockPos pos;

public pickUpBlock(BlockPos pos, Callback callback) {
super(callback);
this.pos = pos;
}

@Override
public void init() {
var net = CodeClient.MC.getNetworkHandler();
var player = CodeClient.MC.player;
var inter = CodeClient.MC.interactionManager;
boolean sneaky = !player.isSneaking();
if (sneaky) net.sendPacket(new ClientCommandC2SPacket(player, ClientCommandC2SPacket.Mode.PRESS_SHIFT_KEY));
inter.interactBlock(player, Hand.MAIN_HAND, new BlockHitResult(this.pos.toCenterPos(), Direction.UP, this.pos, false));
if (sneaky)
net.sendPacket(new ClientCommandC2SPacket(player, ClientCommandC2SPacket.Mode.RELEASE_SHIFT_KEY));
}

@Override
public boolean onReceivePacket(Packet<?> packet) {
var net = CodeClient.MC.getNetworkHandler();
if (net != null && packet instanceof ScreenHandlerSlotUpdateS2CPacket slot) {
var data = Utility.templateDataItem(slot.getStack());
var template = Template.parse64(data);
if (template == null) return false;
scanList.add(Template.parse64(data));
net.sendPacket(new CreativeInventoryActionC2SPacket(slot.getSlot(), ItemStack.EMPTY));
this.callback();
return true;
}
goTo.onTick();
return super.onReceivePacket(packet);
}
// if(progress == blocks.size()) {
// if(!waitForResponse) {
// callback();
// }
// return;
// }
// var net = CodeClient.MC.getNetworkHandler();
// var player = CodeClient.MC.player;
// var inter = CodeClient.MC.interactionManager;
// if(CodeClient.location instanceof Dev && this.blocks != null && net != null && player != null && inter != null) {
// if(goTo == null || goTo.complete) {
// if(goTo != null && !waitForResponse) {
// var current = blocks.get(progress);
// boolean sneaky = !player.isSneaking();
// if(sneaky) net.sendPacket(new ClientCommandC2SPacket(player, ClientCommandC2SPacket.Mode.PRESS_SHIFT_KEY));
// inter.interactBlock(player, Hand.MAIN_HAND, new BlockHitResult(current.toCenterPos(), Direction.UP, current,false));
// if(sneaky) net.sendPacket(new ClientCommandC2SPacket(player, ClientCommandC2SPacket.Mode.RELEASE_SHIFT_KEY));
// waitForResponse = true;
// goTo = null;
// }
// goTo = new GoTo(blocks.get(progress).toCenterPos().add(goToOffset), () -> {});
// goTo.init();
// }
// goTo.onTick();
// }
// }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class Template {
public ArrayList<TemplateBlock> blocks;
Expand All @@ -19,6 +19,10 @@ public int getLength() {
return length;
}

public byte[] compress() throws IOException {
return compress(CodeClient.gson.toJson(this));
}

/**
* Parse base64+gzip data
*/
Expand Down Expand Up @@ -58,4 +62,11 @@ private static byte[] decompress(byte[] compressedData) throws IOException {
return bos.toByteArray();
}
}
private static byte[] compress(String uncompressedString) throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
try (GZIPOutputStream gzipStream = new GZIPOutputStream(byteStream)) {
gzipStream.write(uncompressedString.getBytes());
}
return byteStream.toByteArray();
}
}

0 comments on commit 712e2e8

Please sign in to comment.