Skip to content

Commit

Permalink
Implement EntityProperty extension.
Browse files Browse the repository at this point in the history
This allows changing entity rotation with /entityrot
  • Loading branch information
UnknownShadow200 committed Jun 12, 2017
1 parent b1e2163 commit e811864
Show file tree
Hide file tree
Showing 15 changed files with 450 additions and 377 deletions.
250 changes: 125 additions & 125 deletions ProCraft.sln

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions fCraft/Commands/ChatCommands.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public static void Player_IsBack(object sender, Events.PlayerMovedEventArgs e) {
e.Player.Message("You are no longer AFK"); e.Player.Message("You are no longer AFK");
e.Player.IsAFK = false; e.Player.IsAFK = false;
e.Player.oldafkMob = e.Player.afkMob; e.Player.oldafkMob = e.Player.afkMob;
e.Player.afkMob = e.Player.Info.Mob; e.Player.afkMob = e.Player.Info.Model;
Server.UpdateTabList(true); Server.UpdateTabList(true);
} }
e.Player.ResetIdleTimer(); e.Player.ResetIdleTimer();
Expand Down Expand Up @@ -283,7 +283,7 @@ private static void AFKHandler(Player player, CommandReader cmd) {
msg.Length > 0 ? " (" + (msg.Length > 32 ? msg.Remove(32) : msg) + ")" : ""); msg.Length > 0 ? " (" + (msg.Length > 32 ? msg.Remove(32) : msg) + ")" : "");
player.IsAFK = !player.IsAFK; player.IsAFK = !player.IsAFK;
player.oldafkMob = player.afkMob; player.oldafkMob = player.afkMob;
player.afkMob = player.IsAFK ? player.AFKModel : player.Info.Mob; player.afkMob = player.IsAFK ? player.AFKModel : player.Info.Model;
Server.UpdateTabList(true); Server.UpdateTabList(true);
player.ResetIdleTimer(); player.ResetIdleTimer();
} }
Expand Down
330 changes: 192 additions & 138 deletions fCraft/Commands/CpeCommands.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion fCraft/Commands/InfoCommands.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1761,7 +1761,7 @@ private static void ExtraInfoHandler(Player player, CommandReader cmd) {
player.Message("Extra Info about: {0}", info.ClassyName); player.Message("Extra Info about: {0}", info.ClassyName);
player.Message(" Times used &6Bot&S: {0}", info.TimesUsedBot); player.Message(" Times used &6Bot&S: {0}", info.TimesUsedBot);
player.Message(" Promoted: {0} Demoted: {1}", info.PromoCount, info.DemoCount); player.Message(" Promoted: {0} Demoted: {1}", info.PromoCount, info.DemoCount);
player.Message(" Reach Distance: {0} Model: {1}", info.ReachDistance, info.Mob); player.Message(" Reach Distance: {0} Model: {1}", info.ReachDistance, info.Model);


if (target != null && target.ClientName != null) if (target != null && target.ClientName != null)
player.Message(" Client Name: &F{0}", target.ClientName); player.Message(" Client Name: &F{0}", target.ClientName);
Expand Down
6 changes: 3 additions & 3 deletions fCraft/Commands/ModerationCommands.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ static void SetSpawnHandler( Player player, CommandReader cmd ) {
player.Message("Randomized Spawn!"); player.Message("Randomized Spawn!");
} }


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


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


} else if( infos.Length > 0 ) { } else if( infos.Length > 0 ) {
player.MessageManyMatches( "player", infos ); player.MessageManyMatches( "player", infos );

} else { } else {
infos = Server.FindPlayers(player, playerName, SearchOptions.IncludeSelf); infos = Server.FindPlayers(player, playerName, SearchOptions.IncludeSelf);
if( infos.Length > 0 ) { if( infos.Length > 0 ) {
Expand Down
2 changes: 1 addition & 1 deletion fCraft/Commands/WorldCommands.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2694,7 +2694,7 @@ private static void WorldRenameHandler(Player player, CommandReader cmd) {
player.LastUsedWorldName = newName; player.LastUsedWorldName = newName;
Logger.Log(LogType.UserActivity, "{0} renamed the world \"{1}\" to \"{2}\".", player.Name, oldName, newName); Logger.Log(LogType.UserActivity, "{0} renamed the world \"{1}\" to \"{2}\".", player.Name, oldName, newName);
Server.Message("{0}&S renamed the world \"{1}\" to \"{2}\"", player.ClassyName, oldName, newName); Server.Message("{0}&S renamed the world \"{1}\" to \"{2}\"", player.ClassyName, oldName, newName);
foreach(Entity bot in Entity.Entities.Where(e => oldName.CaselessEquals(e.World))) { foreach(Entity bot in Entity.EntityList.Where(e => oldName.CaselessEquals(e.World))) {
Entity.UpdateEntityWorld(bot, newName); Entity.UpdateEntityWorld(bot, newName);
} }
} }
Expand Down
4 changes: 2 additions & 2 deletions fCraft/Network/Packet.CPE.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ public static Packet MakeSetBlockPermission( Block block, bool canPlace, bool ca
} }


[Pure] [Pure]
public static Packet MakeChangeModel( byte entityId, [NotNull] string modelName, bool hasCP437) { public static Packet MakeChangeModel( sbyte id, [NotNull] string modelName, bool hasCP437) {
if( modelName == null ) throw new ArgumentNullException( "modelName" ); if( modelName == null ) throw new ArgumentNullException( "modelName" );
//Logger.Log(LogType.Debug, "Send: MakeChangeModel({0}, {1})", entityId, modelName); //Logger.Log(LogType.Debug, "Send: MakeChangeModel({0}, {1})", entityId, modelName);
Packet packet = new Packet( OpCode.ChangeModel ); Packet packet = new Packet( OpCode.ChangeModel );
packet.Bytes[1] = entityId; packet.Bytes[1] = (byte)id;
PacketWriter.WriteString( modelName, packet.Bytes, 2, hasCP437 ); PacketWriter.WriteString( modelName, packet.Bytes, 2, hasCP437 );
return packet; return packet;
} }
Expand Down
1 change: 1 addition & 0 deletions fCraft/Network/Player.Handshake.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ bool NegotiateProtocolExtension() {
case ExtPlayerPositionsExtName: case ExtPlayerPositionsExtName:
if (version == 1) ext = CpeExt.ExtPlayerPositions; if (version == 1) ext = CpeExt.ExtPlayerPositions;
supportsExtPositions = true; supportsExtPositions = true;
break;
case EntityPropertyExtName: case EntityPropertyExtName:
if (version == 1) ext = CpeExt.EntityProperty; if (version == 1) ext = CpeExt.EntityProperty;
break; break;
Expand Down
91 changes: 29 additions & 62 deletions fCraft/Network/Player.Networking.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ void ProcessSetBlockPacket() {
Message("&SYou are no longer AFK"); Message("&SYou are no longer AFK");
IsAFK = false; IsAFK = false;
oldafkMob = afkMob; oldafkMob = afkMob;
afkMob = Info.Mob; afkMob = Info.Model;
Server.UpdateTabList(true); Server.UpdateTabList(true);
} }
ResetIdleTimer(); ResetIdleTimer();
Expand Down Expand Up @@ -1182,11 +1182,7 @@ void SendJoinMessage(World oldWorld, World newWorld) {


void SendJoinCpeExtensions() { void SendJoinCpeExtensions() {
SendEnvSettings(); SendEnvSettings();
SendNow(SpawnPacket(Packet.SelfId, Info.Rank.Color + Name, Info.Skin, Position)); Entities.Spawn(this, true, this, Packet.SelfId);

if (Supports(CpeExt.ChangeModel)) {
SendNow(Packet.MakeChangeModel(255, !IsAFK ? Info.Mob : AFKModel, HasCP437));
}


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


internal void RemoveOldEntities(World world) { internal void RemoveOldEntities(World world) {
if (world == null) return; if (world == null) return;
foreach (Entity entity in Entity.Entities.Where(e => Entity.getWorld(e) == world)) { foreach (Entity entity in Entity.EntityList.Where(e => Entity.getWorld(e) == world)) {
SendNow(Packet.MakeRemoveEntity(entity.ID)); SendNow(Packet.MakeRemoveEntity(entity.ID));
} }
} }


internal void SendNewEntities(World world) { internal void SendNewEntities(World world) {
foreach (Entity entity in Entity.Entities.Where(e => Entity.getWorld(e) == world)) { foreach (Entity entity in Entity.EntityList.Where(e => Entity.getWorld(e) == world)) {
SendNow(SpawnPacket(entity.ID, entity.Name, entity.Skin, Entity.getPos(entity))); Entities.Spawn(this, true, entity);

if (!entity.Model.CaselessEquals("humanoid") && Supports(CpeExt.ChangeModel))
SendNow(Packet.MakeChangeModel((byte)entity.ID, entity.Model, HasCP437));
} }
} }


Expand Down Expand Up @@ -1372,17 +1365,6 @@ public void Send(Packet packet) {
if( canQueue ) priorityOutputQueue.Enqueue( packet ); if( canQueue ) priorityOutputQueue.Enqueue( packet );
} }



/// <summary> Returns an appropriate spawn packet for this player. </summary>
public Packet SpawnPacket(sbyte id, string name, string skin, Position pos) {
name = Color.SubstituteSpecialColors(name, FallbackColors);
if (Supports(CpeExt.ExtPlayerList2)) {
return Packet.MakeExtAddEntity2(id, name, skin, pos, HasCP437, supportsExtPositions);
} else {
return Packet.MakeAddEntity(id, name, pos, HasCP437, supportsExtPositions);
}
}

/// <summary> Returns an appropriate spawn packet for this player. </summary> /// <summary> Returns an appropriate spawn packet for this player. </summary>
public Packet TeleportPacket(sbyte id, Position pos) { public Packet TeleportPacket(sbyte id, Position pos) {
if (id == Packet.SelfId) pos = pos.GetFixed(); if (id == Packet.SelfId) pos = pos.GetFixed();
Expand Down Expand Up @@ -1596,9 +1578,13 @@ void UpdateVisibleEntities() {
} }
} }
} }

