Skip to content
This repository has been archived by the owner on Sep 24, 2019. It is now read-only.

Commit

Permalink
More abstraction, implement flags, implement zone types
Browse files Browse the repository at this point in the history
  • Loading branch information
Trojaner committed Jul 2, 2015
1 parent 7fe1e58 commit 0fe5588
Show file tree
Hide file tree
Showing 18 changed files with 317 additions and 96 deletions.
31 changes: 15 additions & 16 deletions Rocket_Safezone/Commands/CreateCommand.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Rocket.Unturned;
using Rocket.Unturned.Commands;
using Rocket.Unturned.Player;
using Rocket.Unturned.Plugins;
using Safezone.Model;

namespace Safezone.Commands
{
Expand All @@ -17,32 +19,29 @@ public void Execute(RocketPlayer caller, string[] command)
return;
}

if (!SafeZonePlugin.Instance.HasPositionSet(caller))
{
RocketChat.Say(caller.CSteamID, "Please set pos1 (/spos1) and pos2 (/spos2) before using this command", UnityEngine.Color.red);
return;
}
String name = command.GetStringParameter(0);

if (SafeZonePlugin.Instance.GetSafeZone(name, true) != null)
{
RocketChat.Say(caller.CSteamID, "A safezone with this name already exists!", UnityEngine.Color.red);
return;
}

SafeZone zone = new SafeZone

SafeZoneType type = SafeZoneType.CreateSafeZoneType(name);
ArrayList args = new ArrayList(command);
args.RemoveAt(0); // remove name

SafeZone safeZone = type.Create(caller, name, args);

if (safeZone == null)
{
Name = name,
Position1 = SafeZonePlugin.Instance.GetPosition1(caller),
Position2 = SafeZonePlugin.Instance.GetPosition2(caller),
PickupAllowed = true,
VehiclesAllowed = true,
NoZombies = true
};
RocketChat.Say(caller.CSteamID, "Could't create safezone!", UnityEngine.Color.red);
return;
}

SafeZonePlugin.Instance.Configuration.SafeZones.Add(zone);
SafeZonePlugin.Instance.Configuration.SafeZones.Add(safeZone);
SafeZonePlugin.Instance.Configuration.Save();
SafeZonePlugin.Instance.OnSafeZoneCreated(zone);
SafeZonePlugin.Instance.OnSafeZoneCreated(safeZone);

RocketChat.Say(caller.CSteamID, "Successfully created safezone: " + name, UnityEngine.Color.green);
}
Expand Down
1 change: 1 addition & 0 deletions Rocket_Safezone/Commands/ListCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Rocket.Unturned.Commands;
using Rocket.Unturned.Player;
using Rocket.Unturned.Plugins;
using Safezone.Model;
using Steamworks;
using UnityEngine;

Expand Down
1 change: 1 addition & 0 deletions Rocket_Safezone/Commands/Pos1Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Rocket.Unturned;
using Rocket.Unturned.Commands;
using Rocket.Unturned.Player;
using Safezone.Model;
using UnityEngine;

namespace Safezone.Commands
Expand Down
1 change: 1 addition & 0 deletions Rocket_Safezone/Commands/Pos2Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Rocket.Unturned;
using Rocket.Unturned.Commands;
using Rocket.Unturned.Player;
using Safezone.Model;
using UnityEngine;

namespace Safezone.Commands
Expand Down
7 changes: 5 additions & 2 deletions Rocket_Safezone/Commands/RedefineCommand.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Rocket.Unturned;
using Rocket.Unturned.Commands;
using Rocket.Unturned.Player;
using Rocket.Unturned.Plugins;
using Safezone.Model;
using Steamworks;

namespace Safezone.Commands
Expand Down Expand Up @@ -34,8 +36,9 @@ public void Execute(RocketPlayer caller, string[] command)

