feat: 添加称号授予功能

源代码来自项目:https://github.com/YF-OFFICE/NewDIR
This commit is contained in:
3cxc
2026-02-12 15:39:44 +08:00
parent 9ab2bd7c86
commit b64a45bfe7
5 changed files with 469 additions and 2 deletions

137
BadgeSystem/Badge.cs Normal file
View File

@@ -0,0 +1,137 @@
using EasyTools.Configs;
using EasyTools.Events;
using GameCore;
using LabApi.Features.Wrappers;
using MEC;
using NewXp.IniApi;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Log = LabApi.Features.Console.Logger;
namespace EasyTools.BadgeSystem
{
public class Badge
{
public static string[] FMoreColo = new string[]
{
"pink",
"silver",
"cyan",
"aqua",
"tomato",
"yellow",
"magenta",
"orange",
"lime",
"green",
"red",
"brown",
"red",
"orange",
"yellow",
"green",
"blue_green",
"magenta",
"pink",
"brown",
"silver",
"light_green",
"crimson",
"cyan",
"aqua",
"deep_pink",
"tomato",
"blue_green",
"lime",
"emerald",
"carmine",
"nickel",
"mint",
"army_green",
"pumpkin"
};
public static List<Player> rainbw = new List<Player>();
public static IEnumerator<float> Rainbw()
{
while (true)
{
foreach (var item in rainbw)
{
item.GroupColor = FMoreColo.RandomItem();
}
yield return Timing.WaitForSeconds(CustomEventHandler.BadgeConfig.each);
}
}
public static void Handler(Player player)
{
string filePath = Path.Combine(CustomEventHandler.BadgeConfig.Pach, player.UserId + ".ini");
if (!File.Exists(filePath))
{
IniFile iniFile = new IniFile();
iniFile.Section("DIR").Set("称号", "空", "称号");
iniFile.Section("DIR").Set("称号颜色", "空");
iniFile.Section("DIR").Set("管理权限组", "空");
iniFile.Save(filePath);
}
else
{
IniFile iniFile = new IniFile(filePath);
iniFile.Save(filePath);
if (iniFile.Section("DIR").Get("管理权限组") == "空")
{
if (iniFile.Section("DIR").Get("称号") != "空")
{
player.GroupName = iniFile.Section("DIR").Get("称号");
switch (iniFile.Section("DIR").Get("称号颜色"))
{
case "rainbow":
rainbw.Add(player);
break;
default:
player.GroupColor = iniFile.Section("DIR").Get("称号颜色");
break;
}
}
}
else
{
Server.RunCommand($"/setgroup {player.PlayerId} {iniFile.Section("DIR").Get("")}");
Log.Info($"已经给予{player.Nickname}-{player.UserId}==={iniFile.Section("DIR").Get("")}权限");
if (iniFile.Section("DIR").Get("称号") != "空")
{
player.GroupName = iniFile.Section("DIR").Get("称号");
switch (iniFile.Section("DIR").Get("称号颜色"))
{
case "rainbow":
rainbw.Add(player);
break;
default:
player.GroupColor = iniFile.Section("DIR").Get("称号颜色");
break;
}
}
}
}
}
public static void Remove(Player player)
{
if (Badge.rainbw.Contains(player))
{
Badge.rainbw.Remove(player);
}
}
}
}

20
Configs/BadgeConfig.cs Normal file
View File

@@ -0,0 +1,20 @@
using LabApi.Features.Wrappers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EasyTools.Configs
{
public class BadgeConfig
{
[Description("是否启用称号系统")]
public bool Enable { get; set; } = true;
[Description("存储路径")]
public string Pach { get; set; } = "/home/shiroko/.config/DIRSave";
[Description("彩色称号更新频率")]
public int each { get; set; } = 1;
}
}

View File

