Warning - may still have bugs. Finished per-level BlockDefinitions implementation.

This commit is contained in:
UnknownShadow200 2016-01-26 23:58:24 +11:00
parent b40f352c12
commit 0deb3b3dd3
14 changed files with 79 additions and 28 deletions

View File

@ -149,6 +149,7 @@ namespace MCGalaxy
all.Add(new CmdLoginMessage());
all.Add(new CmdLogoutMessage());
all.Add(new CmdLowlag());
all.Add(new CmdLevelBlock());
all.Add(new CmdMain());
all.Add(new CmdMap());
all.Add(new CmdMapInfo());

View File

@ -41,7 +41,7 @@ namespace MCGalaxy.Commands
Player.GlobalDespawn(p, true);
p.SendUserMOTD();
p.SendMap();
p.SendMap(p.level);
if (!p.hidden) {
Player.GlobalDespawn(p, false);

View File

@ -48,8 +48,8 @@ namespace MCGalaxy.Commands
Player.SendMessage(p, playerNames);
playerDb.Dispose();
System.Math.Sign(System.Convert.ToInt32(0));
}
public override void Help(Player p)
{
p.SendMessage("/whoip <ip address> - Displays players associated with a given IP address.");

View File

@ -61,6 +61,11 @@ namespace MCGalaxy.Commands
File.Move("levels/level properties/" + foundLevel.name, "levels/level properties/" + newName + ".properties");
}
catch { }
try {
if (File.Exists("blockdefs/lvl_" + foundLevel.name + ".json"))
File.Move("blockdefs/lvl_" + foundLevel.name + ".json", "blockdefs/lvl_" + newName + ".json");
} catch {}
//Move and rename backups
try

View File

