new funcs in Block, chatcolor enum

This commit is contained in:
sylvessa 2026-04-18 19:42:25 -05:00
parent c862675999
commit d47e47b4c2
6 changed files with 232 additions and 11 deletions

View file

@ -89,7 +89,19 @@ public class Block
/// <returns>Whether the change was successful.</returns>
public bool setTypeId(int type)
{
NativeBridge.SetTile?.Invoke(_world.getDimensionId(), _x, _y, _z, type, 0);
return setTypeId(type, true);
}
/// <summary>
/// Sets the type ID of this block.
/// </summary>
/// <param name="type">Type ID to change this block to.</param>
/// <param name="applyPhysics">False to cancel physics on the changed block.</param>
/// <returns>Whether the block was changed.</returns>
public bool setTypeId(int type, bool applyPhysics)
{
int flags = applyPhysics ? 3 : 2;
NativeBridge.SetTile?.Invoke(_world.getDimensionId(), _x, _y, _z, type, 0, flags);
return true;
}
@ -108,7 +120,43 @@ public class Block
/// <param name="data">New block specific metadata.</param>
public void setData(byte data)
{
NativeBridge.SetTileData?.Invoke(_world.getDimensionId(), _x, _y, _z, data);
setData(data, true);
}
/// <summary>
/// Sets the metadata for this block.
/// </summary>
/// <param name="data">New block specific metadata.</param>
/// <param name="applyPhysics">False to cancel physics from the changed block.</param>
public void setData(byte data, bool applyPhysics)
{
int flags = applyPhysics ? 3 : 2;
NativeBridge.SetTileData?.Invoke(_world.getDimensionId(), _x, _y, _z, data, flags);
}
/// <summary>
/// Sets the type ID and data of this block.
/// </summary>
/// <param name="type">Type ID to change this block to.</param>
/// <param name="data">The data value to change this block to.</param>
/// <returns>Whether the block was changed.</returns>
public bool setTypeIdAndData(int type, byte data)
{
return setTypeIdAndData(type, data, true);
}
/// <summary>
/// Sets the type ID and data of this block.
/// </summary>
/// <param name="type">Type ID to change this block to.</param>
/// <param name="data">The data value to change this block to.</param>
/// <param name="applyPhysics">False to cancel physics on the changed block.</param>
/// <returns>Whether the block was changed.</returns>
public bool setTypeIdAndData(int type, byte data, bool applyPhysics)
{
int flags = applyPhysics ? 3 : 2;
NativeBridge.SetTile?.Invoke(_world.getDimensionId(), _x, _y, _z, type, data, flags);
return true;
}
/// <summary>

View file

@ -200,7 +200,7 @@ public class BlockState
if (!force && currentType != _typeId)
return false;
NativeBridge.SetTile(_world.getDimensionId(), _x, _y, _z, _typeId, _data);
NativeBridge.SetTile(_world.getDimensionId(), _x, _y, _z, _typeId, _data, applyPhysics ? 3 : 2);
return true;
}

View file

