Skip to content

Commit 2165fba

Browse files
cewnoOcelot5836
andauthored
Fix the command output issue for /sable physics rotation (#138)
Prior to the fix, both 'set' and 'add' commands produced the output 'added'. <img width="1583" height="220" src="https://github.com/user-attachments/assets/b7c437c2-c014-484f-9097-d0f08d17c6ea" /> <img width="1487" height="268" src="https://github.com/user-attachments/assets/98aa0eb6-8f92-445d-88c3-3c28f57dec4a" /> <img width="2033" height="281" src="https://github.com/user-attachments/assets/9205abc8-83f9-4920-baff-f82352993b73" /> After the fix, it is functioning normally. <img width="1407" height="244" src="https://github.com/user-attachments/assets/a61c887a-e357-4b4f-883c-4b9c23bb3a9c" /> <img width="1407" height="244" src="https://github.com/user-attachments/assets/e804e81e-77b4-4a3d-98d5-ced9f81194ba" /> <img width="1798" height="469" src="https://github.com/user-attachments/assets/bfb4bd80-abfb-424a-b4a0-172d77b2072e" /> --------- Co-authored-by: Ocelot <FinntheRaider@gmail.com>
1 parent 096b86f commit 2165fba

1 file changed

Lines changed: 42 additions & 39 deletions

File tree

common/src/main/java/dev/ryanhcode/sable/command/SablePhysicsCommands.java

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ public static void register(final LiteralArgumentBuilder<CommandSourceStack> sab
8383
.then(Commands.literal("global")
8484
.executes((ctx) ->
8585
SablePhysicsCommands.executeAddTranslationCommand(ctx, true)))
86-
.then(Commands.literal("local")
87-
.executes((ctx) ->
88-
SablePhysicsCommands.executeAddTranslationCommand(ctx, false)))
86+
.then(Commands.literal("local")
87+
.executes((ctx) ->
88+
SablePhysicsCommands.executeAddTranslationCommand(ctx, false)))
8989
))
9090

