首先,随便注册一个kjs方块实体:.
StartupEvents.registry("block",event=>{
event.create("test_block","basic")
.blockEntity(blockEntityInfo=>{
blockEntityInfo.enableSync()//重要,设置可以同步data
}).lightLevel(15)
})
注册渲染:startup.
global.inited = false
ClientEvents.init(event => {
global.inited=true
event.registerBlockEntityRenderer("kubejs:test_block", (context) => RenderJSBlockEntityRenderer
.create(context)
.setCustomRender((renderer, context) => {
var poseStack = context.poseStack
var light = LevelRenderer.getLightColor(context.blockEntityJS.level, context.blockEntityJS.blockPos.above())//获取亮度
var item = Item.empty
var data = context.blockEntityJS.data//读取kjs方块实体的data("同步的信息")
if (data.contains("item")) {
item = Item.of(data.getString("item"))
}
if (item != Item.empty) {
poseStack.pushPose()
poseStack.translate(0.5, 1.5, 0.5)//平移(0.5, 1.5, 0.5)
poseStack.mulPose(Vec3f.XN.rotationDegrees(90))//x轴旋转90度
renderer.itemRenderer.renderStatic(item, "ground", light, context.packedOverlay, context.poseStack, context.bufferSource, Client.player.getId())
poseStack.popPose()
}
poseStack.pushPose()
poseStack.translate(0.25, 1, 0.25)
poseStack.scale(0.5, 0.5, 0.5)
renderer.blockRenderDispatcher.renderSingleBlock(Blocks.FIRE.defaultBlockState(), context.poseStack, context.bufferSource, light, context.packedOverlay)
poseStack.popPose()
})
)
})
右键事件: server.
const $BlockEntityJS = Java.loadClass("dev.latvian.mods.kubejs.block.entity.BlockEntityJS")
BlockEvents.rightClicked(event=>{
var block =event.getBlock()
var entity = block.entity
if (entity instanceof $BlockEntityJS) {
entity.data.putString("item",event.player.getMainHandItem().getId())
console.log(entity.data);
entity.sync() //手动同步
}
})
什么?希望在游戏内调整? (client脚本)
if (global.inited) {
var Renderer = Client.getBlockEntityRenderDispatcher().rjs$getRendererByType("kubejs:test_block")//获取render
if (Renderer instanceof RenderJSBlockEntityRenderer) {
//重新设置render
Renderer.setCustomRender((renderer, context) => {
var poseStack = context.poseStack
var light = LevelRenderer.getLightColor(context.blockEntityJS.level, context.blockEntityJS.blockPos.above())
var item = Item.empty
var data = context.blockEntityJS.data
if (data.contains("item")) {
item = Item.of(data.getString("item"))
}
if (item != Item.empty) {
poseStack.pushPose()
poseStack.translate(0.5, 1.5, 0.5)
poseStack.mulPose(Vec3f.XN.rotationDegrees(90))
renderer.itemRenderer.renderStatic(item, "ground", light, context.packedOverlay, context.poseStack, context.bufferSource, Client.player.getId())
poseStack.popPose()
}
poseStack.pushPose()
poseStack.translate(0.25, 1, 0.25)
poseStack.scale(0.5, 0.5, 0.5)
renderer.blockRenderDispatcher.renderSingleBlock(Blocks.FIRE.defaultBlockState(), context.poseStack, context.bufferSource, light, context.packedOverlay)
poseStack.popPose()
})
}
}
重新设置完毕reload client即可.
效果:
右键时火上会显示你手上的物品.