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

完善含水方块

This commit is contained in:
XiaoMoMi
2025-06-28 19:47:37 +08:00
parent 83bd27e774
commit cb48025194
6 changed files with 109 additions and 8 deletions

View File

@@ -220,6 +220,7 @@ public final class BlockGenerator {
public static final int directionIndex = VersionHelper.isOrAbove1_21_2() ? 4 : 1;
public static final int posIndex = VersionHelper.isOrAbove1_21_2() ? 3 : 4;
@SuppressWarnings("deprecation")
@RuntimeType
public Object intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) {
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();

View File

@@ -17,12 +17,14 @@ import net.momirealms.craftengine.bukkit.item.BukkitItemManager;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MBlockStateProperties;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MLootContextParams;
import net.momirealms.craftengine.bukkit.plugin.user.BukkitServerPlayer;
import net.momirealms.craftengine.bukkit.world.BukkitWorld;
import net.momirealms.craftengine.core.block.BlockSettings;
import net.momirealms.craftengine.core.block.DelegatingBlockState;
import net.momirealms.craftengine.core.block.ImmutableBlockState;
import net.momirealms.craftengine.core.block.properties.Property;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.plugin.context.ContextHolder;
import net.momirealms.craftengine.core.plugin.context.parameter.DirectContextParameters;
@@ -54,7 +56,13 @@ public final class BlockStateGenerator {
.method(ElementMatchers.named("setBlockState"))
.intercept(FieldAccessor.ofField("immutableBlockState"))
.method(ElementMatchers.is(CoreReflections.method$BlockStateBase$getDrops))
.intercept(MethodDelegation.to(GetDropsInterceptor.INSTANCE));
.intercept(MethodDelegation.to(GetDropsInterceptor.INSTANCE))
.method(ElementMatchers.is(CoreReflections.method$StateHolder$hasProperty))
.intercept(MethodDelegation.to(HasPropertyInterceptor.INSTANCE))
.method(ElementMatchers.is(CoreReflections.method$StateHolder$getValue))
.intercept(MethodDelegation.to(GetPropertyValueInterceptor.INSTANCE))
.method(ElementMatchers.is(CoreReflections.method$StateHolder$setValue))
.intercept(MethodDelegation.to(SetPropertyValueInterceptor.INSTANCE));
Class<?> clazz$CraftEngineBlock = stateBuilder.make().load(BlockStateGenerator.class.getClassLoader()).getLoaded();
constructor$CraftEngineBlockState = MethodHandles.publicLookup().in(clazz$CraftEngineBlock)
.findConstructor(clazz$CraftEngineBlock, MethodType.methodType(void.class, CoreReflections.clazz$Block, Reference2ObjectArrayMap.class, MapCodec.class))
@@ -114,6 +122,56 @@ public final class BlockStateGenerator {
}
}
public static class HasPropertyInterceptor {
public static final HasPropertyInterceptor INSTANCE = new HasPropertyInterceptor();
@SuppressWarnings("unchecked")
@RuntimeType
public boolean intercept(@This Object thisObj, @AllArguments Object[] args) {
Object property = args[0];
if (property != MBlockStateProperties.WATERLOGGED) return false;
DelegatingBlockState customState = (DelegatingBlockState) thisObj;
ImmutableBlockState state = customState.blockState();
if (state == null) return false;
Property<Boolean> waterloggedProperty = (Property<Boolean>) state.owner().value().getProperty("waterlogged");
return waterloggedProperty != null;
}
}
public static class GetPropertyValueInterceptor {
public static final GetPropertyValueInterceptor INSTANCE = new GetPropertyValueInterceptor();
@SuppressWarnings("unchecked")
@RuntimeType
public Object intercept(@This Object thisObj, @AllArguments Object[] args) {
Object property = args[0];
if (property != MBlockStateProperties.WATERLOGGED) return null;
DelegatingBlockState customState = (DelegatingBlockState) thisObj;
ImmutableBlockState state = customState.blockState();
if (state == null) return null;
Property<Boolean> waterloggedProperty = (Property<Boolean>) state.owner().value().getProperty("waterlogged");
if (waterloggedProperty == null) return null;
return state.get(waterloggedProperty);
}
}
public static class SetPropertyValueInterceptor {
public static final SetPropertyValueInterceptor INSTANCE = new SetPropertyValueInterceptor();
@SuppressWarnings("unchecked")
@RuntimeType
public Object intercept(@This Object thisObj, @AllArguments Object[] args) {
Object property = args[0];
if (property != MBlockStateProperties.WATERLOGGED) return thisObj;
DelegatingBlockState customState = (DelegatingBlockState) thisObj;
ImmutableBlockState state = customState.blockState();
if (state == null) return thisObj;
Property<Boolean> waterloggedProperty = (Property<Boolean>) state.owner().value().getProperty("waterlogged");
if (waterloggedProperty == null) return thisObj;
return state.with(waterloggedProperty, (boolean) args[1]).customBlockState().handle();
}
}
public static class CreateStateInterceptor {
public static final CreateStateInterceptor INSTANCE = new CreateStateInterceptor();

View File

@@ -1715,6 +1715,10 @@ public final class CoreReflections {
)
);
public static final Field field$Property$name = requireNonNull(
ReflectionUtils.getDeclaredField(clazz$Property, String.class, 0)
);
public static final Field field$LeavesBlock$DISTANCE = requireNonNull(
ReflectionUtils.getDeclaredField(clazz$LeavesBlock, clazz$IntegerProperty, 0)
);
@@ -1727,6 +1731,9 @@ public final class CoreReflections {
ReflectionUtils.getMethod(clazz$StateHolder, Object.class, new String[] {"getValue", "c"}, clazz$Property)
);
public static final Method method$StateHolder$setValue = requireNonNull(
ReflectionUtils.getMethod(clazz$StateHolder, Object.class, new String[] {"setValue", "b"}, clazz$Property, Comparable.class)
);
public static final Method method$Block$updateFromNeighbourShapes = requireNonNull(
ReflectionUtils.getStaticMethod(clazz$Block, clazz$BlockState, clazz$BlockState, clazz$LevelAccessor, clazz$BlockPos)
@@ -3542,4 +3549,11 @@ public final class CoreReflections {
"world.level.storage.loot.parameters.LootContextParams"
)
);
public static final Class<?> clazz$BlockStateProperties = requireNonNull(
BukkitReflectionUtils.findReobfOrMojmapClass(
"world.level.block.state.properties.BlockProperties",
"world.level.block.state.properties.BlockStateProperties"
)
);
}

View File

@@ -0,0 +1,29 @@
package net.momirealms.craftengine.bukkit.plugin.reflection.minecraft;
import net.momirealms.craftengine.bukkit.plugin.reflection.ReflectionInitException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import static java.util.Objects.requireNonNull;
public final class MBlockStateProperties {
public static final Object WATERLOGGED;
static {
try {
Object waterlogged = null;
for (Field field : CoreReflections.clazz$BlockStateProperties.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) {
Object instance = field.get(null);
if (CoreReflections.clazz$Property.isInstance(instance) && CoreReflections.field$Property$name.get(instance).equals("waterlogged")) {
waterlogged = instance;
}
}
}
WATERLOGGED = requireNonNull(waterlogged);
} catch (ReflectiveOperationException e) {
throw new ReflectionInitException("Failed to init MBlockStateProperties", e);
}
}
}