Skip to content
Permalink
Browse files

Fix global custom blocks breaking default block names

  • Loading branch information...
UnknownShadow200 committed Aug 12, 2017
1 parent b412bb9 commit f97ab94c51f5f2169e2b277cc1239c67a18f82e1
Showing with 19 additions and 38 deletions.
  1. +4 −4 fCraft/Commands/CpeCommands.cs
  2. +2 −8 fCraft/World/BlockDefinition.cs
  3. +13 −26 fCraft/World/Map.cs
@@ -377,7 +377,7 @@ static class CpeCommands {
byte blockId;
Block block;
if (byte.TryParse(model, out blockId)) {
} else if (Map.GetBlockByName(model, false, out block)) {
} else if (Map.GetBlockByName(player.World, model, false, out block)) {
model = ((byte)block).ToString();
}

@@ -1461,7 +1461,7 @@ static class CpeCommands {

switch (step) {
case 0:
step++; def.Name = args; def.BlockName = args.ToLower().Replace(" ", "");
step++; def.Name = args;
p.Message(" &bSet name to: " + def.Name);
break;
case 1:
@@ -1630,7 +1630,7 @@ static class CpeCommands {
break;
default:
Block block;
if (Map.GetBlockByName(args, false, out block)) {
if (Map.GetBlockByName(p.World, args, false, out block)) {
if (block > Map.MaxCustomBlockType) {
p.Message("&cThe fallback block must be an original block, " +
"or a block defined in the CustomBlocks extension.");
@@ -1903,7 +1903,7 @@ static class CpeCommands {
case "fallback":
case "block":
Block newBlock;
if (Map.GetBlockByName(args, false, out newBlock)) {
if (Map.GetBlockByName(p.World, args, false, out newBlock)) {
if (newBlock > Map.MaxCustomBlockType) {
p.Message("&cThe fallback block must be an original block, " +
"or a block defined in the CustomBlocks extension.");
@@ -73,15 +73,13 @@ public sealed class BlockDefinition {
public static void Add(BlockDefinition def, BlockDefinition[] defs, World world) {
byte id = def.BlockID;
bool global = defs == GlobalDefs;
def.BlockName = def.Name.ToLower().Replace(" ", "");

if (global) {
World[] worlds = WorldManager.Worlds;
foreach (World w in worlds) {
if (w.BlockDefs[id] == null) w.BlockDefs[id] = def;
}

string name = def.Name.ToLower().Replace(" ", "");
Map.BlockNames[name] = (Block)def.BlockID;
Map.BlockNames[def.BlockID.ToString()] = (Block)def.BlockID;
}
defs[id] = def;

@@ -103,10 +101,6 @@ public sealed class BlockDefinition {
foreach (World w in worlds) {
if (w.BlockDefs[id] == GlobalDefs[id]) w.BlockDefs[id] = null;
}

string name = def.Name.ToLower().Replace(" ", "");
Map.BlockNames.Remove(name);
Map.BlockNames.Remove(def.BlockID.ToString());
}
defs[id] = null;

@@ -609,7 +609,7 @@ public void ClearUpdateQueue()
}


internal static readonly Dictionary<string, Block> BlockNames = new Dictionary<string, Block>();
static readonly Dictionary<string, Block> BlockNames = new Dictionary<string, Block>();

static Map() {
// add default names for blocks, and their numeric codes
@@ -849,41 +849,28 @@ public void ClearUpdateQueue()
/// <summary> Finds Block corresponding to given blockName. </summary>
/// <param name="blockName"> Given block name to parse. </param>
/// <param name="allowNoneBlock"> Whether "none" block type is acceptable. </param>
/// <param name="block"> Block corresponding to given blockName;
/// Block.Undefined if value could not be parsed. </param>
/// <param name="block"> Block corresponding to given blockName; Block.None if value could not be parsed. </param>
/// <returns> True if given blockName was parsed as an acceptable block type. </returns>
/// <exception cref="ArgumentNullException"> blockName is null. </exception>
public static bool GetBlockByName([NotNull] string blockName, bool allowNoneBlock, out Block block)
{
if (blockName == null) throw new ArgumentNullException("blockName");
if (BlockNames.TryGetValue(blockName.ToLower(), out block)) {
return block == Block.None ? allowNoneBlock : true;
} else {
block = Block.None;
return false;
}
}

public static bool GetBlockByName([CanBeNull] World world, [NotNull] string blockName,
bool allowNoneBlock, out Block block) {
if (blockName == null) throw new ArgumentNullException("blockName");
if (BlockNames.TryGetValue(blockName.ToLower(), out block)) {
return block == Block.None ? allowNoneBlock : true;
}

BlockDefinition[] defs = world == null ? BlockDefinition.GlobalDefs : world.BlockDefs;
byte id;
if (Byte.TryParse(blockName, out id)) {
BlockDefinition def = defs[id];
if (def != null) { block = (Block)id; return true; }
} else {
BlockDefinition[] defs = world == null ?
BlockDefinition.GlobalDefs : world.BlockDefs;
byte id;
if (Byte.TryParse(blockName, out id)) {
BlockDefinition def = defs[id];
if (def != null) { block = (Block)id; return true; }
} else {
foreach (BlockDefinition def in defs) {
if (def == null || !def.BlockName.CaselessEquals(blockName)) continue;
block = (Block)def.BlockID; return true;
}
foreach (BlockDefinition def in defs) {
if (def == null || !def.BlockName.CaselessEquals(blockName)) continue;
block = (Block)def.BlockID; return true;
}
block = Block.None; return false;
}
block = Block.None; return false;
}

const int bufferSize = 64 * 1024;

0 comments on commit f97ab94

Please sign in to comment.
You can’t perform that action at this time.