SafeZonePlugin.Instance.Configuration.SafeZones.Remove(zone);
SafeZonePlugin.Instance.OnSafeZoneRemoved(zone);
zone.Position1 = SafeZonePlugin.Instance.GetPosition1(caller);
zone.Position2 = SafeZonePlugin.Instance.GetPosition2(caller);
ArrayList args = new ArrayList(command);
args.RemoveAt(0); // remove name
zone.Type.Redefine(caller, args);
SafeZonePlugin.Instance.Configuration.SafeZones.Add(zone);
SafeZonePlugin.Instance.Configuration.Save();
SafeZonePlugin.Instance.OnSafeZoneCreated(zone);
Expand Down
1 change: 1 addition & 0 deletions Rocket_Safezone/Commands/RemoveCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Rocket.Unturned.Commands;
using Rocket.Unturned.Player;
using Rocket.Unturned.Plugins;
using Safezone.Model;
using Steamworks;

namespace Safezone.Commands
Expand Down
9 changes: 9 additions & 0 deletions Rocket_Safezone/Model/Flag/EnterVehiclesFlag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Safezone.Model.Flag
{
public class EnterVehiclesFlag : Flag
{
public EnterVehiclesFlag() : base("EnterVehicles", true)
{
}
}
}
43 changes: 43 additions & 0 deletions Rocket_Safezone/Model/Flag/Flag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Xml.Serialization;

namespace Safezone.Model.Flag
{
[Serializable]
[XmlType(AnonymousType = false)]
[XmlInclude(typeof(PickupAllowedFlag))]
[XmlInclude(typeof(GodmodeFlag))]
[XmlInclude(typeof(NoZombieFlag))]
[XmlInclude(typeof(EnterVehiclesFlag))]

//for now this does not support 3rd party flags
public abstract class Flag
{
public String Name;
public Object Value;
public T GetValue<T>()
{
if (!(Value is T))
{
throw new InvalidOperationException("Can't cast " + Value.GetType().Name + " to " + typeof(T).Name);
}
return (T) Value;
}

public T GetDefaultValue<T>()
{
if (!(_defaultValue is T))
{
throw new InvalidOperationException("Can't cast " + Value.GetType().Name + " to " + typeof(T).Name);
}
return (T)_defaultValue;
}

private readonly Object _defaultValue;
protected Flag(String name, Object defaultValue)
{
Name = name;
_defaultValue = defaultValue;
}
}
}
10 changes: 10 additions & 0 deletions Rocket_Safezone/Model/Flag/GodmodeFlag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Safezone.Model.Flag
{
public class GodmodeFlag : Flag
{
public GodmodeFlag() : base("Godmode", true)
{

}
}
}
9 changes: 9 additions & 0 deletions Rocket_Safezone/Model/Flag/NoZombieFlag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Safezone.Model.Flag
{
public class NoZombieFlag : Flag
{
public NoZombieFlag() : base("NoZombie", true)
{
}
}
}
13 changes: 13 additions & 0 deletions Rocket_Safezone/Model/Flag/PickupAllowedFlag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Xml.Serialization;

namespace Safezone.Model.Flag
{
public class PickupAllowedFlag : Flag
{
public PickupAllowedFlag() : base("PickupAllowed", true)
{

}
}
}
15 changes: 15 additions & 0 deletions Rocket_Safezone/Model/Position.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Xml.Serialization;

namespace Safezone.Model
{
[Serializable]
[XmlType(AnonymousType = true)]
public class Position
{
[XmlAttribute("x")]
public float X;
[XmlAttribute("y")]
public float Y;
}
}
72 changes: 72 additions & 0 deletions Rocket_Safezone/Model/Safezone/RectangleType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections;
using Rocket.Unturned;
using Rocket.Unturned.Player;

