Add ClampPos method to Level class, instead of duplicating the logic in 3 places

This commit is contained in:
UnknownShadow200 2020-06-08 00:25:22 +10:00
parent 924a9d6f02
commit cee0ef96b9
4 changed files with 10 additions and 23 deletions

View File

@ -158,11 +158,7 @@ namespace MCGalaxy.Commands.Building {
}
static int LargestDelta(Level lvl, Vec3S32 point) {
Vec3S32 clamped;
clamped.X = Math.Max(0, Math.Min(point.X, lvl.Width - 1));
clamped.Y = Math.Max(0, Math.Min(point.Y, lvl.Height - 1));
clamped.Z = Math.Max(0, Math.Min(point.Z, lvl.Length - 1));
Vec3S32 clamped = lvl.ClampPos(point);
int dx = Math.Abs(point.X - clamped.X);
int dy = Math.Abs(point.Y - clamped.Y);
int dz = Math.Abs(point.Z - clamped.Z);

View File

@ -45,9 +45,7 @@ namespace MCGalaxy.Commands.Building {
P.Y = (p.Pos.Y - 32) / 32;
if (message.Length > 0 && !ParseCoords(message, p, ref P)) return;
P.X = Clamp(P.X, p.level.Width);
P.Y = Clamp(P.Y, p.level.Height);
P.Z = Clamp(P.Z, p.level.Length);
P = p.level.ClampPos(P);
if (DoMark(p, P.X, P.Y, P.Z)) return;
Vec3U16 mark = (Vec3U16)P;
@ -80,12 +78,6 @@ namespace MCGalaxy.Commands.Building {
return CommandParser.GetCoords(p, args, 0, ref P);
}
static int Clamp(int value, int axisLen) {
if (value < 0) return 0;
if (value >= axisLen) return axisLen - 1;
return value;
}
internal static bool DoMark(Player p, int x, int y, int z) {
if (!p.HasBlockChange()) return false;
if (!p.Ignores.DrawOutput) {

View File

@ -49,9 +49,7 @@ namespace MCGalaxy.Commands.Building {
}
if (!CommandParser.IsBlockAllowed(p, "place", block)) return;
P.X = Clamp(P.X, p.level.Width);
P.Y = Clamp(P.Y, p.level.Height);
P.Z = Clamp(P.Z, p.level.Length);
P = p.level.ClampPos(P);
p.level.UpdateBlock(p, (ushort)P.X, (ushort)P.Y, (ushort)P.Z, block);
string blockName = Block.GetName(p, block);
@ -60,12 +58,6 @@ namespace MCGalaxy.Commands.Building {
}
}
static int Clamp(int value, int axisLen) {
if (value < 0) return 0;
if (value >= axisLen) return axisLen - 1;
return value;
}
public override void Help(Player p) {
p.Message("%T/Place <block>");
p.Message("%HPlaces block at your feet.");

View File

@ -441,6 +441,13 @@ namespace MCGalaxy {
return x >= 0 && y >= 0 && z >= 0 && x < Width && y < Height && z < Length;
}
public Vec3S32 ClampPos(Vec3S32 P) {
P.X = Math.Max(0, Math.Min(P.X, Width - 1));
P.Y = Math.Max(0, Math.Min(P.Y, Height - 1));
P.Z = Math.Max(0, Math.Min(P.Z, Length - 1));
return P;
}
public void UpdateBlock(Player p, ushort x, ushort y, ushort z, BlockID block,
ushort flags = BlockDBFlags.ManualPlace, bool buffered = false) {
int index;