Skip to content
Permalink
Browse files

Implement EntityProperty extension.

This allows changing entity rotation with /entityrot
  • Loading branch information...
UnknownShadow200 committed Jun 12, 2017
1 parent b1e2163 commit e8118643252eaa4f6a5bc4f74f662e001dbafbab

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -253,7 +253,7 @@ static void ReviewHandler(Player player, CommandReader cmd)
e.Player.Message("You are no longer AFK");
e.Player.IsAFK = false;
e.Player.oldafkMob = e.Player.afkMob;
e.Player.afkMob = e.Player.Info.Mob;
e.Player.afkMob = e.Player.Info.Model;
Server.UpdateTabList(true);
}
e.Player.ResetIdleTimer();
@@ -283,7 +283,7 @@ static void ReviewHandler(Player player, CommandReader cmd)
msg.Length > 0 ? " (" + (msg.Length > 32 ? msg.Remove(32) : msg) + ")" : "");
player.IsAFK = !player.IsAFK;
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);
player.ResetIdleTimer();
}

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -1761,7 +1761,7 @@ static void EmotesHandler(Player player, CommandReader cmd)
player.Message("Extra Info about: {0}", info.ClassyName);
player.Message(" Times used &6Bot&S: {0}", info.TimesUsedBot);
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)
player.Message(" Client Name: &F{0}", target.ClientName);
@@ -1209,7 +1209,7 @@ static void RankHandler([NotNull] Player player, [NotNull] CommandReader cmd)
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." );
Logger.Log( LogType.UserActivity,
"{0} changed the spawned point.",
@@ -1222,7 +1222,8 @@ static void RankHandler([NotNull] Player player, [NotNull] CommandReader cmd)
player.LastUsedPlayerName = target.Name;

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 {
player.Message( "You may only set spawn of players ranked {0}&S or lower.",
player.Info.Rank.GetLimit( Permission.Bring ).ClassyName );
@@ -1231,7 +1232,6 @@ static void RankHandler([NotNull] Player player, [NotNull] CommandReader cmd)

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

} else {
infos = Server.FindPlayers(player, playerName, SearchOptions.IncludeSelf);
if( infos.Length > 0 ) {
@@ -2694,7 +2694,7 @@ static void WorldClearHandler(Player player, CommandReader cmd)
player.LastUsedWorldName = 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);
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);
}
}
@@ -169,11 +169,11 @@ public partial struct Packet {
}

[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" );
//Logger.Log(LogType.Debug, "Send: MakeChangeModel({0}, {1})", entityId, modelName);
Packet packet = new Packet( OpCode.ChangeModel );
packet.Bytes[1] = entityId;
packet.Bytes[1] = (byte)id;
PacketWriter.WriteString( modelName, packet.Bytes, 2, hasCP437 );
return packet;
}
@@ -363,6 +363,7 @@ public class Players {
case ExtPlayerPositionsExtName:
if (version == 1) ext = CpeExt.ExtPlayerPositions;
supportsExtPositions = true;
break;
case EntityPropertyExtName:
if (version == 1) ext = CpeExt.EntityProperty;
break;
@@ -484,7 +484,7 @@ public sealed partial class Player {
Message("&SYou are no longer AFK");
IsAFK = false;
oldafkMob = afkMob;
afkMob = Info.Mob;
afkMob = Info.Model;
Server.UpdateTabList(true);
}
ResetIdleTimer();
@@ -1182,11 +1182,7 @@ bool LoginSequence()

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

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

if (Supports(CpeExt.HackControl)) {
SendNow(PlayerHacks.MakePacket(this, World.MOTD));
@@ -1215,17 +1211,14 @@ bool LoginSequence()

internal void RemoveOldEntities(World world) {
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));
}
}

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

if (!entity.Model.CaselessEquals("humanoid") && Supports(CpeExt.ChangeModel))
SendNow(Packet.MakeChangeModel((byte)entity.ID, entity.Model, HasCP437));
foreach (Entity entity in Entity.EntityList.Where(e => Entity.getWorld(e) == world)) {
Entities.Spawn(this, true, entity);
}
}

@@ -1372,17 +1365,6 @@ bool LoginSequence()
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>
public Packet TeleportPacket(sbyte id, Position pos) {
if (id == Packet.SelfId) pos = pos.GetFixed();
@@ -1596,9 +1578,13 @@ bool LoginSequence()
}
}
}

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

lock (entitiesLock)
RemoveNonRetainedEntities();

@@ -1669,32 +1655,28 @@ bool LoginSequence()
} else if( spectatePos != Position ) {
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;
CheckBlock(ref block);
SendNow(Packet.MakeHoldThis((Block)block, false));
}
}

