Skip to content

Commit

Permalink
First working version, not fully tested
Browse files Browse the repository at this point in the history
  • Loading branch information
milutinke committed Jun 30, 2024
1 parent 08c5c15 commit 67e36a9
Show file tree
Hide file tree
Showing 4 changed files with 294 additions and 72 deletions.
144 changes: 143 additions & 1 deletion MinecraftClient/Mapping/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,149 @@ public static void StoreDimensionList(Dictionary<string, object> registryCodec)
}
}

public static void LoadDefaultDimensions1206Plus()
{
// TODO: Move this to a JSON file.

var defaultRegistryCodec = new Dictionary<string, object>
{
{ "minecraft:dimension_type", new Dictionary<string, object>
{
{ "value", new object[]
{
new Dictionary<string, object>
{
{ "name", "minecraft:overworld" },
{ "id", 0 },
{ "element", new Dictionary<string, object>
{
{ "piglin_safe", (byte)0 },
{ "natural", 1 },
{ "ambient_light", 0.0 },
{ "monster_spawn_block_light_limit", 0 },
{ "infiniburn", "#minecraft:infiniburn_overworld" },
{ "respawn_anchor_works", 0 },
{ "has_skylight", 1 },
{ "bed_works", 1 },
{ "effects", "minecraft:overworld" },
{ "has_raids", 1 },
{ "logical_height", 384 },
{ "coordinate_scale", 1.0 },
{ "monster_spawn_light_level", new Dictionary<string, object>
{
{ "min_inclusive", 0 },
{ "max_inclusive", 7 },
{ "type", "minecraft:uniform" }
}
},
{ "min_y", -64 },
{ "ultrawarm", 0 },
{ "has_ceiling", 0 },
{ "height", 384 }
}
}
},
new Dictionary<string, object>
{
{ "name", "minecraft:overworld_caves" },
{ "id", 1 },
{ "element", new Dictionary<string, object>
{
{ "piglin_safe", (byte)0 },
{ "natural", 1 },
{ "ambient_light", 0.0 },
{ "monster_spawn_block_light_limit", 0 },
{ "infiniburn", "#minecraft:infiniburn_overworld" },
{ "respawn_anchor_works", 0 },
{ "has_skylight", 1 },
{ "bed_works", 1 },
{ "effects", "minecraft:overworld" },
{ "has_raids", 1 },
{ "logical_height", 384 },
{ "coordinate_scale", 1.0 },
{ "monster_spawn_light_level", new Dictionary<string, object>
{
{ "min_inclusive", 0 },
{ "max_inclusive", 7 },
{ "type", "minecraft:uniform" }
}
},
{ "min_y", -64 },
{ "ultrawarm", 0 },
{ "has_ceiling", 1 },
{ "height", 384 }
}
}
},
new Dictionary<string, object>
{
{ "name", "minecraft:the_end" },
{ "id", 2 },
{ "element", new Dictionary<string, object>
{
{ "piglin_safe", (byte)0 },
{ "natural", 0 },
{ "ambient_light", 0.0 },
{ "monster_spawn_block_light_limit", 0 },
{ "infiniburn", "#minecraft:infiniburn_end" },
{ "respawn_anchor_works", 0 },
{ "has_skylight", 0 },
{ "bed_works", 0 },
{ "effects", "minecraft:the_end" },
{ "fixed_time", 6000 },
{ "has_raids", 1 },
{ "logical_height", 256 },
{ "coordinate_scale", 1.0 },
{ "monster_spawn_light_level", new Dictionary<string, object>
{
{ "min_inclusive", 0 },
{ "max_inclusive", 7 },
{ "type", "minecraft:uniform" }
}
},
{ "min_y", 0 },
{ "ultrawarm", 0 },
{ "has_ceiling", 0 },
{ "height", 256 }
}
}
},
new Dictionary<string, object>
{
{ "name", "minecraft:the_nether" },
{ "id", 3 },
{ "element", new Dictionary<string, object>
{
{ "piglin_safe", (byte)1 },
{ "natural", 0 },
{ "ambient_light", 0.1 },
{ "monster_spawn_block_light_limit", 15 },
{ "infiniburn", "#minecraft:infiniburn_nether" },
{ "respawn_anchor_works", 1 },
{ "has_skylight", 0 },
{ "bed_works", 0 },
{ "effects", "minecraft:the_nether" },
{ "fixed_time", 18000 },
{ "has_raids", 0 },
{ "logical_height", 128 },
{ "coordinate_scale", 8.0 },
{ "monster_spawn_light_level", 7 },
{ "min_y", 0 },
{ "ultrawarm", 1 },
{ "has_ceiling", 1 },
{ "height", 256 }
}
}
}
}
}
}
}
};

StoreDimensionList(defaultRegistryCodec);
}

