Skip to content

Commit

Permalink
Improve MB/Portal Show for deleted blocks
Browse files Browse the repository at this point in the history
Displays MBs and Portals as black wool if they have been deleted
  • Loading branch information
123DMWM committed Oct 3, 2024
1 parent b23decd commit 112c589
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 58 deletions.
55 changes: 31 additions & 24 deletions MCGalaxy/Commands/building/CmdMessageBlock.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/*
Copyright 2011 MCForge
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
https://opensource.org/license/ecl-2-0/
https://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
Expand Down Expand Up @@ -43,50 +43,55 @@ public override void Use(Player p, string message, CommandData data) {
MBArgs mbArgs = new MBArgs();
string[] args = message.SplitSpaces(2);
string block = args[0].ToLower();

mbArgs.Block = GetBlock(p, block, ref allMessage);
if (mbArgs.Block == Block.Invalid) return;
if (!CommandParser.IsBlockAllowed(p, "place a message block of", mbArgs.Block)) return;

if (allMessage) {
mbArgs.Message = message;
} else if (args.Length == 1) {
p.Message("You need to provide text to put in the messageblock."); return;
} else {
mbArgs.Message = args[1];
}

bool allCmds = HasExtraPerm(p, data.Rank, 1);
if (!MessageBlock.Validate(p, mbArgs.Message, allCmds)) return;

p.Message("Place where you wish the message block to go.");
p.MakeSelection(1, mbArgs, PlacedMark);
}

BlockID GetBlock(Player p, string name, ref bool allMessage) {
if (name == "show") { ShowMessageBlocks(p); return Block.Invalid; }
BlockID block = Block.Parse(p, name);
if (block != Block.Invalid && p.level.Props[block].IsMessageBlock)
return block;

// Hardcoded aliases for backwards compatibility
block = Block.MB_White;
if (name == "white") block = Block.MB_White;
if (name == "black") block = Block.MB_Black;
if (name == "air") block = Block.MB_Air;
if (name == "water") block = Block.MB_Water;
if (name == "lava") block = Block.MB_Lava;

allMessage = block == Block.MB_White && name != "white";
if (p.level.Props[block].IsMessageBlock) return block;

Help(p); return Block.Invalid;
}

static bool IsMBBlock(Level lvl, Vec3U16 pos) {
BlockID block = lvl.GetBlock(pos.X, pos.Y, pos.Z);
return lvl.Props[block].IsMessageBlock;
}

bool PlacedMark(Player p, Vec3S32[] marks, object state, BlockID block) {
ushort x = (ushort)marks[0].X, y = (ushort)marks[0].Y, z = (ushort)marks[0].Z;
MBArgs args = (MBArgs)state;

BlockID old = p.level.GetBlock(x, y, z);
if (p.level.CheckAffect(p, x, y, z, old, args.Block)) {
p.level.UpdateBlock(p, x, y, z, args.Block);
Expand All @@ -100,66 +105,68 @@ bool PlacedMark(Player p, Vec3S32[] marks, object state, BlockID block) {
}
return true;
}

void UpdateDatabase(Player p, MBArgs args, ushort x, ushort y, ushort z) {
string map = p.level.name;
object locker = ThreadSafeCache.DBCache.GetLocker(map);

lock (locker) {
MessageBlock.Set(map, x, y, z, args.Message);
}
}

class MBArgs { public string Message; public BlockID Block; }


void ShowMessageBlocks(Player p) {
p.showMBs = !p.showMBs;
List<Vec3U16> coords = MessageBlock.GetAllCoords(p.level.MapName);

foreach (Vec3U16 pos in coords) {
if (p.showMBs) {
p.SendBlockchange(pos.X, pos.Y, pos.Z, Block.Green);
BlockID block = IsMBBlock(p.level, pos) ? Block.Green : Block.Black;
p.SendBlockchange(pos.X, pos.Y, pos.Z, block);
} else {
p.RevertBlock(pos.X, pos.Y, pos.Z);
}
}

p.Message("Now {0} &SMBs.",
p.Message("Now {0} &SMBs.",
p.showMBs ? "showing &a" + coords.Count : "hiding");
}

static string Format(BlockID block, Player p, BlockProps[] props) {
if (!props[block].IsMessageBlock) return null;

// We want to use the simple aliases if possible
if (block == Block.MB_Black) return "black";
if (block == Block.MB_White) return "white";
if (block == Block.MB_Air) return "air";
if (block == Block.MB_Lava) return "lava";
if (block == Block.MB_Water) return "water";
if (block == Block.MB_Water) return "water";
return Block.GetName(p, block);
}

static List<string> SupportedBlocks(Player p) {
List<string> names = new List<string>();
BlockProps[] props = p.IsSuper ? Block.Props : p.level.Props;

for (int i = 0; i < props.Length; i++) {
string name = Format((BlockID)i, p, props);
if (name != null) names.Add(name);
}
return names;
}

public override void Help(Player p) {
p.Message("&T/MB [block] [message]");
p.Message("&HPlaces a message in your next block.");
List<string> names = SupportedBlocks(p);
p.Message("&H Supported blocks: &S{0}", names.Join());
p.Message("&H Use | to separate commands, e.g. /say 1 |/say 2");
p.Message("&H Note: \"@p\" is a placeholder for player who clicked.");
p.Message("&T/MB show &H- Shows or hides message blocks");
p.Message("&T/MB show &H- Shows or hides message blocks on the map");
p.Message("&H (green = valid message block, black = queued for removal)");
}
}
}
81 changes: 47 additions & 34 deletions MCGalaxy/Commands/building/CmdPortal.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/*
Copyright 2011 MCForge
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
https://opensource.org/license/ecl-2-0/
https://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
Expand Down Expand Up @@ -49,18 +49,18 @@ public override void Use(Player p, string message, CommandData data) {
if (pArgs.Block == Block.Invalid) return;
if (!CommandParser.IsBlockAllowed(p, "place a portal of", pArgs.Block)) return;
pArgs.Entries = new List<PortalPos>();

p.Message("Place an &aEntry block &Sfor the portal");
p.ClearBlockchange();
p.blockchangeObject = pArgs;
p.Blockchange += EntryChange;
}

BlockID GetBlock(Player p, string name) {
if (name == "show") { ShowPortals(p); return Block.Invalid; }
BlockID block = Block.Parse(p, name);
if (block != Block.Invalid && p.level.Props[block].IsPortal) return block;

// Hardcoded aliases for backwards compatibility
block = Block.Invalid;
if (name.Length == 0) block = Block.Portal_Blue;
Expand All @@ -69,11 +69,16 @@ BlockID GetBlock(Player p, string name) {
if (name == "air") block = Block.Portal_Air;
if (name == "water") block = Block.Portal_Water;
if (name == "lava") block = Block.Portal_Lava;

if (p.level.Props[block].IsPortal) return block;
Help(p); return Block.Invalid;
}

static bool IsPortalBlock(Level lvl, Vec3U16 pos) {
BlockID block = lvl.GetBlock(pos.X, pos.Y, pos.Z);
return lvl.Props[block].IsPortal;
}

void EntryChange(Player p, ushort x, ushort y, ushort z, BlockID block) {
PortalArgs args = (PortalArgs)p.blockchangeObject;
BlockID old = p.level.GetBlock(x, y, z);
Expand Down Expand Up @@ -104,19 +109,19 @@ void EntryChange(Player p, ushort x, ushort y, ushort z, BlockID block) {
Block.GetName(p, Block.Red));
}
}

void ExitChange(Player p, ushort x, ushort y, ushort z, BlockID block) {
p.ClearBlockchange();
p.RevertBlock(x, y, z);

PortalArgs args = (PortalArgs)p.blockchangeObject;
string exitMap = p.level.name;

foreach (PortalPos P in args.Entries) {
string map = P.Map;
if (map == p.level.name) p.RevertBlock(P.x, P.y, P.z);
object locker = ThreadSafeCache.DBCache.GetLocker(map);

lock (locker) {
Portal.Set(map, P.x, P.y, P.z, x, y, z, exitMap);
}
Expand All @@ -133,67 +138,75 @@ void ExitChange(Player p, ushort x, ushort y, ushort z, BlockID block) {
class PortalArgs { public List<PortalPos> Entries; public BlockID Block; public bool Multi; }
struct PortalPos { public ushort x, y, z; public string Map; }


static void ShowPortals(Player p) {
p.showPortals = !p.showPortals;
List<Vec3U16> coords = Portal.GetAllCoords(p.level.MapName);

List<Vec3U16> exits = new List<Vec3U16>();

foreach (Vec3U16 pos in coords) {
PortalExit exit = Portal.Get(p.level.MapName, pos.X, pos.Y, pos.Z);
if (p.showPortals) {
p.SendBlockchange(pos.X, pos.Y, pos.Z, Block.Green);
bool exists = IsPortalBlock(p.Level, pos);
Vec3U16 exitPos = new Vec3U16(exit.X, exit.Y, exit.Z);
if (exists && !exits.Contains(exitPos)) exits.Add(exitPos);

// Show Entry
BlockID entryBlock = exists ? Block.Green : Block.Black;
p.SendBlockchange(pos.X, pos.Y, pos.Z, entryBlock);

// Show Exit
if (exit.Map != p.level.MapName) continue;
BlockID exitBlock = exists || exits.Contains(exitPos) ? Block.Red : Block.Black;
p.SendBlockchange(exit.X, exit.Y, exit.Z, exitBlock);
} else {
// Revert Entry
p.RevertBlock(pos.X, pos.Y, pos.Z);
}
}

List<PortalExit> exits = Portal.GetAllExits(p.level.MapName);
foreach (PortalExit exit in exits) {
if (exit.Map != p.level.MapName) continue;

if (p.showPortals) {
p.SendBlockchange(exit.X, exit.Y, exit.Z, Block.Red);
} else {

// Revert Exit
if (exit.Map != p.level.MapName) continue;
p.RevertBlock(exit.X, exit.Y, exit.Z);
}
}
p.Message("Now {0} &Sportals.",

p.Message("Now {0} &Sportals.",
p.showPortals ? "showing &a" + coords.Count : "hiding");
}


static string Format(BlockID block, Player p, BlockProps[] props) {
if (!props[block].IsPortal) return null;

// We want to use the simple aliases if possible
if (block == Block.Portal_Orange) return "orange";
if (block == Block.Portal_Blue) return "blue";
if (block == Block.Portal_Air) return "air";
if (block == Block.Portal_Lava) return "lava";
if (block == Block.Portal_Water) return "water";
if (block == Block.Portal_Water) return "water";
return Block.GetName(p, block);
}

static List<string> SupportedBlocks(Player p) {
List<string> names = new List<string>();
BlockProps[] props = p.IsSuper ? Block.Props : p.level.Props;

for (int i = 0; i < props.Length; i++) {
string name = Format((BlockID)i, p, props);
if (name != null) names.Add(name);
}
return names;
}

public override void Help(Player p) {
p.Message("&T/Portal [block]");
p.Message("&HPlace a block for the entry, then another block for exit.");
p.Message("&T/Portal [block] multi");
p.Message("&HPlace multiple blocks for entries, then a {0} block for exit.", Block.GetName(p, Block.Red));
p.Message("&H Note: The exit can be on a different level.");
List<string> names = SupportedBlocks(p);
List<string> names = SupportedBlocks(p);
p.Message("&H Supported blocks: &S{0}", names.Join());
p.Message("&T/Portal show &H- Shows portals (green = entry, red = exit)");
p.Message("&T/Portal show &H- Shows or hides portals on the map");
p.Message("&H (green = entry, red = exit, black = queued for removal)");
}
}
}

0 comments on commit 112c589

Please sign in to comment.