oldskinName = Info.skinName; oldskinName = Info.skinName;
oldMob = Info.Mob; oldMob = Info.Model;
oldafkMob = afkMob; oldafkMob = afkMob;
oldRotX = RotX;
oldRotZ = RotZ;

lock (entitiesLock) lock (entitiesLock)
RemoveNonRetainedEntities(); RemoveNonRetainedEntities();


Expand Down Expand Up @@ -1669,32 +1655,28 @@ void FollowSpectatedEntity() {
} else if( spectatePos != Position ) { } else if( spectatePos != Position ) {
SendNow( TeleportPacket( Packet.SelfId, spectatePos ) ); SendNow( TeleportPacket( Packet.SelfId, spectatePos ) );
} }
if (SpectatedPlayer.HeldBlock != HeldBlock && SpectatedPlayer.Supports(CpeExt.HeldBlock))
{ if (SpectatedPlayer.HeldBlock != HeldBlock && SpectatedPlayer.Supports(CpeExt.HeldBlock)) {
byte block = (byte)SpectatedPlayer.HeldBlock; byte block = (byte)SpectatedPlayer.HeldBlock;
CheckBlock(ref block); CheckBlock(ref block);
SendNow(Packet.MakeHoldThis((Block)block, false)); SendNow(Packet.MakeHoldThis((Block)block, false));
} }
} }