@@ -115,16 +115,19 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="BadgeSystem\Badge.cs" />
<Compile Include="Commands\Chat\AcCommand.cs" /> <Compile Include="Commands\Chat\AcCommand.cs" />
<Compile Include="Commands\Chat\BcCommand.cs" /> <Compile Include="Commands\Chat\BcCommand.cs" />
<Compile Include="Commands\Chat\CCommand.cs" /> <Compile Include="Commands\Chat\CCommand.cs" />
<Compile Include="Commands\RescueCommand.cs" /> <Compile Include="Commands\RescueCommand.cs" />
<Compile Include="Configs\BadgeConfig.cs" />
<Compile Include="Configs\Config.cs" /> <Compile Include="Configs\Config.cs" />
<Compile Include="Configs\TranslateConfig.cs" /> <Compile Include="Configs\TranslateConfig.cs" />
<Compile Include="Events\CustomEventHandler.cs" /> <Compile Include="Events\CustomEventHandler.cs" />
<Compile Include="Plugins.cs" /> <Compile Include="Plugins.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utils\ChatUtils.cs" /> <Compile Include="Utils\ChatUtils.cs" />
<Compile Include="Utils\Ini.cs" />
<Compile Include="Utils\Pool\IPool.cs" /> <Compile Include="Utils\Pool\IPool.cs" />
<Compile Include="Utils\Pool\StringBuilderPool.cs" /> <Compile Include="Utils\Pool\StringBuilderPool.cs" />
<Compile Include="Utils\Util.cs" /> <Compile Include="Utils\Util.cs" />

View File

