Skip to content

Commit b412bb9

Browse files
Initial support for custom blocks replacing core blocks.
1 parent b400a3b commit b412bb9

File tree

4 files changed

+31
-59
lines changed

4 files changed

+31
-59
lines changed

fCraft/Commands/BuildingCommands.cs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -765,22 +765,16 @@ private static void WallsHandler(Player player, CommandReader cmd)
765765
};
766766

767767
private static void DisPlace(Player player, CommandReader cmd) {
768-
Block block;
769768
if (cmd.CountRemaining < 2) {
770769
CdDisPlace.PrintUsage(player);
771770
return;
772-
}
773-
string tryBlock = cmd.Next();
774-
if (!Map.GetBlockByName(player.World, tryBlock, false, out block)) {
775-
player.Message("Invalid block name/id: {0}", tryBlock);
776-
return;
777-
}
771+
}
772+
773+
Block block;
774+
if (!cmd.NextBlock(player, false, out block)) return;
778775
int dis;
779-
string tryInt = cmd.Next();
780-
if (!int.TryParse(tryInt, out dis)) {
781-
player.Message("Invalid distance: {0}", tryInt);
782-
return;
783-
}
776+
if (!cmd.NextInt(out dis)) return;
777+
784778
dis = dis * 32;
785779
byte yaw = player.Position.R, pitch = player.Position.L;
786780
Vector3I Pos;
@@ -857,26 +851,16 @@ private static void PlaceHandler(Player player, CommandReader cmd) {
857851
Vector3I coords;
858852
int x, y, z;
859853
if (cmd.NextInt(out x) && cmd.NextInt(out y) && cmd.NextInt(out z)) {
860-
if (cmd.HasNext) {
861-
string last = cmd.Next();
862-
if (!Map.GetBlockByName(player.World, last, false, out block)) {
863-
player.Message("\"{0}\" is not a valid block type", last);
864-
return;
865-
}
866-
}
854+
if (cmd.HasNext && !cmd.NextBlock(player, false, out block)) return;
855+
867856
coords = new Vector3I(x, y, z);
868857
} else if (isConsole) {
869858
player.Message("Invalid coordinates!");
870859
return;
871860
} else {
872861
cmd.Rewind();
873-
if (cmd.HasNext) {
874-
string last = cmd.Next();
875-
if (!Map.GetBlockByName(player.World, last, false, out block)) {
876-
player.Message("\"{0}\" is not a valid block type", last);
877-
return;
878-
}
879-
}
862+
if (cmd.HasNext && !cmd.NextBlock(player, false, out block)) return;
863+
880864
coords = new Vector3I(player.Position.BlockX, player.Position.BlockY, (player.Position.Z - 64) / 32);
881865
}
882866
World world;

fCraft/Commands/CpeCommands.cs

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,15 +1369,11 @@ static void CustomBlockAddHandler(Player p, CommandReader cmd, bool global, Bloc
13691369
}
13701370

13711371
static void CustomBlockInfoHandler(Player p, CommandReader cmd, bool global, BlockDefinition[] defs) {
1372-
string input = cmd.Next() ?? "n/a";
13731372
string scope = global ? "global" : "level";
13741373
string name = global ? "/gb" : "/lb";
13751374

13761375
Block id;
1377-
if (!Map.GetBlockByName(p.World, input, false, out id) || id < Map.MaxCustomBlockType) {
1378-
p.Message("No blocks by that name or id!");
1379-
return;
1380-
}
1376+
if (!cmd.NextBlock(p, false, out id)) return;
13811377

13821378
BlockDefinition def = GetCustomBlock(global, defs, (byte)id);
13831379
if (def == null) {
@@ -1429,14 +1425,11 @@ static void CustomBlockListHandler(Player p, CommandReader cmd, bool global, Blo
14291425
}
14301426

14311427
static void CustomBlockRemoveHandler(Player p, CommandReader cmd, bool global, BlockDefinition[] defs) {
1432-
string input = cmd.Next() ?? "n/a";
14331428
Block blockID;
14341429
string scope = global ? "global" : "level";
14351430
string name = global ? "/gb" : "/lb";
1436-
if (!Map.GetBlockByName(p.World, input, false, out blockID) || blockID < Map.MaxCustomBlockType) {
1437-
p.Message("No blocks by that Name/ID!");
1438-
return;
1439-
}
1431+
if (!cmd.NextBlock(p, false, out blockID)) return;
1432+
14401433
BlockDefinition def = GetCustomBlock(global, defs, (byte)blockID);
14411434
if (def == null) {
14421435
p.Message("There is no {0} custom block with that name/id.", scope);
@@ -1664,22 +1657,20 @@ static void CustomBlockDefineHandler(Player p, string args, bool global, BlockDe
16641657
}
16651658

16661659
static void CustomBlockDuplicateHandler(Player p, CommandReader cmd, bool global, BlockDefinition[] defs) {
1667-
string input1 = cmd.Next() ?? "n/a", input2 = cmd.Next() ?? "n/a";
1668-
Block srcBlock = Block.None;
1669-
byte dstBlock = (byte)Block.None;
1660+
Block srcBlock, dstBlock;
16701661
string scope = global ? "global" : "level";
16711662
string name = global ? "/gb" : "/lb";
16721663

1673-
if (!Map.GetBlockByName(p.World, input1, false, out srcBlock) || srcBlock <= Map.MaxCustomBlockType) {
1674-
p.Message("There is no {1} custom block with the id or name: &a{0}", input1, scope);
1675-
p.Message("Use \"&h{1} list&S\" to see a list of {0} custom blocks.", scope, name);
1676-
return;
1677-
}
1678-
if (!Byte.TryParse(input2, out dstBlock) || dstBlock <= (byte)Map.MaxCustomBlockType) {
1679-
p.Message("Destination must be a numerical id and greater than 65."); return;
1664+
if (!cmd.NextBlock(p, false, out srcBlock)) return;
1665+
if (!cmd.NextBlock(p, false, out dstBlock)) return;
1666+
if (dstBlock == Block.Air || dstBlock == Block.None) {
1667+
p.Message("Destination block cannot have 0 or 255 ID."); return;
16801668
}
16811669

16821670
BlockDefinition srcDef = GetCustomBlock(global, defs, (byte)srcBlock);
1671+
if (srcDef == null && srcBlock <= Map.MaxCustomBlockType)
1672+
srcDef = DefaultSet.MakeCustomBlock(srcBlock);
1673+
16831674
if (srcDef == null) {
16841675
p.Message("There is no {1} custom block with the id: &a{0}", (byte)srcBlock, scope);
16851676
p.Message("Use \"&H{1} list&S\" to see a list of {0} custom blocks.", scope, name);
@@ -1706,16 +1697,13 @@ static void CustomBlockDuplicateHandler(Player p, CommandReader cmd, bool global
17061697
}
17071698

17081699
static void CustomBlockEditHandler(Player p, CommandReader cmd, bool global, BlockDefinition[] defs) {
1709-
string input = cmd.Next() ?? "n/a";
17101700
Block blockID;
1711-
if (!Map.GetBlockByName(p.World, input, false, out blockID) || blockID < Map.MaxCustomBlockType) {
1712-
p.Message("No blocks by that Name/ID!");
1713-
return;
1714-
}
1715-
1716-
BlockDefinition def = GetCustomBlock(global, defs, (byte)blockID);
17171701
string scope = global ? "global" : "level";
17181702
string name = global ? "/gb" : "/lb";
1703+
1704+
if (!cmd.NextBlock(p, false, out blockID)) return;
1705+
1706+
BlockDefinition def = GetCustomBlock(global, defs, (byte)blockID);
17191707
if (def == null) {
17201708
p.Message("There is no {0} custom block with that Name/ID", scope); return;
17211709
}
@@ -2017,8 +2005,8 @@ static bool CheckBlockId(Player p, CommandReader cmd, out int blockId) {
20172005
p.Message("Provided block id is not a number.");
20182006
return false;
20192007
}
2020-
if (blockId <= 65 || blockId >= 255) {
2021-
p.Message("Block id must be between 65-254");
2008+
if (blockId == 0 || blockId >= 255) {
2009+
p.Message("Block id must be between 1-254");
20222010
return false;
20232011
}
20242012
return true;

fCraft/Network/Player.Networking.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ void UpdateHeldBlock() {
373373

374374
// Holding an invalid block
375375
Block held = (Block)id;
376-
if (held > Block.StoneBrick && World.BlockDefs[id] == null) {
376+
if (held > Map.MaxCustomBlockType && World.BlockDefs[id] == null) {
377377
HeldBlock = Block.Stone; return;
378378
}
379379
if (HeldBlock == held) return;
@@ -1253,7 +1253,7 @@ internal void SendBlockPermissions() {
12531253

12541254
if (!supportsBlockDefs) return;
12551255
BlockDefinition[] defs = World.BlockDefs;
1256-
for (int i = (int)Map.MaxCustomBlockType + 1; i < defs.Length; i++) {
1256+
for (int i = (int)Block.Air + 1; i < defs.Length; i++) {
12571257
if (defs[i] == null) continue;
12581258
Send(Packet.MakeSetBlockPermission((Block)i, build, delete));
12591259
}

fCraft/World/BlockDefinition.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public static void Remove(BlockDefinition def, BlockDefinition[] defs, World wor
123123

124124
internal static void SendNowRemoveOldBlocks(Player p, World oldWorld) {
125125
BlockDefinition[] defs = oldWorld.BlockDefs;
126-
for (int i = (int)Map.MaxCustomBlockType + 1; i < defs.Length; i++) {
126+
for (int i = (int)Block.Air + 1; i < defs.Length; i++) {
127127
BlockDefinition def = defs[i];
128128
if (def == null || def == GlobalDefs[i]) continue;
129129
p.SendNow(Packet.MakeRemoveBlockDefinition((byte)i));
@@ -132,7 +132,7 @@ internal static void SendNowRemoveOldBlocks(Player p, World oldWorld) {
132132

133133
internal static void SendNowBlocks(Player p) {
134134
BlockDefinition[] defs = p.World.BlockDefs;
135-
for (int i = (int)Map.MaxCustomBlockType + 1; i < defs.Length; i++) {
135+
for (int i = (int)Block.Air + 1; i < defs.Length; i++) {
136136
BlockDefinition def = defs[i];
137137
if (def == null) continue;
138138
p.SendNow(GetPacket(p, def));

0 commit comments

Comments
 (0)