From 4f0645492c59fd70c7d2550fba63c99c53f2301a Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 18 May 2021 00:44:09 +0200 Subject: [PATCH] Fix block color and group --- TechbloxModdingAPI/Block.cs | 8 +++---- TechbloxModdingAPI/Blocks/BlockColor.cs | 2 +- TechbloxModdingAPI/Blocks/PlacementEngine.cs | 24 ++++++++++++++++++- .../Tests/TechbloxModdingAPIPluginTest.cs | 2 ++ 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/TechbloxModdingAPI/Block.cs b/TechbloxModdingAPI/Block.cs index 8947b41..12abed7 100644 --- a/TechbloxModdingAPI/Block.cs +++ b/TechbloxModdingAPI/Block.cs @@ -195,7 +195,7 @@ namespace TechbloxModdingAPI if (value.y < 4e-5) value.y = uscale; if (value.z < 4e-5) value.z = uscale; BlockEngine.GetBlockInfo(this).scale = value; - BlockEngine.GetBlockInfo(this).gridScale = value - (int3) value + 1; + //BlockEngine.GetBlockInfo(this).gridScale = value - (int3) value + 1; if (!Exists) return; //UpdateCollision needs the block to exist ScalingEngine.UpdateCollision(Id); BlockEngine.UpdateDisplayedBlock(Id); @@ -225,7 +225,7 @@ namespace TechbloxModdingAPI get => BlockEngine.GetBlockInfo(this).scale.x < 0; set { - var st = BlockEngine.GetBlockInfo(this); + ref var st = ref BlockEngine.GetBlockInfo(this); st.scale.x = math.abs(st.scale.x) * (value ? -1 : 1); BlockEngine.UpdatePrefab(this, (ushort) Type, (byte) Material, value); } @@ -256,7 +256,7 @@ namespace TechbloxModdingAPI set { //TODO: Check if setting to 255 works - var color = BlockEngine.GetBlockInfo(this); + ref var color = ref BlockEngine.GetBlockInfo(this); color.indexInPalette = value.Index; color.hasNetworkChange = true; color.paletteColour = BlockEngine.ConvertBlockColor(color.indexInPalette); @@ -311,7 +311,7 @@ namespace TechbloxModdingAPI private BlockGroup blockGroup; /// /// Returns the block group this block is a part of. Block groups can also be placed using blueprints. - /// Returns null if not part of a group.
+ /// Returns null if not part of a group, although all blocks should have their own by default.
/// Setting the group after the block has been initialized will not update everything properly, /// so you can only set this property on blocks newly placed by your code.
/// To set it for existing blocks, you can use the Copy() method and set the property on the resulting block diff --git a/TechbloxModdingAPI/Blocks/BlockColor.cs b/TechbloxModdingAPI/Blocks/BlockColor.cs index 33363a2..42e1f9b 100644 --- a/TechbloxModdingAPI/Blocks/BlockColor.cs +++ b/TechbloxModdingAPI/Blocks/BlockColor.cs @@ -26,7 +26,7 @@ namespace TechbloxModdingAPI.Blocks { if (darkness > 9) throw new ArgumentOutOfRangeException(nameof(darkness), "Darkness must be 0-9 where 0 is default."); - if (color > BlockColors.Red) //Last valid color + if (color > BlockColors.Red && color != BlockColors.Default) //Last valid color throw new ArgumentOutOfRangeException(nameof(color), "Invalid color!"); Index = (byte) (darkness * 10 + (byte) color); } diff --git a/TechbloxModdingAPI/Blocks/PlacementEngine.cs b/TechbloxModdingAPI/Blocks/PlacementEngine.cs index b62d2d8..e1bbb09 100644 --- a/TechbloxModdingAPI/Blocks/PlacementEngine.cs +++ b/TechbloxModdingAPI/Blocks/PlacementEngine.cs @@ -2,6 +2,7 @@ using System; using System.Reflection; using DataLoader; +using Gamecraft.Blocks.BlockGroups; using Gamecraft.Wires; using HarmonyLib; using RobocraftX.Blocks; @@ -40,6 +41,7 @@ namespace TechbloxModdingAPI.Blocks public EntitiesDB entitiesDB { get; set; } private static BlockEntityFactory _blockEntityFactory; //Injected from PlaceSingleBlockEngine + private static IEntityFactory _entityFactory; public EntityInitializer PlaceBlock(BlockIDs block, float3 position, Player player, bool autoWire) { //It appears that only the non-uniform scale has any visible effect, but if that's not given here it will be set to the uniform one @@ -78,6 +80,25 @@ namespace TechbloxModdingAPI.Blocks placedBy = playerId, triggerAutoWiring = autoWire && structInitializer.Has() }); + + + /*structInitializer.Init(new OverrideStaticComponent() + { //TODO + staticIfUnconnected = true + });*/ + + int nextFilterId = BlockGroupUtility.NextFilterId; + structInitializer.Init(new BlockGroupEntityComponent + { + currentBlockGroup = nextFilterId + }); + _entityFactory.BuildEntity((uint) nextFilterId, + BlockGroupExclusiveGroups.BlockGroupEntityGroup) + .Init(new BlockGroupTransformEntityComponent + { + blockGroupGridRotation = quaternion.identity, + blockGroupGridPosition = position + }); foreach (var group in CharacterExclusiveGroups.AllCharacters) { @@ -98,9 +119,10 @@ namespace TechbloxModdingAPI.Blocks [HarmonyPatch] public class FactoryObtainerPatch { - static void Postfix(BlockEntityFactory blockEntityFactory) + static void Postfix(BlockEntityFactory blockEntityFactory, IEntityFactory entityFactory) { _blockEntityFactory = blockEntityFactory; + _entityFactory = entityFactory; Logging.MetaDebugLog("Block entity factory injected."); } diff --git a/TechbloxModdingAPI/Tests/TechbloxModdingAPIPluginTest.cs b/TechbloxModdingAPI/Tests/TechbloxModdingAPIPluginTest.cs index d9cfedb..a4665f2 100644 --- a/TechbloxModdingAPI/Tests/TechbloxModdingAPIPluginTest.cs +++ b/TechbloxModdingAPI/Tests/TechbloxModdingAPIPluginTest.cs @@ -181,6 +181,8 @@ namespace TechbloxModdingAPI.Tests .Action((float x, float y, float z) => { var block = Block.PlaceNew(BlockIDs.Cube, new float3(x, y, z)); + block.Scale *= 2; + new Block(BlockIDs.Cube, new float3(x + 1, y, z)).Scale *= 2; Logging.CommandLog("Block placed with type: " + block.Type); }) .Build();