C#:
using System;
using System.Collections.Generic;
using System.Globalization;
using Oxide.Core;
using Oxide.Core.Libraries;
using Oxide.Core.Plugins;
using Oxide.Core.Libraries.Covalence;
using Rust;
// ____ _ _ _
// / ___|(_) __ _(_) | ___
// \___ \| |/ _` | | |/ _ \
// ___) | | (_| | | | (_) |
// |____/|_|\__, |_|_|\___/
// |___/
// ☽✧✵✧✵✧✵✧✵✧✵✧✵✧☾
// ✦ RUST PLUGINS ✦
// ✦ Sigilo.dev ✦
// ☽✧✵✧✵✧✵✧✵✧✵✧✵✧☾
namespace Oxide.Plugins
{
[Info("XTravel", "Sigilo", "1.0.2")]
[Description("Connects players to other servers using custom commands")]
public class XTravel : CovalencePlugin
{
private Dictionary<string, ServerInfo> serverInfos;
private Dictionary<string, Timer> travelTimers = new Dictionary<string, Timer>();
private Dictionary<string, bool> forcedTravels = new Dictionary<string, bool>();
private class ServerInfo
{
public string IP { get; set; }
public int Port { get; set; }
}
private void Init()
{
permission.RegisterPermission("xtravel.use", this);
permission.RegisterPermission("xtravel.admin", this);
LoadConfig();
foreach (var command in serverInfos.Keys)
{
AddCovalenceCommand(command, "TravelCommand");
}
AddCovalenceCommand("canceltravel", "CancelTravelCommand");
}
protected override void LoadDefaultConfig()
{
Config["Servers"] = new Dictionary<string, Dictionary<string, object>>
{
{ "xtravel", new Dictionary<string, object> { { "IP", "0.0.0.0" }, { "Port", 28015 } } }
};
}
protected new void LoadConfig()
{
try
{
var configData = Config.Get<Dictionary<string, Dictionary<string, object>>>("Servers");
serverInfos = new Dictionary<string, ServerInfo>();
foreach (var kvp in configData)
{
var serverInfoData = kvp.Value;
if (serverInfoData != null)
{
var serverInfo = new ServerInfo
{
IP = serverInfoData["IP"] as string,
Port = Convert.ToInt32(serverInfoData["Port"])
};
serverInfos.Add(kvp.Key, serverInfo);
}
}
}
catch (Exception ex)
{
PrintError($"Error loading config: {ex.Message}");
throw;
}
SaveConfig();
}
protected new void SaveConfig()
{
var configData = new Dictionary<string, Dictionary<string, object>>();
foreach (var kvp in serverInfos)
{
configData[kvp.Key] = new Dictionary<string, object> { { "IP", kvp.Value.IP }, { "Port", kvp.Value.Port } };
}
Config["Servers"] = configData;
Config.Save();
}
private void TravelCommand(IPlayer player, string cmd, string[] args)
{
if (!serverInfos.TryGetValue(cmd, out var serverInfo))
{
player.Reply(lang.GetMessage("InvalidCommand", this, player.Id));
return;
}
if (args.Length > 0 && args[0] == "all")
{
// Handle 'all' argument
if (!permission.UserHasPermission(player.Id, "xtravel.admin"))
{
player.Reply(lang.GetMessage("NoPermission", this, player.Id));
return;
}
foreach (var p in players.Connected)
{
if (p.Id == player.Id)
{
continue;
}
ScheduleTravel(p, serverInfo, true);
}
}
else
{
IPlayer targetPlayer = player;
if (args.Length > 0 && args[0].Length == 17 && args[0].StartsWith("765611") && permission.UserHasPermission(player.Id, "xtravel.admin"))
{
targetPlayer = players.FindPlayerById(args[0]);
if (targetPlayer == null)
{
player.Reply(lang.GetMessage("PlayerNotFound", this, player.Id));
return;
}
}
ScheduleTravel(targetPlayer, serverInfo, permission.UserHasPermission(player.Id, "xtravel.admin"));
}
}
private void ScheduleTravel(IPlayer player, ServerInfo serverInfo, bool isForced = false)
{
if (travelTimers.ContainsKey(player.Id))
{
player.Reply(lang.GetMessage("TravelAlreadyScheduled", this, player.Id));
return;
}
player.Reply(lang.GetMessage("TravelScheduled", this, player.Id));
Action callback = () =>
{
var basePlayer = player.Object as BasePlayer;
if (basePlayer != null)
{
var networkConnection = basePlayer.net?.connection;
if (networkConnection != null)
{
ConsoleNetwork.SendClientCommandImmediate(networkConnection, "nexus.redirect", new object[] { serverInfo.IP, serverInfo.Port, "" });
}
else
{
PrintWarning("Failed to get player's network connection.");
}
}
else
{
PrintWarning("Failed to get BasePlayer object.");
}
travelTimers.Remove(player.Id);
forcedTravels.Remove(player.Id);
};
var newTimer = timer.Once(10f, callback);
travelTimers[player.Id] = newTimer;
if (isForced)
{
forcedTravels[player.Id] = true;
}
}
private void CancelTravelCommand(IPlayer player, string cmd, string[] args)
{
if (travelTimers.TryGetValue(player.Id, out var timer))
{
if (forcedTravels.ContainsKey(player.Id) && !permission.UserHasPermission(player.Id, "xtravel.admin"))
{
player.Reply(lang.GetMessage("CannotCancelTravel", this, player.Id));
return;
}
timer.Destroy();
travelTimers.Remove(player.Id);
forcedTravels.Remove(player.Id);
player.Reply(lang.GetMessage("TravelCancelled", this, player.Id));
}
else
{
player.Reply(lang.GetMessage("NoTravelScheduled", this, player.Id));
}
}
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
{"NoPermission", "You don't have permission to use this command."},
{"InvalidCommand", "Invalid command. Please use a valid server command."},
{"TravelScheduled", "You will be teleported to the destination server in 10 seconds. Use /canceltravel to cancel."},
{"TravelAlreadyScheduled", "You already have a travel scheduled. Use /canceltravel to cancel."},
{"TravelCancelled", "Your travel has been cancelled."},
{"NoTravelScheduled", "You don't have any travel scheduled."},
{"CannotCancelTravel", "You cannot cancel this travel."}
}, this);
}
}
}