namespace Safezone.Model
{
public class RectangleType : SafeZoneType
{
public Position Position1;
public Position Position2;

public override string GetName()
{
return "Rectangle";
}

public override SafeZone Create(RocketPlayer player, String name, ArrayList args)
{
if (!SafeZonePlugin.Instance.HasPositionSet(player))
{
RocketChat.Say(player.CSteamID, "Please set pos1 (/spos1) and pos2 (/spos2) before using this command", UnityEngine.Color.red);
return null;
}

Position1 = SafeZonePlugin.Instance.GetPosition1(player);
Position2 = SafeZonePlugin.Instance.GetPosition2(player);
SafeZone zone = new SafeZone
{
Name = name,
Owner = SafeZonePlugin.GetId(player),
Type = this
};

return zone;
}

public override bool IsInSafeZone(Position p)
{
Position p1 = Position1;
Position p2 = Position2;

//float x2 = p1.X;
//float y2 = p4.X;
//float x3 = p4.X;
//float y3 = p1.Y;

//Position p2 = new Position() {X = x2, Y = y2};
//Position p3 = new Position() {X = x3, Y = y3 };

bool b1 = p.X >= Math.Min(p1.X, p2.X);
bool b2 = p.X <= Math.Max(p1.X, p2.X);
bool b3 = p.Y >= Math.Min(p1.Y, p2.Y);
bool b4 = p.Y <= Math.Max(p1.Y, p2.Y);

return b1 && b2 && b3 && b4;
}

public override bool Redefine(RocketPlayer player, ArrayList args)
{
if (!SafeZonePlugin.Instance.HasPositionSet(player))
{
RocketChat.Say(player.CSteamID, "Please set pos1 (/spos1) and pos2 (/spos2) before using this command", UnityEngine.Color.red);
return false;
}

Position1 = SafeZonePlugin.Instance.GetPosition1(player);
Position2 = SafeZonePlugin.Instance.GetPosition2(player);
return true;
}
}
}
37 changes: 37 additions & 0 deletions Rocket_Safezone/Model/Safezone/SafeZone.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;

namespace Safezone.Model
{
public class SafeZone
{
[XmlAttribute("Name")]
public string Name;
[XmlAttribute("Owner")]
public uint Owner;
[XmlElement("Type")]
public SafeZoneType Type;

[XmlArray("Flags")]
public List<Flag.Flag> Flags;

public Flag.Flag GetFlag(Type t, bool createIfNotFound = true)
{
if (t != typeof(Flag.Flag))
{
throw new ArgumentException("Can't get " + t.Name + " as flag!");
}

foreach (Flag.Flag flag in Flags.Where(flag => flag.GetType().FullName == t.FullName))
{
return flag;
}

if (!createIfNotFound) return null;

return (Flag.Flag)Activator.CreateInstance(t);
}
}
}
49 changes: 49 additions & 0 deletions Rocket_Safezone/Model/Safezone/SafeZoneType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Serialization;
using Rocket.Unturned.Player;

namespace Safezone.Model
{
[XmlInclude(typeof(RectangleType))]
public abstract class SafeZoneType
{
public SafeZoneType()
{
// dummy
}

private static readonly Dictionary<String, Type> RegistereTypes = new Dictionary<String, Type>();

public static SafeZoneType CreateSafeZoneType(String name)
{
if (!RegistereTypes.ContainsKey(name))
{
return null;
}
Type t = RegistereTypes[name];
return (SafeZoneType)Activator.CreateInstance(t);
}

public static void RegisterSafeZoneType(String name, Type t)
{
if(t != typeof(SafeZoneType))
{
throw new ArgumentException(t.Name + " is not a SafeZoneType!");
}

if (RegistereTypes.ContainsKey(name))
{
throw new ArgumentException(name + " is already registered!");
}

RegistereTypes.Add(name, t);
}

public abstract String GetName();
public abstract SafeZone Create(RocketPlayer player, String name, ArrayList args);
public abstract bool IsInSafeZone(Position p);
public abstract bool Redefine(RocketPlayer player, ArrayList args);
}
}
Loading

0 comments on commit 0fe5588

Please sign in to comment.