Skip to content

Commit 9c9134e

Browse files
committed
Rotate the aligned poses to the closest 90 degree mark
1 parent 0192db7 commit 9c9134e

File tree

1 file changed

+152
-55
lines changed

1 file changed

+152
-55
lines changed

common/src/main/java/com/mrbysco/armorposer/client/gui/ArmorStandScreen.java

+152-55
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import net.minecraft.nbt.TagParser;
2828
import net.minecraft.network.chat.Component;
2929
import net.minecraft.resources.ResourceLocation;
30+
import net.minecraft.util.Mth;
3031
import net.minecraft.world.entity.decoration.ArmorStand;
3132
import net.minecraft.world.phys.Vec3;
3233

@@ -283,29 +284,50 @@ public void init() {
283284
ImageButton blockButton = this.addRenderableWidget(new ImageButton(offsetX - (22 * buttonsLeft) - buttonOffset, offsetY, 20, 20, BLOCK_SPRITES, (button) -> {
284285
try {
285286
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;
292287

293288
//Get the amount subtracted of x to get .0725
294-
double xDiff = 1.0725D - x;
289+
double xDiff = getDesiredOffset(pos.x, 1.0725D);
295290
//Get the amount subtracted of y to get .345
296-
double yDiff = -0.655D - y;
291+
double yDiff = getDesiredOffset(pos.y, -0.655D);
297292
//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+
}
299321

300322
CompoundTag tag = TagParser.parseTag(Reference.alignedBlockPose);
301323
this.readFieldsFromNBT(tag);
302324
this.toggleButtons[0].setValue(true); //Set invisible
303325
this.toggleButtons[2].setValue(true); //Set no gravity
304326
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
309331
this.textFieldUpdated();
310332
} catch (CommandSyntaxException e) {
311333
//Nope
@@ -319,59 +341,99 @@ public void init() {
319341
if (hasShiftDown()) { //If shift is held the item will be upright
320342
try {
321343
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;
328344

329345
//Get the amount subtracted of x to get .86
330-
double xDiff = 0.86D - x;
346+
double xDiff = getDesiredOffset(pos.x, 0.86D);
331347
//Get the amount subtracted of y to get .59
332-
double yDiff = -1.41D - y;
348+
double yDiff = getDesiredOffset(pos.y, -1.41D);
333349
//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+
}
335377

336378
CompoundTag tag = TagParser.parseTag(Reference.alignedUprightItemPose);
337379
this.readFieldsFromNBT(tag);
338380
this.toggleButtons[0].setValue(true); //Set invisible
339381
this.toggleButtons[2].setValue(true); //Set no gravity
340382
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
345387
this.textFieldUpdated();
346388
} catch (CommandSyntaxException e) {
347389
//Nope
348390
}
349391
} else {
350392
try {
351393
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;
358394

359395
//Get the amount subtracted of x to get .886
360-
double xDiff = 0.886D - x;
396+
double xDiff = getDesiredOffset(pos.x, 0.886D);
361397
//Get the amount subtracted of y to get .22
362-
double yDiff = -0.78D - y;
398+
double yDiff = getDesiredOffset(pos.y, -0.78D);
363399
//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+
}
365427

366428
CompoundTag tag = TagParser.parseTag(Reference.alignedFlatItemPose);
367429
this.readFieldsFromNBT(tag);
368430
this.toggleButtons[0].setValue(true); //Set invisible
369431
this.toggleButtons[2].setValue(true); //Set no gravity
370432
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
375437
this.textFieldUpdated();
376438
} catch (CommandSyntaxException e) {
377439
//Nope
@@ -384,29 +446,49 @@ public void init() {
384446
ImageButton toolButton = this.addRenderableWidget(new ImageButton(offsetX - (22 * buttonsLeft) - buttonOffset, offsetY, 20, 20, TOOL_SPRITES, (button) -> {
385447
try {
386448
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);
396452
//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+
}
400482

401483
CompoundTag tag = TagParser.parseTag(Reference.alignedToolPose);
402484
this.readFieldsFromNBT(tag);
403485
this.toggleButtons[0].setValue(true); //Set invisible
404486
this.toggleButtons[2].setValue(true); //Set no gravity
405487
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
410492
this.textFieldUpdated();
411493
} catch (CommandSyntaxException e) {
412494
//Nope
@@ -436,6 +518,21 @@ public void init() {
436518
}).bounds(offsetX - 95, offsetY + 22, 97, 20).build());
437519
}
438520

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+
439536
@Override
440537
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) {
441538
super.render(guiGraphics, mouseX, mouseY, partialTicks);

0 commit comments

Comments
 (0)