Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ org.gradle.jvmargs=-Xmx1G
## Check these on https://fabricmc.net/develop/
minecraft_version=1.21.3
yarn_mappings=1.21.3+build.2
loader_version=0.16.9
loader_version=0.17.2

# Mod Properties
mod_version=2.0.0-beta.4
maven_group=dev.dfonline
archives_base_name=CodeClient

# Dependencies
fabric_version=0.108.0+1.21.3
fabric_version=0.114.1+1.21.3
modmenu_version=12.0.0-beta.1
yacl_version=3.6.1+1.21.2-fabric
adventure_api_version=4.17.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public class PlaceTemplates extends Action {
private GoTo goTo = null;
private boolean shouldBeSwapping = false;

public PlaceTemplates(ArrayList<Operation> operations, Callback callback) {
super(callback);
this.operations = operations;
}

public PlaceTemplates(List<ItemStack> templates, Callback callback) {
super(callback);
if (CodeClient.location instanceof Dev plot) {
Expand Down Expand Up @@ -89,11 +94,11 @@ public PlaceTemplates(HashMap<BlockPos, ItemStack> templates, Callback callback)
TemplateBlock.Block blockName = TemplateBlock.Block.valueOf(block.block.toUpperCase());
String name = ObjectUtils.firstNonNull(block.action, block.data);
for (Map.Entry<BlockPos, SignText> sign : scan.entrySet()) { // Loop through scanned signs
SignText text = sign.getValue(); // ↓ If the blockName and name match
SignText text = sign.getValue(); // ↓ If the blockName and name match
if (text.getMessage(0, false).getString().equals(blockName.name) && text.getMessage(1, false).getString().equals(name)) {
map.put(sign.getKey().east(), item); // Put it into map
leftOvers.remove(item); // Remove the template, so we can see if there's anything left over
break; // break out :D
map.put(sign.getKey().east(), item); // Put it into map
leftOvers.remove(item); // Remove the template, so we can see if there's anything left over
break; // break out :D
}
}
} catch (Exception e) {
Expand Down Expand Up @@ -161,6 +166,27 @@ public static PlaceTemplates createPlacer(List<ItemStack> templates, Callback ca
return null;
}

public static PlaceTemplates breakTemplates(List<String> names, Callback callback) {
if(CodeClient.location instanceof Dev dev) { // TODO: make this passed in as a parameter
var targets = new ArrayList<Operation>();
HashMap<BlockPos, SignText> scan = dev.scanForSigns(Pattern.compile(".*"));
for (var entry: scan.entrySet()) {
var sign = entry.getValue();
for (var name : names) {
var type = name.split(" ")[0];
var action = name.substring(type.length() + 1);
if (type.equals("any") || sign.getMessage(0, false).getString().toLowerCase().contains(type.toLowerCase())) {
if (action.equals(sign.getMessage(1, false).getString())) {
targets.add(new DestroyOperation(entry.getKey()));
}
}
}
}
return new PlaceTemplates(targets,callback);
}
return null;
}

/**
* If there is a template in the way, replace it.
*/
Expand Down Expand Up @@ -193,7 +219,7 @@ public boolean onReceivePacket(Packet<?> packet) {
block.state = blockState;
}
});
if (operation instanceof TemplateToPlace) {
if (operation instanceof TemplateToPlace || operation instanceof DestroyOperation) {
if (!block.isTemplate) continue;
operation.setComplete();
}
Expand Down Expand Up @@ -237,6 +263,16 @@ public void tick() {
((ClientPlayerInteractionManagerAccessor) (CodeClient.MC.interactionManager)).invokeSequencedPacket(CodeClient.MC.world, sequence -> new PlayerInteractBlockC2SPacket(Hand.MAIN_HAND, blockHitResult, sequence));
template.setOpen(false);
}
if (operation instanceof DestroyOperation destroy) {
var player = CodeClient.MC.player;
boolean sneaky = !player.isSneaking();
if (sneaky)
net.sendPacket(new ClientCommandC2SPacket(player, ClientCommandC2SPacket.Mode.PRESS_SHIFT_KEY));
net.sendPacket(new PlayerActionC2SPacket(PlayerActionC2SPacket.Action.START_DESTROY_BLOCK, destroy.pos, Direction.UP));
net.sendPacket(new PlayerActionC2SPacket(PlayerActionC2SPacket.Action.ABORT_DESTROY_BLOCK, destroy.pos, Direction.UP));
if (sneaky)
net.sendPacket(new ClientCommandC2SPacket(player, ClientCommandC2SPacket.Mode.RELEASE_SHIFT_KEY));
}
}
return;
}
Expand All @@ -256,7 +292,7 @@ public void tick() {
}
}

private static abstract class Operation {
public static abstract class Operation {
protected final BlockPos pos;
/**
* If this operation can be acted on, and needs to be completed.
Expand Down Expand Up @@ -313,4 +349,10 @@ public TemplateToPlace(BlockPos pos, ItemStack template) {
this.template = template;
}
}

private static class DestroyOperation extends Operation {
public DestroyOperation(BlockPos pos) {
super(pos);
}
}
}
35 changes: 35 additions & 0 deletions src/main/java/dev/dfonline/codeclient/websocket/SocketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public void onMessage(String message) {
case "size" -> assertScopeLevel(new Size());
case "scan" -> assertScopeLevel(new Scan());
case "place" -> assertScopeLevel(new Place());
case "break" -> assertScopeLevel(new Break());
case "inv" -> assertScopeLevel(new SendInventory());
case "setinv" -> assertScopeLevel(new SetInventory(content));
case "give" -> assertScopeLevel(new Give(content));
Expand Down Expand Up @@ -463,6 +464,40 @@ private interface CreatePlacer {
}
}

private class Break extends SocketHandler.Action {
private final ArrayList<String> targets = new ArrayList<>();
public boolean ready = false;

Break(){
super("break", AuthScope.WRITE_CODE);
}

@Override
public void set(WebSocket responder) {
}

@Override
public void start(WebSocket responder) {
if (!ready) return;
PlaceTemplates breaker = PlaceTemplates.breakTemplates(targets, SocketHandler.this::next);
if(breaker == null) return;
CodeClient.currentAction = breaker;
breaker.init();
}

@Override
public void message(WebSocket responder, String message) {
if(message.equals("go") && !targets.isEmpty()) {
ready = true;
if (Objects.equals(actionQueue.getFirst(), this)) {
this.start(responder);
}
} else {
targets.add(message);
}
}
}

private class SendInventory extends SocketHandler.Action {
SendInventory() {
super("inv", AuthScope.INVENTORY);
Expand Down