From 3ff98f29bbfadabd6c70907bb39a9c0fa4c9d1a7 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 20 May 2020 00:08:02 +0200 Subject: [PATCH] Improve GetBlockInfo(), block colors Made GetBlockInfo() always return a reference without needing it as a parameter Fixed Color property Added CustomColor property for temporarily setting any color --- GamecraftModdingAPI/Block.cs | 47 ++++++++++++----- GamecraftModdingAPI/Blocks/BlockEngine.cs | 52 ++++++++++++------- GamecraftModdingAPI/Blocks/BlockIDs.cs | 3 ++ GamecraftModdingAPI/Blocks/ConsoleBlock.cs | 16 +++--- GamecraftModdingAPI/Blocks/TextBlock.cs | 13 +++-- .../Tests/GamecraftModdingAPIPluginTest.cs | 26 ++++++++-- 6 files changed, 106 insertions(+), 51 deletions(-) diff --git a/GamecraftModdingAPI/Block.cs b/GamecraftModdingAPI/Block.cs index 11a7ddc..6a5b2a1 100644 --- a/GamecraftModdingAPI/Block.cs +++ b/GamecraftModdingAPI/Block.cs @@ -4,7 +4,7 @@ using System.Reflection; using Svelto.ECS; using Svelto.ECS.EntityStructs; using RobocraftX.Common; -using RobocraftX.Blocks.Scaling; +using RobocraftX.Blocks; using Unity.Mathematics; using GamecraftModdingAPI.Blocks; @@ -113,11 +113,10 @@ namespace GamecraftModdingAPI /// public float3 Scale { - get => BlockEngine.GetBlockInfo(Id)?.scale ?? float3.zero; + get => BlockEngine.GetBlockInfo(Id).scale; set { - var def = new ScalingEntityStruct(); - BlockEngine.GetBlockInfo(Id, ref def).scale = value; + BlockEngine.GetBlockInfo(Id).scale = value; } } @@ -126,13 +125,11 @@ namespace GamecraftModdingAPI /// public int UniformScale { - get => BlockEngine.GetBlockInfo(Id)?.desiredScaleFactor ?? 0; + get => BlockEngine.GetBlockInfo(Id).scaleFactor; set { - var def = new BlockPlacementScaleEntityStruct(); - ref var scaleStruct = ref BlockEngine.GetBlockInfo(Id, ref def); - scaleStruct.blockPlacementHeight = scaleStruct.blockPlacementWidth = - scaleStruct.desiredScaleFactor = scaleStruct.snapGridScale = value; + ref var scaleStruct = ref BlockEngine.GetBlockInfo(Id); + scaleStruct.scaleFactor = value; Scale = new float3(value, value, value); } } @@ -140,7 +137,14 @@ namespace GamecraftModdingAPI /// /// The block's type (ID). Returns BlockIDs.Invalid if the block doesn't exist anymore. /// - public BlockIDs Type => (BlockIDs)(BlockEngine.GetBlockInfo(Id)?.DBID ?? ushort.MaxValue); + public BlockIDs Type + { + get + { + var id = (BlockIDs) BlockEngine.GetBlockInfo(Id, out var exists).DBID; + return exists ? id : BlockIDs.Invalid; + } + } /// /// The block's color. Returns BlockColors.Default if the block no longer exists. @@ -149,15 +153,32 @@ namespace GamecraftModdingAPI { get { - byte index = BlockEngine.GetBlockInfo(Id)?.indexInPalette ?? byte.MaxValue; + byte index = BlockEngine.GetBlockInfo(Id, out var exists).indexInPalette; + if (!exists) index = byte.MaxValue; if (index == byte.MaxValue) return new BlockColor { Color = BlockColors.Default }; return new BlockColor { Color = (BlockColors)(index % 10), Darkness = (byte)(index / 10) }; } set { - var def = new ColourParameterEntityStruct(); - ref var color = ref BlockEngine.GetBlockInfo(Id, ref def); + ref var color = ref BlockEngine.GetBlockInfo(Id); color.indexInPalette = (byte)(value.Color + value.Darkness * 10); + color.overridePaletteColour = false; + color.needsUpdate = true; + BlockEngine.SetBlockColorFromPalette(ref color); + } + } + + /// + /// The block's exact color. Gets reset to the palette color (Color property) after reentering the game. + /// + public float4 CustomColor + { + get => BlockEngine.GetBlockInfo(Id).overriddenColour; + set + { + ref var color = ref BlockEngine.GetBlockInfo(Id); + color.overriddenColour = value; + color.overridePaletteColour = true; color.needsUpdate = true; } } diff --git a/GamecraftModdingAPI/Blocks/BlockEngine.cs b/GamecraftModdingAPI/Blocks/BlockEngine.cs index d8182ca..377fa5b 100644 --- a/GamecraftModdingAPI/Blocks/BlockEngine.cs +++ b/GamecraftModdingAPI/Blocks/BlockEngine.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using RobocraftX.Blocks; using RobocraftX.Common; +using RobocraftX.GUI.Hotbar.Colours; using Svelto.DataStructures; using Svelto.ECS; @@ -37,33 +38,44 @@ namespace GamecraftModdingAPI.Blocks return ret; } - /// - /// Get a struct of a block. Can be used to set properties. - /// When only querying parameters, use the other overload for convenience. - /// - /// - /// - /// - /// - public ref T GetBlockInfo(EGID blockID, ref T def) where T : struct, IEntityComponent + public void SetBlockColorFromPalette(ref ColourParameterEntityStruct color) { - if (entitiesDB.Exists(blockID)) - return ref entitiesDB.QueryEntity(blockID); - return ref def; + ref var paletteEntry = ref entitiesDB.QueryEntity(color.indexInPalette, + CommonExclusiveGroups.COLOUR_PALETTE_GROUP); + color.paletteColour = paletteEntry.Colour; } /// - /// Get a struct of a block. Can only be used to retrieve information. - /// Use the overload with a default parameter to get the struct by reference to set values. + /// Get a struct of a block. Can be used to set properties. + /// Returns a default value if not found. /// - /// The block's EGID - /// The struct's type to get - /// A copy of the struct or null - public T? GetBlockInfo(EGID blockID) where T : struct, IEntityComponent + /// The block's ID + /// The struct to query + /// An editable reference to the struct + public ref T GetBlockInfo(EGID blockID) where T : struct, IEntityComponent { if (entitiesDB.Exists(blockID)) - return entitiesDB.QueryEntity(blockID); - return null; + return ref entitiesDB.QueryEntity(blockID); + T[] structHolder = new T[1]; //Create something that can be referenced + return ref structHolder[0]; //Gets a default value automatically + } + + /// + /// Get a struct of a block. Can be used to set properties. + /// Returns a default value if not found. + /// + /// The block's ID + /// Whether the specified struct exists for the block + /// The struct to query + /// An editable reference to the struct + public ref T GetBlockInfo(EGID blockID, out bool exists) where T : struct, IEntityComponent + { + exists = entitiesDB.Exists(blockID); + if (exists) + return ref entitiesDB.QueryEntity(blockID); + T[] structHolder = new T[1]; + ref T structRef = ref structHolder[0]; + return ref structRef; } public bool BlockExists(EGID id) diff --git a/GamecraftModdingAPI/Blocks/BlockIDs.cs b/GamecraftModdingAPI/Blocks/BlockIDs.cs index a4d07ff..4babe62 100644 --- a/GamecraftModdingAPI/Blocks/BlockIDs.cs +++ b/GamecraftModdingAPI/Blocks/BlockIDs.cs @@ -5,6 +5,9 @@ namespace GamecraftModdingAPI.Blocks /// public enum BlockIDs : ushort { + /// + /// A custom value for the API. Doesn't exist for Gamecraft. + /// Invalid = ushort.MaxValue, AluminiumCube = 0, AxleS, diff --git a/GamecraftModdingAPI/Blocks/ConsoleBlock.cs b/GamecraftModdingAPI/Blocks/ConsoleBlock.cs index c1dec27..b4fcaa4 100644 --- a/GamecraftModdingAPI/Blocks/ConsoleBlock.cs +++ b/GamecraftModdingAPI/Blocks/ConsoleBlock.cs @@ -55,42 +55,42 @@ namespace GamecraftModdingAPI.Blocks { get { - return BlockEngine.GetBlockInfo(Id)?.commandName ?? null; + return BlockEngine.GetBlockInfo(Id).commandName; } set { - BlockEngine.GetBlockInfo(Id)?.commandName.Set(value); + BlockEngine.GetBlockInfo(Id).commandName.Set(value); } } public string Arg1 { - get => BlockEngine.GetBlockInfo(Id)?.arg1 ?? null; + get => BlockEngine.GetBlockInfo(Id).arg1; set { - BlockEngine.GetBlockInfo(Id)?.arg1.Set(value); + BlockEngine.GetBlockInfo(Id).arg1.Set(value); } } public string Arg2 { - get => BlockEngine.GetBlockInfo(Id)?.arg2 ?? null; + get => BlockEngine.GetBlockInfo(Id).arg2; set { - BlockEngine.GetBlockInfo(Id)?.arg2.Set(value); + BlockEngine.GetBlockInfo(Id).arg2.Set(value); } } public string Arg3 { - get => BlockEngine.GetBlockInfo(Id)?.arg3 ?? null; + get => BlockEngine.GetBlockInfo(Id).arg3; set { - BlockEngine.GetBlockInfo(Id)?.arg3.Set(value); + BlockEngine.GetBlockInfo(Id).arg3.Set(value); } } } diff --git a/GamecraftModdingAPI/Blocks/TextBlock.cs b/GamecraftModdingAPI/Blocks/TextBlock.cs index bd2a23b..a905a6d 100644 --- a/GamecraftModdingAPI/Blocks/TextBlock.cs +++ b/GamecraftModdingAPI/Blocks/TextBlock.cs @@ -59,16 +59,15 @@ namespace GamecraftModdingAPI.Blocks { get { - return BlockEngine.GetBlockInfo(Id)?.textCurrent ?? null; + return BlockEngine.GetBlockInfo(Id).textCurrent; } set { - TextBlockDataStruct def = default; - ref TextBlockDataStruct tbds = ref BlockEngine.GetBlockInfo(Id, ref def); + ref TextBlockDataStruct tbds = ref BlockEngine.GetBlockInfo(Id); tbds.textCurrent.Set(value); tbds.textStored.Set(value); - BlockEngine.GetBlockInfo(Id)?.newTextBlockStringContent.Set(value); + BlockEngine.GetBlockInfo(Id).newTextBlockStringContent.Set(value); } } @@ -79,13 +78,13 @@ namespace GamecraftModdingAPI.Blocks { get { - return BlockEngine.GetBlockInfo(Id)?.textBlockID ?? null; + return BlockEngine.GetBlockInfo(Id).textBlockID; } set { - BlockEngine.GetBlockInfo(Id)?.textBlockID.Set(value); - BlockEngine.GetBlockInfo(Id)?.newTextBlockID.Set(value); + BlockEngine.GetBlockInfo(Id).textBlockID.Set(value); + BlockEngine.GetBlockInfo(Id).newTextBlockID.Set(value); } } } diff --git a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs index e37a5bb..9d180ea 100644 --- a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs +++ b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs @@ -8,12 +8,14 @@ using Svelto.ECS; using RobocraftX.Blocks; using RobocraftX.Common; using RobocraftX.SimulationModeState; +using RobocraftX.FrontEnd; +using Unity.Mathematics; using GamecraftModdingAPI.Commands; using GamecraftModdingAPI.Events; using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Blocks; -using RobocraftX.FrontEnd; +using GamecraftModdingAPI.Players; namespace GamecraftModdingAPI.Tests { @@ -177,8 +179,26 @@ namespace GamecraftModdingAPI.Tests .Action(() => uREPL.Log.Output(new Player(Players.PlayerType.Local).GetBlockLookedAt()+"")).Build(); CommandBuilder.Builder("Error", "Throw an error to make sure SimpleCustomCommandEngine's wrapper catches it.") - .Action(() => { throw new Exception("Error Command always throws an error"); }) - .Build(); + .Action(() => { throw new Exception("Error Command always throws an error"); }) + .Build(); + + CommandBuilder.Builder("ColorBlock", + "Change color of the block looked at if there's any.") + .Action(str => + { + if (!Enum.TryParse(str, out BlockColors color)) + { + Logging.CommandLog("Color " + str + " not found! Interpreting as 4 color values."); + var s = str.Split(' '); + new Player(PlayerType.Local).GetBlockLookedAt().CustomColor = new float4(float.Parse(s[0]), + float.Parse(s[1]), float.Parse(s[2]), float.Parse(s[3])); + return; + } + new Player(PlayerType.Local).GetBlockLookedAt().Color = + new BlockColor {Color = color}; + Logging.CommandLog("Colored block to " + color); + + }).Build(); GameClient.SetDebugInfo("lookedAt", LookedAt);