void CheckOwnChange(sbyte id, Player otherPlayer) { void CheckOwnChange(sbyte id, Player other) {
if (oldskinName != Info.skinName && otherPlayer.Supports(CpeExt.ExtPlayerList2)) { bool needsUpdate = false;
otherPlayer.Send(otherPlayer.SpawnPacket(id, Info.Rank.Color + Name, Info.Skin, Position)); if (oldskinName != Info.skinName && other.Supports(CpeExt.ExtPlayerList2)) needsUpdate = true;
//otherPlayer.Send(Packet.MakeTeleport(id, Position)); if ((oldRotX != RotX || oldRotZ != RotZ) && other.Supports(CpeExt.EntityProperty)) needsUpdate = true;
if (otherPlayer.Supports(CpeExt.ChangeModel)) {
string thisModel = IsAFK ? AFKModel : Info.Mob; if (needsUpdate) {
if (otherPlayer.Info.Rank.CanSee(Info.Rank) && (thisModel.CaselessEquals("air") || thisModel.CaselessEquals("0"))) { Entities.Spawn(other, false, this, id);
thisModel = "Humanoid"; // need to send teleport packet for correct position
} other.Send(TeleportPacket(id, Position));
otherPlayer.Send(Packet.MakeChangeModel((byte)id, thisModel, otherPlayer.HasCP437));
}
} }
if ((oldMob != Info.Mob || oldafkMob != afkMob) && otherPlayer.Supports(CpeExt.ChangeModel)) {
string thisModel = IsAFK ? AFKModel : Info.Mob; if ((oldMob != Info.Model || oldafkMob != afkMob) && other.Supports(CpeExt.ChangeModel)) {
if (otherPlayer.Info.Rank.CanSee(Info.Rank) && (thisModel.CaselessEquals("air") || thisModel.CaselessEquals("0"))) { string model = Entities.ModelFor(other, this);
thisModel = "Humanoid"; other.Send(Packet.MakeChangeModel(id, model, other.HasCP437));
}
otherPlayer.Send(Packet.MakeChangeModel((byte)id, thisModel, otherPlayer.HasCP437));
} }
} }


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


