Skip to content

Commit

Permalink
Reduce code duplication for classic and normal player list code.
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed May 29, 2016
1 parent c4f1f69 commit 6c3f72c
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 164 deletions.
100 changes: 1 addition & 99 deletions ClassicalSharp/2D/Widgets/PlayerList/ClassicPlayerListWidget.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,15 @@
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System;
using System.Collections.Generic;
using System.Drawing;
using ClassicalSharp.Entities;
using ClassicalSharp.Events;

namespace ClassicalSharp.Gui {

public sealed class ClassicPlayerListWidget : PlayerListWidget {
public sealed class ClassicPlayerListWidget : NormalPlayerListWidget {

bool extList;
int elemHeight;
ChatTextWidget overview;
static FastColour lightTableCol = new FastColour( 20, 20, 20, 180 );
public ClassicPlayerListWidget( Game game, Font font ) : base( game, font ) {
textures = new Texture[256];
extList = game.Network.UsingExtPlayerList;
}

PlayerInfo[] info = new PlayerInfo[256];
class PlayerInfo {

public string Name, ColouredName;
public byte Id;

public PlayerInfo( TabListEntry p ) {
ColouredName = p.PlayerName;
Name = Utils.StripColours( p.PlayerName );
Id = p.NameId;
}
}

public override string GetNameUnder( int mouseX, int mouseY ) {
for( int i = 0; i < namesCount; i++ ) {
Texture texture = textures[i];
if( texture.IsValid && texture.Bounds.Contains( mouseX, mouseY ) )
return Utils.StripColours( info[i].Name );
}
return null;
}

protected override void OnSort() {
Expand Down Expand Up @@ -82,81 +54,11 @@ public override void Init() {
Anchor.Centre, Anchor.Centre, font );

base.Init();
game.EntityEvents.TabListEntryAdded += TabyEntryAdded;
game.EntityEvents.TabListEntryRemoved += TabEntryRemoved;
game.EntityEvents.TabListEntryChanged += TabEntryChanged;
}

public override void Dispose() {
base.Dispose();
overview.Dispose();
game.EntityEvents.TabListEntryAdded -= TabyEntryAdded;
game.EntityEvents.TabListEntryChanged -= TabEntryChanged;
game.EntityEvents.TabListEntryRemoved -= TabEntryRemoved;
}

protected override void CreateInitialPlayerInfo() {
TabListEntry[] entries = game.TabList.Entries;
for( int i = 0; i < entries.Length; i++ ) {
TabListEntry e = entries[i];
if( e != null )
AddPlayerInfo( new PlayerInfo( e ), -1 );
}
}

void AddPlayerInfo( PlayerInfo pInfo, int index ) {
DrawTextArgs args = new DrawTextArgs( pInfo.ColouredName, font, false );
Texture tex = game.Drawer2D.MakeChatTextTexture( ref args, 0, 0 );
game.Drawer2D.ReducePadding( ref tex, Utils.Floor( font.Size ), 3 );

if( index < 0 ) {
info[namesCount] = pInfo;
textures[namesCount] = tex;
namesCount++;
} else {
info[index] = pInfo;
textures[index] = tex;
}
}

void TabyEntryAdded( object sender, IdEventArgs e ) {
AddPlayerInfo( new PlayerInfo( game.TabList.Entries[e.Id] ), -1 );
SortPlayerInfo();
}

void TabEntryChanged( object sender, IdEventArgs e ) {
for( int i = 0; i < namesCount; i++ ) {
PlayerInfo pInfo = info[i];
if( pInfo.Id != e.Id ) continue;

Texture tex = textures[i];
api.DeleteTexture( ref tex );
AddPlayerInfo( new PlayerInfo( game.TabList.Entries[e.Id] ), i );
SortPlayerInfo();
return;
}
}

void TabEntryRemoved( object sender, IdEventArgs e ) {
for( int i = 0; i < namesCount; i++ ) {
PlayerInfo pInfo = info[i];
if( pInfo.Id == e.Id ) {
RemoveInfoAt( info, i );
return;
}
}
}

PlayerInfoComparer comparer = new PlayerInfoComparer();
class PlayerInfoComparer : IComparer<PlayerInfo> {

public int Compare( PlayerInfo x, PlayerInfo y ) {
return x.Name.CompareTo( y.Name );
}
}

protected override void SortInfoList() {
Array.Sort( info, textures, 0, namesCount, comparer );
}
}
}
112 changes: 66 additions & 46 deletions ClassicalSharp/2D/Widgets/PlayerList/NormalPlayerListWidget.cs
Original file line number Diff line number Diff line change
@@ -1,80 +1,100 @@
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System;
using System.Collections.Generic;
using System.Drawing;
using ClassicalSharp.Entities;
using ClassicalSharp.Events;
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System;
using System.Collections.Generic;
using System.Drawing;
using ClassicalSharp.Entities;
using ClassicalSharp.Events;

namespace ClassicalSharp.Gui {

public sealed class NormalPlayerListWidget : PlayerListWidget {
public class NormalPlayerListWidget : PlayerListWidget {

public NormalPlayerListWidget( Game game, Font font ) : base( game, font ) {
textures = new Texture[256];
}

PlayerInfo[] info = new PlayerInfo[256];
PlayerInfo[] info = new PlayerInfo[256];
class PlayerInfo {

public string Name;
public byte PlayerId;
public string Name, ColouredName;
public byte Id;

public PlayerInfo( Player p ) {
Name = Utils.StripColours( p.DisplayName );
PlayerId = p.ID;
public PlayerInfo( TabListEntry p ) {
ColouredName = p.PlayerName;
Name = Utils.StripColours( p.PlayerName );
Id = p.NameId;
}
}

public override string GetNameUnder( int mouseX, int mouseY ) {
for( int i = 0; i < namesCount; i++ ) {
Texture texture = textures[i];
if( texture.IsValid && texture.Bounds.Contains( mouseX, mouseY ) )
return Utils.StripColours( info[i].Name );
}
return null;
}

public override void Init() {
base.Init();
game.EntityEvents.Added += PlayerSpawned;
game.EntityEvents.Removed += PlayerDespawned;
game.EntityEvents.TabListEntryAdded += TabEntryAdded;
game.EntityEvents.TabListEntryRemoved += TabEntryRemoved;
game.EntityEvents.TabListEntryChanged += TabEntryChanged;
}

public override void Dispose() {
base.Dispose();
game.EntityEvents.Added -= PlayerSpawned;
game.EntityEvents.Removed -= PlayerDespawned;
}

void PlayerSpawned( object sender, IdEventArgs e ) {
Player player = game.Entities[e.Id];
AddPlayerInfo( player );
SortPlayerInfo();
game.EntityEvents.TabListEntryAdded -= TabEntryAdded;
game.EntityEvents.TabListEntryChanged -= TabEntryChanged;
game.EntityEvents.TabListEntryRemoved -= TabEntryRemoved;
}

protected override void CreateInitialPlayerInfo() {
for( int i = 0; i < EntityList.MaxCount; i++ ) {
Player player = game.Entities[i];
if( player != null ) {
AddPlayerInfo( player );
}
TabListEntry[] entries = game.TabList.Entries;
for( int i = 0; i < entries.Length; i++ ) {
TabListEntry e = entries[i];
if( e != null )
AddPlayerInfo( new PlayerInfo( e ), -1 );
}
}

public override string GetNameUnder( int mouseX, int mouseY ) {
for( int i = 0; i < namesCount; i++ ) {
Texture texture = textures[i];
if( texture.IsValid && texture.Bounds.Contains( mouseX, mouseY ) )
return Utils.StripColours( info[i].Name );
}
return null;
}

void AddPlayerInfo( Player player ) {
DrawTextArgs args = new DrawTextArgs( player.DisplayName, font, true );
void AddPlayerInfo( PlayerInfo pInfo, int index ) {
DrawTextArgs args = new DrawTextArgs( pInfo.ColouredName, font, false );
Texture tex = game.Drawer2D.MakeChatTextTexture( ref args, 0, 0 );
game.Drawer2D.ReducePadding( ref tex, Utils.Floor( font.Size ), 3 );

info[namesCount] = new PlayerInfo( player );
textures[namesCount] = tex;
namesCount++;
if( index < 0 ) {
info[namesCount] = pInfo;
textures[namesCount] = tex;
namesCount++;
} else {
info[index] = pInfo;
textures[index] = tex;
}
}

void TabEntryAdded( object sender, IdEventArgs e ) {
AddPlayerInfo( new PlayerInfo( game.TabList.Entries[e.Id] ), -1 );
SortPlayerInfo();
}

void TabEntryChanged( object sender, IdEventArgs e ) {
for( int i = 0; i < namesCount; i++ ) {
PlayerInfo pInfo = info[i];
if( pInfo.Id != e.Id ) continue;

Texture tex = textures[i];
api.DeleteTexture( ref tex );
AddPlayerInfo( new PlayerInfo( game.TabList.Entries[e.Id] ), i );
SortPlayerInfo();
return;
}
}

void PlayerDespawned( object sender, IdEventArgs e ) {
void TabEntryRemoved( object sender, IdEventArgs e ) {
for( int i = 0; i < namesCount; i++ ) {
PlayerInfo pInfo = info[i];
if( pInfo.PlayerId == e.Id ) {
if( pInfo.Id == e.Id ) {
RemoveInfoAt( info, i );
return;
}
Expand All @@ -89,8 +109,8 @@ public int Compare( PlayerInfo x, PlayerInfo y ) {
}
}

protected override void SortInfoList() {
Array.Sort( info, textures, 0, namesCount, comparer );
protected override void SortInfoList() {
Array.Sort( info, textures, 0, namesCount, comparer );
}
}
}
1 change: 1 addition & 0 deletions ClassicalSharp/ClassicalSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
<Compile Include="Entities\LocalPlayer.cs" />
<Compile Include="Entities\LocationUpdate.cs" />
<Compile Include="Entities\NetPlayer.cs" />
<Compile Include="Entities\TabList.cs" />
<Compile Include="Events\EntityEvents.cs" />
<Compile Include="Events\Events.cs" />
<Compile Include="Events\UserEvents.cs" />
Expand Down
4 changes: 2 additions & 2 deletions ClassicalSharp/Network/NetworkProcessor.CPE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,10 @@ internal void HandleHackControl() {
}

internal void HandleExtAddEntity2() {
byte entityId = reader.ReadUInt8();
byte id = reader.ReadUInt8();
string displayName = reader.ReadAsciiString();
string skinName = reader.ReadAsciiString();
AddEntity( entityId, displayName, skinName, true );
AddEntity( id, displayName, skinName, true );
}

const int bulkCount = 256;
Expand Down
34 changes: 17 additions & 17 deletions ClassicalSharp/Network/NetworkProcessor.Original.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,39 +213,39 @@ internal void HandleEntityTeleport() {
}

internal void HandleRelPosAndOrientationUpdate() {
byte playerId = reader.ReadUInt8();
byte id = reader.ReadUInt8();
float x = reader.ReadInt8() / 32f;
float y = reader.ReadInt8() / 32f;
float z = reader.ReadInt8() / 32f;

float yaw = (float)Utils.PackedToDegrees( reader.ReadUInt8() );
float pitch = (float)Utils.PackedToDegrees( reader.ReadUInt8() );
LocationUpdate update = LocationUpdate.MakePosAndOri( x, y, z, yaw, pitch, true );
UpdateLocation( playerId, update, true );
UpdateLocation( id, update, true );
}

internal void HandleRelPositionUpdate() {
byte playerId = reader.ReadUInt8();
byte id = reader.ReadUInt8();
float x = reader.ReadInt8() / 32f;
float y = reader.ReadInt8() / 32f;
float z = reader.ReadInt8() / 32f;

LocationUpdate update = LocationUpdate.MakePos( x, y, z, true );
UpdateLocation( playerId, update, true );
UpdateLocation( id, update, true );
}

internal void HandleOrientationUpdate() {
byte playerId = reader.ReadUInt8();
byte id = reader.ReadUInt8();
float yaw = (float)Utils.PackedToDegrees( reader.ReadUInt8() );
float pitch = (float)Utils.PackedToDegrees( reader.ReadUInt8() );

LocationUpdate update = LocationUpdate.MakeOri( yaw, pitch );
UpdateLocation( playerId, update, true );
UpdateLocation( id, update, true );
}

internal void HandleRemoveEntity() {
byte entityId = reader.ReadUInt8();
RemoveEntity( entityId );
byte id = reader.ReadUInt8();
RemoveEntity( id );
}

internal void HandleMessage() {
Expand All @@ -271,16 +271,16 @@ internal void HandleSetPermission() {
game.LocalPlayer.Hacks.SetUserType( reader.ReadUInt8() );
}

void AddEntity( byte entityId, string displayName, string skinName, bool readPosition ) {
void AddEntity( byte id, string displayName, string skinName, bool readPosition ) {
skinName = Utils.StripColours( skinName );
if( entityId != 0xFF ) {
Player oldPlayer = game.Entities[entityId];
if( id != 0xFF ) {
Player oldPlayer = game.Entities[id];
if( oldPlayer != null ) {
game.EntityEvents.RaiseRemoved( entityId );
game.EntityEvents.RaiseRemoved( id );
oldPlayer.Despawn();
}
game.Entities[entityId] = new NetPlayer( displayName, skinName, game, entityId );
game.EntityEvents.RaiseAdded( entityId );
game.Entities[id] = new NetPlayer( displayName, skinName, game, id );
game.EntityEvents.RaiseAdded( id );
} else {
// Server is only allowed to change our own name colours.
if( Utils.StripColours( displayName ) != game.Username )
Expand All @@ -290,12 +290,12 @@ void AddEntity( byte entityId, string displayName, string skinName, bool readPos
game.LocalPlayer.UpdateName();
}

string identifier = game.Entities[entityId].SkinIdentifier;
string identifier = game.Entities[id].SkinIdentifier;
game.AsyncDownloader.DownloadSkin( identifier, skinName );
if( !readPosition ) return;

ReadAbsoluteLocation( entityId, false );
if( entityId == 0xFF ) {
ReadAbsoluteLocation( id, false );
if( id == 0xFF ) {
LocalPlayer p = game.LocalPlayer;
p.Spawn = p.Position;
p.SpawnYaw = p.HeadYawDegrees;
Expand Down

0 comments on commit 6c3f72c

Please sign in to comment.