Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using fCraft.Events;
- namespace fCraft
- {
- public static class Doors
- {
- static readonly TimeSpan DoorCloseTimer = TimeSpan.FromMilliseconds(1500); // time that door will stay open
- public static Dictionary<string, string[]> DoorLookUp = new Dictionary<string, string[]>();
- public static void Init()
- {
- CommandManager.RegisterCommand(cdDoor);
- CommandManager.RegisterCommand(cdDoorRemove);
- Server.PlayerClicked += PlayerClickedDoor;
- }
- static readonly CommandDescriptor cdDoorRemove = new CommandDescriptor
- {
- Name = "removedoor",
- Aliases = new[] { "rd" },
- Category = CommandCategory.Zone,
- Permissions = new[] { Permission.Chat },
- Help = "Removes door.",
- Handler = DoorRemove
- };
- static void DoorRemove(Player player, Command cmd)
- {
- if (player.World.Map.Zones.Find(player.Info.Name + "door") != null)
- {
- player.World.Map.Zones.Remove(player.Info.Name + "door");
- if(DoorLookUp.ContainsKey(player.Name + "door"))
- DoorLookUp.Remove(player.Info.Name + "door");
- player.Message("Door removed.");
- }
- }
- static readonly CommandDescriptor cdDoor = new CommandDescriptor
- {
- Name = "door",
- Category = CommandCategory.Zone,
- Permissions = new[] { Permission.Chat },
- Help = "Creates door zone. Left click to open doors.",
- Handler = Door
- };
- static void Door(Player player, Command cmd)
- {
- if (player.World.Map.Zones.Find(player.Info.Name + "door") != null)
- {
- player.Message("One door per person.");
- return;
- }
- Zone door = new Zone();
- door.Name = player.Info.Name + "door";
- player.SelectionStart(2, DoorAdd, door, cdDoor.Permissions);
- player.Message("Door: Place a block or type /mark to use your location.");
- }
- static void DoorAdd(Player player, Position[] marks, object tag)
- {
- int maxBlocks = 12; //max door size, change as needed... should add config key
- int sx = Math.Min(marks[0].X, marks[1].X);
- int ex = Math.Max(marks[0].X, marks[1].X);
- int sy = Math.Min(marks[0].Y, marks[1].Y);
- int ey = Math.Max(marks[0].Y, marks[1].Y);
- int sh = Math.Min(marks[0].Z, marks[1].Z);
- int eh = Math.Max(marks[0].Z, marks[1].Z);
- int volume = (ex - sx + 1) * (ey - sy + 1) * (eh - sh + 1);
- if (volume > maxBlocks)
- {
- player.Message("Doors are only allowed to be {0} blocks", maxBlocks);
- return;
- }
- ZoneCommands.ZoneAddCallback(player, marks, tag);
- player.Message("Door successfully created");
- Logger.Log("{0} created door {1} (on world {2})", LogType.UserActivity, player.Name, player.Name + "door", player.World.Name);
- }
- static void DoorTimer_Elapsed(SchedulerTask task)
- {
- string door = (string)task.UserState;
- string[] doorVars = DoorLookUp[door];
- string x, y, z, b;
- Player player = Server.FindPlayerExact(doorVars[4]);
- //parse values from DoorLookUp
- x = doorVars[0].TrimEnd(' '); y = doorVars[1].TrimEnd(' '); z = doorVars[2].TrimEnd(' '); b = doorVars[3].TrimEnd(' ');
- string[] xs = x.Split(' '), ys = y.Split(' '), zs = z.Split(' '), bs = b.Split(' ');
- int i = 0;
- foreach (string arg in xs)
- {
- player.World.Map.QueueUpdate(new BlockUpdate(null, Int32.Parse(xs[i]), Int32.Parse(ys[i]), Int32.Parse(zs[i]), Byte.Parse(bs[i])));
- i++;
- }
- DoorLookUp.Remove(player.Name + "door");
- }
- public static void PlayerClickedDoor(object sender, PlayerClickedEventArgs e)
- {
- Zone[] allowed, denied;
- if (e.Player.World.Map.Zones.CheckDetailed(e.X, e.Y, e.Z, e.Player, out allowed, out denied))
- {
- foreach (Zone zone in allowed)
- {
- if (zone.Name.Contains("door"))
- {
- if (!DoorLookUp.ContainsKey(zone.Name))
- CutDoor(zone, e.Player);
- else
- e.Player.Message("That door has already been opened");
- }
- }
- }
- }
- public static void CutDoor(Zone zone, Player player)
- {
- int sx = zone.Bounds.XMin;
- int ex = zone.Bounds.XMax;
- int sy = zone.Bounds.YMin;
- int ey = zone.Bounds.YMax;
- int sh = zone.Bounds.ZMin;
- int eh = zone.Bounds.ZMax;
- string[] DL = new string[5];
- DL[0] = ""; DL[1] = ""; DL[2] = ""; DL[3] = ""; DL[4] = player.Name;
- //for each block in selection, add x, y, z coords and block type to string array
- for (int i = sx; i <= ex; i++)
- {
- for (int j = sy; j <= ey; j++)
- {
- for (int k = sh; k <= eh; k++)
- {
- DL[0] += i.ToString() + " "; //x
- DL[1] += j.ToString() + " "; //y
- DL[2] += k.ToString() + " "; //z
- DL[3] += player.World.Map.GetBlockByte(i, j, k) + " "; //block
- //clear the door
- player.World.Map.QueueUpdate(new BlockUpdate(null, i, j, k, Block.Air));
- }
- }
- }
- Logger.Log("{0} opened door {1} (on world {2})", LogType.UserActivity, player.Name, zone.Name, player.World.Name);
- DoorLookUp.Add(zone.Name, DL);
- Scheduler.NewTask(DoorTimer_Elapsed).RunOnce(zone.Name, DoorCloseTimer); //timer to re-close door
- }
- }
- }
Add Comment
Please, Sign In to add comment