Skip to content

Commit 41c66a0

Browse files
Implement InventoryOrder CPE extension.
1 parent 48b6282 commit 41c66a0

9 files changed

Lines changed: 80 additions & 14 deletions

File tree

fCraft/Commands/CpeCommands.cs

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ static Dictionary<string, string> MakeHelpSections(string scope, string name) {
12661266
"&SCreates a new custom block, using all the data of the given existing " + scope + " custom block. " },
12671267
{ "edit", "&H" + name + " edit [id] [option] {args}&N" +
12681268
"&SEdits already defined blocks so you don't have to re-add them to change something. " +
1269-
"Options: Name, Solidity, Speed, AllId, TopId, SideID, BottomID, Light, Sound, FullBright, Shape, Draw, FogDensity, (FogHex or FogR, FogG, FogB), FallBack"},
1269+
"Options: Name, Solidity, Speed, AllId, TopId, SideID, BottomID, Light, Sound, FullBright, Shape, Draw, FogDensity, (FogHex or FogR, FogG, FogB), FallBack, Order"},
12701270
{ "reload", "&H" + name + " reload&N" +
12711271
"&SReloads all " + scope + " custom blocks from file. " },
12721272
{ "info", "&H" + name + " info [id]&N" +
@@ -1911,7 +1911,10 @@ static void CustomBlockEditHandler(Player p, CommandReader cmd, bool global, Blo
19111911
}
19121912
p.Message("&bChanged fallback block of &a{0}&b from &a{1}&b to &a{2}", def.Name, def.FallBack, newBlock.ToString());
19131913
def.FallBack = (byte)newBlock;
1914-
hasChanged = true;
1914+
1915+
DisplayEditMessage(p, def, global);
1916+
UpdateFallback();
1917+
return;
19151918
}
19161919
break;
19171920
case "min":
@@ -1961,28 +1964,55 @@ static void CustomBlockEditHandler(Player p, CommandReader cmd, bool global, Blo
19611964
def.Shape = value;
19621965
}
19631966
break;
1967+
case "order":
1968+
byte order = 0;
1969+
if (byte.TryParse(args, out order) && order > 0) {
1970+
def.InventoryOrder = order;
1971+
DisplayEditMessage(p, def, global);
1972+
UpdateOrder(p.World, def, global);
1973+
BlockDefinition.Save(global, p.World);
1974+
}
1975+
return;
19641976
default:
19651977
p.Message("Usage: &H" + name + " [type/value] {args}");
19661978
return;
19671979
}
1968-
if (!hasChanged) return;
19691980

1970-
if (global) {
1971-
Server.Message("{0} &Sedited a {3} custom block &a{1} &Swith ID &a{2}",
1972-
p.ClassyName, def.Name, def.BlockID, scope);
1981+
if (!hasChanged) return;
1982+
DisplayEditMessage(p, def, global);
1983+
BlockDefinition.Add(def, defs, p.World);
1984+
}
1985+
1986+
static void DisplayEditMessage(Player p, BlockDefinition def, bool global) {
1987+
if (global) {
1988+
Server.Message("{0} &Sedited a global custom block &a{1} &Swith ID &a{2}",
1989+
p.ClassyName, def.Name, def.BlockID);
19731990
} else {
1974-
p.World.Players.Message("{0} &Sedited a {3} custom block &a{1} &Swith ID &a{2}",
1975-
p.ClassyName, def.Name, def.BlockID, scope);
1991+
p.World.Players.Message("{0} &Sedited a level custom block &a{1} &Swith ID &a{2}",
1992+
p.ClassyName, def.Name, def.BlockID);
19761993
}
1977-
BlockDefinition.Add(def, defs, p.World);
1994+
}
1995+
1996+
static void UpdateOrder(World world, BlockDefinition def, bool global) {
1997+
if (def.InventoryOrder == -1) return;
1998+
byte id = def.BlockID, order = (byte)def.InventoryOrder;
19781999

1979-
foreach (Player pl in Server.Players) {
1980-
if (!p.Supports(CpeExt.BlockDefinitions) &&
1981-
(option.CaselessEquals("block") || option.CaselessEquals("fallback"))) {
1982-
p.JoinWorld(p.World, WorldChangeReason.Rejoin, p.Position);
1983-
}
2000+
foreach (Player player in Server.Players) {
2001+
if (!global && player.World != world) continue;
2002+
if (global && player.World.BlockDefs[id] != BlockDefinition.GlobalDefs[id]) continue;
2003+
2004+
if (!player.Supports(CpeExt.InventoryOrder)) continue;
2005+
player.Send(Packet.SetInventoryOrder(id, order));
19842006
}
19852007
}
2008+
2009+
static void UpdateFallback() {
2010+
foreach (Player player in Server.Players) {
2011+
if (!player.Supports(CpeExt.BlockDefinitions)) {
2012+
player.JoinWorld(player.World, WorldChangeReason.Rejoin, player.Position);
2013+
}
2014+
}
2015+
}
19862016

19872017
static byte EditCoord(Player p, string coord, string name,
19882018
string args, byte origValue, ref bool hasChanged) {

fCraft/Network/CpeConstants.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ public enum CpeExt
101101

102102
/// <summary> Allows both client and server to measure average ping. </summary>
103103
TwoWayPing,
104+
105+
/// <summary> Allows hiding blocks from and reordering blocks in the inventory. </summary>
106+
InventoryOrder,
104107
}
105108

106109

fCraft/Network/OpCode.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ public enum OpCode
139139

140140
/// <summary> This extension allows both client and server to measure average ping. </summary>
141141
TwoWayPing = 43,
142+
143+
/// <summary> This extension allows hiding blocks from and reordering blocks in the inventory. </summary>
144+
SetInventoryOrder = 44,
142145
}
143146
}
144147