void CheckOwnChange(sbyte id, Player otherPlayer) {
if (oldskinName != Info.skinName && otherPlayer.Supports(CpeExt.ExtPlayerList2)) {
otherPlayer.Send(otherPlayer.SpawnPacket(id, Info.Rank.Color + Name, Info.Skin, Position));
//otherPlayer.Send(Packet.MakeTeleport(id, Position));
if (otherPlayer.Supports(CpeExt.ChangeModel)) {
string thisModel = IsAFK ? AFKModel : Info.Mob;
if (otherPlayer.Info.Rank.CanSee(Info.Rank) && (thisModel.CaselessEquals("air") || thisModel.CaselessEquals("0"))) {
thisModel = "Humanoid";
}
otherPlayer.Send(Packet.MakeChangeModel((byte)id, thisModel, otherPlayer.HasCP437));
}
void CheckOwnChange(sbyte id, Player other) {
bool needsUpdate = false;
if (oldskinName != Info.skinName && other.Supports(CpeExt.ExtPlayerList2)) needsUpdate = true;
if ((oldRotX != RotX || oldRotZ != RotZ) && other.Supports(CpeExt.EntityProperty)) needsUpdate = true;

if (needsUpdate) {
Entities.Spawn(other, false, this, id);
// need to send teleport packet for correct position
other.Send(TeleportPacket(id, Position));
}
if ((oldMob != Info.Mob || oldafkMob != afkMob) && otherPlayer.Supports(CpeExt.ChangeModel)) {
string thisModel = IsAFK ? AFKModel : Info.Mob;
if (otherPlayer.Info.Rank.CanSee(Info.Rank) && (thisModel.CaselessEquals("air") || thisModel.CaselessEquals("0"))) {
thisModel = "Humanoid";
}
otherPlayer.Send(Packet.MakeChangeModel((byte)id, thisModel, otherPlayer.HasCP437));

if ((oldMob != Info.Model || oldafkMob != afkMob) && other.Supports(CpeExt.ChangeModel)) {
string model = Entities.ModelFor(other, this);
other.Send(Packet.MakeChangeModel(id, model, other.HasCP437));
}
}

@@ -1712,16 +1694,8 @@ bool LoginSequence()
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));

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;
} else {
throw new InvalidOperationException("Player.AddEntity: Ran out of entity IDs.");
@@ -1757,16 +1731,9 @@ bool LoginSequence()
Logger.Log( LogType.Debug, "ReAddEntity: {0} re-added {1} ({2})", Name, entity.Id, player.Name );
#endif
SendNow( Packet.MakeRemoveEntity( entity.Id ) );
SendNow(SpawnPacket(entity.Id, player.Info.Rank.Color + player.Name, player.Info.Skin, player.WorldMap.Spawn));
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));
}
Entities.Spawn( this, true, player, entity.Id );
// need to send teleport packet for correct position
SendNow( TeleportPacket( entity.Id, player.Position ) );
}


@@ -51,6 +51,7 @@ public sealed partial class Player : IClassy {
public bool IsSuper;

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

public string quitmessage = "/Quit";

@@ -344,7 +345,7 @@ public FontFamily LoadFontFamily(string fileName)
Message("&SYou are no longer AFK");
IsAFK = false;
oldafkMob = afkMob;
afkMob = Info.Mob;
afkMob = Info.Model;
Server.UpdateTabList(true);
}

@@ -425,7 +425,7 @@ public string BlocksDrawnString
/// Use Player.CanSee() method to check visibility to specific observers. </summary>
public bool IsHidden;

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

public string Skin { get { return skinName == "" ? Name : skinName; } }
@@ -714,7 +714,7 @@ public PlayerInfo( [NotNull] string name, [NotNull] IPAddress lastIP, [NotNull]
Int32.TryParse(fields[59], out info.DemoCount);
}

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

if (count > 61)
{
@@ -1229,7 +1229,7 @@ public PlayerInfo( [NotNull] string name, [NotNull] IPAddress lastIP, [NotNull]
sb.Append(DemoCount); // 59
sb.Append(',');

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

sb.Append(ReachDistance); // 61
@@ -1324,8 +1324,8 @@ public PlayerInfo( [NotNull] string name, [NotNull] IPAddress lastIP, [NotNull]
IsOnline = true;
PlayerObject = player;
LastModified = DateTime.UtcNow;
if (Mob == null)
Mob = "humanoid";
if (Model == null)
Model = "humanoid";
}


@@ -796,7 +796,7 @@ public static partial class Server {
player.Info.TotalTime = player.Info.TotalTime - player.IdleTime;
player.IsAFK = false;
player.oldafkMob = player.afkMob;
player.afkMob = player.Info.Mob;
player.afkMob = player.Info.Model;
player.ResetIdleTimer(); // to prevent kick from firing more than once
}
}
@@ -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);
}
}
}
}
Oops, something went wrong.

0 comments on commit e811864

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