Skip to content

Commit

Permalink
Allow lb/gb copying multiple blocks at once (Thanks TropicalMemes)
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed May 2, 2020
1 parent d29bf82 commit 781c407
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 deletions MCGalaxy/Commands/CPE/CustomBlockCommand.cs
Expand Up @@ -125,6 +125,26 @@ internal static class CustomBlockCommand {
string path = Paths.MapBlockDefs(map);
return BlockDefinition.Load(path);
}

static bool DoCopy(Player p, bool global, string cmd, bool keepOrder,
BlockDefinition srcDef, BlockID src, BlockID dst) {
if (srcDef == null && src < Block.CpeCount) {
srcDef = DefaultSet.MakeCustomBlock(src);
}
if (srcDef == null) { MessageNoBlock(p, src, global, cmd); return false; }

BlockDefinition[] defs = global ? BlockDefinition.GlobalDefs : p.level.CustomBlockDefs;
BlockDefinition dstDef = defs[dst];
if (ExistsInScope(dstDef, dst, global)) { MessageAlreadyBlock(p, dst, global, cmd); return false; }

BlockProps props = global ? Block.Props[src] : p.level.Props[src];
dstDef = srcDef.Copy();
dstDef.SetBlock(dst);
if (!keepOrder) dstDef.InventoryOrder = -1;

AddBlock(p, dstDef, global, cmd, props);
return true;
}

static void CopyAllHandler(Player p, string[] parts, CommandData data, bool global, string cmd) {
if (parts.Length < 2) { Help(p, cmd); return; }
Expand All @@ -151,8 +171,8 @@ internal static class CustomBlockCommand {

static void CopyHandler(Player p, string[] parts, CommandData data, bool global, string cmd) {
if (parts.Length < 2) { Help(p, cmd); return; }
BlockID src, dst;
if (!CheckBlock(p, parts[1], out src, true)) return;
BlockID min, max, dst;
if (!CheckBlocks(p, parts[1], out min, out max, true)) return;

BlockDefinition[] defs = global ? BlockDefinition.GlobalDefs : p.level.CustomBlockDefs;
if (parts.Length > 2) {
Expand All @@ -172,30 +192,13 @@ internal static class CustomBlockCommand {
}
}

if (!DoCopy(p, global, cmd, false, defs[src], src, dst)) return;
string scope = global ? "global" : "level";
p.Message("Duplicated the {0} custom block with id \"{1}\" to \"{2}\".",
scope, Block.ToRaw(src), Block.ToRaw(dst));
}

static bool DoCopy(Player p, bool global, string cmd, bool keepOrder,
BlockDefinition srcDef, BlockID src, BlockID dst) {
if (srcDef == null && src < Block.CpeCount) {
srcDef = DefaultSet.MakeCustomBlock(src);
for (int i = min; i <= max && Block.ToRaw(dst) < Block.MaxRaw; i++, dst++) {
if (!DoCopy(p, global, cmd, false, defs[i], (BlockID)i, dst)) continue;
string scope = global ? "global" : "level";

p.Message("Duplicated the {0} custom block with id \"{1}\" to \"{2}\".",
scope, Block.ToRaw((BlockID)i), Block.ToRaw(dst));
}
if (srcDef == null) { MessageNoBlock(p, src, global, cmd); return false; }

BlockDefinition[] defs = global ? BlockDefinition.GlobalDefs : p.level.CustomBlockDefs;
BlockDefinition dstDef = defs[dst];
if (ExistsInScope(dstDef, dst, global)) { MessageAlreadyBlock(p, dst, global, cmd); return false; }

BlockProps props = global ? Block.Props[src] : p.level.Props[src];
dstDef = srcDef.Copy();
dstDef.SetBlock(dst);
if (!keepOrder) dstDef.InventoryOrder = -1;

AddBlock(p, dstDef, global, cmd, props);
return true;
}

static void DoInfo(Player p, BlockID block, bool global, string cmd) {
Expand Down Expand Up @@ -649,26 +652,26 @@ internal static class CustomBlockCommand {
return true;
}

static bool CheckBlock(Player p, string arg, out BlockID block, bool allowAir = false) {
static bool CheckBlock(Player p, string arg, out BlockID block, bool air = false) {
block = Block.Invalid;
BlockID raw = 0;
BlockID min = (BlockID)(allowAir ? 0 : 1);
BlockID min = (BlockID)(air ? 0 : 1);
BlockID max = Block.MaxRaw;
bool success = CommandParser.GetUShort(p, arg, "Block ID", ref raw, min, max);

block = Block.FromRaw(raw);
return success;
}

static bool CheckBlocks(Player p, string arg, out BlockID min, out BlockID max) {
static bool CheckBlocks(Player p, string arg, out BlockID min, out BlockID max, bool air = false) {
bool success;
// Either "[id]" or "[min]-[max]"
if (arg.IndexOf('-') == -1) {
success = CheckBlock(p, arg, out min);
success = CheckBlock(p, arg, out min, air);
max = min;
} else {
string[] bits = arg.Split(new char[] { '-' }, 2);
success = CheckBlock(p, bits[0], out min) & CheckBlock(p, bits[1], out max);
success = CheckBlock(p, bits[0], out min, air) & CheckBlock(p, bits[1], out max, air);
}
return success;
}
Expand Down

0 comments on commit 781c407

Please sign in to comment.