Skip to content

Commit

Permalink
improve place templates
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeRNG committed Nov 1, 2023
1 parent 9ac8c1c commit 0b926c7
Showing 1 changed file with 103 additions and 31 deletions.
134 changes: 103 additions & 31 deletions src/main/java/dev/dfonline/codeclient/action/impl/PlaceTemplates.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,138 @@
import dev.dfonline.codeclient.action.Action;
import dev.dfonline.codeclient.location.Dev;
import dev.dfonline.codeclient.mixin.entity.player.ClientPlayerInteractionManagerAccessor;
import net.minecraft.block.BlockState;
import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.c2s.play.PlayerInteractBlockC2SPacket;
import net.minecraft.network.packet.s2c.play.ChunkDeltaUpdateS2CPacket;
import net.minecraft.network.packet.s2c.play.OpenScreenS2CPacket;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

public class PlaceTemplates extends Action {
public static final int rowSize = 6;

private final List<ItemStack> templates;

public int currentIndex = -1;
private int timeSinceLastTick = 0;
private final ArrayList<TemplateToPlace> templates;
private int cooldown = 0;
private ItemStack recoverMainHand;
private GoTo goTo = null;

public PlaceTemplates(List<ItemStack> templates, Callback callback) {
super(callback);
this.templates = templates;
if(CodeClient.location instanceof Dev plot) {
int i = 0;
ArrayList<TemplateToPlace> templatesToPlace = new ArrayList<>();
for (ItemStack template: templates) {
int row = i % rowSize;
int level = i / rowSize;
i++;
Vec3d pos = new Vec3d(plot.getX(), 50, plot.getZ()).add((2 + (row * 3)) * -1, level * 5, 0);
templatesToPlace.add(new TemplateToPlace(pos, template));
}
this.templates = templatesToPlace;
}
else {
throw new IllegalStateException("Player must be in dev mode.");
}
}

@Override
public void init() {
currentIndex = 0;
cooldown = 4;
recoverMainHand = CodeClient.MC.player.getMainHandStack();
}

static void placeTemplateAt(int row, int level) {
if(CodeClient.location instanceof Dev plot) {
Vec3d pos = new Vec3d(plot.getX(), 50, plot.getZ()).add((2 + (row * 3)) * -1, level * 5, 0);
new MoveToLocation(CodeClient.MC.player).setPos(pos.add(1,2,1));
BlockHitResult blockHitResult = new BlockHitResult(pos.add(0,1,0), Direction.UP, new BlockPos.Mutable(pos.x, pos.y , pos.z), false);
((ClientPlayerInteractionManagerAccessor) (CodeClient.MC.interactionManager)).invokeSequencedPacket(CodeClient.MC.world, sequence -> new PlayerInteractBlockC2SPacket(Hand.MAIN_HAND, blockHitResult, sequence));
// CodeClient.MC.interactionManager.interactBlock(CodeClient.MC.player, Hand.MAIN_HAND, blockHitResult);
@Override
public boolean onReceivePacket(Packet<?> packet) {
if(packet instanceof OpenScreenS2CPacket) return true;
if(packet instanceof ChunkDeltaUpdateS2CPacket updates) {
for (TemplateToPlace template : templates) {
var block = new Object() {
boolean isTemplate = false;
BlockState state = null;
};
updates.visitUpdates((blockPos, blockState) -> {
if (template.pos.equals(blockPos)) {
block.isTemplate = true;
block.state = blockState;
}
});
if (!block.isTemplate) continue;
template.setComplete();
}
}
return super.onReceivePacket(packet);
}

@Override
public void onTick() {
if(CodeClient.location instanceof Dev plot) {
if(currentIndex >= templates.size()) {
currentIndex = -1;
Utility.makeHolding(recoverMainHand);
this.callback();
};
if(currentIndex == -1) return;
timeSinceLastTick = timeSinceLastTick + 1;
if(timeSinceLastTick == 5) {
timeSinceLastTick = 0;
Utility.makeHolding(templates.get(currentIndex));
int level = currentIndex / rowSize;
placeTemplateAt(currentIndex % rowSize, level);
currentIndex += 1;
if(currentIndex % rowSize == 0) {
MoveToLocation.shove(CodeClient.MC.player, new Vec3d(plot.getX(), 50, plot.getZ()).add(0,(level * 5) + 2,0));
}
if(CodeClient.MC.interactionManager == null) return;
if(CodeClient.location instanceof Dev) {
if(templates.size() == 0) {
callback();
return;
}
if(goTo != null) {
goTo.onTick();
}
if(cooldown > 0) cooldown--;
TemplateToPlace template = templates.stream().filter(TemplateToPlace::isOpen).findFirst().orElse(null);
if(template == null) {
templates.removeIf(TemplateToPlace::isComplete);
for(TemplateToPlace templateToPlace : templates) templateToPlace.setOpen(!templateToPlace.isComplete());
return;
}
if(template.pos().distanceTo(CodeClient.MC.player.getEyePos()) > 5.8) {
goTo = new GoTo(template.pos().add(-2, 0.5, 0), () -> this.goTo = null);
goTo.init();
cooldown = 2;
return;
}
if(cooldown == 0) {
Utility.makeHolding(template.template);
BlockHitResult blockHitResult = new BlockHitResult(template.pos().add(0,1,0), Direction.UP, template.pos, false);
((ClientPlayerInteractionManagerAccessor) (CodeClient.MC.interactionManager)).invokeSequencedPacket(CodeClient.MC.world, sequence -> new PlayerInteractBlockC2SPacket(Hand.MAIN_HAND, blockHitResult, sequence));
template.setOpen(false);
}
}
}

private static class TemplateToPlace {
private final BlockPos pos;
private final ItemStack template;
private boolean open = true;
private boolean complete = false;


public TemplateToPlace(Vec3d pos, ItemStack template) {
this.pos = new BlockPos((int) pos.x, (int) pos.y, (int) pos.z);
this.template = template;
}

public Vec3d pos() {
return pos.toCenterPos();
}

public void setOpen(boolean open) {
this.open = open;
}
public boolean isOpen() {
return open;
}
public void setComplete() {
this.open = false;
this.complete = true;
}

public boolean isComplete() {
return complete;
}
}
}

0 comments on commit 0b926c7

Please sign in to comment.