Fix saving block props saving blocks from other scopes.

e.g. level custom block props also saving global custom block props, global custom block props also saving core block props
This commit is contained in:
UnknownShadow200 2017-06-12 13:51:18 +10:00
parent 286a2cc0bb
commit 9d87cb06c0
7 changed files with 29 additions and 16 deletions

View File

@ -63,7 +63,7 @@ namespace MCGalaxy.Gui {
}
}
BlockProps.Save("core", Block.Props, true);
BlockProps.Save("core", Block.Props, i => true);
BlockPerms.Save();
Block.SetBlocks();
LoadBlocks();

View File

@ -77,7 +77,7 @@ namespace MCGalaxy.Blocks {
}
public static void Save(string group, BlockProps[] scope, bool custom) {
public static void Save(string group, BlockProps[] scope, Predicate<int> selector) {
if (!Directory.Exists("blockprops"))
Directory.CreateDirectory("blockprops");
@ -86,11 +86,8 @@ namespace MCGalaxy.Blocks {
w.WriteLine("# id : Is rails : Is tdoor : Is door : Is message block : Is portal : " +
"Killed by water : Killed by lava : Kills players : death message : Animal AI type");
for (int i = 0; i < scope.Length; i++) {
if (!scope[i].Changed) continue;
if (!scope[i].Changed || !selector(i)) continue;
BlockProps props = scope[i];
// Don't save physics blocks
if (custom && (i > Block.CpeCount && i < Block.Count)) continue;
// Convert ext to raw ids
int id = i >= Block.Count ? (i - Block.Count) : i;
@ -121,7 +118,7 @@ namespace MCGalaxy.Blocks {
byte raw;
if (!Byte.TryParse(parts[0], out raw)) {
Server.s.Log("Invalid line \"" + line + "\" in " + group + " block properties");
continue;
continue;
}
int idx = raw;
if (custom && raw >= Block.CpeCount) idx += Block.Count;

View File

@ -66,6 +66,12 @@ namespace MCGalaxy {
return new ExtBlock(Block.custom_block, raw);
}
public static ExtBlock FromIndex(int index) {
if (index < Block.Count)
return new ExtBlock((byte)index, 0);
return new ExtBlock(Block.custom_block, (byte)(index - Block.Count));
}
/// <summary> Constructs an extended block. </summary>
public static explicit operator ExtBlock(byte block) { return new ExtBlock(block, 0); }

View File

@ -176,7 +176,7 @@ namespace MCGalaxy.Commands.World {
scope[block.Index].Changed = true;
if (scope == Block.Props) {
BlockProps.Save("core", scope, true);
BlockProps.Save("core", scope, i=> true);
Level[] loaded = LevelInfo.Loaded.Items;
foreach (Level lvl in loaded) {
@ -185,7 +185,7 @@ namespace MCGalaxy.Commands.World {
}
} else if (scope == BlockDefinition.GlobalProps) {
Level[] loaded = LevelInfo.Loaded.Items;
BlockProps.Save("global", scope, false);
BlockProps.Save("global", scope, SelectGlobal);
byte raw = block.RawID;
foreach (Level lvl in loaded) {
@ -194,11 +194,22 @@ namespace MCGalaxy.Commands.World {
lvl.SetBlockHandler(block);
}
} else {
BlockProps.Save("lvl_" + level.name, scope, false);
BlockProps.Save("lvl_" + level.name, scope, i => SelectLevel(level, i));
level.SetBlockHandler(block);
}
}
static bool SelectGlobal(int i) {
ExtBlock block = ExtBlock.FromIndex(i);
return !block.IsPhysicsType && BlockDefinition.GlobalDefs[block.RawID] != null;
}
static bool SelectLevel(Level lvl, int i) {
ExtBlock block = ExtBlock.FromIndex(i);
return !block.IsPhysicsType &&
lvl.CustomBlockDefs[block.RawID] != BlockDefinition.GlobalDefs[block.RawID];
}
static string BlockName(BlockProps[] scope, Level lvl, ExtBlock block) {
if (scope == Block.Props) return Block.Name(block.RawID);
BlockDefinition def = null;

View File

@ -205,7 +205,7 @@ namespace MCGalaxy.Commands.Building {
static void GetCoreNames(List<string> names, Level lvl) {
BlockProps[] props = lvl == null ? lvl.BlockProps : Block.Props;
for (int i = Block.air; i < Block.Count; i++) {
ExtBlock block = new ExtBlock((byte)i, 0);
ExtBlock block = ExtBlock.FromIndex(i);
if (block.BlockID == Block.custom_block) continue;
string name = Format(block, lvl, props);

View File

@ -207,7 +207,7 @@ namespace MCGalaxy.Commands.Building {
static void GetCoreNames(List<string> names, Level lvl) {
BlockProps[] props = lvl == null ? lvl.BlockProps : Block.Props;
for (int i = Block.air; i < Block.Count; i++) {
ExtBlock block = new ExtBlock((byte)i, 0);
ExtBlock block = ExtBlock.FromIndex(i);
if (block.BlockID == Block.custom_block) continue;
string name = Format(block, lvl, props);

View File

@ -504,9 +504,8 @@ namespace MCGalaxy {
}
public void SetBlockHandlers() {
for (int i = 0; i < Block.Count; i++) {
SetBlockHandler(new ExtBlock((byte)i, 0));
SetBlockHandler(new ExtBlock(Block.custom_block, (byte)i));
for (int i = 0; i < BlockProps.Length; i++) {
SetBlockHandler(ExtBlock.FromIndex(i));
}
}
@ -515,7 +514,7 @@ namespace MCGalaxy {
if (GetBlockDef(block) == null) {
nonSolid = Block.Walkthrough(Block.Convert(block.BlockID));
} else {
nonSolid = CustomBlockDefs[block.BlockID].CollideType != CollideType.Solid;
nonSolid = CustomBlockDefs[block.RawID].CollideType != CollideType.Solid;
}
int i = block.Index;