9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-19 15:09:15 +00:00

添加扩散方块行为

This commit is contained in:
jhqwqmc
2025-11-09 01:03:09 +08:00
parent 939db3b3b3
commit 3083325b7f
6 changed files with 117 additions and 2 deletions

View File

@@ -44,6 +44,7 @@ public class BukkitBlockBehaviors extends BlockBehaviors {
public static final Key CHIME_BLOCK = Key.from("craftengine:chime_block");
public static final Key BUDDING_BLOCK = Key.from("craftengine:budding_block");
public static final Key SEAT_BLOCK = Key.from("craftengine:seat_block");
public static final Key SPREADING_BLOCK = Key.from("craftengine:spreading_block");
public static void init() {
register(EMPTY, (block, args) -> EmptyBlockBehavior.INSTANCE);
@@ -86,5 +87,6 @@ public class BukkitBlockBehaviors extends BlockBehaviors {
register(CHIME_BLOCK, ChimeBlockBehavior.FACTORY);
register(BUDDING_BLOCK, BuddingBlockBehavior.FACTORY);
register(SEAT_BLOCK, SeatBlockBehavior.FACTORY);
register(SPREADING_BLOCK, SpreadingBlockBehavior.FACTORY);
}
}

View File

@@ -0,0 +1,109 @@
package net.momirealms.craftengine.bukkit.block.behavior;
import net.momirealms.craftengine.bukkit.block.BukkitBlockManager;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MBlocks;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MTagKeys;
import net.momirealms.craftengine.bukkit.util.BlockStateUtils;
import net.momirealms.craftengine.bukkit.util.LocationUtils;
import net.momirealms.craftengine.core.block.*;
import net.momirealms.craftengine.core.block.behavior.BlockBehaviorFactory;
import net.momirealms.craftengine.core.block.properties.BooleanProperty;
import net.momirealms.craftengine.core.item.context.BlockPlaceContext;
import net.momirealms.craftengine.core.util.LazyReference;
import net.momirealms.craftengine.core.util.RandomUtils;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.util.VersionHelper;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Callable;
public class SpreadingBlockBehavior extends BukkitBlockBehavior {
public static final Factory FACTORY = new Factory();
private final int spreadLight;
private final LazyReference<Object> spreadBlock;
public SpreadingBlockBehavior(CustomBlock customBlock, int spreadLight, String spreadBlock) {
super(customBlock);
this.spreadLight = spreadLight;
this.spreadBlock = LazyReference.lazyReference(() -> Objects.requireNonNull(BukkitBlockManager.instance().createBlockState(spreadBlock)).literalObject());
}
@Override
public Object updateShape(Object thisBlock, Object[] args, Callable<Object> superMethod) throws Exception {
if (args[updateShape$direction] != CoreReflections.instance$Direction$UP) return superMethod.call();
return BlockStateUtils.toBlockStateWrapper(args[0]).withProperty("snowy", String.valueOf(isSnowySetting(args[updateShape$neighborState]))).literalObject();
}
@Override
public ImmutableBlockState updateStateForPlacement(BlockPlaceContext context, ImmutableBlockState state) {
BooleanProperty snowy = (BooleanProperty) this.block().getProperty("snowy");
if (snowy == null) return state;
Object blockState = FastNMS.INSTANCE.method$BlockGetter$getBlockState(context.getLevel().serverWorld(), LocationUtils.toBlockPos(context.getClickedPos().above()));
return state.with(snowy, isSnowySetting(blockState));
}
@Override
public void randomTick(Object thisBlock, Object[] args, Callable<Object> superMethod) throws Exception {
Object state = args[0];
Object level = args[1];
Object pos = args[2];
if (!canBeGrass(state, level, pos)) {
FastNMS.INSTANCE.method$LevelWriter$setBlock(level, pos, this.spreadBlock.get(), 3);
return;
}
if (FastNMS.INSTANCE.method$LevelReader$getMaxLocalRawBrightness(level, FastNMS.INSTANCE.method$BlockPos$relative(pos, CoreReflections.instance$Direction$UP)) < this.spreadLight) return;
ImmutableBlockState blockState = this.block().defaultState();
BooleanProperty snowy = (BooleanProperty) this.block().getProperty("snowy");
for (int i = 0; i < 4; i++) {
Object blockPos = FastNMS.INSTANCE.method$BlockPos$offset(pos, RandomUtils.generateRandomInt(-1, 2), RandomUtils.generateRandomInt(-3, 2), RandomUtils.generateRandomInt(-1, 2));
if (FastNMS.INSTANCE.method$BlockStateBase$isBlock(FastNMS.INSTANCE.method$BlockGetter$getBlockState(level, blockPos), FastNMS.INSTANCE.method$BlockState$getBlock(this.spreadBlock.get())) && canPropagate(state, level, blockPos)) {
if (snowy != null) blockState = blockState.with(snowy, FastNMS.INSTANCE.method$BlockStateBase$isBlock(FastNMS.INSTANCE.method$BlockGetter$getBlockState(level, FastNMS.INSTANCE.method$BlockPos$relative(pos, CoreReflections.instance$Direction$UP)), MBlocks.SNOW));
FastNMS.INSTANCE.method$LevelWriter$setBlock(level, blockPos, blockState.customBlockState().literalObject(), 3);
}
}
}
private static boolean canBeGrass(Object state, Object level, Object pos) {
Object blockPos = FastNMS.INSTANCE.method$BlockPos$relative(pos, CoreReflections.instance$Direction$UP);
Object blockState = FastNMS.INSTANCE.method$BlockGetter$getBlockState(level, blockPos);
if (FastNMS.INSTANCE.method$BlockStateBase$isBlock(blockState, MBlocks.SNOW) && ((Integer) FastNMS.INSTANCE.method$StateHolder$getValue(blockState, CoreReflections.instance$SnowLayerBlock$LAYERS)) == 1) return true;
else if (FastNMS.INSTANCE.field$FluidState$amount(FastNMS.INSTANCE.field$BlockBehaviour$BlockStateBase$fluidState(blockState)) == 8) return false;
else {
return FastNMS.INSTANCE.method$LightEngine$getLightBlockInto(
VersionHelper.isOrAbove1_21_2() ? null : level,
state,
VersionHelper.isOrAbove1_21_2() ? null : pos,
blockState,
VersionHelper.isOrAbove1_21_2() ? null : blockPos,
CoreReflections.instance$Direction$UP,
FastNMS.INSTANCE.method$BlockBehaviour$BlockStateBase$getLightBlock(
blockState,
VersionHelper.isOrAbove1_21_2() ? null : level,
VersionHelper.isOrAbove1_21_2() ? null : pos
)
) < 15;
}
}
private static boolean isSnowySetting(Object state) {
return FastNMS.INSTANCE.method$BlockStateBase$is(state, MTagKeys.Block$SNOW);
}
private static boolean canPropagate(Object state, Object level, Object pos) {
Object blockPos = FastNMS.INSTANCE.method$BlockPos$relative(pos, CoreReflections.instance$Direction$UP);
return canBeGrass(state, level, pos) && !FastNMS.INSTANCE.method$FluidState$is(FastNMS.INSTANCE.method$BlockGetter$getFluidState(level, blockPos), MTagKeys.Fluid$WATER);
}
public static class Factory implements BlockBehaviorFactory {
@Override
public BlockBehavior create(CustomBlock block, Map<String, Object> arguments) {
int spreadLight = ResourceConfigUtils.getAsInt(arguments.getOrDefault("spread-light", 9), "spread-light");
String spreadBlock = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.getOrDefault("spread-block", "minecraft:dirt"), "warning.config.block.behavior.spreading.missing_spread_block");
return new SpreadingBlockBehavior(block, spreadLight, spreadBlock);
}
}
}