@@ -1,11 +1,14 @@
using EasyTools.Configs; using EasyTools.Configs;
using EasyTools.Events; using EasyTools.Events;
using LabApi.Events.CustomHandlers; using LabApi.Events.CustomHandlers;
using LabApi.Events.Handlers;
using LabApi.Features; using LabApi.Features;
using LabApi.Features.Wrappers;
using LabApi.Loader; using LabApi.Loader;
using LabApi.Loader.Features.Plugins; using LabApi.Loader.Features.Plugins;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -14,15 +17,19 @@ namespace EasyTools
{ {
public class Plugins : Plugin public class Plugins : Plugin
{ {
public CustomEventHandler Events { get; } = new(); public CustomEventHandler Events { get; } = new();
public override void LoadConfigs() public override void LoadConfigs()
{ {
base.LoadConfigs(); base.LoadConfigs();
CustomEventHandler.Config = this.LoadConfig<Config>("config.yml"); CustomEventHandler.Config = this.LoadConfig<Config>("config.yml");
CustomEventHandler.TranslateConfig = this.LoadConfig<TranslateConfig>("translateConfig.yml"); CustomEventHandler.TranslateConfig = this.LoadConfig<TranslateConfig>("translateConfig.yml");
CustomEventHandler.BadgeConfig = this.LoadConfig<BadgeConfig>("badgeConfig.yml");
if (!Directory.Exists(CustomEventHandler.BadgeConfig.Pach))
{
Directory.CreateDirectory(CustomEventHandler.BadgeConfig.Pach);
}
} }

300
Utils/Ini.cs Normal file
View File

@@ -0,0 +1,300 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NewXp.IniApi
{
/// <summary>
/// Represents a property in an INI file.
/// </summary>
public class IniProperty
{
/// <summary>
/// Property name (key).
/// </summary>
public string Name { get; set; }
/// <summary>
/// Property value.
/// </summary>
public string Value { get; set; }
/// <summary>
/// Set the comment to display above this property.
/// </summary>
public string Comment { get; set; }
}
/// <summary>
/// Represents a section in an INI file.
/// </summary>
public class IniSection
{
private readonly IDictionary<string, IniProperty> _properties;
/// <summary>
/// Section name.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Set the comment to display above this section.
/// </summary>
public string Comment { get; set; }
/// <summary>
/// Get the properties in this section.
/// </summary>
public IniProperty[] Properties => _properties.Values.ToArray();
/// <summary>
/// Create a new IniSection.
/// </summary>
/// <param name="name"></param>
public IniSection(string name)
{
Name = name;
_properties = new Dictionary<string, IniProperty>();
}
/// <summary>
/// Get a property value.
/// </summary>
/// <param name="name">Name of the property.</param>
/// <returns>Value of the property or null if it doesn't exist.</returns>
public string Get(string name)
{
if (_properties.ContainsKey(name))
return _properties[name].Value;
return null;
}
/// <summary>
/// Get a property value, coercing the type of the value
/// into the type given by the generic parameter.
/// </summary>
/// <param name="name">Name of the property.</param>
/// <typeparam name="T">The type to coerce the value into.</typeparam>
/// <returns></returns>
public T Get<T>(string name)
{
if (_properties.ContainsKey(name))
return (T)Convert.ChangeType(_properties[name].Value, typeof(T));
return default(T);
}
/// <summary>
/// Set a property value.
/// </summary>
/// <param name="name">Name of the property.</param>
/// <param name="value">Value of the property.</param>
/// <param name="comment">A comment to display above the property.</param>
public void Set(string name, string value, string comment = null)
{
if (string.IsNullOrWhiteSpace(value))
{
RemoveProperty(name);
return;
}
if (!_properties.ContainsKey(name))
_properties.Add(name, new IniProperty { Name = name, Value = value, Comment = comment });
else
{
_properties[name].Value = value;
if (comment != null)
_properties[name].Comment = comment;
}
}
/// <summary>
/// Remove a property from this section.
/// </summary>
/// <param name="propertyName">The property name to remove.</param>
public void RemoveProperty(string propertyName)
{
if (_properties.ContainsKey(propertyName))
_properties.Remove(propertyName);
}
}
/// <summary>
/// Represenst an INI file that can be read from or written to.
/// </summary>
public class IniFile
{
private readonly IDictionary<string, IniSection> _sections;
/// <summary>
/// If True, writes extra spacing between the property name and the property value.
/// (foo=bar) vs (foo = bar)
/// </summary>
public bool WriteSpacingBetweenNameAndValue { get; set; }
/// <summary>
/// The character a comment line will begin with. Default '#'.
/// </summary>
public char CommentChar { get; set; }
/// <summary>
/// Get the sections in this IniFile.
/// </summary>
public IniSection[] Sections => _sections.Values.ToArray();
/// <summary>
/// Create a new IniFile instance.
/// </summary>
public IniFile()
{
_sections = new Dictionary<string, IniSection>();
CommentChar = '#';
}
/// <summary>
/// Load an INI file from the file system.
/// </summary>
/// <param name="path">Path to the INI file.</param>
public IniFile(string path) : this()
{
Load(path);
}
/// <summary>
/// Load an INI file.
/// </summary>
/// <param name="reader">A TextReader instance.</param>
public IniFile(TextReader reader) : this()
{
Load(reader);
}
private void Load(string path)
{
using (var file = new StreamReader(path))
Load(file);
}
private void Load(TextReader reader)
{
IniSection section = null;
string line;
while ((line = reader.ReadLine()) != null)
{
line = line.Trim();
// skip empty lines
if (line == string.Empty)
continue;
// skip comments
if (line.StartsWith(";") || line.StartsWith("#"))
continue;
if (line.StartsWith("[") && line.EndsWith("]"))
{
var sectionName = line.Substring(1, line.Length - 2);
if (!_sections.ContainsKey(sectionName))
{
section = new IniSection(sectionName);
_sections.Add(sectionName, section);
}
continue;
}
if (section != null)
{
var keyValue = line.Split(new[] { "=" }, 2, StringSplitOptions.RemoveEmptyEntries);
if (keyValue.Length != 2)
continue;
section.Set(keyValue[0].Trim(), keyValue[1].Trim());
}
}
}
/// <summary>
/// Get a section by name. If the section doesn't exist, it is created.
/// </summary>
/// <param name="sectionName">The name of the section.</param>
/// <returns>A section. If the section doesn't exist, it is created.</returns>
public IniSection Section(string sectionName)
{
IniSection section;
if (!_sections.TryGetValue(sectionName, out section))
{
section = new IniSection(sectionName);
_sections.Add(sectionName, section);
}
return section;
}
/// <summary>
/// Remove a section.
/// </summary>
/// <param name="sectionName">Name of the section to remove.</param>
public void RemoveSection(string sectionName)
{
if (_sections.ContainsKey(sectionName))
_sections.Remove(sectionName);
}
/// <summary>
/// Create a new INI file.
/// </summary>
/// <param name="path">Path to the INI file to create.</param>
public void Save(string path)
{
using (var file = new StreamWriter(path))
Save(file);
}
/// <summary>
/// Create a new INI file.
/// </summary>
/// <param name="writer">A TextWriter instance.</param>
public void Save(TextWriter writer)
{
foreach (var section in _sections.Values)
{
if (section.Properties.Length == 0)
continue;
if (section.Comment != null)
writer.WriteLine($"{CommentChar} {section.Comment}");
writer.WriteLine($"[{section.Name}]");
foreach (var property in section.Properties)
{
if (property.Comment != null)
writer.WriteLine($"{CommentChar} {property.Comment}");
var format = WriteSpacingBetweenNameAndValue ? "{0} = {1}" : "{0}={1}";
writer.WriteLine(format, property.Name, property.Value);
}
writer.WriteLine();
}
}
/// <summary>
/// Returns the content of this INI file as a string.
/// </summary>
/// <returns>The text content of this INI file.</returns>
public override string ToString()
{
using (var sw = new StringWriter())
{
Save(sw);
return sw.ToString();
}
}
}
}