Add lb/gb edit order to change inventory position, fix a few lb/gb edit causing KeyNotFound when given invalid values.

This commit is contained in:
UnknownShadow200 2017-08-15 18:04:59 +10:00
parent 97e009e6f8
commit 0441503bd4
3 changed files with 64 additions and 22 deletions

View File

@ -43,6 +43,8 @@ namespace MCGalaxy {
public bool Version2; public bool Version2;
public byte LeftTex, RightTex, FrontTex, BackTex; public byte LeftTex, RightTex, FrontTex, BackTex;
public int InventoryOrder = -1;
public const string GlobalPath = "blockdefs/global.json", GlobalBackupPath = "blockdefs/global.json.bak"; public const string GlobalPath = "blockdefs/global.json", GlobalBackupPath = "blockdefs/global.json.bak";
public static BlockDefinition[] GlobalDefs; public static BlockDefinition[] GlobalDefs;
@ -209,6 +211,20 @@ namespace MCGalaxy {
Save(global, level); Save(global, level);
} }
public static void UpdateOrder(BlockDefinition def, bool global, Level level) {
if (def.InventoryOrder == -1) return;
byte raw = def.BlockID, order = (byte)def.InventoryOrder;
Player[] players = PlayerInfo.Online.Items;
foreach (Player pl in players) {
if (!global && pl.level != level) continue;
if (global && pl.level.CustomBlockDefs[raw] != GlobalDefs[raw]) continue;
if (!pl.HasCpeExt(CpeExt.InventoryOrder)) continue;
pl.Send(Packet.SetInventoryOrder(raw, order));
}
}
static void UpdateGlobalCustom(byte raw, BlockDefinition def) { static void UpdateGlobalCustom(byte raw, BlockDefinition def) {
Level[] loaded = LevelInfo.Loaded.Items; Level[] loaded = LevelInfo.Loaded.Items;
foreach (Level lvl in loaded) { foreach (Level lvl in loaded) {
@ -249,9 +265,16 @@ namespace MCGalaxy {
internal static void SendLevelCustomBlocks(Player pl) { internal static void SendLevelCustomBlocks(Player pl) {
BlockDefinition[] defs = pl.level.CustomBlockDefs; BlockDefinition[] defs = pl.level.CustomBlockDefs;
bool supportsOrder = pl.HasCpeExt(CpeExt.InventoryOrder);
for (int i = 1; i < defs.Length; i++) { for (int i = 1; i < defs.Length; i++) {
BlockDefinition def = defs[i]; BlockDefinition def = defs[i];
if (def != null) pl.Send(def.MakeDefinePacket(pl)); if (def == null) continue;
pl.Send(def.MakeDefinePacket(pl));
if (def.InventoryOrder >= 0 && supportsOrder) {
pl.Send(Packet.SetInventoryOrder((byte)i, (byte)def.InventoryOrder));
}
} }
} }

View File

@ -320,83 +320,84 @@ namespace MCGalaxy.Commands.CPE {
string value = parts[3], blockName = def.Name; string value = parts[3], blockName = def.Name;
float fTemp; float fTemp;
bool temp = false, changedFallback = false; bool temp = false, changedFallback = false;
Level level = p == null ? null : p.level;
string arg = MapPropertyName(parts[2].ToLower()); string arg = MapPropertyName(parts[2].ToLower());
switch (arg) { switch (arg) {
case "name": case "name":
def.Name = value; break; def.Name = value; break;
case "collide": case "collide":
if (!EditByte(p, value, "Collide type", ref def.CollideType, "collide", 0, 6)) return; if (!EditByte(p, value, "Collide type", ref def.CollideType, arg, 0, 6)) return;
break; break;
case "speed": case "speed":
if (!Utils.TryParseDecimal(value, out fTemp) || fTemp < 0.25f || fTemp > 3.96f) { if (!Utils.TryParseDecimal(value, out fTemp) || fTemp < 0.25f || fTemp > 3.96f) {
SendEditHelp(p, "speed"); return; SendEditHelp(p, arg); return;
} }
def.Speed = fTemp; break; def.Speed = fTemp; break;
case "toptex": case "toptex":
if (!EditByte(p, value, "Top texture", ref def.TopTex, "top")) return; if (!EditByte(p, value, "Top texture", ref def.TopTex, arg)) return;
break; break;
case "alltex": case "alltex":
if (!EditByte(p, value, "All textures", ref def.SideTex, "all")) return; if (!EditByte(p, value, "All textures", ref def.SideTex, arg)) return;
def.SetAllTex(def.SideTex); def.SetAllTex(def.SideTex);
break; break;
case "sidetex": case "sidetex":
if (!EditByte(p, value, "Side texture", ref def.SideTex, "sides")) return; if (!EditByte(p, value, "Side texture", ref def.SideTex, arg)) return;
def.SetSideTex(def.SideTex); def.SetSideTex(def.SideTex);
break; break;
case "lefttex": case "lefttex":
if (!EditByte(p, value, "Left texture", ref def.LeftTex, "left")) return; if (!EditByte(p, value, "Left texture", ref def.LeftTex, arg)) return;
break; break;
case "righttex": case "righttex":
if (!EditByte(p, value, "Right texture", ref def.RightTex, "right")) return; if (!EditByte(p, value, "Right texture", ref def.RightTex, arg)) return;
break; break;
case "fronttex": case "fronttex":
if (!EditByte(p, value, "Front texture", ref def.FrontTex, "front")) return; if (!EditByte(p, value, "Front texture", ref def.FrontTex, arg)) return;
break; break;
case "backtex": case "backtex":
if (!EditByte(p, value, "Back texture", ref def.BackTex, "back")) return; if (!EditByte(p, value, "Back texture", ref def.BackTex, arg)) return;
break; break;
case "bottomtex": case "bottomtex":
if (!EditByte(p, value, "Bottom texture", ref def.BottomTex, "bottom")) return; if (!EditByte(p, value, "Bottom texture", ref def.BottomTex, arg)) return;
break; break;
case "blockslight": case "blockslight":
if (!CommandParser.GetBool(p, value, ref temp)) { if (!CommandParser.GetBool(p, value, ref temp)) {
SendEditHelp(p, "shadow"); return; SendEditHelp(p, arg); return;
} }
def.BlocksLight = temp; def.BlocksLight = temp;
break; break;
case "sound": case "sound":
if (!EditByte(p, value, "Walk sound", ref def.WalkSound, "sound", 0, 11)) return; if (!EditByte(p, value, "Walk sound", ref def.WalkSound, arg, 0, 11)) return;
break; break;
case "fullbright": case "fullbright":
if (!CommandParser.GetBool(p, value, ref temp)) { if (!CommandParser.GetBool(p, value, ref temp)) {
SendEditHelp(p, "bright"); return; SendEditHelp(p, arg); return;
} }
def.FullBright = temp; def.FullBright = temp;
break; break;
case "shape": case "shape":
if (!CommandParser.GetBool(p, value, ref temp)) { if (!CommandParser.GetBool(p, value, ref temp)) {
SendEditHelp(p,"shape"); return; SendEditHelp(p, arg); return;
} }
def.Shape = temp ? (byte)0 : def.MaxZ; def.Shape = temp ? (byte)0 : def.MaxZ;
break; break;
case "blockdraw": case "blockdraw":
if (!EditByte(p, value, "Block draw", ref def.BlockDraw, "blockdraw", 0, 255)) return; if (!EditByte(p, value, "Block draw", ref def.BlockDraw, arg, 0, 255)) return;
break; break;
case "min": case "min":
if (!ParseCoords(p, value, ref def.MinX, ref def.MinY, ref def.MinZ)) { if (!ParseCoords(p, value, ref def.MinX, ref def.MinY, ref def.MinZ)) {
SendEditHelp(p, "min"); return; SendEditHelp(p, arg); return;
} break; } break;
case "max": case "max":
if (!ParseCoords(p, value, ref def.MaxX, ref def.MaxY, ref def.MaxZ)) { if (!ParseCoords(p, value, ref def.MaxX, ref def.MaxY, ref def.MaxZ)) {
SendEditHelp(p, "max"); return; SendEditHelp(p, arg); return;
} break; } break;
case "fogdensity": case "fogdensity":
if (!EditByte(p, value, "Fog density", ref def.FogDensity, "fogdensity")) return; if (!EditByte(p, value, "Fog density", ref def.FogDensity, arg)) return;
break; break;
case "fogcolor": case "fogcolor":
ColorDesc rgb = default(ColorDesc); ColorDesc rgb = default(ColorDesc);
@ -410,14 +411,27 @@ namespace MCGalaxy.Commands.CPE {
value = Block.Name(fallback); value = Block.Name(fallback);
def.FallBack = fallback; break; def.FallBack = fallback; break;
case "order":
int order = 0;
if (!CommandParser.GetInt(p, value, "Inventory order", ref order, 1, 256)) {
SendEditHelp(p, arg); return;
}
def.InventoryOrder = order - 1;
BlockDefinition.UpdateOrder(def, global, level);
BlockDefinition.Save(global, level);
Player.Message(p, "Set inventory order for {0} to {1}", blockName, value);
return;
default: default:
Player.Message(p, "Unrecognised property: " + arg); return; Player.Message(p, "Unrecognised property: " + arg); return;
} }
Player.Message(p, "Set {0} for {1} to {2}", arg, blockName, value); Player.Message(p, "Set {0} for {1} to {2}", arg, blockName, value);
BlockDefinition.Add(def, defs, p == null ? null : p.level); BlockDefinition.Add(def, defs, level);
if (changedFallback) if (changedFallback) {
BlockDefinition.UpdateFallback(global, def.BlockID, p == null ? null : p.level); BlockDefinition.UpdateFallback(global, def.BlockID, level);
}
} }
@ -684,6 +698,10 @@ namespace MCGalaxy.Commands.CPE {
{ "fallback", new string[] { "Enter the fallback block (Block shown to players who can't see custom blocks).", { "fallback", new string[] { "Enter the fallback block (Block shown to players who can't see custom blocks).",
"You can use any block name or block ID from the normal blocks." } "You can use any block name or block ID from the normal blocks." }
}, },
{ "order", new string[] { "Enter the position/order of this block in the inventory.",
"The default position of a block is its ID.",
"A position of 255 hides the block from the inventory." }
},
}; };

View File

@ -91,6 +91,7 @@ namespace MCGalaxy {
} }
} }
/// <summary> Returns whether this player's client supports the given CPE extension. </summary>
public bool HasCpeExt(string extName, int version = 1) { public bool HasCpeExt(string extName, int version = 1) {
if (!hasCpe) return false; if (!hasCpe) return false;
ExtEntry ext = FindExtension(extName); ExtEntry ext = FindExtension(extName);