@ -72,7 +72,7 @@ namespace MCGalaxy.Commands {
ushort x = who.pos[0], y = who.pos[1], z = who.pos[2];
Player.GlobalDespawn(who, true);
who.SendUserMOTD(); who.SendMap();
who.SendUserMOTD(); who.SendMap(who.level);
if (!who.hidden)
Player.GlobalSpawn(who, x, y, z, who.level.rotx, who.level.roty, true);

View File

@ -60,7 +60,8 @@ namespace MCGalaxy.Commands
try {
File.Copy("levels/" + msg1 + ".lvl", "levels/" + msg2 + ".lvl");
File.Copy("levels/level properties/" + msg1 + ".properties", "levels/level properties/" + msg1 + ".properties", false);
if (File.Exists("blockdefs/lvl_" + msg1 + ".json"))
File.Copy("blockdefs/lvl_" + msg1 + ".json", "blockdefs/lvl_" + msg2 + ".json");
} catch (System.IO.FileNotFoundException) {
Player.SendMessage(p, msg2 + " does not exist!");
return;

View File

@ -71,6 +71,10 @@ namespace MCGalaxy.Commands {
try { File.Delete("levels/level properties/" + message + ".properties"); } catch { }
try { File.Delete("levels/level properties/" + message); } catch { }
try {
if (File.Exists("blockdefs/lvl_" + message + ".json"))
File.Delete("blockdefs/lvl_" + message + ".json");
} catch {}
//safe against SQL injections because the levelname (message) is first being checked if it exists
Database.executeQuery("DROP TABLE `Block" + message + "`");

View File

@ -58,7 +58,8 @@ namespace MCGalaxy.Commands
foreach (PlayerBot b in PlayerBot.playerbots) if (p.level == b.level) p.SendDespawn(b.id);
Player.GlobalDespawn(p, true);
p.level = foundLevel; p.SendUserMOTD(); p.SendMap();
Level oldLevel = p.level;
p.level = foundLevel; p.SendUserMOTD(); p.SendMap(oldLevel);
GC.Collect();

View File

@ -63,9 +63,10 @@ namespace MCGalaxy.Commands
Player.GlobalDespawn(p, true);
Level oldLevel = p.level;
p.level = level;
p.SendMotd();
if (!p.SendRawMap(level))
if (!p.SendRawMap(oldLevel, level))
return;
ushort x = (ushort)((0.5 + level.spawnx) * 32);

View File

@ -66,9 +66,10 @@ namespace MCGalaxy.Commands {
int targetId;
if (parts.Length >= 2 ) {
string id = parts[1];
if (!CheckBlockId(p, id, out targetId)) return;
if (!CheckBlockId(p, id, global, out targetId)) return;
BlockDefinition[] defs = global ? BlockDefinition.GlobalDefs : p.level.CustomBlockDefs;
BlockDefinition def = defs[targetId];
if (!global && def == BlockDefinition.GlobalDefs[targetId]) def = null;
if (def != null) {
Player.SendMessage(p, "There is already a custom block with the id " + id +
@ -100,10 +101,10 @@ namespace MCGalaxy.Commands {
if (parts.Length > 1) int.TryParse(parts[1], out offset);
BlockDefinition[] defs = global ? BlockDefinition.GlobalDefs : p.level.CustomBlockDefs;
string cmd = global ? "/gb" : "/lb";
for( int i = 1; i < 256; i++ ) {
BlockDefinition def = defs[i];
if (def == null) continue;
if (def == null) continue;
if (!global && def == BlockDefinition.GlobalDefs[i]) continue;
if (index >= offset) {
count++;
@ -122,15 +123,16 @@ namespace MCGalaxy.Commands {
void RemoveHandler(Player p, string[] parts, bool global) {
if (parts.Length <= 1) { Help(p); return; }
int blockID;
if (!CheckBlockId(p, parts[1], out blockID)) return;
int blockId;
if (!CheckBlockId(p, parts[1], global, out blockId)) return;
BlockDefinition[] defs = global ? BlockDefinition.GlobalDefs : p.level.CustomBlockDefs;
BlockDefinition def = defs[blockID];
BlockDefinition def = defs[blockId];
if (!global && def == BlockDefinition.GlobalDefs[blockId]) def = null;
if (def == null) { MessageNoBlock(p, global); return; }
BlockDefinition.Remove(def, defs, p == null ? null : p.level);
BlockDefinition globalDef = BlockDefinition.GlobalDefs[blockID];
BlockDefinition globalDef = BlockDefinition.GlobalDefs[blockId];
if (!global && globalDef != null) {
BlockDefinition.Add(globalDef, defs, p == null ? null : p.level);
}
@ -215,6 +217,7 @@ namespace MCGalaxy.Commands {
bd.FallBack = Block.Byte(value);
BlockDefinition[] defs = global ? BlockDefinition.GlobalDefs : p.level.CustomBlockDefs;
BlockDefinition def = defs[bd.BlockID];
if (!global && def == BlockDefinition.GlobalDefs[bd.BlockID]) def = null;
// in case the list is modified before we finish the command.
if (def != null) {
@ -223,6 +226,8 @@ namespace MCGalaxy.Commands {
string cmd = global ? "/gb" : "/lb";
Player.SendMessage(p, "There are no custom block ids left, " +
"you must " + cmd + " remove a custom block first.");
if (!global)
Player.SendMessage(p, "You may also manually specify the same existing id of a global custom block.");
return;
}
}
@ -248,7 +253,7 @@ namespace MCGalaxy.Commands {
return;
}
int blockId;
if (!CheckBlockId(p, parts[1], out blockId)) return;
if (!CheckBlockId(p, parts[1], global, out blockId)) return;
BlockDefinition[] defs = global ? BlockDefinition.GlobalDefs : p.level.CustomBlockDefs;
BlockDefinition def = defs[blockId];
if (def == null) { MessageNoBlock(p, global); return; }
@ -454,13 +459,16 @@ namespace MCGalaxy.Commands {
Player.SendMessage(p, help[i].Replace("Type", "Use"));
}
static bool CheckBlockId(Player p, string arg, out int blockId) {
static bool CheckBlockId(Player p, string arg, bool global, out int blockId) {
if (!int.TryParse(arg, out blockId)) {
Player.SendMessage(p, "Provided block id is not a number."); return false;
}
if (blockId <= 0 || blockId >= 255) {
Player.SendMessage(p, "Block id must be between 1-254"); return false;
}
if (!global && blockId < Block.CpeCount) {
Player.SendMessage(p, "You can only redefine standard blocks with /gb."); return false;
}
return true;
}

View File

@ -60,12 +60,12 @@ namespace MCGalaxy {
Save(true, null);
}
static BlockDefinition[] Load(bool global, Level lvl) {
internal static BlockDefinition[] Load(bool global, Level lvl) {
BlockDefinition[] defs = new BlockDefinition[256];
string path = global ? GlobalPath : "blockdefs/" + lvl.name;
string path = global ? GlobalPath : "blockdefs/lvl_" + lvl.name + ".json";
try {
if (File.Exists(GlobalPath)) {
string json = File.ReadAllText(GlobalPath);
if (File.Exists(path)) {
string json = File.ReadAllText(path);
defs = JsonConvert.DeserializeObject<BlockDefinition[]>(json);
}
} catch (Exception ex) {
@ -82,8 +82,16 @@ namespace MCGalaxy {
static void Save(bool global, Level lvl) {
BlockDefinition[] defs = global ? GlobalDefs : lvl.CustomBlockDefs;
// We don't want to save global blocks in the level's custom blocks list
if (!global) {
BlockDefinition[] realDefs = new BlockDefinition[256];
for (int i = 0; i < 256; i++)
realDefs[i] = defs[i] == GlobalDefs[i] ? null : defs[i];
defs = realDefs;
}
string json = JsonConvert.SerializeObject(defs);
string path = global ? GlobalPath : "blockdefs/" + lvl.name;
string path = global ? GlobalPath : "blockdefs/lvl_" + lvl.name + ".json";
File.WriteAllText(path, json);
}
@ -127,6 +135,8 @@ namespace MCGalaxy {
foreach (Player pl in Player.players) {
if (!global && pl.level != level) continue;
if (global && pl.level.CustomBlockDefs[id] != null) continue;
if (pl.HasCpeExt(CpeExt.BlockDefinitions))
pl.SendRaw(Opcode.CpeRemoveBlockDefinition, id);
}

View File

@ -554,7 +554,7 @@ namespace MCGalaxy
{
try
{
Level level = LvlFile.Load(givenName,path);
Level level = LvlFile.Load(givenName, path);
level.permissionbuild = LevelPermission.Builder;
level.setPhysics(phys);
//level.textures = new LevelTextures(level);
@ -631,6 +631,12 @@ namespace MCGalaxy
} catch (Exception e) {
Server.ErrorLog(e);
}
BlockDefinition[] defs = BlockDefinition.Load(false, level);
for (int i = 0; i < defs.Length; i++) {
if (defs[i] == null) continue;
level.CustomBlockDefs[i] = defs[i];
}
Server.s.Log(string.Format("Level \"{0}\" loaded.", level.name));
if (LevelLoaded != null)

View File

@ -292,9 +292,9 @@ namespace MCGalaxy {
SendRaw(Opcode.Handshake, buffer);
}
public void SendMap() { SendRawMap(level); }
public void SendMap(Level oldLevel) { SendRawMap(oldLevel, level); }
public bool SendRawMap(Level level) {
public bool SendRawMap(Level oldLevel, Level level) {
if ( level.blocks == null ) return false;
bool success = true;
bool hasBlockDefinitions = HasCpeExt(CpeExt.BlockDefinitions);
@ -361,8 +361,12 @@ namespace MCGalaxy {
SendCurrentEnvColors();
if (HasCpeExt(CpeExt.EnvMapAppearance) || HasCpeExt(CpeExt.EnvMapAppearance, 2))
SendCurrentMapAppearance();
if (HasCpeExt(CpeExt.BlockDefinitions))
BlockDefinition.SendLevelCustomBlocks(this);
if (HasCpeExt(CpeExt.BlockDefinitions)) {
if (oldLevel != null && oldLevel != level)
RemoveOldLevelCustomBlocks(oldLevel);
BlockDefinition.SendLevelCustomBlocks(this);
}
if ( OnSendMap != null )
OnSendMap(this, buffer);
if (!level.guns)
@ -380,7 +384,16 @@ namespace MCGalaxy {
if (HasCpeExt(CpeExt.BlockPermissions))
SendCurrentBlockPermissions();
return success;
}
}
void RemoveOldLevelCustomBlocks(Level oldLevel) {
BlockDefinition[] defs = oldLevel.CustomBlockDefs;
for (int i = Block.CpeCount; i < 256; i++) {
BlockDefinition def = defs[i];
if (def == null || def == BlockDefinition.GlobalDefs[i]) continue;
SendRaw(Opcode.CpeRemoveBlockDefinition, (byte)i);
}
}
public void SendSpawn(byte id, string name, ushort x, ushort y, ushort z, byte rotx, byte roty) {
byte[] buffer = new byte[74];

View File

@ -765,7 +765,7 @@ namespace MCGalaxy {
void CompleteLoginProcess() {
try {
SendMotd();
SendMap();
SendMap(null);
if (disconnected) return;
loggedIn = true;
@ -1057,7 +1057,8 @@ namespace MCGalaxy {
if (type >= Block.CpeCount) {
if (!HasCpeExt(CpeExt.BlockDefinitions) || level.CustomBlockDefs[type] == null) {
SendMessage("Invalid block type: " + type); return;
SendMessage("Invalid block type: " + type);
RevertBlock(x, y, z); return;
}
extType = type;
type = Block.custom_block;