From 3c26d97c0791ce6e9f553e08bbe7a441ca42fbcd Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 21 Jun 2017 16:33:26 +1000 Subject: [PATCH] Fix being able to send opchat messages with no contents, prefer explicit delegates to Func --- MCGalaxy/Chat/ChatModes.cs | 2 ++ MCGalaxy/Chat/ChatTokens.cs | 4 ++-- MCGalaxy/Commands/World/CmdBlockProperties.cs | 4 +++- MCGalaxy/Database/Stats/TopStat.cs | 16 ++++++++++++---- MCGalaxy/Drawing/Image/PixelGetter.cs | 11 +++++------ MCGalaxy/Generator/Foliage/Tree.cs | 6 ++++-- MCGalaxy/Generator/RealisticGenParams.cs | 7 +++++-- MCGalaxy/Levels/LevelOptions.cs | 4 +++- MCGalaxy/util/NET_20.cs | 10 +--------- 9 files changed, 37 insertions(+), 27 deletions(-) diff --git a/MCGalaxy/Chat/ChatModes.cs b/MCGalaxy/Chat/ChatModes.cs index 0f1544a73..ed51f0900 100644 --- a/MCGalaxy/Chat/ChatModes.cs +++ b/MCGalaxy/Chat/ChatModes.cs @@ -76,6 +76,8 @@ namespace MCGalaxy { string name = p == null ? "(console)" : p.name; string format = "To " + group + " &f-{0}&f- {1}"; + if (message == "") { Player.Message(p, "No message to send."); return; } + Chat.MessageWhere(format, pl => (p == pl || pl.Rank >= perm) && Chat.NotIgnoring(p, pl), displayName, message); diff --git a/MCGalaxy/Chat/ChatTokens.cs b/MCGalaxy/Chat/ChatTokens.cs index 8d2704529..53bca9f2d 100644 --- a/MCGalaxy/Chat/ChatTokens.cs +++ b/MCGalaxy/Chat/ChatTokens.cs @@ -54,8 +54,8 @@ namespace MCGalaxy { } - internal static Dictionary> standardTokens - = new Dictionary> { + internal static Dictionary> standardTokens + = new Dictionary> { { "$name", p => p.DisplayName == null ? null : (Server.dollarNames ? "$" : "") + Colors.StripColors(p.DisplayName) }, { "$truename", p => p.truename == null ? null : diff --git a/MCGalaxy/Commands/World/CmdBlockProperties.cs b/MCGalaxy/Commands/World/CmdBlockProperties.cs index dbf81c520..2ffa7d5d8 100644 --- a/MCGalaxy/Commands/World/CmdBlockProperties.cs +++ b/MCGalaxy/Commands/World/CmdBlockProperties.cs @@ -133,8 +133,10 @@ namespace MCGalaxy.Commands.World { delegate void BoolSetter(ref BlockProps props); + delegate bool BoolGetter(BlockProps props); + static void Toggle(Player p, BlockProps[] scope, ExtBlock block, string type, - BoolSetter setter, Func getter) { + BoolSetter setter, BoolGetter getter) { BlockProps props = scope[block.Index]; setter(ref props); scope[block.Index] = props; diff --git a/MCGalaxy/Database/Stats/TopStat.cs b/MCGalaxy/Database/Stats/TopStat.cs index 0f6e8203d..649d60aa5 100644 --- a/MCGalaxy/Database/Stats/TopStat.cs +++ b/MCGalaxy/Database/Stats/TopStat.cs @@ -20,15 +20,23 @@ using System.Collections.Generic; namespace MCGalaxy.DB { + /// Retrieves a title for a column. + /// Title is displayed on a line before the values of that column. + public delegate string TopStatTitle(); + + /// Formats a value in a column. + public delegate string TopStatFormatter(string input); + + /// Outputs ordered stats from a column in a database table. public sealed class TopStat { public readonly string Identifier, Table, Column, OrderBy; - public readonly Func Title; - public readonly Func Formatter; + public readonly TopStatTitle Title; + public readonly TopStatFormatter Formatter; - public TopStat(string identifier, string table, string col, Func title, - Func formatter, bool ascending = false, string orderBy = null) { + public TopStat(string identifier, string table, string col, TopStatTitle title, + TopStatFormatter formatter, bool ascending = false, string orderBy = null) { Identifier = identifier; Table = table; Column = col; diff --git a/MCGalaxy/Drawing/Image/PixelGetter.cs b/MCGalaxy/Drawing/Image/PixelGetter.cs index bbd50af24..c5d8ba524 100644 --- a/MCGalaxy/Drawing/Image/PixelGetter.cs +++ b/MCGalaxy/Drawing/Image/PixelGetter.cs @@ -22,6 +22,8 @@ using MCGalaxy.Drawing.Ops; namespace MCGalaxy.Drawing { + public delegate void PixelGetterCallback(Pixel pixel, DrawOpOutput output); + public sealed class PixelGetter : IDisposable { Bitmap bmp; @@ -41,14 +43,12 @@ namespace MCGalaxy.Drawing { data = bmp.LockBits(r, ImageLockMode.ReadOnly, bmp.PixelFormat); } - public void Iterate(DrawOpOutput output, - Action callback) { + public void Iterate(DrawOpOutput output, PixelGetterCallback callback) { if (data == null) IterateSlow(output, callback); else IterateFast(output, callback); } - unsafe void IterateFast(DrawOpOutput output, - Action callback) { + unsafe void IterateFast(DrawOpOutput output, PixelGetterCallback callback) { Pixel pixel; int width = bmp.Width, height = bmp.Height; byte* scan0 = (byte*)data.Scan0; @@ -69,8 +69,7 @@ namespace MCGalaxy.Drawing { } } - void IterateSlow(DrawOpOutput output, - Action callback) { + void IterateSlow(DrawOpOutput output, PixelGetterCallback callback) { Pixel pixel; int width = bmp.Width, height = bmp.Height; for (int y = 0; y < height; y++) diff --git a/MCGalaxy/Generator/Foliage/Tree.cs b/MCGalaxy/Generator/Foliage/Tree.cs index 06ea8981c..1ee75d74c 100644 --- a/MCGalaxy/Generator/Foliage/Tree.cs +++ b/MCGalaxy/Generator/Foliage/Tree.cs @@ -23,6 +23,8 @@ namespace MCGalaxy.Generator.Foliage { public delegate void TreeOutput(ushort x, ushort y, ushort z, byte block); + public delegate Tree TreeConstructor(); + public abstract class Tree { protected internal int height, size; protected Random rnd; @@ -60,8 +62,8 @@ namespace MCGalaxy.Generator.Foliage { } - public static Dictionary> TreeTypes = - new Dictionary>() { + public static Dictionary TreeTypes = + new Dictionary() { { "Fern", () => new NormalTree() }, { "Cactus", () => new CactusTree() }, { "Notch", () => new ClassicTree() }, { "Swamp", () => new SwampTree() }, { "Bamboo", () => new BambooTree() }, { "Palm", () => new PalmTree() }, diff --git a/MCGalaxy/Generator/RealisticGenParams.cs b/MCGalaxy/Generator/RealisticGenParams.cs index 606a841d8..e6d3d50f6 100644 --- a/MCGalaxy/Generator/RealisticGenParams.cs +++ b/MCGalaxy/Generator/RealisticGenParams.cs @@ -18,7 +18,10 @@ using System; using System.Collections.Generic; -namespace MCGalaxy.Generator { +namespace MCGalaxy.Generator { + + public delegate ushort CalcLiquidLevel(ushort lvlHeight); + public sealed class RealisticGenParams { public float RangeLow = 0.2f; public float RangeHigh = 0.8f; @@ -26,7 +29,7 @@ namespace MCGalaxy.Generator { public bool FalloffEdges = false; public bool UseLavaLiquid = false; public bool GenerateOverlay2 = true; - public Func GetLiquidLevel = (height) => (ushort)(height / 2 + 2); + public CalcLiquidLevel GetLiquidLevel = (lvlHeight) => (ushort)(lvlHeight / 2 + 2); // Decoration parameters public float TreeDens = 0.35f; diff --git a/MCGalaxy/Levels/LevelOptions.cs b/MCGalaxy/Levels/LevelOptions.cs index 9a40fd5ad..0aab02850 100644 --- a/MCGalaxy/Levels/LevelOptions.cs +++ b/MCGalaxy/Levels/LevelOptions.cs @@ -26,6 +26,8 @@ namespace MCGalaxy { public delegate void OptionSetter(Player p, Level lvl, string value); + public delegate bool OptionIntValidator(Player p, int value); + public static Dictionary Options = new Dictionary() { { "motd", SetMotd }, { "RealmOwner", SetRealmOwner }, @@ -153,7 +155,7 @@ namespace MCGalaxy { } static void SetInt(Player p, Level lvl, ref int target, string value, string name, - Func validator = null) { + OptionIntValidator validator = null) { if (value == "") { Player.Message(p, "You must provide an integer."); return; } int raw = 0; if (!CommandParser.GetInt(p, value, name, ref raw)) return; diff --git a/MCGalaxy/util/NET_20.cs b/MCGalaxy/util/NET_20.cs index 2f8101c9c..62f9514b9 100644 --- a/MCGalaxy/util/NET_20.cs +++ b/MCGalaxy/util/NET_20.cs @@ -25,15 +25,7 @@ namespace System.Runtime.CompilerServices { public sealed class ExtensionAttribute : Attribute {} } -namespace System { - public delegate TReturn Func(); - public delegate TReturn Func(T1 arg1); - public delegate TReturn Func(T1 arg1, T2 arg2); - public delegate TReturn Func(T1 arg1, T2 arg2, T3 arg3); - - public delegate void Action(); - public delegate void Action(T1 arg1, T2 arg2); -} +namespace System { public delegate void Action(); } namespace MCGalaxy.Util {