9191
.then(Commands.literal("set")
@@ -154,50 +154,47 @@ private static int executeAngularImpulseCommand(final CommandContext<CommandSour
154154
return 0;
155155
}
156156

157-
private static ArgumentBuilder<CommandSourceStack, ?> wrapRotationWithMode(final boolean add)
158-
{
159-
return Commands.literal(add?"add":"set").then(wrapRotationWithReferenceFrame(add,false)).then(wrapRotationWithReferenceFrame(add,true));
157+
private static ArgumentBuilder<CommandSourceStack, ?> wrapRotationWithMode(final boolean add) {
158+
return Commands.literal(add ? "add" : "set").then(wrapRotationWithReferenceFrame(add, false)).then(wrapRotationWithReferenceFrame(add, true));
160159
}
161-
private static ArgumentBuilder<CommandSourceStack, ?> wrapRotationWithReferenceFrame(final boolean add, final boolean axis)
162-
{
163-
final Command<CommandSourceStack> c = (ctx) -> SablePhysicsCommands.executeRotationCommand(ctx,add,axis, true);
164-
final Function<ArgumentBuilder<CommandSourceStack, ?>,ArgumentBuilder<CommandSourceStack, ?>> f = (b) -> {
165-
if(add)
166-
b.then(wrapRotationWithGlobality(axis,true)).then(wrapRotationWithGlobality(axis,false));
160+
161+
private static ArgumentBuilder<CommandSourceStack, ?> wrapRotationWithReferenceFrame(final boolean add, final boolean axis) {
162+
final Command<CommandSourceStack> c = (ctx) -> SablePhysicsCommands.executeRotationCommand(ctx, add, axis, true);
163+
final Function<ArgumentBuilder<CommandSourceStack, ?>, ArgumentBuilder<CommandSourceStack, ?>> f = (b) -> {
164+
if (add)
165+
b.then(wrapRotationWithGlobality(axis, true)).then(wrapRotationWithGlobality(axis, false));
167166
return b;
168167
};
169-
final ArgumentBuilder<CommandSourceStack, ?> b = axis?
170-
Commands.argument("axis", Vec3ArgumentAbsolute.vec3()).then(f.apply(Commands.argument("angle", DoubleArgumentType.doubleArg()).executes(c))):
168+
final ArgumentBuilder<CommandSourceStack, ?> b = axis ?
169+
Commands.argument("axis", Vec3ArgumentAbsolute.vec3()).then(f.apply(Commands.argument("angle", DoubleArgumentType.doubleArg()).executes(c))) :
171170
f.apply(Commands.argument("rotation", RotationArgument.rotation()).executes(c));
172171

173-
return Commands.literal(axis?"axis":"entity").then(b);
174-
}
175-
private static ArgumentBuilder<CommandSourceStack, ?> wrapRotationWithGlobality(final boolean axis, final boolean global)
176-
{
177-
return Commands.literal(global?"global":"local").executes((ctx) ->
178-
SablePhysicsCommands.executeRotationCommand(ctx,true,axis, global));
172+
return Commands.literal(axis ? "axis" : "entity").then(b);
179173
}
180174

181-
private static int executeRotationCommand(final CommandContext<CommandSourceStack> ctx,final boolean add,final boolean axis, final boolean global) throws CommandSyntaxException {
182-
175+
private static ArgumentBuilder<CommandSourceStack, ?> wrapRotationWithGlobality(final boolean axis, final boolean global) {
176+
return Commands.literal(global ? "global" : "local").executes((ctx) ->
177+
SablePhysicsCommands.executeRotationCommand(ctx, true, axis, global));
178+
}
183179

180+
private static int executeRotationCommand(final CommandContext<CommandSourceStack> ctx, final boolean add, final boolean axis, final boolean global) throws CommandSyntaxException {
184181
final PhysicsPipeline pipeline = SableCommandHelper.requireSubLevelPhysicsPipeline(ctx);
185182

186183
final Quaterniond orientation = new Quaterniond();
187184

188-
Vec2 rotation2=new Vec2(0,0);
189-
Vec3 rotationAxis=new Vec3(0,0,0);
190-
double rotationAngle=0;
185+
Vec2 rotation2 = new Vec2(0, 0);
186+
Vec3 rotationAxis = new Vec3(0, 0, 0);
187+
double rotationAngle = 0;
191188

192-
if(axis) {
189+
if (axis) {
193190
rotationAxis = ctx.getArgument("axis", Vec3.class);
194-
rotationAngle = ctx.getArgument("angle",Double.class);
195-
orientation.fromAxisAngleDeg(rotationAxis.x,rotationAxis.y,rotationAxis.z,rotationAngle);
191+
rotationAngle = ctx.getArgument("angle", Double.class);
192+
orientation.fromAxisAngleDeg(rotationAxis.x, rotationAxis.y, rotationAxis.z, rotationAngle);
196193

197-
if(rotationAxis.lengthSqr()==0)
194+
if (rotationAxis.lengthSqr() == 0) {
198195
throw SableCommandHelper.ERROR_NO_AXIS_FOR_ROTATION.create();
199-
}else
200-
{
196+
}
197+
} else {
201198
rotation2 = RotationArgument.getRotation(ctx, "rotation").getRotation(ctx.getSource());
202199
orientation.rotateY(-Math.toRadians(rotation2.y));
203200
orientation.rotateX(Math.toRadians(rotation2.x));
@@ -211,23 +208,29 @@ private static int executeRotationCommand(final CommandContext<CommandSourceStac
211208

212209
for (final ServerSubLevel subLevel : subLevels) {
213210
final Pose3d pose = subLevel.logicalPose();
214-
if(add) {
211+
if (add) {
215212
if (global) {
216213
pose.orientation().premul(orientation);
217214
} else {
218215
pose.orientation().mul(orientation);
219216
}
220-
}else
217+
} else {
221218
pose.orientation().set(orientation);
219+
}
222220
pipeline.teleport(subLevel, pose.position(), pose.orientation());
223221
}
224222

225-
if(axis) {
226-
SableCommandHelper.sendSuccessDescribingSubLevelsAtIndex("commands.sable.physics.rotation.add.success", ctx, subLevels, 1,
227-
getGlobalComponent(global), rotationAxis.x + ", " + rotationAxis.y + ", "+ rotationAxis.z + ", " + rotationAngle);
228-
}else
229-
{
230-
SableCommandHelper.sendSuccessDescribingSubLevelsAtIndex("commands.sable.physics.rotation.add.success", ctx, subLevels, 1,
223+
if (axis) {
224+
SableCommandHelper.sendSuccessDescribingSubLevelsAtIndex(
225+
add ? "commands.sable.physics.rotation.add.success"
226+
: "commands.sable.physics.rotation.set.success",
227+
ctx, subLevels, 1,
228+
getGlobalComponent(global), rotationAxis.x + ", " + rotationAxis.y + ", " + rotationAxis.z + ", " + rotationAngle);
229+
} else {
230+
SableCommandHelper.sendSuccessDescribingSubLevelsAtIndex(
231+
add ? "commands.sable.physics.rotation.add.success"
232+
: "commands.sable.physics.rotation.set.success",
233+
ctx, subLevels, 1,
231234
getGlobalComponent(global), rotation2.x + ", " + rotation2.y);
232235
}
233236
return 0;

0 commit comments

Comments
 (0)