Skip to content

Commit

Permalink
Merge pull request #2944 from AgaSpace/tsplayer-changes
Browse files Browse the repository at this point in the history
TSPlayer update
  • Loading branch information
QuiCM authored Jun 6, 2023
2 parents c67d5cf + b30a8cb commit d00e78e
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 16 deletions.
4 changes: 0 additions & 4 deletions TShockAPI/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6740,10 +6740,6 @@ private static void ToggleGodMode(CommandArgs args)

playerToGod.GodMode = !playerToGod.GodMode;

var godPower = CreativePowerManager.Instance.GetPower<CreativePowers.GodmodePower>();

godPower.SetEnabledState(playerToGod.Index, playerToGod.GodMode);

if (playerToGod != args.Player)
{
args.Player.SendSuccessMessage(playerToGod.GodMode
Expand Down
105 changes: 94 additions & 11 deletions TShockAPI/TSPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ You should have received a copy of the GNU General Public License
using TShockAPI.Net;
using Timer = System.Timers.Timer;
using System.Linq;
using Terraria.GameContent.Creative;

namespace TShockAPI
{
Expand Down Expand Up @@ -935,9 +936,15 @@ public bool HasModifiedIceSuccessfully(int x, int y, short tileType, GetDataHand
public bool LoginHarassed = false;

/// <summary>
/// Player cant die, unless onehit
/// Controls the journey godmode
/// </summary>
public bool GodMode = false;
public bool GodMode
{
get =>
CreativePowerManager.Instance.GetPower<CreativePowers.GodmodePower>().IsEnabledForPlayer(Index);
set =>
CreativePowerManager.Instance.GetPower<CreativePowers.GodmodePower>().SetEnabledState(Index, value);
}

/// <summary>
/// Players controls are inverted if using SSC
Expand Down Expand Up @@ -981,7 +988,7 @@ public bool ConnectionAlive
get
{
return RealPlayer
&& (Netplay.Clients[Index] != null && Netplay.Clients[Index].IsActive && !Netplay.Clients[Index].PendingTermination);
&& (Client != null && Client.IsActive && !Client.PendingTermination);
}
}

Expand All @@ -998,16 +1005,16 @@ public Item SelectedItem
/// </summary>
public int State
{
get { return Netplay.Clients[Index].State; }
set { Netplay.Clients[Index].State = value; }
get { return Client.State; }
set { Client.State = value; }
}

/// <summary>
/// Gets the player's UUID.
/// </summary>
public string UUID
{
get { return RealPlayer ? Netplay.Clients[Index].ClientUUID : ""; }
get { return RealPlayer ? Client.ClientUUID : ""; }
}

/// <summary>
Expand All @@ -1019,8 +1026,8 @@ public string IP
{
if (string.IsNullOrEmpty(CacheIP))
return
CacheIP = RealPlayer ? (Netplay.Clients[Index].Socket.IsConnected()
? TShock.Utils.GetRealIP(Netplay.Clients[Index].Socket.GetRemoteAddress().ToString())
CacheIP = RealPlayer ? (Client.Socket.IsConnected()
? TShock.Utils.GetRealIP(Client.Socket.GetRemoteAddress().ToString())
: "")
: "127.0.0.1";
else
Expand Down Expand Up @@ -1103,6 +1110,11 @@ public bool SendServerCharacter()

}

/// <summary>
/// Player RemoteClient.
/// </summary>
public RemoteClient Client => Netplay.Clients[Index];

/// <summary>
/// Gets the Terraria Player object associated with the player.
/// </summary>
Expand Down Expand Up @@ -1135,6 +1147,11 @@ public int Team
get { return TPlayer.team; }
}

/// <summary>
/// Gets PvP player mode.
/// </summary>
public bool Hostile => TPlayer.hostile;

/// <summary>
/// Gets the player's X coordinate.
/// </summary>
Expand Down Expand Up @@ -1487,6 +1504,41 @@ public virtual bool SendTileRect(short x, short y, byte width = 10, byte length
return false;
}

/// <summary>
/// Changes the values of the <see cref="RemoteClient.TileSections"/> array.
/// </summary>
/// <param name="rectangle">The area of the sections you want to set a value to.
/// The minimum size should be set to 200x150. If null, then the entire map is specified.</param>
/// <param name="isLoaded">Is the section loaded.</param>
// The server does not send the player the whole world, it sends it in sections. To do this, it sets up visible and invisible sections.
// If the player was not in any section(Client.TileSections[x, y] == false) then the server will send the missing section of the world.
// This method allows you to simulate what the player has or has not seen these sections.
// For example, we can put some number of earths blocks in some vast area, for example, for the whole world, but the player will not see the changes, because some section is already loaded for him. At this point this method can come into effect! With it we will be able to select some zone and make it both visible and invisible to the player.
// The server will assume that the zone is not loaded on the player, and will resend the data, but with earth blocks.
public void UpdateSection(Rectangle? rectangle = null, bool isLoaded = false)
{
if (rectangle.HasValue)
{
for (int i = Netplay.GetSectionX(rectangle.Value.X); i < Netplay.GetSectionX(rectangle.Value.X + rectangle.Value.Width) && i < Main.maxSectionsX; i++)
{
for (int j = Netplay.GetSectionY(rectangle.Value.Y); j < Netplay.GetSectionY(rectangle.Value.Y + rectangle.Value.Height) && j < Main.maxSectionsY; j++)
{
Client.TileSections[i, j] = isLoaded;
}
}
}
else
{
for (int i = 0; i < Main.maxSectionsX; i++)
{
for (int j = 0; j < Main.maxSectionsY; j++)
{
Client.TileSections[i, j] = isLoaded;
}
}
}
}

/// <summary>
/// Gives an item to the player. Includes banned item spawn prevention to check if the player can spawn the item.
/// </summary>
Expand Down Expand Up @@ -1519,6 +1571,15 @@ public virtual void GiveItem(int type, int stack, int prefix = 0)
GiveItemByDrop(type, stack, prefix);
}

/// <summary>
/// Gives an item to the player.
/// </summary>
/// <param name="item">Item with data to be given to the player.</param>
public virtual void GiveItem(NetItem item)
{
GiveItem(item.NetId, item.Stack, item.PrefixId);
}

private Item EmptySentinelItem = new Item();

private bool Depleted(Item item)
Expand Down Expand Up @@ -1815,15 +1876,34 @@ public void SendFileTextAsMessage(string file)
/// <param name="damage">The amount of damage the player will take.</param>
public virtual void DamagePlayer(int damage)
{
NetMessage.SendPlayerHurt(Index, PlayerDeathReason.LegacyDefault(), damage, (new Random()).Next(-1, 1), false, false, 0, -1, -1);
DamagePlayer(damage, PlayerDeathReason.LegacyDefault());
}

/// <summary>
/// Wounds the player with the given damage.
/// </summary>
/// <param name="damage">The amount of damage the player will take.</param>
/// <param name="reason">The reason for causing damage to player.</param>
public virtual void DamagePlayer(int damage, PlayerDeathReason reason)
{
NetMessage.SendPlayerHurt(Index, reason, damage, (new Random()).Next(-1, 1), false, false, 0, -1, -1);
}

/// <summary>
/// Kills the player.
/// </summary>
public virtual void KillPlayer()
{
NetMessage.SendPlayerDeath(Index, PlayerDeathReason.LegacyDefault(), 99999, (new Random()).Next(-1, 1), false, -1, -1);
KillPlayer(PlayerDeathReason.LegacyDefault());
}

/// <summary>
/// Kills the player.
/// </summary>
/// <param name="reason">Reason for killing a player.</param>
public virtual void KillPlayer(PlayerDeathReason reason)
{
NetMessage.SendPlayerDeath(Index, reason, 99999, (new Random()).Next(-1, 1), false, -1, -1);
}

/// <summary>
Expand All @@ -1832,6 +1912,8 @@ public virtual void KillPlayer()
/// <param name="team">The team color index.</param>
public virtual void SetTeam(int team)
{
if (team < 0 || team >= Main.teamColor.Length)
throw new ArgumentException("The player's team is not in the range of available.");
Main.player[Index].team = team;
NetMessage.SendData((int)PacketTypes.PlayerTeam, -1, -1, NetworkText.Empty, Index);
}
Expand All @@ -1840,6 +1922,7 @@ public virtual void SetTeam(int team)
/// Sets the player's pvp.
/// </summary>
/// <param name="mode">The state of the pvp mode.</param>
/// <param name="withMsg">Whether a chat message about the change should be sent.</param>
public virtual void SetPvP(bool mode, bool withMsg = false)
{
Main.player[Index].hostile = mode;
Expand Down Expand Up @@ -2064,7 +2147,7 @@ public virtual void SendRawData(byte[] data)
if (!RealPlayer || !ConnectionAlive)
return;

Netplay.Clients[Index].Socket.AsyncSend(data, 0, data.Length, Netplay.Clients[Index].ServerWriteCallBack);
Client.Socket.AsyncSend(data, 0, data.Length, Client.ServerWriteCallBack);
}

/// <summary>
Expand Down
11 changes: 10 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,16 @@ Use past tense when adding new entries; sign your name off when you add or chang
* If there is no section called "Upcoming changes" below this line, please add one with `## Upcoming changes` as the first line, and then a bulleted item directly after with the first change. -->

## Upcoming changes
Your changes could be here!
* Updated `TSPlayer.GodMode`. (@AgaSpace)
* Previously the field was used as some kind of dataset changed by /godmode command, but now it is a property that receives/changes data in journey mode.
* Added the `TSPlayer.Client` property. It allows the developer to get the `RemoteClient` player, without an additional call to `Terraria.Netplay.Clients`. (@AgaSpace)
* Updated the documentation for the `TSPlayer.SetPvP` method. The `sendMsg` parameter, which is responsible for sending a pvp mode change message, was not documented earlier. (@AgaSpace)
* Added methods `TSPlayer.KillPlayer` and `TSPlayer.DamagePlayer` for which you can specify the cause (`PlayerDeathReason`) in parameters.
* Added an error when trying to change a `TSPlayer` team to, say, 9, when there are only 6. (@AgaSpace)
* Added an error when trying to call the `TSPlayer.SetTeam` method with an argument (team) greater than 5 or less than 0. (@AgaSpace)
* Added a method `TSPlayer.UpdateSection` with arguments `rectangle` and `isLoaded`, which will load some area from the server to the player. (@AgaSpace)
* Added a method `TSPlayer.GiveItem`, which has `TShockAPI.NetItem` structure in its arguments. (@AgaSpace)
* Added a property `TSPlayer.Hostile`, which gets pvp player mode.

## TShock 5.2
* An additional option `pvpwithnoteam` is added at `PvPMode` to enable PVP with no team. (@CelestialAnarchy, #2617, @ATFGK)
Expand Down

0 comments on commit d00e78e

Please sign in to comment.