Send(SpawnPacket(newEntity.Id, player.Info.Rank.Color + player.Name, player.Info.Skin, pos)); Entities.Spawn( this, false, player, newEntity.Id );
Send(TeleportPacket(newEntity.Id, player.Position)); Send(TeleportPacket(newEntity.Id, player.Position));

if (Supports(CpeExt.ChangeModel)) {
string addedModel = player.IsAFK ? player.AFKModel : player.Info.Mob;
if (Info.Rank.CanSee(player.Info.Rank) && (addedModel.CaselessEquals("air") || addedModel.CaselessEquals("0"))) {
addedModel = "Humanoid";
}
Send(Packet.MakeChangeModel((byte)newEntity.Id, addedModel, HasCP437));
}
return newEntity; return newEntity;
} else { } else {
throw new InvalidOperationException("Player.AddEntity: Ran out of entity IDs."); throw new InvalidOperationException("Player.AddEntity: Ran out of entity IDs.");
Expand Down Expand Up @@ -1757,16 +1731,9 @@ void ReAddEntity( [NotNull] VisibleEntity entity, [NotNull] Player player ) {
Logger.Log( LogType.Debug, "ReAddEntity: {0} re-added {1} ({2})", Name, entity.Id, player.Name ); Logger.Log( LogType.Debug, "ReAddEntity: {0} re-added {1} ({2})", Name, entity.Id, player.Name );
#endif #endif
SendNow( Packet.MakeRemoveEntity( entity.Id ) ); SendNow( Packet.MakeRemoveEntity( entity.Id ) );
SendNow(SpawnPacket(entity.Id, player.Info.Rank.Color + player.Name, player.Info.Skin, player.WorldMap.Spawn)); Entities.Spawn( this, true, player, entity.Id );
SendNow(TeleportPacket(entity.Id, player.Position)); // need to send teleport packet for correct position

SendNow( TeleportPacket( entity.Id, player.Position ) );
if (Supports(CpeExt.ChangeModel)) {
string readdedModel = player.IsAFK ? player.AFKModel : player.Info.Mob;
if (Info.Rank.CanSee(player.Info.Rank) && (readdedModel.CaselessEquals("air") || readdedModel.CaselessEquals("0"))) {
readdedModel = "Humanoid";
}
SendNow(Packet.MakeChangeModel((byte)entity.Id, readdedModel, HasCP437));
}
} }




Expand Down
3 changes: 2 additions & 1 deletion fCraft/Player/Player.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public sealed partial class Player : IClassy {
public bool IsSuper; public bool IsSuper;


public bool usedquit = false; public bool usedquit = false;
public int RotX, RotZ, oldRotX, oldRotZ;


public string quitmessage = "/Quit"; public string quitmessage = "/Quit";


Expand Down Expand Up @@ -344,7 +345,7 @@ public void ParseMessage( [NotNull] string rawMessage, bool fromConsole ) {
Message("&SYou are no longer AFK"); Message("&SYou are no longer AFK");
IsAFK = false; IsAFK = false;
oldafkMob = afkMob; oldafkMob = afkMob;
afkMob = Info.Mob; afkMob = Info.Model;
Server.UpdateTabList(true); Server.UpdateTabList(true);
} }


Expand Down
10 changes: 5 additions & 5 deletions fCraft/Player/PlayerInfo.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public bool AllowThirdPerson {
/// Use Player.CanSee() method to check visibility to specific observers. </summary> /// Use Player.CanSee() method to check visibility to specific observers. </summary>
public bool IsHidden; public bool IsHidden;


public string Mob = "Humanoid"; public string Model = "Humanoid";
public string skinName = ""; public string skinName = "";


public string Skin { get { return skinName == "" ? Name : skinName; } } public string Skin { get { return skinName == "" ? Name : skinName; } }
Expand Down Expand Up @@ -714,7 +714,7 @@ internal static PlayerInfo LoadFormat2( string[] fields, int count ) {
Int32.TryParse(fields[59], out info.DemoCount); Int32.TryParse(fields[59], out info.DemoCount);
} }


