Guest User

Untitled

a guest
Nov 24th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.44 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, Tuple<string[],Player>> DoorLookUp = new Dictionary<string,Tuple<string[],Player>>();
  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.FindZone(player.Info.Name + "door") != null)
  35.             {
  36.                 player.World.Map.RemoveZone(player.Info.Name + "door");
  37.                 if(DoorLookUp.ContainsKey(player.Info.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.FindZone(player.Info.Name + "door") != null)
  56.             {
  57.                 player.Message("One door per person.");
  58.                 return;
  59.             }
  60.  
  61.             Zone door = new Zone();
  62.             door.Name = player.Info.Name + "door";
  63.             player.SetCallback(2, DoorAdd, door, cdDoor.Permissions);
  64.             player.Message("Door: Place a block or type /mark to use your location.");
  65.  
  66.         }
  67.  
  68.         static void DoorAdd(Player player, Position[] marks, object tag)
  69.         {
  70.             int maxBlocks = 12;  //max door size, change as needed... should add config key
  71.  
  72.             int sx = Math.Min(marks[0].X, marks[1].X);
  73.             int ex = Math.Max(marks[0].X, marks[1].X);
  74.             int sy = Math.Min(marks[0].Y, marks[1].Y);
  75.             int ey = Math.Max(marks[0].Y, marks[1].Y);
  76.             int sh = Math.Min(marks[0].H, marks[1].H);
  77.             int eh = Math.Max(marks[0].H, marks[1].H);
  78.  
  79.             int volume = (ex - sx + 1) * (ey - sy + 1) * (eh - sh + 1);
  80.             if (volume > maxBlocks)
  81.             {
  82.                 player.Message("Doors are only allowed to be {0} blocks", maxBlocks);
  83.                 return;
  84.             }
  85.  
  86.             ZoneCommands.ZoneAddCallback(player, marks, tag);
  87.             player.Message("Door successfully created");
  88.  
  89.             Logger.Log("{0} created door {1} (on world {2})", LogType.UserActivity, player.Name, player.Name + "door", player.World.Name);
  90.         }
  91.  
  92.  
  93.         static void DoorTimer_Elapsed(SchedulerTask task)
  94.         {
  95.             string door = (string)task.UserState;
  96.             string[] doorVars = DoorLookUp[door].Item1;
  97.             string x, y, z, b;
  98.            
  99.             //parse values from DoorLookUp
  100.             x = doorVars[0].TrimEnd(' '); y = doorVars[1].TrimEnd(' '); z = doorVars[2].TrimEnd(' '); b = doorVars[3].TrimEnd(' ');
  101.             string[] xs = x.Split(' '), ys = y.Split(' '), zs = z.Split(' '), bs = b.Split(' ');
  102.  
  103.             int i = 0;
  104.  
  105.             foreach (string arg in xs)
  106.             {
  107.                 DoorLookUp[door].Item2.World.Map.QueueUpdate(new BlockUpdate(null, Int32.Parse(xs[i]), Int32.Parse(ys[i]), Int32.Parse(zs[i]), Byte.Parse(bs[i])));
  108.                 i++;
  109.             }
  110.  
  111.  
  112.         }
  113.  
  114.         public static void PlayerClickedDoor(object sender, PlayerClickedEventArgs e)
  115.         {
  116.            
  117.             Zone[] allowed, denied;
  118.             if (e.Player.World.Map.TestZones(e.X, e.Y, e.H, e.Player, out allowed, out denied))
  119.             {
  120.                 foreach (Zone zone in allowed)
  121.                 {
  122.                     if (zone.Name.Contains("door"))
  123.                     {
  124.                      
  125.                         CutDoor(zone, e.Player);
  126.                        
  127.                     }
  128.                 }
  129.                
  130.             }
  131.         }
  132.  
  133.         public static void CutDoor(Zone zone, Player player)
  134.         {
  135.          
  136.             int sx = zone.Bounds.XMin;
  137.             int ex = zone.Bounds.XMax;
  138.             int sy = zone.Bounds.YMin;
  139.             int ey = zone.Bounds.YMax;
  140.             int sh = zone.Bounds.HMin;
  141.             int eh = zone.Bounds.HMax;
  142.                      
  143.             string[] DL = new string[4];
  144.            
  145.             DL[0] = ""; DL[1] = ""; DL[2] = ""; DL[3] = "";
  146.  
  147.             //for each block in selection, add x, y, z coords and block type to string array
  148.             for (int i = sx; i <= ex; i++)
  149.             {
  150.                 for (int j = sy; j <= ey; j++)
  151.                 {
  152.                     for (int k = sh; k <= eh; k++)
  153.                     {
  154.                         DL[0] += i.ToString() + " "; //x
  155.                         DL[1] += j.ToString() + " "; //y
  156.                         DL[2] += k.ToString() + " "; //z
  157.                         DL[3] += player.World.Map.GetBlockByte(i, j, k) + " "; //block
  158.                        
  159.                         //clear the door
  160.                         player.World.Map.QueueUpdate(new BlockUpdate(null, i, j, k, Block.Air));
  161.                        
  162.                     }
  163.                 }
  164.             }
  165.  
  166.             Logger.Log("{0} opened door {1} (on world {2})", LogType.UserActivity, player.Name, zone.Name, player.World.Name);
  167.  
  168.             var values = Tuple.Create(DL, player);
  169.  
  170.             if (!DoorLookUp.ContainsKey(zone.Name))
  171.             {
  172.                 DoorLookUp.Add(zone.Name, values);
  173.             }
  174.             else
  175.             {
  176.                 DoorLookUp[zone.Name] = values;
  177.             }
  178.                                
  179.             Scheduler.NewTask(DoorTimer_Elapsed).RunOnce(zone.Name, DoorCloseTimer); //timer to re-close door
  180.         }
  181.     }
  182. }
Add Comment
Please, Sign In to add comment