/// <summary>
/// Store one dimension - Directly used in 1.16.2 to 1.18.2
/// </summary>
Expand All @@ -92,7 +235,6 @@ public static void SetDimension(string name)
curDimension = dimensionList[name]; // Should not fail
}


/// <summary>
/// Get current dimension
/// </summary>
Expand Down
78 changes: 76 additions & 2 deletions MinecraftClient/McClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ public enum MovementType { Sneak, Walk, Sprint }
public List<ChatBot> GetLoadedChatBots() { return new List<ChatBot>(bots); }
public void GetCookie(string key, out byte[]? data) => Cookies.TryGetValue(key, out data);
public void SetCookie(string key, byte[] data) => Cookies[key] = data;
public void DeleteCookie(string key) => Cookies.Remove(key, out var data);

readonly TcpClient client;
readonly IMinecraftCom handler;
TcpClient client;
IMinecraftCom handler;
SessionToken _sessionToken;
CancellationTokenSource? cmdprompt = null;
Tuple<Thread, CancellationTokenSource>? timeoutdetector = null;

Expand Down Expand Up @@ -186,6 +188,7 @@ public McClient(SessionToken session, PlayerKeyPair? playerKeyPair, string serve
this.port = port;
this.protocolversion = protocolversion;
this.playerKeyPair = playerKeyPair;
_sessionToken = session;

Log = Settings.Config.Logging.LogToFile
? new FileLogLogger(Config.AppVar.ExpandVars(Settings.Config.Logging.LogFile), Settings.Config.Logging.PrependTimestamp)
Expand Down Expand Up @@ -294,6 +297,77 @@ public McClient(SessionToken session, PlayerKeyPair? playerKeyPair, string serve
}
}
}

public void Transfer(string newHost, int newPort)
{
try
{
Log.Info($"Initiating a transfer to: {host}:{port}");

// Unload bots
UnloadAllBots();
bots.Clear();

// Close existing connection
client.Close();

// Establish new connection
client = ProxyHandler.NewTcpClient(newHost, newPort);
client.ReceiveBufferSize = 1024 * 1024;
client.ReceiveTimeout = Config.Main.Advanced.TcpTimeout * 1000;

// Reinitialize the protocol handler
handler = Protocol.ProtocolHandler.GetProtocolHandler(client, protocolversion, null, this);
Log.Info($"Connected to {host}:{port}");

// Retry login process
if (handler.Login(playerKeyPair, _sessionToken))
{
foreach (var bot in botsOnHold)
BotLoad(bot, false);
botsOnHold.Clear();

Log.Info("Successfully transferred connection and logged in.");
cmdprompt = new CancellationTokenSource();
ConsoleInteractive.ConsoleReader.BeginReadThread();
ConsoleInteractive.ConsoleReader.MessageReceived += ConsoleReaderOnMessageReceived;
ConsoleInteractive.ConsoleReader.OnInputChange += ConsoleIO.AutocompleteHandler;
}
else
{
Log.Error("Failed to login to the new host.");
throw new Exception("Login failed after transfer.");
}
}
catch (Exception ex)
{
Log.Error($"Transfer to {newHost}:{newPort} failed: {ex.Message}");

// Handle reconnection attempts
if (timeoutdetector != null)
{
timeoutdetector.Item2.Cancel();
timeoutdetector = null;
}

if (ReconnectionAttemptsLeft > 0)
{
Log.Info($"Reconnecting... Attempts left: {ReconnectionAttemptsLeft}");
Thread.Sleep(5000);
ReconnectionAttemptsLeft--;
Program.Restart();
}
else if (InternalConfig.InteractiveMode)
{
ConsoleInteractive.ConsoleReader.StopReadThread();
ConsoleInteractive.ConsoleReader.MessageReceived -= ConsoleReaderOnMessageReceived;
ConsoleInteractive.ConsoleReader.OnInputChange -= ConsoleIO.AutocompleteHandler;
Program.HandleFailure();
}

throw new Exception("Transfer failed and reconnection attempts exhausted.");
}
}

/// <summary>
/// Register bots
Expand Down
Loading

0 comments on commit 67e36a9

Please sign in to comment.