if (count > 60) info.Mob = fields[60]; if (count > 60) info.Model = fields[60];


if (count > 61) if (count > 61)
{ {
Expand Down Expand Up @@ -1229,7 +1229,7 @@ internal void Serialize( StringBuffer sb ) {
sb.Append(DemoCount); // 59 sb.Append(DemoCount); // 59
sb.Append(','); sb.Append(',');


sb.Append(Mob); // 60 sb.Append(Model); // 60
sb.Append(','); sb.Append(',');


sb.Append(ReachDistance); // 61 sb.Append(ReachDistance); // 61
Expand Down Expand Up @@ -1324,8 +1324,8 @@ public void ProcessLogin( [NotNull] Player player ) {
IsOnline = true; IsOnline = true;
PlayerObject = player; PlayerObject = player;
LastModified = DateTime.UtcNow; LastModified = DateTime.UtcNow;
if (Mob == null) if (Model == null)
Mob = "humanoid"; Model = "humanoid";
} }




Expand Down
2 changes: 1 addition & 1 deletion fCraft/System/Server.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ private static void CheckIdles(SchedulerTask task) {
player.Info.TotalTime = player.Info.TotalTime - player.IdleTime; player.Info.TotalTime = player.Info.TotalTime - player.IdleTime;
player.IsAFK = false; player.IsAFK = false;
player.oldafkMob = player.afkMob; player.oldafkMob = player.afkMob;
player.afkMob = player.Info.Mob; player.afkMob = player.Info.Model;
player.ResetIdleTimer(); // to prevent kick from firing more than once player.ResetIdleTimer(); // to prevent kick from firing more than once
} }
} }
Expand Down
62 changes: 62 additions & 0 deletions fCraft/World/Entities.cs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,62 @@
// ProCraft Copyright 2014-2016 Joseph Beauvais <123DMWM@gmail.com>
using System;
using System.IO;
using System.Collections.Generic;

namespace fCraft {

/// <summary> Helper methods for spawning and despawning entities. </summary>
public static class Entities {

public static string ModelFor(Player dst, Player entity) {
string model = entity.IsAFK ? entity.AFKModel : entity.Info.Model;
if (dst.Info.Rank.CanSee(entity.Info.Rank) && (model.CaselessEquals("air") || model.CaselessEquals("0"))) {
model = "humanoid";
}
return model;
}

public static void Spawn(Player dst, bool sendNow, Player entity, sbyte id) {
string name = entity.Info.Rank.Color + entity.Name;
string skin = entity.Info.Skin, model = ModelFor(dst, entity);
Spawn(dst, sendNow, id, name, skin, model,
entity.Position, entity.RotX, entity.RotZ);
}

public static void Spawn(Player dst, bool sendNow, Entity entity) {
Spawn(dst, sendNow, entity.ID, entity.Name, entity.Skin,
entity.Model, Entity.getPos(entity), 0, 0);
}

public static void Spawn(Player dst, bool sendNow, sbyte id, string name,
string skin, string model, Position pos, int rotX, int rotZ) {
Send(dst, sendNow, SpawnPacket(dst, id, name, skin, pos));
if (dst.Supports(CpeExt.ChangeModel)) {
Send(dst, sendNow, Packet.MakeChangeModel(id, model, dst.HasCP437));
}

if (dst.Supports(CpeExt.EntityProperty)) {
Send(dst, sendNow, Packet.MakeEntityProperty(id, EntityProp.RotationX, rotX));
Send(dst, sendNow, Packet.MakeEntityProperty(id, EntityProp.RotationZ, rotZ));
}
}


static Packet SpawnPacket(Player dst, sbyte id, string name, string skin, Position pos) {
name = Color.SubstituteSpecialColors(name, dst.FallbackColors);
if (dst.Supports(CpeExt.ExtPlayerList2)) {
return Packet.MakeExtAddEntity2(id, name, skin, pos, dst.HasCP437, dst.supportsExtPositions);
} else {
return Packet.MakeAddEntity(id, name, pos, dst.HasCP437, dst.supportsExtPositions);
}
}

static void Send(Player dst, bool sendNow, Packet p) {
if (sendNow) {
dst.SendNow(p);
} else {
dst.Send(p);
}
}
}
}
Loading

0 comments on commit e811864

Please sign in to comment.