mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-25 18:09:27 +00:00
feat(core): 添加监听onRemove内部事件
This commit is contained in:
@@ -225,6 +225,13 @@ public class UnsafeCompositeBlockBehavior extends BukkitBlockBehavior {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRemove(Object thisBlock, Object[] args, Callable<Object> superMethod) throws Exception {
|
||||
for (AbstractBlockBehavior behavior : this.behaviors) {
|
||||
behavior.onRemove(thisBlock, args, superMethod);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSignal(Object thisBlock, Object[] args, Callable<Object> superMethod) {
|
||||
for (AbstractBlockBehavior behavior : this.behaviors) {
|
||||
|
||||
@@ -188,6 +188,10 @@ public final class BlockGenerator {
|
||||
builder.method(ElementMatchers.is(CoreReflections.method$BlockBehaviour$affectNeighborsAfterRemoval))
|
||||
.intercept(MethodDelegation.to(AffectNeighborsAfterRemovalInterceptor.INSTANCE));
|
||||
}
|
||||
if (CoreReflections.method$BlockBehaviour$onRemove != null) {
|
||||
builder.method(ElementMatchers.is(CoreReflections.method$BlockBehaviour$onRemove))
|
||||
.intercept(MethodDelegation.to(OnRemoveInterceptor.INSTANCE));
|
||||
}
|
||||
|
||||
Class<?> clazz$CraftEngineBlock = builder.make().load(BlockGenerator.class.getClassLoader()).getLoaded();
|
||||
constructor$CraftEngineBlock = MethodHandles.publicLookup().in(clazz$CraftEngineBlock)
|
||||
@@ -618,6 +622,20 @@ public final class BlockGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
public static class OnRemoveInterceptor {
|
||||
public static final OnRemoveInterceptor INSTANCE = new OnRemoveInterceptor();
|
||||
|
||||
@RuntimeType
|
||||
public void intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) {
|
||||
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
|
||||
try {
|
||||
holder.value().onRemove(thisObj, args, superMethod);
|
||||
} catch (Exception e) {
|
||||
CraftEngine.instance().logger().severe("Failed to run onRemove", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class EntityInsideInterceptor {
|
||||
public static final EntityInsideInterceptor INSTANCE = new EntityInsideInterceptor();
|
||||
|
||||
|
||||
@@ -3822,4 +3822,12 @@ public final class CoreReflections {
|
||||
clazz$BlockBehaviour, void.class, clazz$BlockState, clazz$ServerLevel, clazz$BlockPos, clazz$ItemStack, boolean.class
|
||||
)
|
||||
);
|
||||
|
||||
// 1.20~1.21.4
|
||||
public static final Method method$BlockBehaviour$onRemove = MiscUtils.requireNonNullIf(
|
||||
ReflectionUtils.getDeclaredMethod(
|
||||
clazz$BlockBehaviour, void.class, clazz$BlockState, clazz$Level, clazz$BlockPos, clazz$BlockState, boolean.class
|
||||
),
|
||||
!VersionHelper.isOrAbove1_21_5()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -123,6 +123,9 @@ public abstract class BlockBehavior {
|
||||
public void affectNeighborsAfterRemoval(Object thisBlock, Object[] args, Callable<Object> superMethod) throws Exception {
|
||||
}
|
||||
|
||||
public void onRemove(Object thisBlock, Object[] args, Callable<Object> superMethod) throws Exception {
|
||||
}
|
||||
|
||||
// BlockState blockState, BlockGetter blockAccess, BlockPos pos, Direction side
|
||||
public int getSignal(Object thisBlock, Object[] args, Callable<Object> superMethod) {
|
||||
return 0;
|
||||
@@ -145,7 +148,6 @@ public abstract class BlockBehavior {
|
||||
|
||||
// BlockState state, ServerLevel level, BlockPos pos, ItemStack stack, boolean dropExperience
|
||||
public void spawnAfterBreak(Object thisBlock, Object[] args, Callable<Object> superMethod) throws Exception {
|
||||
superMethod.call();
|
||||
}
|
||||
|
||||
public ImmutableBlockState updateStateForPlacement(BlockPlaceContext context, ImmutableBlockState state) {
|
||||
|
||||
Reference in New Issue
Block a user