Skip to content

Commit

Permalink
fixes and tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
RestoreMonarchy committed Jun 15, 2020
1 parent 61e223e commit 263fe53
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 63 deletions.
17 changes: 7 additions & 10 deletions Commands/DestroyHomeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Rocket.Unturned.Chat;
using RestoreMonarchy.MoreHomes.Models;
using RestoreMonarchy.MoreHomes.Helpers;
using Steamworks;

namespace RestoreMonarchy.MoreHomes.Commands
{
Expand All @@ -31,18 +32,14 @@ public void Execute(IRocketPlayer caller, string[] command)
return;
}

if (HomesHelper.TryRemoveHome(player.CSteamID, home.Transform))
if (!HomesHelper.TryRemoveHome(player.CSteamID, home.InteractableBed))
return;

BarricadeManager.tryGetInfo(home.Transform, out byte b, out byte b2, out ushort num, out ushort num2, out _);
BarricadeManager.instance.channel.send("tellTakeBarricade", ESteamCall.ALL, b, b2, BarricadeManager.BARRICADE_REGIONS, ESteamPacket.UPDATE_RELIABLE_BUFFER, new object[]
{
b,
b2,
num,
num2
});
UnturnedChat.Say(caller, pluginInstance.Translate("DestroyHomeSuccess", home.Name), pluginInstance.MessageColor);
if (BarricadeManager.tryGetInfo(home.InteractableBed.transform, out var x, out var y, out var plant, out var index, out var region))
{
BarricadeManager.destroyBarricade(region, x, y, plant, index);
UnturnedChat.Say(caller, pluginInstance.Translate("DestroyHomeSuccess", home.Name), pluginInstance.MessageColor);
}
}

public AllowedCaller AllowedCaller => AllowedCaller.Player;
Expand Down
20 changes: 15 additions & 5 deletions Commands/HomeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using RestoreMonarchy.Teleportation.Utils;
using System;
using RestoreMonarchy.MoreHomes.Helpers;
using UnityEngine;

namespace RestoreMonarchy.MoreHomes.Commands
{
Expand All @@ -24,7 +25,7 @@ public void Execute(IRocketPlayer caller, string[] command)

if (home == null)
{
UnturnedChat.Say(caller, pluginInstance.Translate("NoBedsToTeleport"), pluginInstance.MessageColor);
UnturnedChat.Say(caller, pluginInstance.Translate("NoHomeToTeleport"), pluginInstance.MessageColor);
return;
}

Expand All @@ -37,6 +38,8 @@ public void Execute(IRocketPlayer caller, string[] command)
return;
}

pluginInstance.PlayerCooldowns[caller.Id] = DateTime.Now.AddSeconds(VipHelper.GetPlayerHomeCooldown(caller.Id));

float delay = VipHelper.GetPlayerHomeDelay(player.Id);

if (delay > 0)
Expand All @@ -47,17 +50,24 @@ public void Execute(IRocketPlayer caller, string[] command)
TaskDispatcher.QueueOnMainThread(() =>
{
if (!ValidateTeleportation(player, home))
{
pluginInstance.PlayerCooldowns.Remove(caller.Id);
return;
}

player.Teleport(home.Transform.position, player.Rotation);
UnturnedChat.Say(caller, pluginInstance.Translate("HomeSuccess", home.Name), pluginInstance.MessageColor);
pluginInstance.PlayerCooldowns[caller.Id] = DateTime.Now.AddSeconds(VipHelper.GetPlayerHomeCooldown(caller.Id));
if (!player.Player.teleportToLocation(home.LivePosition + new Vector3(0f, 0.5f, 0f), player.Rotation))
{
UnturnedChat.Say(caller, pluginInstance.Translate("HomeFailed", home.Name), pluginInstance.MessageColor);
pluginInstance.PlayerCooldowns.Remove(caller.Id);
return;
}
UnturnedChat.Say(caller, pluginInstance.Translate("HomeSuccess", home.Name), pluginInstance.MessageColor);
}, delay);
}

