Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static readonly CommandDescriptor CdWrite = new CommandDescriptor
- {
- Name = "Write",
- Aliases = new[] { "Text", "Wt" },
- Category = CommandCategory.Building,
- Permissions = new Permission[] { Permission.DrawAdvanced },
- RepeatableSelection = true,
- IsConsoleSafe = false,
- Help = "/Write, then click 2 blocks. The first is the starting point, the second is the direction",
- Usage = "/Write Sentence",
- Handler = WriteHandler,
- };
- //TODO: add a collection of fonts. Performance++
- static void WriteHandler(Player player, Command cmd)
- {
- string sentence = cmd.NextAll();
- if (sentence.Length < 1)
- {
- CdWrite.PrintUsage(player);
- return;
- }
- else
- {
- player.Message("Write: Click 2 blocks or use &H/Mark&S to set direction.");
- player.SelectionStart(2, WriteCallback, sentence, Permission.DrawAdvanced);
- }
- }
- static void WriteCallback(Player player, Vector3I[] marks, object tag)
- {
- Block block = new Block();
- string sentence = (string)tag;
- //block bugfix kinda
- if (player.LastUsedBlockType == Block.Undefined)
- {
- block = Block.Stone;
- }
- else
- {
- block = player.LastUsedBlockType;
- }
- //find the direction (needs attention)
- Direction direction = DirectionFinder.GetDirection(marks);
- try
- {
- FontHandler render = new FontHandler(block, marks, player, direction); //create new instance
- render.CreateGraphicsAndDraw(sentence); //render the sentence
- if (render.blockCount > 0)
- {
- player.Message("/Write (Size {0}, {1}: Writing '{2}' using {3} blocks of {4}",
- player.font.Size,
- player.font.FontFamily.Name,
- sentence, render.blockCount,
- block.ToString());
- }
- else
- {
- player.Message("&WNo direction was set");
- }
- render = null; //get lost
- }
- catch (Exception e)
- {
- player.Message(e.Message);
- Logger.Log(LogType.Error, "WriteCommand: " + e);
- }
- }
- static string[] GetFontSectionList()
- {
- if (Directory.Exists(Paths.FontsPath))
- {
- string[] sections = Directory.GetFiles(Paths.FontsPath, "*.ttf", SearchOption.TopDirectoryOnly)
- .Select(name => Path.GetFileNameWithoutExtension(name))
- .Where(name => !String.IsNullOrEmpty(name))
- .ToArray();
- if (sections.Length != 0)
- {
- return sections;
- }
- }
- return null;
- }
- static CommandDescriptor CdSetFont = new CommandDescriptor()
- {
- Name = "SetFont",
- Aliases = new[] { "FontSet", "Font", "Sf" },
- Category = CommandCategory.Building,
- Permissions = new Permission[] { Permission.DrawAdvanced },
- IsConsoleSafe = false,
- Help = "Sets the properties for /Write, such as: font and size",
- Handler = SetFontHandler,
- Usage = "/SetFont < Font | Size | Reset > <Variable>"
- };
- static void SetFontHandler(Player player, Command cmd)
- {
- string Param = cmd.Next();
- if (Param == null)
- {
- CdSetFont.PrintUsage(player);
- return;
- }
- if (Param.ToLower() == "reset")
- {
- player.font = new Font("Times New Roman", 20, FontStyle.Regular);
- player.Message("SetFont: Font reverted back to default ({0} size {1})",
- player.font.FontFamily.Name, player.font.Size);
- return;
- }
- if (Param.ToLower() == "font")
- {
- string sectionName = cmd.NextAll();
- if (!Directory.Exists(Paths.FontsPath))
- {
- Directory.CreateDirectory(Paths.FontsPath);
- player.Message("There are no fonts available for this server. Font is set to default: {0}", player.font.FontFamily.Name);
- return;
- }
- string fontFileName = null;
- string[] sectionFiles = Directory.GetFiles(Paths.FontsPath, "*.ttf", SearchOption.TopDirectoryOnly);
- if (sectionName.Length < 1)
- {
- var sectionList = GetFontSectionList();
- player.Message("{0} fonts Available: {1}", sectionList.Length, sectionList.JoinToString()); //print the folder contents
- return;
- }
- for (int i = 0; i < sectionFiles.Length; i++)
- {
- string sectionFullName = Path.GetFileNameWithoutExtension(sectionFiles[i]);
- if (sectionFullName == null) continue;
- if (sectionFullName.StartsWith(sectionName, StringComparison.OrdinalIgnoreCase))
- {
- if (sectionFullName.Equals(sectionName, StringComparison.OrdinalIgnoreCase))
- {
- fontFileName = sectionFiles[i];
- break;
- }
- else if (fontFileName == null)
- {
- fontFileName = sectionFiles[i];
- }
- else
- {
- var matches = sectionFiles.Select(f => Path.GetFileNameWithoutExtension(f))
- .Where(sn => sn != null && sn.StartsWith(sectionName, StringComparison.OrdinalIgnoreCase));
- player.Message("Multiple font files matched \"{0}\": {1}",
- sectionName, matches.JoinToString());
- return;
- }
- }
- }
- if (fontFileName != null)
- {
- string sectionFullName = Path.GetFileNameWithoutExtension(fontFileName);
- player.Message("Your font has changed to \"{0}\":", sectionFullName);
- //change font here
- player.font = new System.Drawing.Font(player.LoadFontFamily(fontFileName), player.font.Size);
- return;
- }
- else
- {
- var sectionList = GetFontSectionList();
- if (sectionList == null)
- {
- player.Message("No fonts have been found.");
- }
- else
- {
- player.Message("No fonts found for \"{0}\". Available fonts: {1}",
- sectionName, sectionList.JoinToString());
- }
- }
- }
- if (Param.ToLower() == "size")
- {
- int Size = -1;
- if (cmd.NextInt(out Size))
- {
- if (Size > 48 || Size < 10)
- {
- player.Message("&WIncorrect font size ({0}): Size needs to be between 10 and 48", Size);
- return;
- }
- player.Message("SetFont: Size changed from {0} to {1} ({2})", player.font.Size, Size, player.font.FontFamily.Name);
- player.font = new System.Drawing.Font(player.font.FontFamily, Size);
- }
- else
- {
- player.Message("&WInvalid size, use /SetFont Size FontSize. Example: /SetFont Size 14");
- return;
- }
- return;
- }
- else
- {
- CdSetFont.PrintUsage(player);
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement