1616import net .minecraft .util .math .BlockPos ;
1717import net .minecraft .util .math .Direction ;
1818import net .minecraft .util .math .Vec3d ;
19+ import org .jetbrains .annotations .Nullable ;
1920
2021import java .util .ArrayList ;
22+ import java .util .HashMap ;
2123import java .util .List ;
22- import java .util .concurrent .atomic .AtomicInteger ;
2324import java .util .regex .Pattern ;
2425
2526public class ScanPlot extends Action {
2627 private List <BlockPos > blocks = null ;
28+ private HashMap <BlockPos , ItemStack > scanned = null ;
2729 private Action step = null ;
2830 private static final Vec3d goToOffset = new Vec3d (0 , 1.5 , 0 );
29- private final ArrayList <ItemStack > scanList ;
31+ private final ArrayList <ItemStack > returnList ;
3032
3133 public ScanPlot (Callback callback , ArrayList <ItemStack > scanList ) {
3234 super (callback );
33- this .scanList = scanList ;
35+ this .returnList = scanList ;
3436 if (!(CodeClient .location instanceof Dev )) {
3537 throw new IllegalStateException ("Player must be in dev mode." );
3638 }
@@ -40,6 +42,7 @@ public ScanPlot(Callback callback, ArrayList<ItemStack> scanList) {
4042 public void init () {
4143 if (CodeClient .location instanceof Dev plot ) {
4244 blocks = plot .scanForSigns (Pattern .compile ("(PLAYER|ENTITY) EVENT|FUNCTION|PROCESS" ), Pattern .compile (".*" )).keySet ().stream ().toList ();
45+ scanned = new HashMap <>(blocks .size ());
4346 }
4447 }
4548
@@ -60,14 +63,37 @@ public boolean onReceivePacket(Packet<?> packet) {
6063// }
6164 }
6265
63- private void next (int progress ) {
64- if (progress >= blocks .size ()) {
66+ @ Nullable
67+ private BlockPos findNextBlock () {
68+ var player = CodeClient .MC .player .getPos ();
69+ BlockPos nearest = null ;
70+ for (BlockPos pos : blocks ) {
71+ if (nearest == null || pos .isWithinDistance (player ,nearest .toCenterPos ().distanceTo (player ))) {
72+ CodeClient .LOGGER .info (String .valueOf (scanned .containsKey (nearest )));
73+ if (scanned .containsKey (pos ) && scanned .get (pos ) != null ) {
74+ continue ;
75+ }
76+ nearest = pos ;
77+ }
78+ }
79+ return nearest ;
80+ }
81+
82+ private void next () {
83+ var block = findNextBlock ();
84+ if (block == null ) {
85+ returnList .clear ();
86+ returnList .addAll (scanned .values ());
6587 callback ();
6688 return ;
6789 }
68- step = new GoTo (blocks .get (progress ).toCenterPos ().add (goToOffset ), () -> {
69- this .step = new pickUpBlock (blocks .get (progress ),() -> {
70- next (progress + 1 );
90+ CodeClient .LOGGER .info ("Going to " + block );
91+ step = new GoTo (block .toCenterPos ().add (goToOffset ), () -> {
92+ CodeClient .LOGGER .info ("Reached " + block + ", now picking up." );
93+ this .step = new PickUpBlock (block ,() -> {
94+ CodeClient .LOGGER .info ("Callback on " + block );
95+ this .step = null ;
96+ // next(progress + 1);
7197 });
7298 this .step .init ();
7399 });
@@ -79,15 +105,17 @@ public void onTick() {
79105 if (blocks == null ) return ;
80106 if (step != null ) step .onTick ();
81107 else {
82- next (0 );
108+ CodeClient .LOGGER .info ("Step was null." );
109+ next ();
83110 }
84111 }
85112
86113
87- private class pickUpBlock extends Action {
114+ private class PickUpBlock extends Action {
88115 private BlockPos pos ;
116+ private Integer ticks = 0 ;
89117
90- public pickUpBlock (BlockPos pos , Callback callback ) {
118+ public PickUpBlock (BlockPos pos , Callback callback ) {
91119 super (callback );
92120 this .pos = pos ;
93121 }
@@ -99,10 +127,10 @@ public void init() {
99127 var player = CodeClient .MC .player ;
100128 var inter = CodeClient .MC .interactionManager ;
101129 boolean sneaky = !player .isSneaking ();
130+ CodeClient .LOGGER .info ("Send interact for " + this .pos );
102131 if (sneaky ) net .sendPacket (new ClientCommandC2SPacket (player , ClientCommandC2SPacket .Mode .PRESS_SHIFT_KEY ));
103132 inter .interactBlock (player , Hand .MAIN_HAND , new BlockHitResult (this .pos .toCenterPos (), Direction .UP , this .pos , false ));
104- if (sneaky )
105- net .sendPacket (new ClientCommandC2SPacket (player , ClientCommandC2SPacket .Mode .RELEASE_SHIFT_KEY ));
133+ if (sneaky ) net .sendPacket (new ClientCommandC2SPacket (player , ClientCommandC2SPacket .Mode .RELEASE_SHIFT_KEY ));
106134 }
107135
108136 @ Override
@@ -112,14 +140,26 @@ public boolean onReceivePacket(Packet<?> packet) {
112140 var data = Utility .templateDataItem (slot .getStack ());
113141 var template = Template .parse64 (data );
114142 if (template == null ) return false ;
115- scanList .add (slot .getStack ());
143+ scanned .put (pos ,slot .getStack ());
144+ CodeClient .LOGGER .info ("Got item for " + this .pos );
116145 net .sendPacket (new CreativeInventoryActionC2SPacket (slot .getSlot (), ItemStack .EMPTY ));
117146 this .callback ();
118147 return true ;
119148 }
120149 return super .onReceivePacket (packet );
121150 }
122- // if(progress == blocks.size()) {
151+
152+ @ Override
153+ public void onTick () {
154+ ticks += 1 ;
155+ if (ticks == 10 ) {
156+ CodeClient .LOGGER .info ("Took too long to pick up " + this .pos + "; retrying." );
157+ ticks = 0 ;
158+ init ();
159+ }
160+ }
161+
162+ // if(progress == blocks.size()) {
123163// if(!waitForResponse) {
124164// callback();
125165// }
0 commit comments