private bool ValidateTeleportation(UnturnedPlayer player, PlayerHome home)
{
if (home.Transform == null || home.Owner == null)
if (home.InteractableBed == null)
{
UnturnedChat.Say(player, pluginInstance.Translate("BedDestroyed"), pluginInstance.MessageColor);
return false;
Expand Down
6 changes: 3 additions & 3 deletions Commands/RestoreHomesCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public void Execute(IRocketPlayer caller, string[] command)
{
foreach (BarricadeDrop drop in region.drops)
{
InteractableBed bed = drop.interactable as InteractableBed;
if (bed != null && bed.isClaimed)
InteractableBed interactableBed = drop.interactable as InteractableBed;
if (interactableBed != null && interactableBed.isClaimed)
{
HomesHelper.TryClaimHome(bed.owner, bed.transform);
HomesHelper.TryClaimHome(interactableBed.owner, interactableBed);
num++;
}
}
Expand Down
12 changes: 6 additions & 6 deletions Helpers/HomesHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ public static PlayerHome GetPlayerHome(CSteamID steamID, string name = null)
return player.Homes.FirstOrDefault(x => name == null || x.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
}

public static bool TryClaimHome(CSteamID steamID, Transform transform)
public static bool TryClaimHome(CSteamID steamID, InteractableBed interactableBed)
{
var player = GetOrCreatePlayer(steamID);
if (player.Homes.Count >= VipHelper.GetPlayerMaxHomes(steamID.ToString()))
{
UnturnedChat.Say(steamID, pluginInstance.Translate("MaxHomes"), pluginInstance.MessageColor);
UnturnedChat.Say(steamID, pluginInstance.Translate("MaxHomesWarn"), pluginInstance.MessageColor);
return false;
}
var home = new PlayerHome(player.GetUniqueHomeName(), transform, player);
var home = new PlayerHome(player.GetUniqueHomeName(), interactableBed);
player.Homes.Add(home);
UnturnedChat.Say(steamID, pluginInstance.Translate("Home", home.Name), pluginInstance.MessageColor);
UnturnedChat.Say(steamID, pluginInstance.Translate("HomeClaimed", home.Name), pluginInstance.MessageColor);
return true;
}

public static bool TryRemoveHome(CSteamID steamID, Transform transform)
public static bool TryRemoveHome(CSteamID steamID, InteractableBed interactableBed)
{
var player = GetOrCreatePlayer(steamID);
var home = player.Homes.FirstOrDefault(x => x.Transform == transform);
var home = player.Homes.FirstOrDefault(x => x.InteractableBed == interactableBed);
if (home != null)
{
player.Homes.Remove(home);
Expand Down
16 changes: 7 additions & 9 deletions Models/PlayerHome.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using Newtonsoft.Json;
using SDG.Unturned;
using UnityEngine;

namespace RestoreMonarchy.MoreHomes.Models
{
public class PlayerHome
{
public PlayerHome() { }
public PlayerHome(string name, Transform transform, PlayerData owner)
public PlayerHome(string name, InteractableBed interactableBed)
{
Name = name;
Transform = transform;
Position = new ConvertablePosition(Transform.position);
Owner = owner;
InteractableBed = interactableBed;
Position = new ConvertablePosition(InteractableBed.transform.position);
}

public string Name { get; set; }
Expand All @@ -22,16 +22,14 @@ public Vector3 LivePosition
{
get
{
if (Transform != null)
return Transform.position;
if (InteractableBed != null)
return InteractableBed.transform.position;
else
return Position.ToVector3();
}
}

[JsonIgnore]
public Transform Transform { get; set; }
[JsonIgnore]
public PlayerData Owner { get; set; }
public InteractableBed InteractableBed { get; set; }
}
}
27 changes: 17 additions & 10 deletions MoreHomesPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class MoreHomesPlugin : RocketPlugin<MoreHomesConfiguration>
private Harmony HarmonyInstance;

protected override void Load()
{
{
Instance = this;
MessageColor = UnturnedChat.GetColorFromName(Configuration.Instance.MessageColor, Color.green);

Expand All @@ -55,15 +55,16 @@ private void OnPluginsLoaded()
}

public override TranslationList DefaultTranslations => new TranslationList(){
{ "HomeCooldownWarn", "You have to wait {0} to use this command again" },
{ "HomeDelayWarn", "You will be teleported to your bed in {0} seconds" },
{ "MaxHomesWarn", "You cannot have more beds" },
{ "NoBedsToTeleport", "You don't have any bed to teleport or name doesn't match any" },
{ "BedDestroyed", "Your bed got destroyed. Teleportation canceled" },

{ "HomeCooldown", "You have to wait {0} to use this command again" },
{ "HomeDelayWarn", "You will be teleported to your home in {0} seconds" },
{ "MaxHomesWarn", "You cannot have more homes" },
{ "BedDestroyed", "Your home got destroyed. Teleportation canceled" },
{ "WhileDriving", "You cannot teleport while driving" },
{ "HomeSuccess", "Successfully teleported You to your {0} bed!" },
{ "HomeList", "Your beds: " },
{ "NoHomes", "You don't have any bed claimed" },
{ "NoHome", "You don't have any home to teleport or name doesn't match any" },
{ "HomeSuccess", "Successfully teleported You to your {0} home!" },
{ "HomeList", "Your homes: " },
{ "NoHomes", "You don't have any home" },
{ "DestroyHomeFormat", "Format: /destroyhome <BedName>" },
{ "HomeNotFound", "No home match {0} name" },
{ "DestroyHomeSuccess", "Successfully destroyed and unclaimed your {0} home!" },
Expand All @@ -72,7 +73,13 @@ private void OnPluginsLoaded()
{ "RenameHomeSuccess", "Successfully renamed home {0} to {1}!" },
{ "WhileRaid", "You can't teleport while in raiding" },
{ "WhileCombat", "You can't teleport while in combat" },
{ "RestoreHomesSuccess", "Successfully restored {0} homes!" }
{ "RestoreHomesSuccess", "Successfully restored {0} homes!" },
{ "RemoveHome", "Your {0} home got removed!" },
{ "RemoveHomeFail", "Failed to remove your home" },
{ "RenameHomeFail", "You already have home named {0}" },
{ "RenameHomeSuccess", "Successfully renamed home {0} to {1}!" },
{ "HomeClaimed", "Your new home name: {0}" },
{ "HomeFailed", "Failed to teleport you to {0} home" }
};

protected override void Unload()
Expand Down
18 changes: 9 additions & 9 deletions Patches/BarricadeManager_AskClaimBed_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,12 @@ public static bool AskClaimBed_Prefix(BarricadeManager __instance, CSteamID stea
{
if (interactableBed.isClaimed)
{
#region AddHome
#region RemoveHome

if (HomesHelper.TryClaimHome(steamID, interactableBed.transform))
{
return false;
}
HomesHelper.TryRemoveHome(steamID, interactableBed);

#endregion


if (plant == 65535)
{
BarricadeManager.instance.channel.send("tellClaimBed", ESteamCall.ALL, x, y, BarricadeManager.BARRICADE_REGIONS, ESteamPacket.UPDATE_RELIABLE_BUFFER, new object[]
Expand All @@ -72,11 +68,15 @@ public static bool AskClaimBed_Prefix(BarricadeManager __instance, CSteamID stea
}
else
{
#region RemoveHome
#region AddHome

if (!HomesHelper.TryClaimHome(steamID, interactableBed))
{
return false;
}

HomesHelper.TryRemoveHome(steamID, interactableBed.transform);

#endregion

BarricadeManager.unclaimBeds(player.channel.owner.playerID.steamID);
if (plant == 65535)
{
Expand Down
2 changes: 1 addition & 1 deletion Patches/BarricadeManager_TellTakeBarricade_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static void TellTakeBarricade_Prefix(BarricadeManager __instance, CSteamI
InteractableBed interactableBed = barricadeRegion.drops[index].interactable as InteractableBed;
if (interactableBed != null)
{
HomesHelper.TryRemoveHome(interactableBed.owner, interactableBed.transform);
HomesHelper.TryRemoveHome(interactableBed.owner, interactableBed);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@

[assembly: Guid("3e883397-b5b2-459a-b0b9-ff035d919d78")]

[assembly: AssemblyVersion("1.4.0")]
[assembly: AssemblyFileVersion("1.4.0")]
[assembly: AssemblyVersion("1.4.1")]
[assembly: AssemblyFileVersion("1.4.1")]
16 changes: 8 additions & 8 deletions Services/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class DataService : MonoBehaviour
void Awake()
{
PlayersDataStorage = new DataStorage<List<PlayerData>>(pluginInstance.Directory, "MoreHomesData.json");

SaveManager.onPostSave += SaveData;
}

Expand All @@ -42,28 +41,29 @@ public void ReloadData()
if (PlayersData == null)
PlayersData = new List<PlayerData>();

List<InteractableBed> beds = new List<InteractableBed>();
var interactableBeds = new List<InteractableBed>();

foreach (var region in BarricadeManager.regions)
{
foreach (var drop in region.drops)
{
if (drop.interactable as InteractableBed != null)
beds.Add(drop.interactable as InteractableBed);
interactableBeds.Add(drop.interactable as InteractableBed);
}
}

foreach (var player in PlayersData)
{
foreach (var home in player.Homes)
{
foreach (var bed in beds)
foreach (var interactableBed in interactableBeds)
{
if (bed.transform.position.x == home.Position.X && bed.transform.position.y == home.Position.Y
&& bed.transform.position.z == home.Position.Z)
if (interactableBed.transform.position.x == home.Position.X && interactableBed.transform.position.y == home.Position.Y
&& interactableBed.transform.position.z == home.Position.Z)
{
home.Transform = bed.transform;
beds.Remove(bed);
System.Console.WriteLine("Equal bed found!");
home.InteractableBed = interactableBed;
interactableBeds.Remove(interactableBed);
break;
}
}
Expand Down

0 comments on commit 263fe53

Please sign in to comment.