Private
Public Access
1
0

add compatibility for 1.22 bushes to be treated as flowers

This commit is contained in:
2026-05-25 07:50:27 +02:00
parent b8e356cb23
commit d7ca3f8e99
3 changed files with 75 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
{ {
"total": 665981, "total": 673269,
"sessions": [ "sessions": [
{ {
"begin": "2026-01-09T17:26:02+01:00", "begin": "2026-01-09T17:26:02+01:00",
@@ -1530,6 +1530,11 @@
"begin": "2026-05-25T04:46:47+02:00", "begin": "2026-05-25T04:46:47+02:00",
"end": "2026-05-25T05:48:38+02:00", "end": "2026-05-25T05:48:38+02:00",
"duration": 3710 "duration": 3710
},
{
"begin": "2026-05-25T05:48:38+02:00",
"end": "2026-05-25T07:50:07+02:00",
"duration": 7288
} }
] ]
} }

View File

@@ -0,0 +1,54 @@
using System;
using System.Linq.Expressions;
using System.Reflection;
using Vintagestory.API.Common;
namespace OrekiWoofsBees.Common;
public static class CompatibilityUtil
{
private const string fruiting_bush_behavior_name = "BEBehaviorFruitingBush";
private const string b_state_field_name = "BState";
private const string growthstate_property_name = "Growthstate";
private const int flowering_growth_state_value = 2;
private static System.Func<BlockEntityBehavior, bool>? is_fruiting_bush_flowering_check;
public static bool IsFruitingBushFlowering(this BlockEntityBehavior behavior)
{
var behaviorType = behavior.GetType();
if (!IsFruitingBushBehaviorType(behaviorType))
return false;
var check = is_fruiting_bush_flowering_check ??= BuildIsFruitingBushFloweringCheck(behaviorType);
return check(behavior);
}
private static bool IsFruitingBushBehaviorType(Type behaviorType)
{
for (var type = behaviorType; type is not null; type = type.BaseType)
{
if (type.Name == fruiting_bush_behavior_name)
return true;
}
return false;
}
private static System.Func<BlockEntityBehavior, bool> BuildIsFruitingBushFloweringCheck(Type behaviorType)
{
var bStateField = behaviorType.GetField(b_state_field_name, BindingFlags.Instance | BindingFlags.Public)!;
var growthStateProperty = bStateField.FieldType.GetProperty(growthstate_property_name, BindingFlags.Instance | BindingFlags.Public)!;
var behaviorParameter = Expression.Parameter(typeof(BlockEntityBehavior), "behavior");
var typedBehavior = Expression.Convert(behaviorParameter, behaviorType);
var bState = Expression.Field(typedBehavior, bStateField);
var growthState = Expression.Property(bState, growthStateProperty);
var comparison = Expression.Equal(
Expression.Convert(growthState, typeof(int)),
Expression.Constant(flowering_growth_state_value));
return Expression.Lambda<System.Func<BlockEntityBehavior, bool>>(comparison, behaviorParameter).Compile();
}
}

View File

@@ -15,9 +15,23 @@ public static class PlantRecognitionUtilities
public static bool IsFlower(Block block, IBlockAccessor accessor, BlockPos pos) public static bool IsFlower(Block block, IBlockAccessor accessor, BlockPos pos)
{ {
if (block.FirstCodePart() == "flower") var code = block.FirstCodePart();
if (code == "flower")
return true; return true;
if (code == "fruitingbush")
{
var blockEntity = accessor.GetBlockEntity(pos);
if (blockEntity is null)
return false;
foreach (var behavior in blockEntity.Behaviors)
{
if (behavior.IsFruitingBushFlowering())
return true;
}
}
if (block is BlockPlantContainer) if (block is BlockPlantContainer)
{ {
var plantContainer = block.GetBlockEntity<BlockEntityPlantContainer?>(pos); var plantContainer = block.GetBlockEntity<BlockEntityPlantContainer?>(pos);