Im trying to save multiple homes into my Dictionary Homes in Playerinfo, but i can only save 1 home in that Dictionary per Userid.
Does anyone got an idea how to get that working?
Adding homes at [Chatcommand("Home")]
using System;
using System.Text;
using System.Collections.Generic;
using UnityEngine;
using Oxide.Core;
namespace Oxide.Plugins
{
[Info("Test Mod", "Dev", 1.0)]
[Description("Test Mod in Csharp")]
public class TeleportPlugin : RustPlugin
{
static TeleportPlugin tpPlugin = null;
bool initialized;
bool newSaveDetected = false;
bool Changed = false;
PlayerData playerPrefs = new PlayerData();
#region Config File
int maxHomes;
int maxTeleportsPerDay;
bool enableMaxTeleports;
bool wipeDataOnNewSave;
bool enablePermission;
string permissionName;
#endregion
#region Classes
class PlayerData
{
public Dictionary<ulong, PlayerInfo> PlayerInfo = new Dictionary<ulong, PlayerInfo>();
public PlayerData() { }
}
class PlayerInfo
{
//Home List => Name , Position
public Dictionary<string, Vector3> Homes = new Dictionary<string, Vector3>();
}
void SaveData() => Interface.Oxide.DataFileSystem.WriteObject(this.Title, playerPrefs);
#endregion
void Loaded() => tpPlugin = this;
void OnServerSave()
{
if (initialized)
SaveData();
}
void Unload()
{
if (!initialized)
return;
SaveData();
//foreach (var player in BasePlayer.activePlayerList)
// DestroyGUI(player);
}
void OnNewSave(string strFilename)
{
if (wipeDataOnNewSave)
newSaveDetected = true;
}
void OnServerInitialized()
{
if (newSaveDetected || (playerPrefs == null || playerPrefs.PlayerInfo == null || playerPrefs.PlayerInfo.Count == 0))
{
playerPrefs = new PlayerData();
SaveData();
}
initialized = true;
SaveData();
Puts("Stats can be reset by > pp.reset <");
}
#region Chat Messages
void sendMessage(BasePlayer player, string msg, params object[] args)
{
PrintToChat(player, msg, args);
}
void Broadcast(string msg, params object[] args)
{
PrintToChat(msg, args);
}
string msg(string key, string id = null) => lang.GetMessage(key, this, id);
#endregion
#region Functions
object GetConfig(string menu, string datavalue, object defaultValue)
{
var data = Config[menu] as Dictionary<string, object>;
if (data == null)
{
data = new Dictionary<string, object>();
Config[menu] = data;
}
object value;
if (!data.TryGetValue(datavalue, out value))
{
value = defaultValue;
data[datavalue] = value;
}
return value;
}
bool hasRights(string UserIDString)
{
if (!enablePermission)
return true;
if (!permission.UserHasPermission(UserIDString, permissionName))
return false;
return true;
}
#endregion
protected override void LoadDefaultConfig()
{
Config.Clear();
LoadVariables();
}
void LoadVariables()
{
maxHomes = Convert.ToInt32(GetConfig("Settings", "Max Homes", 10));
maxTeleportsPerDay = Convert.ToInt32(GetConfig("Settings", "Max Teleports per Day", 100));
enableMaxTeleports = Convert.ToBoolean(GetConfig("Settings", "Teleport Cap", true));
wipeDataOnNewSave = Convert.ToBoolean(GetConfig("Settings", "WipeDataOnNewSave", false));
enablePermission = Convert.ToBoolean(GetConfig("Permission", "Enable Permission", false));
permissionName = Convert.ToString(GetConfig("Permission", "Permission Name", "teleportplugin.use"));
if (!Changed) return;
SaveConfig();
Changed = false;
}
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
{"Teleport", "<color=#ff0000>Teleport!</color> You have teleported to <color=#66FF33>{0}</color> !"},
{"InvalidUsage", "<color=#ff0000>Invalid Params!</color> {0}!"},
{"Home_Valid", "<color=#ff0000>Home Valid!</color>\nName: {0}!"},
{"Home_Found", "<color=#ff0000>Home Found!</color>\nName: {0}!"},
{"Home_set", "<color=#ff0000>Home Set!</color>\nName: {0}\nPos:{1}!"},
{"PluginPlayerOn", "The plugin functions are now enabled again"},
{"PluginPlayerOff", "The plugin functions are now disabled for your character"},
}, this);
}
void Init()
{
LoadVariables();
LoadDefaultMessages();
initialized = false;
if (!permission.PermissionExists(permissionName))
permission.RegisterPermission(permissionName, this);
}
#region Chat Commands
[ChatCommand("savehome")]
void saveHome(BasePlayer player, string command)
{
SaveData();
}
[ChatCommand("home")]
void setHome(BasePlayer player, string command, string[] args)
{
if (args.Length < 1 || args.Length > 1 || args == null)
{
PrintToChat(player, string.Format(msg("InvalidUsage"), "Use /sethome Home name"));
return;
}
if (args.Length.Equals(1))
{
// Only allows one save per steam id ...
// How can i prevent that?
PlayerInfo p = null;
if (!playerPrefs.PlayerInfo.TryGetValue(player.userID, out p))
{
var info = new PlayerInfo();
info.Homes.Add(args[0].ToString(), player.transform.position);
playerPrefs.PlayerInfo.Add(player.userID, info);
PrintToChat(player, string.Format(msg("Home_set"), args[0].ToString(), player.transform.position));
}
}
}
/*
*
* Vector3 pos = playerPrefs.PlayerInfo[player.userID].Homes[home];
PrintToChat(player, string.Format(msg("Home_Valid"), args[0] + "\nPos:" + pos));
*/
#endregion
#region Console Commands
[ConsoleCommand("pp.reset")]
void ResetCommand(ConsoleSystem.Arg arg)
{
if (arg.Connection != null && arg.Connection.authLevel < 2)
return;
if (arg.Args == null || arg.Args.Length != 1 || arg.Args[0] != "true")
{
SendReply(arg, "Usage: pp.reset true | Resets all userdata to zero");
return;
}
playerPrefs = new PlayerData();
Interface.Oxide.DataFileSystem.WriteObject(this.Title, playerPrefs);
/* foreach (var player in BasePlayer.activePlayerList)
{
if (player != null)
{
CuiHelper.DestroyUi(player, "ZLevelsUI");
UpdatePlayer(player);
if (cuiEnabled)
CreateGUI(player);
}
}*/
SendReply(arg, "Userdata was successfully reset to zero");
}
#endregion
} //end class
} // end namespace