View File

@@ -7,12 +7,14 @@ import java.util.Objects;
public final class MTagKeys {
private MTagKeys() {}
public static final Object Fluid$WATER = create(MRegistries.FLUID, "water");
public static final Object Item$WOOL = create(MRegistries.ITEM, "wool");
public static final Object Block$WALLS = create(MRegistries.BLOCK, "walls");
public static final Object Block$SHULKER_BOXES = create(MRegistries.BLOCK, "shulker_boxes");
public static final Object Block$FENCES = create(MRegistries.BLOCK, "fences");
public static final Object Block$WOODEN_FENCES = create(MRegistries.BLOCK, "wooden_fences");
public static final Object Block$DIRT = create(MRegistries.BLOCK, "dirt");
public static final Object Block$SNOW = create(MRegistries.BLOCK, "snow");
private static Object create(Object registry, String location) {
Object resourceLocation = FastNMS.INSTANCE.method$ResourceLocation$fromNamespaceAndPath("minecraft", location);

View File

@@ -353,6 +353,7 @@ warning.config.block.behavior.attached_stem.missing_facing: "<yellow>Issue found
warning.config.block.behavior.attached_stem.missing_fruit: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'fruit' argument for 'attached_stem_block' behavior.</yellow>"
warning.config.block.behavior.attached_stem.missing_stem: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'stem' argument for 'attached_stem_block' behavior.</yellow>"
warning.config.block.behavior.chime.missing_sounds_projectile_hit: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'sounds.projectile-hit' argument for 'chime_block' behavior.</yellow>"
warning.config.block.behavior.spreading.missing_spread_block: "<yellow>Issue found in file <arg:0> - The block '<arg:1>' is missing the required 'spread-block' argument for 'spreading_block' behavior.</yellow>"
warning.config.model.generation.missing_parent: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is missing the required 'parent' argument in 'generation' section.</yellow>"
warning.config.model.generation.conflict: "<yellow>Issue found in file <arg:0> - Failed to generate model for '<arg:1>' as two or more configurations attempt to generate different json models with the same path: '<arg:2>'.</yellow>"
warning.config.model.generation.invalid_display_position: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is using an invalid display position '<arg:2>' in 'generation.display' section. Allowed display positions: [<arg:3>]</yellow>"

View File

@@ -353,6 +353,7 @@ warning.config.block.behavior.attached_stem.missing_facing: "<yellow>在文件 <
warning.config.block.behavior.attached_stem.missing_fruit: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'attached_stem_block' 行为缺少必需的 'fruit' 选项</yellow>"
warning.config.block.behavior.attached_stem.missing_stem: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'attached_stem_block' 行为缺少必需的 'stem' 选项</yellow>"
warning.config.block.behavior.chime.missing_sounds_projectile_hit: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'chime_block' 行为缺少必需的 'sounds.projectile-hit' 选项</yellow>"
warning.config.block.behavior.spreading.missing_spread_block: "<yellow>在文件 <arg:0> 发现问题 - 方块 '<arg:1>' 的 'spreading_block' 行为缺少必需的 'spread-block' 选项</yellow>"
warning.config.model.generation.missing_parent: "<yellow>在文件 <arg:0> 发现问题 - 配置项 '<arg:1>' 的 'generation' 段落缺少必需的 'parent' 参数</yellow>"
warning.config.model.generation.conflict: "<yellow>在文件 <arg:0> 发现问题 - 无法为 '<arg:1>' 生成模型 存在多个配置尝试使用相同路径 '<arg:2>' 生成不同的 JSON 模型</yellow>"
warning.config.model.generation.invalid_display_position: "<yellow>在文件 <arg:0> 发现问题 - 配置项 '<arg:1>' 在 'generation.display' 区域使用了无效的 display 位置类型 '<arg:2>'. 可用展示类型: [<arg:3>]</yellow>"

View File

@@ -4,7 +4,7 @@ org.gradle.jvmargs=-Xmx1G
# Rule: [major update].[feature update].[bug fix]
project_version=0.0.65.6
config_version=53
lang_version=37
lang_version=38
project_group=net.momirealms
latest_supported_version=1.21.10
@@ -49,7 +49,7 @@ byte_buddy_version=1.17.8
ahocorasick_version=0.6.3
snake_yaml_version=2.5
anti_grief_version=1.0.4
nms_helper_version=1.0.127
nms_helper_version=1.0.128
evalex_version=3.5.0
reactive_streams_version=1.0.4
amazon_awssdk_version=2.34.5