如果一个玩家看着一个蜂箱,我想得到那个蜂箱的
BeehiveBlockEntity
对象并在F3调试屏幕上显示它的属性。具体来说,当前F3菜单中没有显示的蜂巢中的蜜蜂数量。
我在
DebugScreenOverlay
类的 getCustomSystemInformation
方法中使用 mixin。
试图获得附加到
BeehiveBlockEntity
的BeehiveBlock
对象并没有提供所有需要的信息。
在返回的对象中,
beeCount
始终为0,isFull
和isEmpty
分别始终为false和true。即使有蜜蜂居住在蜂箱中也是如此。奇怪的是,唯一正确更新对象的属性是 isSedated
属性,当周围有火和没有火时。
运行
/data get block <x> <y> <z>
表示蜂巢被占用,同时显示蜂巢和里面的蜜蜂的NBT。 这里是蜂箱和蜜蜂的正确NBT
但是,代码不会记录任何蜜蜂的 NBT。当查看已满的蜂箱时,mod 记录:
[16:16:04] [Render thread/INFO] [minecraft/DebugScreenOverlay]:{Bees[],id:"minecraft:beehive",x:50,y:65,z:41}
注意
Bees
是空的。
模组使用Forge 1.19.2-43.2.0。下面是重要的代码。
有没有其他方法可以仅根据位置获取块实体数据?我是否缺少此信息的客户端-服务器交互?
@Mixin(DebugScreenOverlay.class)
public class DebugScreenOverlayMixin extends GuiComponent {
// Directly reference a slf4j logger
private static final Logger LOGGER = LogUtils.getLogger();
/**
* Add more information into the right-hand menu of the F3 debug screen
*
* @param cir The mixin object to return the strings we get
* @param i Runtime max memory
* @param j Runtime total memory
* @param k Runtime free memory
* @param l Runtime used memory
* @param list The list of strings to render
* @param entity The entity under the crosshair, null if none
*/
@Inject(remap = false, method = "getSystemInformation", at = @At(value = "RETURN", ordinal = 1), cancellable = true, locals = LocalCapture.CAPTURE_FAILHARD)
public void getCustomSystemInformation(CallbackInfoReturnable<List<String>> cir, long i, long j, long k, long l, List<String> list, Entity entity) {
Minecraft mc = Minecraft.getInstance();
HitResult hitResult = mc.hitResult;
LocalPlayer player = mc.player;
DebugScreenOverlay currObj = (DebugScreenOverlay)(Object)this;
if (hitResult != null && hitResult.getType() != HitResult.Type.MISS) {
if (currObj.block.getType() == HitResult.Type.BLOCK) {
BlockPos blockPos = ((BlockHitResult) hitResult).getBlockPos();
BlockState blockState = mc.level.getBlockState(blockPos);
BlockEntity blockEntity = player.level.getBlockEntity(blockPos);
if (blockState.hasBlockEntity()) {
if (blockEntity instanceof BeehiveBlockEntity beehiveBlockEntity) {
LOGGER.info(beehiveBlockEntity.serializeNBT().toString());
int pos = list.indexOf(ChatFormatting.UNDERLINE + "Targeted Block: " + blockPos.getX() + ", " + blockPos.getY() + ", " + blockPos.getZ());
list.add(pos + 2, "bee count: " + beehiveBlockEntity.getOccupantCount());
list.add(pos + 3, "is full: " + beehiveBlockEntity.isFull());
list.add(pos + 4, "is empty: " + beehiveBlockEntity.isEmpty());
list.add(pos + 5, "is fire nearby: " + beehiveBlockEntity.isFireNearby());
list.add(pos + 6, "is sedated: " + beehiveBlockEntity.isSedated());
}
}
}
}
}
}