Skip to content
Permalink
Browse files

Implement InventoryOrder CPE extension.

  • Loading branch information...
UnknownShadow200 committed Aug 22, 2017
1 parent 48b6282 commit 41c66a031452ef67556c6cc23e26fa648dcd6882
@@ -1266,7 +1266,7 @@ static class CpeCommands {
"&SCreates a new custom block, using all the data of the given existing " + scope + " custom block. " },
{ "edit", "&H" + name + " edit [id] [option] {args}&N" +
"&SEdits already defined blocks so you don't have to re-add them to change something. " +
"Options: Name, Solidity, Speed, AllId, TopId, SideID, BottomID, Light, Sound, FullBright, Shape, Draw, FogDensity, (FogHex or FogR, FogG, FogB), FallBack"},
"Options: Name, Solidity, Speed, AllId, TopId, SideID, BottomID, Light, Sound, FullBright, Shape, Draw, FogDensity, (FogHex or FogR, FogG, FogB), FallBack, Order"},
{ "reload", "&H" + name + " reload&N" +
"&SReloads all " + scope + " custom blocks from file. " },
{ "info", "&H" + name + " info [id]&N" +
@@ -1911,7 +1911,10 @@ static class CpeCommands {
}
p.Message("&bChanged fallback block of &a{0}&b from &a{1}&b to &a{2}", def.Name, def.FallBack, newBlock.ToString());
def.FallBack = (byte)newBlock;
hasChanged = true;

DisplayEditMessage(p, def, global);
UpdateFallback();
return;
}
break;
case "min":
@@ -1961,28 +1964,55 @@ static class CpeCommands {
def.Shape = value;
}
break;
case "order":
byte order = 0;
if (byte.TryParse(args, out order) && order > 0) {
def.InventoryOrder = order;
DisplayEditMessage(p, def, global);
UpdateOrder(p.World, def, global);
BlockDefinition.Save(global, p.World);
}
return;
default:
p.Message("Usage: &H" + name + " [type/value] {args}");
return;
}
if (!hasChanged) return;

if (global) {
Server.Message("{0} &Sedited a {3} custom block &a{1} &Swith ID &a{2}",
p.ClassyName, def.Name, def.BlockID, scope);
if (!hasChanged) return;
DisplayEditMessage(p, def, global);
BlockDefinition.Add(def, defs, p.World);
}

static void DisplayEditMessage(Player p, BlockDefinition def, bool global) {
if (global) {
Server.Message("{0} &Sedited a global custom block &a{1} &Swith ID &a{2}",
p.ClassyName, def.Name, def.BlockID);
} else {
p.World.Players.Message("{0} &Sedited a {3} custom block &a{1} &Swith ID &a{2}",
p.ClassyName, def.Name, def.BlockID, scope);
p.World.Players.Message("{0} &Sedited a level custom block &a{1} &Swith ID &a{2}",
p.ClassyName, def.Name, def.BlockID);
}
BlockDefinition.Add(def, defs, p.World);
}

static void UpdateOrder(World world, BlockDefinition def, bool global) {
if (def.InventoryOrder == -1) return;
byte id = def.BlockID, order = (byte)def.InventoryOrder;

foreach (Player pl in Server.Players) {
if (!p.Supports(CpeExt.BlockDefinitions) &&
(option.CaselessEquals("block") || option.CaselessEquals("fallback"))) {
p.JoinWorld(p.World, WorldChangeReason.Rejoin, p.Position);
}
foreach (Player player in Server.Players) {
if (!global && player.World != world) continue;
if (global && player.World.BlockDefs[id] != BlockDefinition.GlobalDefs[id]) continue;

if (!player.Supports(CpeExt.InventoryOrder)) continue;
player.Send(Packet.SetInventoryOrder(id, order));
}
}

static void UpdateFallback() {
foreach (Player player in Server.Players) {
if (!player.Supports(CpeExt.BlockDefinitions)) {
player.JoinWorld(player.World, WorldChangeReason.Rejoin, player.Position);
}
}
}

static byte EditCoord(Player p, string coord, string name,
string args, byte origValue, ref bool hasChanged) {
@@ -101,6 +101,9 @@ public enum CpeExt

/// <summary> Allows both client and server to measure average ping. </summary>
TwoWayPing,

/// <summary> Allows hiding blocks from and reordering blocks in the inventory. </summary>
InventoryOrder,
}


@@ -139,6 +139,9 @@ public enum OpCode

/// <summary> This extension allows both client and server to measure average ping. </summary>
TwoWayPing = 43,

/// <summary> This extension allows hiding blocks from and reordering blocks in the inventory. </summary>
SetInventoryOrder = 44,
}
}

@@ -359,5 +359,13 @@ public static Packet HackControl(bool flying, bool noclip, bool speedhack, bool
WriteU16( data, packet.Bytes, 2 );
return packet;
}

[Pure]
public static Packet SetInventoryOrder( byte block, byte position ) {
Packet packet = new Packet( OpCode.SetInventoryOrder );
packet.Bytes[1] = block;
packet.Bytes[2] = position;
return packet;
}
}
}
@@ -306,6 +306,7 @@ public partial struct Packet {
6, // SetEnvMapProperty
7, // SetEntityProperty
4, // TwoWayPing
3, // SetInventoryOrder
};
}

@@ -372,6 +372,9 @@ public class Players {
case TwoWayPingExtName:
if (version == 1) ext = CpeExt.TwoWayPing;
break;
case InventoryOrderExtName:
if (version == 1) ext = CpeExt.InventoryOrder;
break;
}
if (ext != CpeExt.None)
supportedExts.Add(ext);
@@ -1116,6 +1116,10 @@ bool LoginSequence()
BlockDefinition.SendNowRemoveOldBlocks(this, oldWorld);
}
BlockDefinition.SendNowBlocks(this);

if (Supports(CpeExt.InventoryOrder)) {
BlockDefinition.SendNowInventoryOrder(this);
}
}

// enable Nagle's algorithm (in case it was turned off by LowLatencyMode)
@@ -2144,6 +2144,7 @@ public void ResetIdleTimer()
const string ExtPlayerPositionsExtName = "ExtEntityPositions";
const string EntityPropertyExtName = "EntityProperty";
const string TwoWayPingExtName = "TwoWayPing";
const string InventoryOrderExtName = "InventoryOrder";

bool supportsBlockDefs, supportsCustomBlocks;
internal bool supportsExtPositions;
@@ -40,6 +40,9 @@ public sealed class BlockDefinition {
public byte FrontTex { get; set; }
public byte BackTex { get; set; }

int order = -1;
public int InventoryOrder { get { return order; } set { order = value; } }

/// <summary> Name used in commands. (e.g. "Mossy Slabs" becomes "mossyslabs")"</summary>
public string BlockName;

@@ -133,6 +136,16 @@ public sealed class BlockDefinition {
}
}

internal static void SendNowInventoryOrder(Player p) {
BlockDefinition[] defs = p.World.BlockDefs;
for (int i = (int)Block.Air + 1; i < defs.Length; i++) {
BlockDefinition def = defs[i];
if (def != null && def.InventoryOrder >= 0) {
p.SendNow(Packet.SetInventoryOrder((byte)i, (byte)def.InventoryOrder));
}
}
}

public static void SendAdd(Player p, BlockDefinition def) {
p.Send(GetPacket(p, def));
if (!p.Supports(CpeExt.BlockPermissions)) return;

0 comments on commit 41c66a0

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