mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-10-04 11:08:38 -04:00
Add ClampPos method to Level class, instead of duplicating the logic in 3 places
This commit is contained in:
parent
924a9d6f02
commit
cee0ef96b9
@ -158,11 +158,7 @@ namespace MCGalaxy.Commands.Building {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int LargestDelta(Level lvl, Vec3S32 point) {
|
static int LargestDelta(Level lvl, Vec3S32 point) {
|
||||||
Vec3S32 clamped;
|
Vec3S32 clamped = lvl.ClampPos(point);
|
||||||
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));
|
|
||||||
|
|
||||||
int dx = Math.Abs(point.X - clamped.X);
|
int dx = Math.Abs(point.X - clamped.X);
|
||||||
int dy = Math.Abs(point.Y - clamped.Y);
|
int dy = Math.Abs(point.Y - clamped.Y);
|
||||||
int dz = Math.Abs(point.Z - clamped.Z);
|
int dz = Math.Abs(point.Z - clamped.Z);
|
||||||
|
@ -45,9 +45,7 @@ namespace MCGalaxy.Commands.Building {
|
|||||||
P.Y = (p.Pos.Y - 32) / 32;
|
P.Y = (p.Pos.Y - 32) / 32;
|
||||||
if (message.Length > 0 && !ParseCoords(message, p, ref P)) return;
|
if (message.Length > 0 && !ParseCoords(message, p, ref P)) return;
|
||||||
|
|
||||||
P.X = Clamp(P.X, p.level.Width);
|
P = p.level.ClampPos(P);
|
||||||
P.Y = Clamp(P.Y, p.level.Height);
|
|
||||||
P.Z = Clamp(P.Z, p.level.Length);
|
|
||||||
if (DoMark(p, P.X, P.Y, P.Z)) return;
|
if (DoMark(p, P.X, P.Y, P.Z)) return;
|
||||||
|
|
||||||
Vec3U16 mark = (Vec3U16)P;
|
Vec3U16 mark = (Vec3U16)P;
|
||||||
@ -80,12 +78,6 @@ namespace MCGalaxy.Commands.Building {
|
|||||||
return CommandParser.GetCoords(p, args, 0, ref P);
|
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) {
|
internal static bool DoMark(Player p, int x, int y, int z) {
|
||||||
if (!p.HasBlockChange()) return false;
|
if (!p.HasBlockChange()) return false;
|
||||||
if (!p.Ignores.DrawOutput) {
|
if (!p.Ignores.DrawOutput) {
|
||||||
|
@ -49,9 +49,7 @@ namespace MCGalaxy.Commands.Building {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!CommandParser.IsBlockAllowed(p, "place", block)) return;
|
if (!CommandParser.IsBlockAllowed(p, "place", block)) return;
|
||||||
P.X = Clamp(P.X, p.level.Width);
|
P = p.level.ClampPos(P);
|
||||||
P.Y = Clamp(P.Y, p.level.Height);
|
|
||||||
P.Z = Clamp(P.Z, p.level.Length);
|
|
||||||
|
|
||||||
p.level.UpdateBlock(p, (ushort)P.X, (ushort)P.Y, (ushort)P.Z, block);
|
p.level.UpdateBlock(p, (ushort)P.X, (ushort)P.Y, (ushort)P.Z, block);
|
||||||
string blockName = Block.GetName(p, 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) {
|
public override void Help(Player p) {
|
||||||
p.Message("%T/Place <block>");
|
p.Message("%T/Place <block>");
|
||||||
p.Message("%HPlaces block at your feet.");
|
p.Message("%HPlaces block at your feet.");
|
||||||
|
@ -441,6 +441,13 @@ namespace MCGalaxy {
|
|||||||
return x >= 0 && y >= 0 && z >= 0 && x < Width && y < Height && z < Length;
|
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,
|
public void UpdateBlock(Player p, ushort x, ushort y, ushort z, BlockID block,
|
||||||
ushort flags = BlockDBFlags.ManualPlace, bool buffered = false) {
|
ushort flags = BlockDBFlags.ManualPlace, bool buffered = false) {
|
||||||
int index;
|
int index;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user