27
27
import net .minecraft .nbt .TagParser ;
28
28
import net .minecraft .network .chat .Component ;
29
29
import net .minecraft .resources .ResourceLocation ;
30
+ import net .minecraft .util .Mth ;
30
31
import net .minecraft .world .entity .decoration .ArmorStand ;
31
32
import net .minecraft .world .phys .Vec3 ;
32
33
@@ -283,29 +284,50 @@ public void init() {
283
284
ImageButton blockButton = this .addRenderableWidget (new ImageButton (offsetX - (22 * buttonsLeft ) - buttonOffset , offsetY , 20 , 20 , BLOCK_SPRITES , (button ) -> {
284
285
try {
285
286
Vec3 pos = this .entityArmorStand .position ();
286
- //Get the x decimals after the comma
287
- double x = pos .x - (int ) pos .x ;
288
- //Get the y decimals after the comma
289
- double y = pos .y - (int ) pos .y ;
290
- //Get the z decimals after the comma
291
- double z = pos .z - (int ) pos .z ;
292
287
293
288
//Get the amount subtracted of x to get .0725
294
- double xDiff = 1.0725D - x ;
289
+ double xDiff = getDesiredOffset ( pos . x , 1.0725D ) ;
295
290
//Get the amount subtracted of y to get .345
296
- double yDiff = -0.655D - y ;
291
+ double yDiff = getDesiredOffset ( pos . y , -0.655D ) ;
297
292
//Get the amount subtracted of z to get .852
298
- double zDiff = 0.852D - z ;
293
+ double zDiff = getDesiredOffset (pos .z , 0.852D );
294
+
295
+ Vec3 offset = new Vec3 (xDiff , yDiff , zDiff );
296
+ int closestDegree = Mth .roundToward ((int ) this .rotationTextField .getFloat (), 90 );
297
+ System .out .println (closestDegree );
298
+ switch (closestDegree ) {
299
+ case 90 : {
300
+ //Rotate the desired position to have the correct values
301
+ double newX = offset .z - 0.7D ;
302
+ double newZ = -offset .x + 1.18D ;
303
+ offset = new Vec3 (newX , offset .y , newZ );
304
+ break ;
305
+ }
306
+ case -180 : {
307
+ //Rotate the desired position to have the correct values
308
+ double newX = -offset .x ;
309
+ double newZ = -offset .z ;
310
+ offset = new Vec3 (newX , offset .y , newZ );
311
+ break ;
312
+ }
313
+ case -90 : {
314
+ //Rotate the desired position to have the correct values
315
+ double newX = -offset .z + 0.7D ;
316
+ double newZ = offset .x - 1.18D ;
317
+ offset = new Vec3 (newX , offset .y , newZ );
318
+ break ;
319
+ }
320
+ }
299
321
300
322
CompoundTag tag = TagParser .parseTag (Reference .alignedBlockPose );
301
323
this .readFieldsFromNBT (tag );
302
324
this .toggleButtons [0 ].setValue (true ); //Set invisible
303
325
this .toggleButtons [2 ].setValue (true ); //Set no gravity
304
326
this .toggleButtons [3 ].setValue (true ); //Set show arms
305
- this .rotationTextField .setValue ("0" ); //Set rotation
306
- this .poseTextFields [18 ].setValue (String .valueOf (xDiff )); //Set X
307
- this .poseTextFields [19 ].setValue (String .valueOf (yDiff )); //Set Y
308
- this .poseTextFields [20 ].setValue (String .valueOf (zDiff )); //Set Z
327
+ this .rotationTextField .setValue (String . valueOf ( closestDegree ) ); //Set rotation
328
+ this .poseTextFields [18 ].setValue (String .valueOf (offset . x )); //Set X
329
+ this .poseTextFields [19 ].setValue (String .valueOf (offset . y )); //Set Y
330
+ this .poseTextFields [20 ].setValue (String .valueOf (offset . z )); //Set Z
309
331
this .textFieldUpdated ();
310
332
} catch (CommandSyntaxException e ) {
311
333
//Nope
@@ -319,59 +341,99 @@ public void init() {
319
341
if (hasShiftDown ()) { //If shift is held the item will be upright
320
342
try {
321
343
Vec3 pos = this .entityArmorStand .position ();
322
- //Get the x decimals after the comma
323
- double x = pos .x - (int ) pos .x ;
324
- //Get the y decimals after the comma
325
- double y = pos .y - (int ) pos .y ;
326
- //Get the z decimals after the comma
327
- double z = pos .z - (int ) pos .z ;
328
344
329
345
//Get the amount subtracted of x to get .86
330
- double xDiff = 0.86D - x ;
346
+ double xDiff = getDesiredOffset ( pos . x , 0.86D ) ;
331
347
//Get the amount subtracted of y to get .59
332
- double yDiff = -1.41D - y ;
348
+ double yDiff = getDesiredOffset ( pos . y , -1.41D ) ;
333
349
//Get the amount subtracted of z to get .9375
334
- double zDiff = -0.0625D - z ;
350
+ double zDiff = getDesiredOffset (pos .z , -0.0625D );
351
+
352
+ Vec3 offset = new Vec3 (xDiff , yDiff , zDiff );
353
+ int closestDegree = Mth .roundToward ((int ) this .rotationTextField .getFloat (), 90 );
354
+ switch (closestDegree ) {
355
+ case 90 : {
356
+ //Rotate the desired position to have the correct values
357
+ double newX = offset .z + 1.12D ;
358
+ double newZ = -offset .x + 0.74D ;
359
+ offset = new Vec3 (newX , offset .y , newZ );
360
+ break ;
361
+ }
362
+ case -180 : {
363
+ //Rotate the desired position to have the correct values
364
+ double newX = -offset .x ;
365
+ double newZ = -offset .z ;
366
+ offset = new Vec3 (newX , offset .y , newZ );
367
+ break ;
368
+ }
369
+ case -90 : {
370
+ //Rotate the desired position to have the correct values
371
+ double newX = -offset .z - 1.12D ;
372
+ double newZ = offset .x - 0.74D ;
373
+ offset = new Vec3 (newX , offset .y , newZ );
374
+ break ;
375
+ }
376
+ }
335
377
336
378
CompoundTag tag = TagParser .parseTag (Reference .alignedUprightItemPose );
337
379
this .readFieldsFromNBT (tag );
338
380
this .toggleButtons [0 ].setValue (true ); //Set invisible
339
381
this .toggleButtons [2 ].setValue (true ); //Set no gravity
340
382
this .toggleButtons [3 ].setValue (true ); //Set show arms
341
- this .rotationTextField .setValue ("0" ); //Set rotation
342
- this .poseTextFields [18 ].setValue (String .valueOf (xDiff )); //Set X
343
- this .poseTextFields [19 ].setValue (String .valueOf (yDiff )); //Set Y
344
- this .poseTextFields [20 ].setValue (String .valueOf (zDiff )); //Set Z
383
+ this .rotationTextField .setValue (String . valueOf ( closestDegree ) ); //Set rotation
384
+ this .poseTextFields [18 ].setValue (String .valueOf (offset . x )); //Set X
385
+ this .poseTextFields [19 ].setValue (String .valueOf (offset . y )); //Set Y
386
+ this .poseTextFields [20 ].setValue (String .valueOf (offset . z )); //Set Z
345
387
this .textFieldUpdated ();
346
388
} catch (CommandSyntaxException e ) {
347
389
//Nope
348
390
}
349
391
} else {
350
392
try {
351
393
Vec3 pos = this .entityArmorStand .position ();
352
- //Get the x decimals after the comma
353
- double x = pos .x - (int ) pos .x ;
354
- //Get the y decimals after the comma
355
- double y = pos .y - (int ) pos .y ;
356
- //Get the z decimals after the comma
357
- double z = pos .z - (int ) pos .z ;
358
394
359
395
//Get the amount subtracted of x to get .886
360
- double xDiff = 0.886D - x ;
396
+ double xDiff = getDesiredOffset ( pos . x , 0.886D ) ;
361
397
//Get the amount subtracted of y to get .22
362
- double yDiff = -0.78D - y ;
398
+ double yDiff = getDesiredOffset ( pos . y , -0.78D ) ;
363
399
//Get the amount subtracted of z to get .205
364
- double zDiff = 0.205D - z ;
400
+ double zDiff = getDesiredOffset (pos .z , 0.205D );
401
+
402
+ Vec3 offset = new Vec3 (xDiff , yDiff , zDiff );
403
+ int closestDegree = Mth .roundToward ((int ) this .rotationTextField .getFloat (), 90 );
404
+ switch (closestDegree ) {
405
+ case 90 : {
406
+ //Rotate the desired position to have the correct values
407
+ double newX = offset .z + 0.59D ;
408
+ double newZ = -offset .x + 0.78D ;
409
+ offset = new Vec3 (newX , offset .y , newZ );
410
+ break ;
411
+ }
412
+ case -180 : {
413
+ //Rotate the desired position to have the correct values
414
+ double newX = -offset .x ;
415
+ double newZ = -offset .z ;
416
+ offset = new Vec3 (newX , offset .y , newZ );
417
+ break ;
418
+ }
419
+ case -90 : {
420
+ //Rotate the desired position to have the correct values
421
+ double newX = -offset .z - 0.59D ;
422
+ double newZ = offset .x - 0.78D ;
423
+ offset = new Vec3 (newX , offset .y , newZ );
424
+ break ;
425
+ }
426
+ }
365
427
366
428
CompoundTag tag = TagParser .parseTag (Reference .alignedFlatItemPose );
367
429
this .readFieldsFromNBT (tag );
368
430
this .toggleButtons [0 ].setValue (true ); //Set invisible
369
431
this .toggleButtons [2 ].setValue (true ); //Set no gravity
370
432
this .toggleButtons [3 ].setValue (true ); //Set show arms
371
- this .rotationTextField .setValue ("0" ); //Set rotation
372
- this .poseTextFields [18 ].setValue (String .valueOf (xDiff )); //Set X
373
- this .poseTextFields [19 ].setValue (String .valueOf (yDiff )); //Set Y
374
- this .poseTextFields [20 ].setValue (String .valueOf (zDiff )); //Set Z
433
+ this .rotationTextField .setValue (String . valueOf ( closestDegree ) ); //Set rotation
434
+ this .poseTextFields [18 ].setValue (String .valueOf (offset . x )); //Set X
435
+ this .poseTextFields [19 ].setValue (String .valueOf (offset . y )); //Set Y
436
+ this .poseTextFields [20 ].setValue (String .valueOf (offset . z )); //Set Z
375
437
this .textFieldUpdated ();
376
438
} catch (CommandSyntaxException e ) {
377
439
//Nope
@@ -384,29 +446,49 @@ public void init() {
384
446
ImageButton toolButton = this .addRenderableWidget (new ImageButton (offsetX - (22 * buttonsLeft ) - buttonOffset , offsetY , 20 , 20 , TOOL_SPRITES , (button ) -> {
385
447
try {
386
448
Vec3 pos = this .entityArmorStand .position ();
387
- //Get the x decimals after the comma
388
- double x = pos .x - (int ) pos .x ;
389
- //Get the y decimals after the comma
390
- double y = pos .y - (int ) pos .y ;
391
- //Get the z decimals after the comma
392
- double z = pos .z - (int ) pos .z ;
393
-
394
- //Get the amount subtracted of x to get .886
395
- double xDiff = 0.33D - x ;
449
+
450
+ //Get the amount subtracted of x to get .33
451
+ double xDiff = getDesiredOffset (pos .x , 0.33D );
396
452
//Get the amount subtracted of y to get .22
397
- double yDiff = -1.285D - y ;
398
- //Get the amount subtracted of z to get .205
399
- double zDiff = 0.059999D - z ;
453
+ double yDiff = getDesiredOffset (pos .y , -1.285D );
454
+ //Get the amount subtracted of z to get .059999D
455
+ double zDiff = getDesiredOffset (pos .z , 0.059999D );
456
+
457
+ Vec3 offset = new Vec3 (xDiff , yDiff , zDiff );
458
+ int closestDegree = Mth .roundToward ((int ) this .rotationTextField .getFloat (), 90 );
459
+ switch (closestDegree ) {
460
+ case 90 : {
461
+ //Rotate the desired position to have the correct values
462
+ double newX = offset .z + 0.88D ;
463
+ double newZ = -offset .x - 0.34D ;
464
+ offset = new Vec3 (newX , offset .y , newZ );
465
+ break ;
466
+ }
467
+ case -180 : {
468
+ //Rotate the desired position to have the correct values
469
+ double newX = -offset .x ;
470
+ double newZ = -offset .z ;
471
+ offset = new Vec3 (newX , offset .y , newZ );
472
+ break ;
473
+ }
474
+ case -90 : {
475
+ //Rotate the desired position to have the correct values
476
+ double newX = -offset .z - 0.88D ;
477
+ double newZ = offset .x + 0.34D ;
478
+ offset = new Vec3 (newX , offset .y , newZ );
479
+ break ;
480
+ }
481
+ }
400
482
401
483
CompoundTag tag = TagParser .parseTag (Reference .alignedToolPose );
402
484
this .readFieldsFromNBT (tag );
403
485
this .toggleButtons [0 ].setValue (true ); //Set invisible
404
486
this .toggleButtons [2 ].setValue (true ); //Set no gravity
405
487
this .toggleButtons [3 ].setValue (true ); //Set show arms
406
- this .rotationTextField .setValue ("0" ); //Set rotation
407
- this .poseTextFields [18 ].setValue (String .valueOf (xDiff )); //Set X
408
- this .poseTextFields [19 ].setValue (String .valueOf (yDiff )); //Set Y
409
- this .poseTextFields [20 ].setValue (String .valueOf (zDiff )); //Set Z
488
+ this .rotationTextField .setValue (String . valueOf ( closestDegree ) ); //Set rotation
489
+ this .poseTextFields [18 ].setValue (String .valueOf (offset . x )); //Set X
490
+ this .poseTextFields [19 ].setValue (String .valueOf (offset . y )); //Set Y
491
+ this .poseTextFields [20 ].setValue (String .valueOf (offset . z )); //Set Z
410
492
this .textFieldUpdated ();
411
493
} catch (CommandSyntaxException e ) {
412
494
//Nope
@@ -436,6 +518,21 @@ public void init() {
436
518
}).bounds (offsetX - 95 , offsetY + 22 , 97 , 20 ).build ());
437
519
}
438
520
521
+ /**
522
+ * Get the desired offset to get the armor stand in the correct position
523
+ *
524
+ * @param posValue The current position value
525
+ * @param desiredValue The desired position value
526
+ * @return The amount subtracted from or added to the current position to get the desired position
527
+ */
528
+ private double getDesiredOffset (double posValue , double desiredValue ) {
529
+ double value = posValue - (int ) posValue ; //Get the decimal value
530
+ if (value < 0 ) { //Make it positive if it's a negative position
531
+ value = -value ;
532
+ }
533
+ return desiredValue - value ;
534
+ }
535
+
439
536
@ Override
440
537
public void render (GuiGraphics guiGraphics , int mouseX , int mouseY , float partialTicks ) {
441
538
super .render (guiGraphics , mouseX , mouseY , partialTicks );
0 commit comments