add compatibility for 1.22 bushes to be treated as flowers
This commit is contained in:
54
OrekiWoofsBees.Common/CompatibilityUtil.cs
Normal file
54
OrekiWoofsBees.Common/CompatibilityUtil.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -15,9 +15,23 @@ public static class PlantRecognitionUtilities
|
||||
|
||||
public static bool IsFlower(Block block, IBlockAccessor accessor, BlockPos pos)
|
||||
{
|
||||
if (block.FirstCodePart() == "flower")
|
||||
var code = block.FirstCodePart();
|
||||
if (code == "flower")
|
||||
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)
|
||||
{
|
||||
var plantContainer = block.GetBlockEntity<BlockEntityPlantContainer?>(pos);
|
||||
|
||||
Reference in New Issue
Block a user