-
Notifications
You must be signed in to change notification settings - Fork 30
添加插件: 设置重生点 1.0.0.1 #273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
添加插件: 设置重生点 1.0.0.1 #273
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <Import Project="..\template.targets" /> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| using System.Reflection; | ||
| using Terraria; | ||
| using TerrariaApi.Server; | ||
| using TShockAPI; | ||
| using TShockAPI.Hooks; | ||
| using Main = Terraria.Main; | ||
| using Version = System.Version; | ||
| using Color = Microsoft.Xna.Framework.Color; | ||
| using System.Net.Sockets; | ||
|
|
||
| namespace Plugin | ||
| { | ||
| [ApiVersion(2, 1)] | ||
| public class Plugin : TerrariaPlugin | ||
| { | ||
| private TerrariaPlugin join; | ||
| public override string Author => "cmgy"; | ||
| public override string Description => ""; | ||
| public override string Name => "BedSet 床设置"; | ||
| public override Version Version => new(1, 0, 0, 1); | ||
| public Plugin(Main game) : base(game) { } | ||
|
|
||
| //hook | ||
| public override void Initialize() | ||
| { | ||
| GetDataHandlers.PlayerUpdate.Register(backbed);//使用回城物品 set | ||
| PlayerHooks.PlayerPostLogin += PlayerHooks_PlayerPostLogin;//登录返回床位置 login | ||
| GetDataHandlers.KillMe.Register(deadafter);//死亡返回床位置 death | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这不该是Spawn事件吗
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不知道,不过改了之后用起来挺怪
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
应该放到Spawn事件处理,这样写不会一死就复活吧。
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 可以多问问羽学他有经验
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
没一死就复活吧,毕竟我写了个while等着玩家复活(
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
测试了下只用spawn的,最大缺点是得等魔镜之类的使用时间结束才触发 因为没接触过spawn事件所以以上特性在我写的时候都没遇到(
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
1不在第1次触发时触发,2只在第1次触发可以互补,不过写起来应该和单独分成3个钩没差多少了
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PlayerSpawnContext判断这个
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spawn事件里 |
||
| } | ||
| //set | ||
| private async void backbed(object? sender, GetDataHandlers.PlayerUpdateEventArgs e) | ||
| { | ||
| //魔镜 | ||
| if (e.Player.TPlayer.controlUseItem && | ||
| (e.Player.SelectedItem.netID == 50 || | ||
| e.Player.SelectedItem.netID == 3199 || | ||
| e.Player.SelectedItem.netID == 3124 || | ||
| e.Player.SelectedItem.netID == 5358 | ||
| ) | ||
| ) | ||
| { | ||
| await Task.Delay(300); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. await 开个线程 |
||
| string path = $@".\重生点\{e.Player.Name}{Main.worldName}重生点.txt"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
{e.Player.Name}{Main.worldName}已经是变量吧
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
你不觉得重复太多代码了吗
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 现在有个问题 |
||
| if (!Directory.Exists(@".\重生点")) | ||
| { | ||
| Directory.CreateDirectory(@".\重生点"); | ||
| } | ||
| if (!File.Exists(path)) | ||
| { | ||
| FileStream bed = File.Create($@".\重生点\{e.Player.Name}{Main.worldName}重生点.txt"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 一样 |
||
| StreamWriter xy = new StreamWriter(bed); | ||
| xy.Close(); | ||
| bed.Close(); | ||
| } | ||
| FileStream clear = new FileStream($@".\重生点\{e.Player.Name}{Main.worldName}重生点.txt", FileMode.Truncate, FileAccess.ReadWrite); | ||
|
Controllerdestiny marked this conversation as resolved.
Outdated
|
||
| clear.Close(); | ||
| e.Player.SendData(PacketTypes.CreateCombatTextExtended, $"updata", 255255255, e.Player.X, e.Player.Y, 3000); | ||
| NetMessage.PlayNetSound(new NetMessage.NetSoundInfo(e.Player.TPlayer.Center, 159), e.Player.Index, -1); | ||
| StreamWriter set = File.AppendText($@".\重生点\{e.Player.Name}{Main.worldName}重生点.txt"); | ||
| set.WriteLine($"{e.Player.TileX+1} {e.Player.TileY}"); | ||
| set.Close(); | ||
| } | ||
| //回忆 | ||
| if (e.Player.TPlayer.controlUseItem && | ||
| (e.Player.SelectedItem.netID == 2350 || | ||
| e.Player.SelectedItem.netID == 4870) | ||
| ) | ||
| { | ||
| await Task.Delay(300); | ||
| string path = $@".\重生点\{e.Player.Name}{Main.worldName}重生点.txt"; | ||
| if (!Directory.Exists(@".\重生点")) | ||
| { | ||
| Directory.CreateDirectory(@".\重生点"); | ||
| } | ||
| if (!File.Exists(path)) | ||
| { | ||
| FileStream bed = File.Create($@".\重生点\{e.Player.Name}{Main.worldName}重生点.txt"); | ||
| StreamWriter xy = new StreamWriter(bed); | ||
| xy.Close(); | ||
| bed.Close(); | ||
| } | ||
| FileStream clear = new FileStream($@".\重生点\{e.Player.Name}{Main.worldName}重生点.txt", FileMode.Truncate, FileAccess.ReadWrite); | ||
| clear.Close(); | ||
| e.Player.SendData(PacketTypes.CreateCombatTextExtended, $"updata", 255255255, e.Player.X, e.Player.Y, 3000); | ||
| NetMessage.PlayNetSound(new NetMessage.NetSoundInfo(e.Player.TPlayer.Center, 159), e.Player.Index, -1); | ||
| StreamWriter set = File.AppendText($@".\重生点\{e.Player.Name}{Main.worldName}重生点.txt"); | ||
| set.WriteLine($"{e.Player.TileX+1} {e.Player.TileY}"); | ||
| set.Close(); | ||
| } | ||
| } | ||
| //login | ||
| private async void PlayerHooks_PlayerPostLogin(PlayerPostLoginEventArgs e) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不建议异步 |
||
| { | ||
| while (true) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个Whiletrue很爆炸 |
||
| { | ||
| await Task.Delay(1000); | ||
| if (e.Player.X != 0) | ||
| { | ||
| string path = $@".\重生点\{e.Player.Name}{Main.worldName}重生点.txt"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这种SQL存可能会更好,而且路径不要放中文,不然任意爆掉,不要用worldName,用WorldID,也不要用PlayerName,用AccountID |
||
| if (!Directory.Exists(@".\重生点")) | ||
| { | ||
| Directory.CreateDirectory(@".\重生点"); | ||
| } | ||
| if (!File.Exists(path)) | ||
| { | ||
| FileStream bed = File.Create($@".\重生点\{e.Player.Name}{Main.worldName}重生点.txt"); | ||
| StreamWriter xy = new StreamWriter(bed); | ||
| xy.Close(); | ||
| bed.Close(); | ||
| } | ||
| string[] line = File.ReadAllLines($@".\重生点\{e.Player.Name}{Main.worldName}重生点.txt"); | ||
| if (line.Length > 0) | ||
| { | ||
| string tile = line[0]; | ||
| string x = tile.Remove(4, tile.Length - 4); | ||
| string y = tile.Remove(0, tile.Length - 4); | ||
| string x2 = x.Replace(" ", ""); | ||
| string y2 = y.Replace(" ", ""); | ||
| int x3 = Convert.ToInt32(x2); | ||
| int y3 = Convert.ToInt32(y2); | ||
| e.Player.SendInfoMessage($" 传送至重生点,记得重新保存床重生点"); | ||
| e.Player.Teleport(x3 * 16, y3 * 16, 6); | ||
| Color c = new Color(0, 169, 255); | ||
| e.Player.SendData(PacketTypes.CreateCombatTextExtended, "return", (int)c.packedValue, e.Player.X, e.Player.Y - 16); | ||
| NetMessage.PlayNetSound(new NetMessage.NetSoundInfo(e.Player.TPlayer.Center, 114), e.Player.Index, -1); | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| //death | ||
| private async void deadafter(object? sender, GetDataHandlers.KillMeEventArgs e) | ||
| { | ||
| while (true) | ||
| { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不要while和异步 |
||
| await Task.Delay(500); | ||
| if (e.Player.Dead == false) | ||
| { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 读取保存位置建议单独出一个方法,这里重复太多了,而且位置必须缓存
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
位置确实)考虑到一个人物逛多个地图的情况
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不是,你缓存到内存里,不要一直从磁盘读数据,一般是建议服务器启动和Reload的时候才从文件读,其余全放内存里 |
||
| string path = $@".\重生点\{e.Player.Name}{Main.worldName}重生点.txt"; | ||
| if (!Directory.Exists(@".\重生点")) | ||
| { | ||
| Directory.CreateDirectory(@".\重生点"); | ||
| } | ||
| if (!File.Exists(path)) | ||
| { | ||
| FileStream bed = File.Create($@".\重生点\{e.Player.Name}{Main.worldName}重生点.txt"); | ||
| StreamWriter xy = new StreamWriter(bed); | ||
| xy.Close(); | ||
| bed.Close(); | ||
| } | ||
| string[] line = File.ReadAllLines($@".\重生点\{e.Player.Name}{Main.worldName}重生点.txt"); | ||
| if (line.Length > 0) | ||
| { | ||
| string tile = line[0]; | ||
| string x = tile.Remove(4, tile.Length - 4); | ||
| string y = tile.Remove(0, tile.Length - 4); | ||
| string x2 = x.Replace(" ", ""); | ||
| string y2 = y.Replace(" ", ""); | ||
| int x3 = Convert.ToInt32(x2); | ||
| int y3 = Convert.ToInt32(y2); | ||
| e.Player.Teleport(x3 * 16, y3 * 16, 6); | ||
| Color c = new Color(0, 169, 255); | ||
| e.Player.SendData(PacketTypes.CreateCombatTextExtended, "return", (int)c.packedValue, e.Player.X, e.Player.Y - 16); | ||
| NetMessage.PlayNetSound(new NetMessage.NetSoundInfo(e.Player.TPlayer.Center, 114), e.Player.Index, -1); | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| protected override void Dispose(bool disposing) | ||
| { | ||
| if (disposing) | ||
| { | ||
| var asm = Assembly.GetExecutingAssembly(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 有点多余了 |
||
| Commands.ChatCommands.RemoveAll(c => c.CommandDelegate.Method?.DeclaringType?.Assembly == asm); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这插件他没指令 |
||
| } | ||
| base.Dispose(disposing); | ||
| } | ||
|
|
||
| private class List | ||
| { | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # BedSet 设置重生点 | ||
|
|
||
| - 作者: pang2333 | ||
| - 使用回城物品记录重生点,在退出及死亡后传送 | ||
| - 无权限,无指令 | ||
|
|
||
|
|
||
| ## 更新日志 | ||
|
|
||
| ``` | ||
| 1.0.0.1 | ||
| 修正位置偏移 | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
| ## 文件位置 | ||
| > tshock/重生点/{PlayerName}{WorldName}.txt | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这文件名改一下不要。Class1.cs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好经典的名字