Merge branch 'master' into 1.20.6

This commit is contained in:
breadbyte 2025-12-02 00:06:16 +08:00 committed by GitHub
commit 494be0930b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 455 additions and 1655 deletions

View file

@ -285,7 +285,7 @@ namespace MinecraftClient.ChatBots
if (Config.Mode == Configs.ModeType.lookat ||
(Config.Mode == Configs.ModeType.both && Config._Locations.Contains(blockLoc)))
{
if (DigBlock(blockLoc, lookAtBlock: false))
if (DigBlock(blockLoc, Direction.Down, lookAtBlock: false))
{
currentDig = blockLoc;
if (Config.Log_Block_Dig)
@ -346,7 +346,7 @@ namespace MinecraftClient.ChatBots
if (minDistance <= 6.0)
{
if (DigBlock(target, lookAtBlock: true))
if (DigBlock(target, Direction.Down, lookAtBlock: true))
{
currentDig = target;
if (Config.Log_Block_Dig)
@ -380,7 +380,7 @@ namespace MinecraftClient.ChatBots
((Config.List_Type == Configs.ListType.whitelist && Config.Blocks.Contains(block.Type)) ||
(Config.List_Type == Configs.ListType.blacklist && !Config.Blocks.Contains(block.Type))))
{
if (DigBlock(blockLoc, lookAtBlock: true))
if (DigBlock(blockLoc, Direction.Down, lookAtBlock: true))
{
currentDig = blockLoc;
if (Config.Log_Block_Dig)

View file

@ -831,7 +831,7 @@ namespace MinecraftClient.ChatBots
// Yoinked from Daenges's Sugarcane Farmer
private bool WaitForDigBlock(Location block, int digTimeout = 1000)
{
if (!DigBlock(block.ToFloor())) return false;
if (!DigBlock(block.ToFloor(), Direction.Down)) return false;
short i = 0; // Maximum wait time of 10 sec.
while (GetWorld().GetBlock(block).Type != Material.Air && i <= digTimeout)
{

File diff suppressed because it is too large Load diff

View file

@ -22,6 +22,7 @@ namespace MinecraftClient.Commands
);
dispatcher.Register(l => l.Literal(CmdName)
// TODO Get blockFace direction from arguments
.Executes(r => DigLookAt(r.Source))
.Then(l => l.Argument("Duration", Arguments.Double())
.Executes(r => DigLookAt(r.Source, Arguments.GetDouble(r, "Duration"))))
@ -58,7 +59,7 @@ namespace MinecraftClient.Commands
Block block = handler.GetWorld().GetBlock(blockToBreak);
if (block.Type == Material.Air)
return r.SetAndReturn(Status.Fail, Translations.cmd_dig_no_block);
else if (handler.DigBlock(blockToBreak, duration: duration))
else if (handler.DigBlock(blockToBreak, Direction.Down, duration: duration))
{
blockToBreak = blockToBreak.ToCenter();
return r.SetAndReturn(Status.Done, string.Format(Translations.cmd_dig_dig, blockToBreak.X, blockToBreak.Y, blockToBreak.Z, block.GetTypeString()));
@ -78,7 +79,7 @@ namespace MinecraftClient.Commands
return r.SetAndReturn(Status.Fail, Translations.cmd_dig_too_far);
else if (block.Type == Material.Air)
return r.SetAndReturn(Status.Fail, Translations.cmd_dig_no_block);
else if (handler.DigBlock(blockLoc, lookAtBlock: false, duration: duration))
else if (handler.DigBlock(blockLoc, Direction.Down, lookAtBlock: false, duration: duration))
return r.SetAndReturn(Status.Done, string.Format(Translations.cmd_dig_dig, blockLoc.X, blockLoc.Y, blockLoc.Z, block.GetTypeString()));
else
return r.SetAndReturn(Status.Fail, Translations.cmd_dig_fail);

View file

@ -0,0 +1,63 @@
using System;
namespace MinecraftClient.Mapping
{
public static class DirectionExtensions
{
public static Direction GetOpposite(this Direction direction)
{
switch (direction)
{
case Direction.SouthEast:
return Direction.NorthEast;
case Direction.SouthWest:
return Direction.NorthWest;
case Direction.NorthEast:
return Direction.SouthEast;
case Direction.NorthWest:
return Direction.SouthWest;
case Direction.West:
return Direction.East;
case Direction.East:
return Direction.West;
case Direction.North:
return Direction.South;
case Direction.South:
return Direction.North;
case Direction.Down:
return Direction.Up;
case Direction.Up:
return Direction.Down;
default:
return Direction.Up;
}
}
public static Direction[] HORIZONTAL =
{
Direction.South,
Direction.West,
Direction.North,
Direction.East
};
public static Direction FromRotation(double rotation)
{
double floor = Math.Floor((rotation / 90.0) + 0.5);
int value = (int)floor & 3;
return FromHorizontal(value);
}
public static Direction FromHorizontal(int value)
{
return HORIZONTAL[Math.Abs(value % HORIZONTAL.Length)];
}
}
}

View file

@ -19,7 +19,7 @@ namespace MinecraftClient.Mapping
/// <summary>
/// The dimension info of the world
/// </summary>
private static Dimension curDimension = new();
private static Dimension curDimension= new();
private static readonly Dictionary<string, Dimension> dimensionList = new();
@ -230,10 +230,32 @@ namespace MinecraftClient.Mapping
/// </summary>
/// <param name="name"> The name of the dimension type</param>
/// <param name="nbt">The dimension type (NBT Tag Compound)</param>
public static void SetDimension(string name)
{
curDimension = dimensionList[name]; // Should not fail
}
public static void SetDimension(string name)
{
// Try to get the dimension using the name as is
if (dimensionList.TryGetValue(name, out Dimension dimension))
{
curDimension = dimension;
return; // Dimension found
}
// If not found, check if name lacks 'minecraft:' prefix and try again
if (!name.StartsWith("minecraft:"))
{
string prefixedName = "minecraft:" + name;
if (dimensionList.TryGetValue(prefixedName, out dimension))
{
curDimension = dimension;
return; // Dimension found with prefixed name
}
}
// If still not found, dimension does not exist
throw new KeyNotFoundException($"Dimension '{name}' not found in dimensions dictionary.");
}
/// <summary>
/// Get current dimension

View file

@ -425,7 +425,6 @@ namespace MinecraftClient
if (Config.ChatBot.ScriptScheduler.Enabled) { BotLoad(new ScriptScheduler()); }
if (Config.ChatBot.TelegramBridge.Enabled) { BotLoad(new TelegramBridge()); }
if (Config.ChatBot.ItemsCollector.Enabled) { BotLoad(new ItemsCollector()); }
if (Config.ChatBot.WebSocketBot.Enabled) { BotLoad(new WebSocketBot()); }
//Add your ChatBot here by uncommenting and adapting
//BotLoad(new ChatBots.YourBot());
}
@ -1166,6 +1165,15 @@ namespace MinecraftClient
#region Getters: Retrieve data for use in other methods or ChatBots
/// <summary>
/// Gets the horizontal direction of the takeoff.
/// </summary>
/// <returns>Return direction of view</returns>
public Direction GetHorizontalFacing()
{
return DirectionExtensions.FromRotation(GetYaw());
}
/// <summary>
/// Get max length for chat messages
/// </summary>
@ -2390,22 +2398,22 @@ namespace MinecraftClient
return InvokeOnMainThread(() => handler.SendPlayerBlockPlacement((int)hand, location, blockFace, sequenceId++));
}
/// <summary>
/// Attempt to dig a block at the specified location
/// </summary>
/// <param name="location">Location of block to dig</param>
/// <param name="swingArms">Also perform the "arm swing" animation</param>
/// <param name="lookAtBlock">Also look at the block before digging</param>
public bool DigBlock(Location location, bool swingArms = true, bool lookAtBlock = true, double duration = 0)
public bool DigBlock(Location location, Direction blockFace, bool swingArms = true, bool lookAtBlock = true, double duration = 0)
{
// TODO select best face from current player location
if (!GetTerrainEnabled())
return false;
if (InvokeRequired)
return InvokeOnMainThread(() => DigBlock(location, swingArms, lookAtBlock, duration));
// TODO select best face from current player location
Direction blockFace = Direction.Down;
return InvokeOnMainThread(() => DigBlock(location, blockFace, swingArms, lookAtBlock, duration));
lock (DigLock)
{

View file

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
@ -749,7 +749,8 @@ namespace MinecraftClient.Protocol.Handlers
{
case >= MC_1_16_2_Version and <= MC_1_18_2_Version:
World.StoreOneDimension(dimensionName, dimensionType!);
World.SetDimension(dimensionName);
// World.SetDimension(dimensionName);
World.SetDimension(dimensionName);
break;
case < MC_1_20_6_Version:
World.SetDimension(dimensionTypeName!);
@ -2234,7 +2235,7 @@ namespace MinecraftClient.Protocol.Handlers
{
var windowId = dataTypes.ReadNextByte(packetData);
var stateId = -1;
var elements = 0;
int elements;
if (protocolVersion >= MC_1_17_1_Version)
{
@ -2245,7 +2246,7 @@ namespace MinecraftClient.Protocol.Handlers
else
{
// Elements as Short - 1.17.0 and below
dataTypes.ReadNextShort(packetData);
elements = dataTypes.ReadNextShort(packetData);
}
Dictionary<int, Item> inventorySlots = new();

View file

@ -56,9 +56,26 @@ namespace MinecraftClient.Protocol.Message
public static void ReadChatType(Dictionary<string, object> registryCodec)
{
var chatTypeDictionary = ChatId2Type ?? new Dictionary<int, MessageType>();
var chatTypeListNbt =
(object[])(((Dictionary<string, object>)registryCodec["minecraft:chat_type"])["value"]);
Dictionary<int, MessageType> chatTypeDictionary = ChatId2Type ?? new();
// Check if the chat type registry is in the correct format
if (!registryCodec.ContainsKey("minecraft:chat_type")) {
// If not, then we force the registry to be in the correct format
if (registryCodec.ContainsKey("chat_type")) {
foreach (var key in registryCodec.Keys.ToArray()) {
// Skip entries with a namespace already
if (key.Contains(':', StringComparison.OrdinalIgnoreCase)) continue;
// Assume all other entries are in the minecraft namespace
registryCodec["minecraft:" + key] = registryCodec[key];
registryCodec.Remove(key);
}
}
}
var chatTypeListNbt = (object[])(((Dictionary<string, object>)registryCodec["minecraft:chat_type"])["value"]);
foreach (var (chatName, chatId) in from Dictionary<string, object> chatTypeNbt in chatTypeListNbt
let chatName = (string)chatTypeNbt["name"]
let chatId = (int)chatTypeNbt["id"]

View file

@ -1075,11 +1075,12 @@ namespace MinecraftClient.Scripting
/// Attempt to dig a block at the specified location
/// </summary>
/// <param name="location">Location of block to dig</param>
/// <param name="direction">Example: if your player is under a block that is being destroyed, use Down</param>
/// <param name="swingArms">Also perform the "arm swing" animation</param>
/// <param name="lookAtBlock">Also look at the block before digging</param>
protected bool DigBlock(Location location, bool swingArms = true, bool lookAtBlock = true)
protected bool DigBlock(Location location, Direction direction, bool swingArms = true, bool lookAtBlock = true)
{
return Handler.DigBlock(location, swingArms, lookAtBlock);
return Handler.DigBlock(location, direction, swingArms, lookAtBlock);
}
/// <summary>
@ -1633,6 +1634,15 @@ namespace MinecraftClient.Scripting
return Handler.GetProtocolVersion();
}
/// <summary>
/// Gets the horizontal direction of the takeoff.
/// </summary>
/// <returns>Return direction of view</returns>
protected Direction GetHorizontalFacing()
{
return Handler.GetHorizontalFacing();
}
/// <summary>
/// Invoke a task on the main thread, wait for completion and retrieve return value.
/// </summary>

View file

@ -94,7 +94,15 @@ namespace MinecraftClient.Scripting.DynamicRun.Builder
{
if (file.EndsWith("mcc-executable"))
{
useExisting = true;
// Check if the file is the same as the current executable.
if (File.ReadAllBytes(file).SequenceEqual(File.ReadAllBytes(executablePath)))
{
useExisting = true;
break;
}
// If not, refresh the cache.
File.Delete(file);
break;
}
}

View file

@ -1429,19 +1429,16 @@ namespace MinecraftClient
get { return ChatBots.TelegramBridge.Config; }
set { ChatBots.TelegramBridge.Config = value; ChatBots.TelegramBridge.Config.OnSettingUpdate(); }
}
[TomlPrecedingComment("$ChatBot.ItemsCollector$")]
public ChatBots.ItemsCollector.Configs ItemsCollector
{
get { return ChatBots.ItemsCollector.Config; }
set { ChatBots.ItemsCollector.Config = value; ChatBots.ItemsCollector.Config.OnSettingUpdate(); }
}
[TomlPrecedingComment("$ChatBot.WebSocketBot$")]
public ChatBots.WebSocketBot.Configs WebSocketBot
{
get { return ChatBots.WebSocketBot.Config!; }
set { ChatBots.WebSocketBot.Config = value; }
set
{
ChatBots.ItemsCollector.Config = value;
ChatBots.ItemsCollector.Config.OnSettingUpdate();
}
}
}
}