Skip to content

Commit e811864

Browse files
Implement EntityProperty extension.
This allows changing entity rotation with /entityrot
1 parent b1e2163 commit e811864

File tree

15 files changed

+450
-377
lines changed

15 files changed

+450
-377
lines changed

ProCraft.sln

Lines changed: 125 additions & 125 deletions
Large diffs are not rendered by default.

fCraft/Commands/ChatCommands.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public static void Player_IsBack(object sender, Events.PlayerMovedEventArgs e) {
253253
e.Player.Message("You are no longer AFK");
254254
e.Player.IsAFK = false;
255255
e.Player.oldafkMob = e.Player.afkMob;
256-
e.Player.afkMob = e.Player.Info.Mob;
256+
e.Player.afkMob = e.Player.Info.Model;
257257
Server.UpdateTabList(true);
258258
}
259259
e.Player.ResetIdleTimer();
@@ -283,7 +283,7 @@ private static void AFKHandler(Player player, CommandReader cmd) {
283283
msg.Length > 0 ? " (" + (msg.Length > 32 ? msg.Remove(32) : msg) + ")" : "");
284284
player.IsAFK = !player.IsAFK;
285285
player.oldafkMob = player.afkMob;
286-
player.afkMob = player.IsAFK ? player.AFKModel : player.Info.Mob;
286+
player.afkMob = player.IsAFK ? player.AFKModel : player.Info.Model;
287287
Server.UpdateTabList(true);
288288
player.ResetIdleTimer();
289289
}

fCraft/Commands/CpeCommands.cs

Lines changed: 192 additions & 138 deletions
Large diffs are not rendered by default.

