Tshock插件编程资料
服务器功能:
关闭服务器:TShock.Utils.StopServer(true,"服务器已关闭");
地图名字:Main.worldName
修改地图名称:Main.instance.SetWorldName();
修改地图类型:Main.GameMode="0-普通"/"1-专家"/"2-大师"/"3-旅途";
修改地图困难模式:Main.hardMode=true/false;
断开用户连接:plr.Disconnect(reason);
获取物品英文名称:args.Player.TPlayer.inventory[args.SelectedItem].Name
弹药= Terraria.Lang.GetProjectileName(弹药ID).ToString();
物品名 = Terraria.Lang.GetItemNameValue(物品ID);
获取用户数据库ID=TShock.UserAccounts.GetUserAccountByName(plr_name).ID
用户组切换:TShock.UserAccounts.SetUserGroup(TShock.UserAccounts.GetUserAccountByName(TShock.Players[args.Who].Name), "vip");
获取用户组名:args.Player.Group.Name;
获取用户名:TShock.Players[args.Who].Name
用户是否登录:plr.IsLoggedIn
系统广播:TShock.Utils.Broadcast("广播内容",Color.Yellow);
类系统广播:TSPlayer.All.SendInfoMessage(message.Text);
头部显示:plr.SendData(PacketTypes.CreateCombatTextExtended, string.Format("+{0}", num), (int)Color.Gold.PackedValue, plr.X, plr.Y);
执行命令:Commands.HandleCommand("/help");
玩家当前位置:tsplayer.TileX;tsplayer.TileY;
录入日志:TShock.Log.ConsoleInfo("日志");
向用户发出信息:args.Player.SendMessage("向用户发出信息");
传送用户:plr.Teleport(Main.spawnTileX * 16, (Main.spawnTileY - 4) * 16, 1);
杀死用户:tsplayer.KillPlayer();
石化用户:player.Disable($"持有禁用物品: {player.TPlayer.inventory[player.TPlayer.selectedItem].Name}", flags);
冻结player.SetBuff(BuffID.Frozen, 300, true);
石化player.SetBuff(BuffID.Stoned, 300, true);
缠住player.SetBuff(BuffID.Webbed, 300, true);
窒息plr.SetBuff(BuffID.Suffocation, 300, true);
获取当前血量TShock.Players[args.Who].TPlayer.statLife;
设置血量NetMessage.SendData((int)PacketTypes.PlayerHp, -1, -1, NetworkText.FromLiteral(""), plr.Index);
获取物品名称:Terraria.Lang.GetItemNameValue(i)
获取全部在线用户:
foreach (TSPlayer tsplayer in TShock.Players){if (tsplayer != null){Console.WriteLine(tsplayer.Name);}}
数据库操作:
①判断数据库类型
string query;
if ((int)DbExt.GetSqlType(TShock.DB) == 1)
{//sqlit}
else
{//mysql}
TShock.DB.Query(query);
②读取SQL
string query = "select * from regions;";
QueryResult queryResult = TShock.DB.QueryReader(query);
while(queryResult.Read())
{
int x = queryResult.Get<int>("X1");
int y = queryResult.Get<int>("Y1");
}
③执行SQL
string query = "select * from regions;";
TShock.DB.Query();
c#
延时:System.Threading.Thread.Sleep(s);
当前时间DateTime.Now;
UTC时间DateTime.UtcNow;
增加时间DateTime.UtcNow.AddHours(封禁时长)
带T的日期格式DateTime.Now.ToString("s");
格式化日期显示:string.Format("{0:yyyyMMddHHmmssffff}",dt);
//二选一,PadLeft是在左边填空格,PadRight是在右边填空格
str.PadLeft(16);
str.PadRight(16);
List升序排序:list.Sort();
List反转顺序:list.Reverse();
遍历list
List<string> 列表 = new List<string>();
列表.ForEach(item => Console.Write(item + "|"));
删除集合的两种方法:
①
plrlist.RemoveAt(list.FindIndex(t => t.姓名 == TShock.Players[args.Who].Name););
②
foreach (var temp in list)
{if (temp.姓名 == TShock.Players[args.Who].Name);
plrlist.Remove(temp);
}
//检测是否数值
int 用户id;
if (!int.TryParse(参数[1], out 用户id)){输入参数不是数值}
//当前应用程序文件名及目录
string 完整目录=Process.GetCurrentProcess().MainModule.FileNam;
string 仅目录=Path.GetDirectoryName(完整目录) + "\\";
string 仅文件名=Path.GetFileName(完整目录);
Commands.ChatCommands.Add(
new Command("ghost.ghost", //权限
new CommandDelegate(this.OnGhost),//处理函数
new string[] { "ghost", "vanish" })//命令
);
GeneralHooks.ReloadEvent += this.OnReload;//重载配置
ServerApi.Hooks.ServerChat.Register(this, OnChat);//聊天时
ServerApi.Hooks.NetGreetPlayer.Register(this, OnLine);//进入时
private void OnLine(GreetPlayerEventArgs args)
ServerApi.Hooks.ServerLeave.Register(this, OffLine);//退出时
ServerApi.Hooks.ServerJoin.Register(this, new HookHandler<JoinEventArgs>(this.OnLine));//上线时间
ServerApi.Hooks.ServerLeave.Register(this, new HookHandler<LeaveEventArgs>(this.OffLine));//下线事件
PlayerHooks.PlayerPostLogin += this.用户登录;
private void 登录时(GreetPlayerEventArgs args)
{
if (TShock.Players[args.Who] != null)
{int plrlistid = plrlist.FindIndex(t => t.姓名 == TShock.Players[args.Who].Name);
if (plrlistid < 0) {plrlist.Add(new PLR(TShock.Players[args.Who].Name, false, -1, -1, -1, -1));}
}
}
private void 离线时(LeaveEventArgs args)
{
if (TShock.Players[args.Who] != null)
{
if (plrlist.Count > 0)
{int plrlistid = plrlist.FindIndex(t => t.姓名 == TShock.Players[args.Who].Name);
if (plrlistid != -1)
{plrlist.RemoveAt(plrlistid);}
}
}
}
Array.IndexOf(数组, 查找项目);//在数组中查找及返回索引,未找到返回-1
List.indexof();查找并返回列表中的值;
多线程
for (var i = 0; i < 100; i++)
{
Thread.Sleep(100);
ThreadPool.QueueUserWorkItem(o => arrWorkMen[i].doSomething());
}
字典排序
备份文件列表 = (from item in 备份文件列表 orderby item.Key descending select item).ToDictionary(item => item.Key, item => item.Value);
扫描二维码推送至手机访问