Guest User

Untitled

a guest
Apr 26th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using fCraft.Events;
  4.  
  5. namespace fCraft
  6. {
  7.     public static class Doors
  8.     {
  9.         static readonly TimeSpan DoorCloseTimer = TimeSpan.FromMilliseconds(1500); // time that door will stay open
  10.         public static Dictionary<string, string[]> DoorLookUp = new Dictionary<string, string[]>();
  11.        
  12.  
  13.         public static void Init()
  14.         {
  15.             CommandManager.RegisterCommand(cdDoor);
  16.             CommandManager.RegisterCommand(cdDoorRemove);
  17.  
  18.             Server.PlayerClicked += PlayerClickedDoor;
  19.  
  20.         }
  21.  
  22.         static readonly CommandDescriptor cdDoorRemove = new CommandDescriptor
  23.         {
  24.             Name = "removedoor",
  25.             Aliases = new[] { "rd" },
  26.             Category = CommandCategory.Zone,
  27.             Permissions = new[] { Permission.Chat },
  28.             Help = "Removes door.",
  29.             Handler = DoorRemove
  30.         };
  31.  
  32.         static void DoorRemove(Player player, Command cmd)
  33.         {
  34.             if (player.World.Map.Zones.Find(player.Info.Name + "door") != null)
  35.             {
  36.                 player.World.Map.Zones.Remove(player.Info.Name + "door");
  37.                 if(DoorLookUp.ContainsKey(player.Name + "door"))
  38.                        DoorLookUp.Remove(player.Info.Name + "door");
  39.                 player.Message("Door removed.");
  40.             }
  41.  
  42.         }
  43.  
  44.         static readonly CommandDescriptor cdDoor = new CommandDescriptor
  45.         {
  46.             Name = "door",
  47.             Category = CommandCategory.Zone,
  48.             Permissions = new[] { Permission.Chat },
  49.             Help = "Creates door zone. Left click to open doors.",
  50.             Handler = Door
  51.         };
  52.  
  53.         static void Door(Player player, Command cmd)
  54.         {
  55.             if (player.World.Map.Zones.Find(player.Info.Name + "door") != null)
  56.            
  57.             {
  58.                 player.Message("One door per person.");
  59.                 return;
  60.             }
  61.  
  62.             Zone door = new Zone();
  63.             door.Name = player.Info.Name + "door";
  64.             player.SelectionStart(2, DoorAdd, door, cdDoor.Permissions);
  65.             player.Message("Door: Place a block or type /mark to use your location.");
  66.  
  67.         }
  68.  
  69.         static void DoorAdd(Player player, Position[] marks, object tag)
  70.         {
  71.             int maxBlocks = 12;  //max door size, change as needed... should add config key
  72.  
  73.             int sx = Math.Min(marks[0].X, marks[1].X);
  74.             int ex = Math.Max(marks[0].X, marks[1].X);
  75.             int sy = Math.Min(marks[0].Y, marks[1].Y);
  76.             int ey = Math.Max(marks[0].Y, marks[1].Y);
  77.             int sh = Math.Min(marks[0].Z, marks[1].Z);
  78.             int eh = Math.Max(marks[0].Z, marks[1].Z);
  79.  
  80.             int volume = (ex - sx + 1) * (ey - sy + 1) * (eh - sh + 1);
  81.             if (volume > maxBlocks)
  82.             {
  83.                 player.Message("Doors are only allowed to be {0} blocks", maxBlocks);
  84.                 return;
  85.             }
  86.  
  87.             ZoneCommands.ZoneAddCallback(player, marks, tag);
  88.             player.Message("Door successfully created");
  89.  
  90.             Logger.Log("{0} created door {1} (on world {2})", LogType.UserActivity, player.Name, player.Name + "door", player.World.Name);
  91.         }
  92.  
  93.  
  94.         static void DoorTimer_Elapsed(SchedulerTask task)
  95.         {
  96.             string door = (string)task.UserState;
  97.             string[] doorVars = DoorLookUp[door];
  98.             string x, y, z, b;
  99.  
  100.             Player player = Server.FindPlayerExact(doorVars[4]);
  101.  
  102.             //parse values from DoorLookUp
  103.             x = doorVars[0].TrimEnd(' '); y = doorVars[1].TrimEnd(' '); z = doorVars[2].TrimEnd(' '); b = doorVars[3].TrimEnd(' ');
  104.             string[] xs = x.Split(' '), ys = y.Split(' '), zs = z.Split(' '), bs = b.Split(' ');
  105.  
  106.             int i = 0;
  107.  
  108.             foreach (string arg in xs)
  109.             {
  110.                 player.World.Map.QueueUpdate(new BlockUpdate(null, Int32.Parse(xs[i]), Int32.Parse(ys[i]), Int32.Parse(zs[i]), Byte.Parse(bs[i])));
  111.                 i++;
  112.             }
  113.  
  114.             DoorLookUp.Remove(player.Name + "door");
  115.         }
  116.  
  117.         public static void PlayerClickedDoor(object sender, PlayerClickedEventArgs e)
  118.         {
  119.  
  120.             Zone[] allowed, denied;
  121.             if (e.Player.World.Map.Zones.CheckDetailed(e.X, e.Y, e.Z, e.Player, out allowed, out denied))
  122.             {
  123.                 foreach (Zone zone in allowed)
  124.                 {
  125.                     if (zone.Name.Contains("door"))
  126.                     {
  127.                         if (!DoorLookUp.ContainsKey(zone.Name))
  128.                             CutDoor(zone, e.Player);
  129.                         else
  130.                             e.Player.Message("That door has already been opened");
  131.                     }
  132.                 }
  133.  
  134.             }
  135.         }
  136.  
  137.         public static void CutDoor(Zone zone, Player player)
  138.         {
  139.  
  140.             int sx = zone.Bounds.XMin;
  141.             int ex = zone.Bounds.XMax;
  142.             int sy = zone.Bounds.YMin;
  143.             int ey = zone.Bounds.YMax;
  144.             int sh = zone.Bounds.ZMin;
  145.             int eh = zone.Bounds.ZMax;
  146.  
  147.             string[] DL = new string[5];
  148.  
  149.             DL[0] = ""; DL[1] = ""; DL[2] = ""; DL[3] = ""; DL[4] = player.Name;
  150.  
  151.             //for each block in selection, add x, y, z coords and block type to string array
  152.             for (int i = sx; i <= ex; i++)
  153.             {
  154.                 for (int j = sy; j <= ey; j++)
  155.                 {
  156.                     for (int k = sh; k <= eh; k++)
  157.                     {
  158.                         DL[0] += i.ToString() + " "; //x
  159.                         DL[1] += j.ToString() + " "; //y
  160.                         DL[2] += k.ToString() + " "; //z
  161.                         DL[3] += player.World.Map.GetBlockByte(i, j, k) + " "; //block
  162.  
  163.                         //clear the door
  164.                         player.World.Map.QueueUpdate(new BlockUpdate(null, i, j, k, Block.Air));
  165.  
  166.                     }
  167.                 }
  168.             }
  169.  
  170.             Logger.Log("{0} opened door {1} (on world {2})", LogType.UserActivity, player.Name, zone.Name, player.World.Name);
  171.  
  172.             DoorLookUp.Add(zone.Name, DL);
  173.            
  174.             Scheduler.NewTask(DoorTimer_Elapsed).RunOnce(zone.Name, DoorCloseTimer); //timer to re-close door
  175.         }
  176.     }
  177. }
Add Comment
Please, Sign In to add comment