fCraft/Commands/InfoCommands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,7 @@ private static void ExtraInfoHandler(Player player, CommandReader cmd) {
17611761
player.Message("Extra Info about: {0}", info.ClassyName);
17621762
player.Message(" Times used &6Bot&S: {0}", info.TimesUsedBot);
17631763
player.Message(" Promoted: {0} Demoted: {1}", info.PromoCount, info.DemoCount);
1764-
player.Message(" Reach Distance: {0} Model: {1}", info.ReachDistance, info.Mob);
1764+
player.Message(" Reach Distance: {0} Model: {1}", info.ReachDistance, info.Model);
17651765

17661766
if (target != null && target.ClientName != null)
17671767
player.Message(" Client Name: &F{0}", target.ClientName);

fCraft/Commands/ModerationCommands.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ static void SetSpawnHandler( Player player, CommandReader cmd ) {
12091209
player.Message("Randomized Spawn!");
12101210
}
12111211

1212-
player.Send(player.SpawnPacket(Packet.SelfId, player.Info.Rank.Color + player.Name, player.Info.Skin, player.Position));
1212+
Entities.Spawn( player, false, player, Packet.SelfId );
12131213
player.Message( "New spawn point saved." );
12141214
Logger.Log( LogType.UserActivity,
12151215
"{0} changed the spawned point.",
@@ -1222,7 +1222,8 @@ static void SetSpawnHandler( Player player, CommandReader cmd ) {
12221222
player.LastUsedPlayerName = target.Name;
12231223

12241224
if( player.Can( Permission.Bring, target.Info.Rank ) ) {
1225-
target.Send(target.SpawnPacket(Packet.SelfId, target.Info.Rank.Color + target.Name, target.Info.Skin, player.Position));
1225+
target.Position = player.Position;
1226+
Entities.Spawn( target, false, target, Packet.SelfId );
12261227
} else {
12271228
player.Message( "You may only set spawn of players ranked {0}&S or lower.",
12281229
player.Info.Rank.GetLimit( Permission.Bring ).ClassyName );
@@ -1231,7 +1232,6 @@ static void SetSpawnHandler( Player player, CommandReader cmd ) {
12311232

12321233
} else if( infos.Length > 0 ) {
12331234
player.MessageManyMatches( "player", infos );
1234-
12351235
} else {
12361236
infos = Server.FindPlayers(player, playerName, SearchOptions.IncludeSelf);
12371237
if( infos.Length > 0 ) {

fCraft/Commands/WorldCommands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2694,7 +2694,7 @@ private static void WorldRenameHandler(Player player, CommandReader cmd) {
26942694
player.LastUsedWorldName = newName;
26952695
Logger.Log(LogType.UserActivity, "{0} renamed the world \"{1}\" to \"{2}\".", player.Name, oldName, newName);
26962696
Server.Message("{0}&S renamed the world \"{1}\" to \"{2}\"", player.ClassyName, oldName, newName);
2697-
foreach(Entity bot in Entity.Entities.Where(e => oldName.CaselessEquals(e.World))) {
2697+
foreach(Entity bot in Entity.EntityList.Where(e => oldName.CaselessEquals(e.World))) {
26982698
Entity.UpdateEntityWorld(bot, newName);
26992699
}
27002700
}

fCraft/Network/Packet.CPE.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ public static Packet MakeSetBlockPermission( Block block, bool canPlace, bool ca
169169
}
170170

171171
[Pure]
172-
public static Packet MakeChangeModel( byte entityId, [NotNull] string modelName, bool hasCP437) {
172+
public static Packet MakeChangeModel( sbyte id, [NotNull] string modelName, bool hasCP437) {
173173
if( modelName == null ) throw new ArgumentNullException( "modelName" );
174174
//Logger.Log(LogType.Debug, "Send: MakeChangeModel({0}, {1})", entityId, modelName);
175175
Packet packet = new Packet( OpCode.ChangeModel );
176-
packet.Bytes[1] = entityId;
176+
packet.Bytes[1] = (byte)id;
177177
PacketWriter.WriteString( modelName, packet.Bytes, 2, hasCP437 );
178178
return packet;
179179
}

fCraft/Network/Player.Handshake.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ bool NegotiateProtocolExtension() {
363363
case ExtPlayerPositionsExtName:
364364
if (version == 1) ext = CpeExt.ExtPlayerPositions;
365365
supportsExtPositions = true;
366+
break;
366367
case EntityPropertyExtName:
367368
if (version == 1) ext = CpeExt.EntityProperty;
368369
break;

fCraft/Network/Player.Networking.cs

Lines changed: 29 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ void ProcessSetBlockPacket() {
484484
Message("&SYou are no longer AFK");
485485
IsAFK = false;
486486
oldafkMob = afkMob;
487-
afkMob = Info.Mob;
487+
afkMob = Info.Model;
488488
Server.UpdateTabList(true);
489489
}
490490
ResetIdleTimer();
@@ -1182,11 +1182,7 @@ void SendJoinMessage(World oldWorld, World newWorld) {
11821182

11831183
void SendJoinCpeExtensions() {
11841184
SendEnvSettings();
1185-
SendNow(SpawnPacket(Packet.SelfId, Info.Rank.Color + Name, Info.Skin, Position));
1186-
1187-
if (Supports(CpeExt.ChangeModel)) {
1188-
SendNow(Packet.MakeChangeModel(255, !IsAFK ? Info.Mob : AFKModel, HasCP437));
1189-
}
1185+
Entities.Spawn(this, true, this, Packet.SelfId);
11901186

11911187
if (Supports(CpeExt.HackControl)) {
11921188
SendNow(PlayerHacks.MakePacket(this, World.MOTD));
@@ -1215,17 +1211,14 @@ void SendJoinCpeExtensions() {
12151211

12161212
internal void RemoveOldEntities(World world) {
12171213
if (world == null) return;
1218-
foreach (Entity entity in Entity.Entities.Where(e => Entity.getWorld(e) == world)) {
1214+
foreach (Entity entity in Entity.EntityList.Where(e => Entity.getWorld(e) == world)) {
12191215
SendNow(Packet.MakeRemoveEntity(entity.ID));
12201216
}
12211217
}
12221218

12231219
internal void SendNewEntities(World world) {
1224-
foreach (Entity entity in Entity.Entities.Where(e => Entity.getWorld(e) == world)) {
1225-
SendNow(SpawnPacket(entity.ID, entity.Name, entity.Skin, Entity.getPos(entity)));
1226-
1227-
if (!entity.Model.CaselessEquals("humanoid") && Supports(CpeExt.ChangeModel))
1228-
SendNow(Packet.MakeChangeModel((byte)entity.ID, entity.Model, HasCP437));
1220+
foreach (Entity entity in Entity.EntityList.Where(e => Entity.getWorld(e) == world)) {
1221+
Entities.Spawn(this, true, entity);
12291222
}
12301223
}
12311224

@@ -1372,17 +1365,6 @@ public void Send(Packet packet) {
13721365
if( canQueue ) priorityOutputQueue.Enqueue( packet );
13731366
}
13741367

1375-
1376-
/// <summary> Returns an appropriate spawn packet for this player. </summary>
1377-
public Packet SpawnPacket(sbyte id, string name, string skin, Position pos) {
1378-
name = Color.SubstituteSpecialColors(name, FallbackColors);
1379-
if (Supports(CpeExt.ExtPlayerList2)) {
1380-
return Packet.MakeExtAddEntity2(id, name, skin, pos, HasCP437, supportsExtPositions);
1381-
} else {
1382-
return Packet.MakeAddEntity(id, name, pos, HasCP437, supportsExtPositions);
1383-
}
1384-
}
1385-
13861368
/// <summary> Returns an appropriate spawn packet for this player. </summary>
13871369
public Packet TeleportPacket(sbyte id, Position pos) {
13881370
if (id == Packet.SelfId) pos = pos.GetFixed();
@@ -1596,9 +1578,13 @@ void UpdateVisibleEntities() {
15961578
}
15971579
}
15981580
}
1581+
15991582
oldskinName = Info.skinName;
1600-
oldMob = Info.Mob;
1583+
oldMob = Info.Model;
16011584
oldafkMob = afkMob;
1585+
oldRotX = RotX;
1586+
oldRotZ = RotZ;
1587+
16021588
lock (entitiesLock)
16031589
RemoveNonRetainedEntities();
16041590

@@ -1669,32 +1655,28 @@ void FollowSpectatedEntity() {
16691655
} else if( spectatePos != Position ) {
16701656
SendNow( TeleportPacket( Packet.SelfId, spectatePos ) );
16711657
}
1672-
if (SpectatedPlayer.HeldBlock != HeldBlock && SpectatedPlayer.Supports(CpeExt.HeldBlock))
1673-
{
1658+
1659+
if (SpectatedPlayer.HeldBlock != HeldBlock && SpectatedPlayer.Supports(CpeExt.HeldBlock)) {
16741660
byte block = (byte)SpectatedPlayer.HeldBlock;
16751661
CheckBlock(ref block);
16761662
SendNow(Packet.MakeHoldThis((Block)block, false));
16771663
}
16781664
}
16791665

1680-
void CheckOwnChange(sbyte id, Player otherPlayer) {
1681-
if (oldskinName != Info.skinName && otherPlayer.Supports(CpeExt.ExtPlayerList2)) {
1682-
otherPlayer.Send(otherPlayer.SpawnPacket(id, Info.Rank.Color + Name, Info.Skin, Position));
1683-
//otherPlayer.Send(Packet.MakeTeleport(id, Position));
1684-
if (otherPlayer.Supports(CpeExt.ChangeModel)) {
1685-
string thisModel = IsAFK ? AFKModel : Info.Mob;
1686-
if (otherPlayer.Info.Rank.CanSee(Info.Rank) && (thisModel.CaselessEquals("air") || thisModel.CaselessEquals("0"))) {
1687-
thisModel = "Humanoid";
1688-
}
1689-
otherPlayer.Send(Packet.MakeChangeModel((byte)id, thisModel, otherPlayer.HasCP437));
1690-
}
1666+
void CheckOwnChange(sbyte id, Player other) {
1667+
bool needsUpdate = false;
1668+
if (oldskinName != Info.skinName && other.Supports(CpeExt.ExtPlayerList2)) needsUpdate = true;
1669+
if ((oldRotX != RotX || oldRotZ != RotZ) && other.Supports(CpeExt.EntityProperty)) needsUpdate = true;
1670+
1671+
if (needsUpdate) {
1672+
Entities.Spawn(other, false, this, id);
1673+
// need to send teleport packet for correct position
1674+
other.Send(TeleportPacket(id, Position));
16911675
}
1692-
if ((oldMob != Info.Mob || oldafkMob != afkMob) && otherPlayer.Supports(CpeExt.ChangeModel)) {
1693-
string thisModel = IsAFK ? AFKModel : Info.Mob;
1694-
if (otherPlayer.Info.Rank.CanSee(Info.Rank) && (thisModel.CaselessEquals("air") || thisModel.CaselessEquals("0"))) {
1695-
thisModel = "Humanoid";
1696-
}
1697-
otherPlayer.Send(Packet.MakeChangeModel((byte)id, thisModel, otherPlayer.HasCP437));
1676+
1677+
if ((oldMob != Info.Model || oldafkMob != afkMob) && other.Supports(CpeExt.ChangeModel)) {
1678+
string model = Entities.ModelFor(other, this);
1679+
other.Send(Packet.MakeChangeModel(id, model, other.HasCP437));
16981680
}
16991681
}
17001682

@@ -1712,16 +1694,8 @@ VisibleEntity AddEntity( [NotNull] Player player ) {
17121694
pos = player.WorldMap.Spawn;
17131695
}
17141696

1715-
Send(SpawnPacket(newEntity.Id, player.Info.Rank.Color + player.Name, player.Info.Skin, pos));
1697+
Entities.Spawn( this, false, player, newEntity.Id );
17161698
Send(TeleportPacket(newEntity.Id, player.Position));
1717-
1718-
if (Supports(CpeExt.ChangeModel)) {
1719-
string addedModel = player.IsAFK ? player.AFKModel : player.Info.Mob;
1720-
if (Info.Rank.CanSee(player.Info.Rank) && (addedModel.CaselessEquals("air") || addedModel.CaselessEquals("0"))) {
1721-
addedModel = "Humanoid";
1722-
}
1723-
Send(Packet.MakeChangeModel((byte)newEntity.Id, addedModel, HasCP437));
1724-
}
17251699
return newEntity;
17261700
} else {
17271701
throw new InvalidOperationException("Player.AddEntity: Ran out of entity IDs.");
@@ -1757,16 +1731,9 @@ void ReAddEntity( [NotNull] VisibleEntity entity, [NotNull] Player player ) {
17571731
Logger.Log( LogType.Debug, "ReAddEntity: {0} re-added {1} ({2})", Name, entity.Id, player.Name );
17581732
#endif
17591733
SendNow( Packet.MakeRemoveEntity( entity.Id ) );
1760-
SendNow(SpawnPacket(entity.Id, player.Info.Rank.Color + player.Name, player.Info.Skin, player.WorldMap.Spawn));
1761-
SendNow(TeleportPacket(entity.Id, player.Position));
1762-
1763-
if (Supports(CpeExt.ChangeModel)) {
1764-
string readdedModel = player.IsAFK ? player.AFKModel : player.Info.Mob;
1765-
if (Info.Rank.CanSee(player.Info.Rank) && (readdedModel.CaselessEquals("air") || readdedModel.CaselessEquals("0"))) {
1766-
readdedModel = "Humanoid";
1767-
}
1768-
SendNow(Packet.MakeChangeModel((byte)entity.Id, readdedModel, HasCP437));
1769-
}
1734+
Entities.Spawn( this, true, player, entity.Id );
1735+
// need to send teleport packet for correct position
1736+
SendNow( TeleportPacket( entity.Id, player.Position ) );
17701737
}
17711738

17721739

fCraft/Player/Player.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public sealed partial class Player : IClassy {
5151
public bool IsSuper;
5252

5353
public bool usedquit = false;
54+
public int RotX, RotZ, oldRotX, oldRotZ;
5455

5556
public string quitmessage = "/Quit";
5657

@@ -344,7 +345,7 @@ public void ParseMessage( [NotNull] string rawMessage, bool fromConsole ) {
344345
Message("&SYou are no longer AFK");
345346
IsAFK = false;
346347
oldafkMob = afkMob;
347-
afkMob = Info.Mob;
348+
afkMob = Info.Model;
348349
Server.UpdateTabList(true);
349350
}
350351

0 commit comments

Comments
 (0)