fCraft/Network/Packet.CPE.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,5 +359,13 @@ public static Packet MakeTwoWayPing( bool serverToClient, ushort data ) {
359359
WriteU16( data, packet.Bytes, 2 );
360360
return packet;
361361
}
362+
363+
[Pure]
364+
public static Packet SetInventoryOrder( byte block, byte position ) {
365+
Packet packet = new Packet( OpCode.SetInventoryOrder );
366+
packet.Bytes[1] = block;
367+
packet.Bytes[2] = position;
368+
return packet;
369+
}
362370
}
363371
}

fCraft/Network/Packet.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ internal static void WriteI32( int number, [NotNull] byte[] arr, int offset ) {
306306
6, // SetEnvMapProperty
307307
7, // SetEntityProperty
308308
4, // TwoWayPing
309+
3, // SetInventoryOrder
309310
};
310311
}
311312

fCraft/Network/Player.Handshake.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ bool NegotiateProtocolExtension() {
372372
case TwoWayPingExtName:
373373
if (version == 1) ext = CpeExt.TwoWayPing;
374374
break;
375+
case InventoryOrderExtName:
376+
if (version == 1) ext = CpeExt.InventoryOrder;
377+
break;
375378
}
376379
if (ext != CpeExt.None)
377380
supportedExts.Add(ext);

fCraft/Network/Player.Networking.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,10 @@ internal bool JoinWorldNow([NotNull] World newWorld, bool doUseWorldSpawn, World
11161116
BlockDefinition.SendNowRemoveOldBlocks(this, oldWorld);
11171117
}
11181118
BlockDefinition.SendNowBlocks(this);
1119+
1120+
if (Supports(CpeExt.InventoryOrder)) {
1121+
BlockDefinition.SendNowInventoryOrder(this);
1122+
}
11191123
}
11201124

11211125
// enable Nagle's algorithm (in case it was turned off by LowLatencyMode)

fCraft/Player/Player.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,6 +2144,7 @@ public void ResetIdleTimer()
21442144
const string ExtPlayerPositionsExtName = "ExtEntityPositions";
21452145
const string EntityPropertyExtName = "EntityProperty";
21462146
const string TwoWayPingExtName = "TwoWayPing";
2147+
const string InventoryOrderExtName = "InventoryOrder";
21472148

21482149
bool supportsBlockDefs, supportsCustomBlocks;
21492150
internal bool supportsExtPositions;

fCraft/World/BlockDefinition.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public sealed class BlockDefinition {
4040
public byte FrontTex { get; set; }
4141
public byte BackTex { get; set; }
4242

43+
int order = -1;
44+
public int InventoryOrder { get { return order; } set { order = value; } }
45+
4346
/// <summary> Name used in commands. (e.g. "Mossy Slabs" becomes "mossyslabs")"</summary>
4447
public string BlockName;
4548

@@ -133,6 +136,16 @@ internal static void SendNowBlocks(Player p) {
133136
}
134137
}
135138

139+
internal static void SendNowInventoryOrder(Player p) {
140+
BlockDefinition[] defs = p.World.BlockDefs;
141+
for (int i = (int)Block.Air + 1; i < defs.Length; i++) {
142+
BlockDefinition def = defs[i];
143+
if (def != null && def.InventoryOrder >= 0) {
144+
p.SendNow(Packet.SetInventoryOrder((byte)i, (byte)def.InventoryOrder));
145+
}
146+
}
147+
}
148+
136149
public static void SendAdd(Player p, BlockDefinition def) {
137150
p.Send(GetPacket(p, def));
138151
if (!p.Supports(CpeExt.BlockPermissions)) return;

0 commit comments

Comments
 (0)