@ -0,0 +1,173 @@
using System.Text;
using System.Text.RegularExpressions;
namespace Minecraft.Server.FourKit;
/// <summary>
/// All supported color values for chat.
/// </summary>
public class ChatColor
{
/// <summary>
/// The special character which prefixes all chat colour codes.
/// </summary>
public static readonly char COLOR_CHAR = '\u00A7'; // F
private static readonly Regex STRIP_COLOR_PATTERN =
new Regex("(?i)" + COLOR_CHAR + "[0-9A-FK-OR]", RegexOptions.Compiled);
private static readonly Dictionary<char, ChatColor> BY_CHAR = new();
/// <summary>Represents black.</summary>
public static readonly ChatColor BLACK = new('0', false);
/// <summary>Represents dark blue.</summary>
public static readonly ChatColor DARK_BLUE = new('1', false);
/// <summary>Represents dark green.</summary>
public static readonly ChatColor DARK_GREEN = new('2', false);
/// <summary>Represents dark blue (aqua).</summary>
public static readonly ChatColor DARK_AQUA = new('3', false);
/// <summary>Represents dark red.</summary>
public static readonly ChatColor DARK_RED = new('4', false);
/// <summary>Represents dark purple.</summary>
public static readonly ChatColor DARK_PURPLE = new('5', false);
/// <summary>Represents gold.</summary>
public static readonly ChatColor GOLD = new('6', false);
/// <summary>Represents gray.</summary>
public static readonly ChatColor GRAY = new('7', false);
/// <summary>Represents dark gray.</summary>
public static readonly ChatColor DARK_GRAY = new('8', false);
/// <summary>Represents blue.</summary>
public static readonly ChatColor BLUE = new('9', false);
/// <summary>Represents green.</summary>
public static readonly ChatColor GREEN = new('a', false);
/// <summary>Represents aqua.</summary>
public static readonly ChatColor AQUA = new('b', false);
/// <summary>Represents red.</summary>
public static readonly ChatColor RED = new('c', false);
/// <summary>Represents light purple.</summary>
public static readonly ChatColor LIGHT_PURPLE = new('d', false);
/// <summary>Represents yellow.</summary>
public static readonly ChatColor YELLOW = new('e', false);
/// <summary>Represents white.</summary>
public static readonly ChatColor WHITE = new('f', false);
/// <summary>Resets all previous chat colors or formats.</summary>
public static readonly ChatColor RESET = new('r', false);
private readonly char _code;
private readonly bool _isFormat;
private readonly string _toString;
private ChatColor(char code, bool isFormat)
{
_code = code;
_isFormat = isFormat;
_toString = new string(new[] { COLOR_CHAR, code });
BY_CHAR[code] = this;
}
/// <summary>
/// Gets the char value associated with this color.
/// </summary>
/// <returns>A char value of this color code.</returns>
public char getChar() => _code;
/// <summary>
/// Checks if this code is a format code as opposed to a color code.
/// </summary>
/// <returns><c>true</c> if this is a format code.</returns>
public bool isFormat() => _isFormat;
/// <summary>
/// Checks if this code is a color code as opposed to a format code.
/// </summary>
/// <returns><c>true</c> if this is a color code.</returns>
public bool isColor() => !_isFormat && this != RESET;
/// <summary>
/// Gets the color represented by the specified color code.
/// </summary>
/// <param name="code">Code to check.</param>
/// <returns>Associative ChatColor with the given code, or null if it doesn't exist.</returns>
public static ChatColor? getByChar(char code)
{
return BY_CHAR.TryGetValue(char.ToLower(code), out var color) ? color : null;
}
/// <summary>
/// Gets the color represented by the specified color code.
/// </summary>
/// <param name="code">Code to check.</param>
/// <returns>Associative ChatColor with the given code, or null if it doesn't exist.</returns>
public static ChatColor? getByChar(string code)
{
if (string.IsNullOrEmpty(code)) return null;
return getByChar(code[0]);
}
/// <summary>
/// Strips the given message of all color codes.
/// </summary>
/// <param name="input">String to strip of color.</param>
/// <returns>A copy of the input string, without any coloring.</returns>
public static string? stripColor(string? input)
{
if (input == null) return null;
return STRIP_COLOR_PATTERN.Replace(input, "");
}
/// <summary>
/// Translates a string using an alternate color code character into a string
/// that uses the internal <see cref="COLOR_CHAR"/> color code character.
/// The alternate color code character will only be replaced if it is immediately
/// followed by 0-9, A-F, a-f, K-O, k-o, R or r.
/// </summary>
/// <param name="altColorChar">The alternate color code character to replace. Ex: &amp;</param>
/// <param name="textToTranslate">Text containing the alternate color code character.</param>
/// <returns>Text containing the <see cref="COLOR_CHAR"/> color code character.</returns>
public static string translateAlternateColorCodes(char altColorChar, string textToTranslate)
{
char[] b = textToTranslate.ToCharArray();
for (int i = 0; i < b.Length - 1; i++)
{
if (b[i] == altColorChar && "0123456789AaBbCcDdEeFfKkLlMmNnOoRr".IndexOf(b[i + 1]) > -1)
{
b[i] = COLOR_CHAR;
}
}
return new string(b);
}
/// <summary>
/// Gets the ChatColors used at the end of the given input string.
/// </summary>
/// <param name="input">Input string to retrieve the colors from.</param>
/// <returns>Any remaining ChatColors to pass onto the next line.</returns>
public static string getLastColors(string input)
{
var result = new StringBuilder();
int length = input.Length;
for (int index = length - 1; index > -1; index--)
{
char section = input[index];
if (section == COLOR_CHAR && index < length - 1)
{
char c = input[index + 1];
ChatColor? color = getByChar(c);
if (color != null)
{
result.Insert(0, color.ToString());
if (color.isColor() || color == RESET)
break;
}
}
}
return result.ToString();
}
public static string operator +(ChatColor color, string text) => color.ToString() + text;
/// <inheritdoc/>
public override string ToString() => _toString;
}

View file

@ -43,10 +43,10 @@ internal static class NativeBridge
internal delegate int NativeGetTileDataDelegate(int dimId, int x, int y, int z);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void NativeSetTileDelegate(int dimId, int x, int y, int z, int tileId, int data);
internal delegate void NativeSetTileDelegate(int dimId, int x, int y, int z, int tileId, int data, int flags);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void NativeSetTileDataDelegate(int dimId, int x, int y, int z, int data);
internal delegate void NativeSetTileDataDelegate(int dimId, int x, int y, int z, int data, int flags);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate int NativeBreakBlockDelegate(int dimId, int x, int y, int z);

View file

@ -309,20 +309,20 @@ int __cdecl NativeGetTileData(int dimId, int x, int y, int z)
return level->getData(x, y, z);
}
void __cdecl NativeSetTile(int dimId, int x, int y, int z, int tileId, int data)
void __cdecl NativeSetTile(int dimId, int x, int y, int z, int tileId, int data, int flags)
{
ServerLevel *level = GetLevel(dimId);
if (!level)
return;
level->setTileAndData(x, y, z, tileId, data, Tile::UPDATE_ALL);
level->setTileAndData(x, y, z, tileId, data, flags);
}
void __cdecl NativeSetTileData(int dimId, int x, int y, int z, int data)
void __cdecl NativeSetTileData(int dimId, int x, int y, int z, int data, int flags)
{
ServerLevel *level = GetLevel(dimId);
if (!level)
return;
level->setData(x, y, z, data, Tile::UPDATE_ALL);
level->setData(x, y, z, data, flags);
}
int __cdecl NativeBreakBlock(int dimId, int x, int y, int z)

View file

@ -18,8 +18,8 @@ namespace FourKitBridge
// World
int __cdecl NativeGetTileId(int dimId, int x, int y, int z);
int __cdecl NativeGetTileData(int dimId, int x, int y, int z);
void __cdecl NativeSetTile(int dimId, int x, int y, int z, int tileId, int data);
void __cdecl NativeSetTileData(int dimId, int x, int y, int z, int data);
void __cdecl NativeSetTile(int dimId, int x, int y, int z, int tileId, int data, int flags);
void __cdecl NativeSetTileData(int dimId, int x, int y, int z, int data, int flags);
int __cdecl NativeBreakBlock(int dimId, int x, int y, int z);
int __cdecl NativeGetHighestBlockY(int dimId, int x, int z);
void __cdecl NativeGetWorldInfo(int dimId, double *outBuf);