Advertisement
Guest User

/Write BuildingCommands.cs

a guest
Oct 7th, 2012
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.51 KB | None | 0 0
  1. static readonly CommandDescriptor CdWrite = new CommandDescriptor
  2.         {
  3.             Name = "Write",
  4.             Aliases = new[] { "Text", "Wt" },
  5.             Category = CommandCategory.Building,
  6.             Permissions = new Permission[] { Permission.DrawAdvanced },
  7.             RepeatableSelection = true,
  8.             IsConsoleSafe = false,
  9.             Help = "/Write, then click 2 blocks. The first is the starting point, the second is the direction",
  10.             Usage = "/Write Sentence",
  11.             Handler = WriteHandler,
  12.         };
  13.  
  14.         //TODO: add a collection of fonts. Performance++
  15.         static void WriteHandler(Player player, Command cmd)
  16.         {
  17.             string sentence = cmd.NextAll();
  18.             if (sentence.Length < 1)
  19.             {
  20.                 CdWrite.PrintUsage(player);
  21.                 return;
  22.             }
  23.             else
  24.             {
  25.                 player.Message("Write: Click 2 blocks or use &H/Mark&S to set direction.");
  26.                 player.SelectionStart(2, WriteCallback, sentence, Permission.DrawAdvanced);
  27.             }
  28.         }
  29.  
  30.         static void WriteCallback(Player player, Vector3I[] marks, object tag)
  31.         {
  32.             Block block = new Block();
  33.             string sentence = (string)tag;
  34.             //block bugfix kinda
  35.             if (player.LastUsedBlockType == Block.Undefined)
  36.             {
  37.                 block = Block.Stone;
  38.             }
  39.             else
  40.             {
  41.                 block = player.LastUsedBlockType;
  42.             }
  43.             //find the direction (needs attention)
  44.             Direction direction = DirectionFinder.GetDirection(marks);
  45.             try
  46.             {
  47.                 FontHandler render = new FontHandler(block, marks, player, direction); //create new instance
  48.                 render.CreateGraphicsAndDraw(sentence); //render the sentence
  49.                 if (render.blockCount > 0)
  50.                 {
  51.                     player.Message("/Write (Size {0}, {1}: Writing '{2}' using {3} blocks of {4}",
  52.                         player.font.Size,
  53.                         player.font.FontFamily.Name,
  54.                         sentence, render.blockCount,
  55.                         block.ToString());
  56.                 }
  57.                 else
  58.                 {
  59.                     player.Message("&WNo direction was set");
  60.                 }
  61.                 render = null; //get lost
  62.             }
  63.             catch (Exception e)
  64.             {
  65.                 player.Message(e.Message);
  66.                 Logger.Log(LogType.Error, "WriteCommand: " + e);
  67.             }
  68.         }
  69.  
  70.         static string[] GetFontSectionList()
  71.         {
  72.             if (Directory.Exists(Paths.FontsPath))
  73.             {
  74.                 string[] sections = Directory.GetFiles(Paths.FontsPath, "*.ttf", SearchOption.TopDirectoryOnly)
  75.                                              .Select(name => Path.GetFileNameWithoutExtension(name))
  76.                                              .Where(name => !String.IsNullOrEmpty(name))
  77.                                              .ToArray();
  78.                 if (sections.Length != 0)
  79.                 {
  80.                     return sections;
  81.                 }
  82.             }
  83.             return null;
  84.         }
  85.  
  86.  static CommandDescriptor CdSetFont = new CommandDescriptor()
  87.         {
  88.             Name = "SetFont",
  89.             Aliases = new[] { "FontSet", "Font", "Sf" },
  90.             Category = CommandCategory.Building,
  91.             Permissions = new Permission[] { Permission.DrawAdvanced },
  92.             IsConsoleSafe = false,
  93.             Help = "Sets the properties for /Write, such as: font and size",
  94.             Handler = SetFontHandler,
  95.             Usage = "/SetFont < Font | Size | Reset > <Variable>"
  96.         };
  97.  
  98.         static void SetFontHandler(Player player, Command cmd)
  99.         {
  100.             string Param = cmd.Next();
  101.             if (Param == null)
  102.             {
  103.                 CdSetFont.PrintUsage(player);
  104.                 return;
  105.             }
  106.             if (Param.ToLower() == "reset")
  107.             {
  108.                 player.font = new Font("Times New Roman", 20, FontStyle.Regular);
  109.                 player.Message("SetFont: Font reverted back to default ({0} size {1})",
  110.                     player.font.FontFamily.Name, player.font.Size);
  111.                 return;
  112.             }
  113.             if (Param.ToLower() == "font")
  114.             {
  115.                 string sectionName = cmd.NextAll();
  116.                 if (!Directory.Exists(Paths.FontsPath))
  117.                 {
  118.                     Directory.CreateDirectory(Paths.FontsPath);
  119.                     player.Message("There are no fonts available for this server. Font is set to default: {0}", player.font.FontFamily.Name);
  120.                     return;
  121.                 }
  122.                 string fontFileName = null;
  123.                 string[] sectionFiles = Directory.GetFiles(Paths.FontsPath, "*.ttf", SearchOption.TopDirectoryOnly);
  124.                 if (sectionName.Length < 1)
  125.                 {
  126.                     var sectionList = GetFontSectionList();
  127.                     player.Message("{0} fonts Available: {1}", sectionList.Length, sectionList.JoinToString()); //print the folder contents
  128.                     return;
  129.                 }
  130.                 for (int i = 0; i < sectionFiles.Length; i++)
  131.                 {
  132.                     string sectionFullName = Path.GetFileNameWithoutExtension(sectionFiles[i]);
  133.                     if (sectionFullName == null) continue;
  134.                     if (sectionFullName.StartsWith(sectionName, StringComparison.OrdinalIgnoreCase))
  135.                     {
  136.                         if (sectionFullName.Equals(sectionName, StringComparison.OrdinalIgnoreCase))
  137.                         {
  138.                             fontFileName = sectionFiles[i];
  139.                             break;
  140.                         }
  141.                         else if (fontFileName == null)
  142.                         {
  143.                             fontFileName = sectionFiles[i];
  144.                         }
  145.                         else
  146.                         {
  147.                             var matches = sectionFiles.Select(f => Path.GetFileNameWithoutExtension(f))
  148.                                                       .Where(sn => sn != null && sn.StartsWith(sectionName, StringComparison.OrdinalIgnoreCase));
  149.                             player.Message("Multiple font files matched \"{0}\": {1}",
  150.                                             sectionName, matches.JoinToString());
  151.                             return;
  152.                         }
  153.                     }
  154.                 }
  155.                 if (fontFileName != null)
  156.                 {
  157.                     string sectionFullName = Path.GetFileNameWithoutExtension(fontFileName);
  158.                     player.Message("Your font has changed to \"{0}\":", sectionFullName);
  159.                     //change font here
  160.                     player.font = new System.Drawing.Font(player.LoadFontFamily(fontFileName), player.font.Size);
  161.                     return;
  162.                 }
  163.                 else
  164.                 {
  165.                     var sectionList = GetFontSectionList();
  166.                     if (sectionList == null)
  167.                     {
  168.                         player.Message("No fonts have been found.");
  169.                     }
  170.                     else
  171.                     {
  172.                         player.Message("No fonts found for \"{0}\". Available fonts: {1}",
  173.                                         sectionName, sectionList.JoinToString());
  174.                     }
  175.                 }
  176.             }
  177.             if (Param.ToLower() == "size")
  178.             {
  179.                 int Size = -1;
  180.                 if (cmd.NextInt(out Size))
  181.                 {
  182.                     if (Size > 48 || Size < 10)
  183.                     {
  184.                         player.Message("&WIncorrect font size ({0}): Size needs to be between 10 and 48", Size);
  185.                         return;
  186.                     }
  187.                     player.Message("SetFont: Size changed from {0} to {1} ({2})", player.font.Size, Size, player.font.FontFamily.Name);
  188.                     player.font = new System.Drawing.Font(player.font.FontFamily, Size);
  189.                 }
  190.                 else
  191.                 {
  192.                     player.Message("&WInvalid size, use /SetFont Size FontSize. Example: /SetFont Size 14");
  193.                     return;
  194.                 }
  195.                 return;
  196.             }
  197.             else
  198.             {
  199.                 CdSetFont.PrintUsage(player);
  200.                 return;
  201.             }
  202.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement