|
| 1 | +using Common.Mod.Extensions; |
| 2 | +using Vintagestory.API.Common; |
| 3 | +using Vintagestory.API.MathTools; |
| 4 | +using Vintagestory.API.Server; |
| 5 | + |
| 6 | +namespace Common.Mod.Example.Commands; |
| 7 | + |
| 8 | +public class DebugCommand |
| 9 | +{ |
| 10 | + private readonly ICoreAPI _api; |
| 11 | + |
| 12 | + public DebugCommand(ICoreAPI api) |
| 13 | + { |
| 14 | + _api = api; |
| 15 | + |
| 16 | + var command = api.ChatCommands.Create("ex-debug") |
| 17 | + .RequiresPlayer() |
| 18 | + .RequiresPrivilege(Privilege.chat); |
| 19 | + |
| 20 | + { |
| 21 | + var walkCommand = command.BeginSubCommand("walk"); |
| 22 | + |
| 23 | + walkCommand.BeginSubCommand("cuboid") |
| 24 | + .WithArgs( |
| 25 | + api.ChatCommands.Parsers.WorldPosition("position"), |
| 26 | + api.ChatCommands.Parsers.Vec3i("size") |
| 27 | + ) |
| 28 | + .HandleWith(DebugWalkCuboid) |
| 29 | + .EndSubCommand(); |
| 30 | + |
| 31 | + walkCommand.BeginSubCommand("cube") |
| 32 | + .WithArgs( |
| 33 | + api.ChatCommands.Parsers.WorldPosition("position"), |
| 34 | + api.ChatCommands.Parsers.IntRange("size", 0, int.MaxValue) |
| 35 | + ) |
| 36 | + .HandleWith(DebugWalkCube) |
| 37 | + .EndSubCommand(); |
| 38 | + |
| 39 | + walkCommand.BeginSubCommand("cylinder") |
| 40 | + .WithArgs( |
| 41 | + api.ChatCommands.Parsers.WorldPosition("position"), |
| 42 | + api.ChatCommands.Parsers.IntRange("radius", 0, int.MaxValue) |
| 43 | + ) |
| 44 | + .HandleWith(DebugWalkCylinder) |
| 45 | + .EndSubCommand(); |
| 46 | + |
| 47 | + walkCommand.BeginSubCommand("sphere") |
| 48 | + .WithArgs( |
| 49 | + api.ChatCommands.Parsers.WorldPosition("position"), |
| 50 | + api.ChatCommands.Parsers.IntRange("radius", 0, int.MaxValue) |
| 51 | + ) |
| 52 | + .HandleWith(DebugWalkSphere) |
| 53 | + .EndSubCommand(); |
| 54 | + |
| 55 | + walkCommand.EndSubCommand(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private TextCommandResult DebugWalkCuboid(TextCommandCallingArgs args) |
| 60 | + { |
| 61 | + var position = (Vec3d)args[0]; |
| 62 | + var size = (Vec3i)args[1]; |
| 63 | + |
| 64 | + var halfSize = (size / 2)!; |
| 65 | + var minPos = position.SubCopy(halfSize.X, halfSize.Y, halfSize.Z); |
| 66 | + var maxPos = position.AddCopy(halfSize.X, halfSize.Y, halfSize.Z); |
| 67 | + |
| 68 | + var devastationRock = _api.World.BlockAccessor.GetBlock(new AssetLocation("game", "drock"))!; |
| 69 | + _api.World.WalkBlocksCuboid(minPos.AsBlockPos, maxPos.AsBlockPos, |
| 70 | + (_, x, y, z) => { _api.World.BlockAccessor.SetBlock(devastationRock.Id, new Vec3i(x, y, z).AsBlockPos); }); |
| 71 | + |
| 72 | + return TextCommandResult.Success(); |
| 73 | + } |
| 74 | + |
| 75 | + private TextCommandResult DebugWalkCube(TextCommandCallingArgs args) |
| 76 | + { |
| 77 | + var position = (Vec3d)args[0]; |
| 78 | + var size = (int)args[1]; |
| 79 | + |
| 80 | + var devastationRock = _api.World.BlockAccessor.GetBlock(new AssetLocation("game", "drock"))!; |
| 81 | + _api.World.WalkBlocksCube(position.AsBlockPos, size / 2, |
| 82 | + (_, x, y, z) => { _api.World.BlockAccessor.SetBlock(devastationRock.Id, new Vec3i(x, y, z).AsBlockPos); }); |
| 83 | + |
| 84 | + return TextCommandResult.Success(); |
| 85 | + } |
| 86 | + |
| 87 | + private TextCommandResult DebugWalkCylinder(TextCommandCallingArgs args) |
| 88 | + { |
| 89 | + var position = (Vec3d)args[0]; |
| 90 | + var radius = (int)args[1]; |
| 91 | + |
| 92 | + const int minYPos = 0; |
| 93 | + var maxYPos = _api.World.BlockAccessor.MapSizeY; |
| 94 | + |
| 95 | + var devastationRock = _api.World.BlockAccessor.GetBlock(new AssetLocation("game", "drock"))!; |
| 96 | + _api.World.WalkBlocksCylinder(position.AsBlockPos, radius, minYPos, maxYPos, |
| 97 | + (_, x, y, z) => { _api.World.BlockAccessor.SetBlock(devastationRock.Id, new Vec3i(x, y, z).AsBlockPos); }); |
| 98 | + |
| 99 | + return TextCommandResult.Success(); |
| 100 | + } |
| 101 | + |
| 102 | + private TextCommandResult DebugWalkSphere(TextCommandCallingArgs args) |
| 103 | + { |
| 104 | + var position = (Vec3d)args[0]; |
| 105 | + var radius = (int)args[1]; |
| 106 | + |
| 107 | + var devastationRock = _api.World.BlockAccessor.GetBlock(new AssetLocation("game", "drock"))!; |
| 108 | + _api.World.WalkBlocksSphere(position.AsBlockPos, radius, |
| 109 | + (_, x, y, z) => { _api.World.BlockAccessor.SetBlock(devastationRock.Id, new Vec3i(x, y, z).AsBlockPos); }); |
| 110 | + |
| 111 | + return TextCommandResult.Success(); |
| 112 | + } |
| 113 | +} |
0 commit comments