Painter API

预计阅读时间: 7 分钟

Painter API 允许您在屏幕上绘制内容

Painter API 在1.21以上版本中已移除!

关于 (About)

Painter API 允许您在屏幕上绘制内容,无论是从服务器还是直接从客户端。这使您能够从服务器端创建小部件或从客户端在屏幕上或世界中创建效果。

目前它不支持任何输入,但将来将支持游戏内菜单甚至类似于Source引擎的GUI。

可绘制对象从NBT/Json对象创建,并且都有一个ID。如果未提供ID,将生成一个随机ID。对象的x和z是基于屏幕的绝对位置,但您可以在屏幕的一个角对齐元素。您可以在一个json对象中批量添加多个对象。所有属性都是可选的,但显然有些您几乎应该总是覆盖,例如矩形的大小和位置。

paint({...}) 基于更新插入原则——如果对象不存在,它将创建它(如果对象也包含有效的 type),否则更新现有对象:

  • event.player.paint({example: {type: 'rectangle', x: 10, y: 10, w: 20, h: 20}}) - 创建新矩形

  • event.player.paint({example: {x: 50}}) - 使用部分数据更新先前的矩形

您可以在同一个对象中批量更新/创建多个内容:

  • event.player.paint({a: {x: 10}, b: {x: 30}, c: {type: 'rectangle', x: 10}})

您可以使用 remove: true 移除对象,批量移除多个对象或移除所有对象:

  • event.player.paint({a: {remove: true}})

  • event.player.paint({a: {remove: true}, b: {remove: true}})

  • event.player.paint({'*': {remove: true}})

这些方法有命令替代方案:

  • /kubejs painter @p {example: {type: 'rectangle', x: 10, y: 10, w: 20, h: 20}}

如果对象是重复出现的,建议在登录时创建带有所有静态属性和 visible: false 的对象,然后稍后更新它以取消隐藏它。当玩家离开世界/服务器时,Painter对象将被清除,如果它是持久的,则必须每次登录时重新添加。

当前可用对象 (Currently available objects)

带下划线的对象不是您可以创建的对象

根 (Root)(所有对象可用)

  • Boolean visible

  • Float x

  • Float y

  • Float z

  • Float w

  • Float h

  • Enum alignX('left'、'center'、'right' 之一)

  • Enum alignY('top'、'center'、'bottom' 之一)

  • Enum draw('ingame'、'gui'、'always' 之一)

  • Float moveX

  • Float moveY

  • Float expandW

  • Float expandH

rectangle

  • Color color

  • String texture

  • Float u0

  • Float v0

  • Float u1

  • Float v1

gradient

  • Color color

  • Color colorT

  • Color colorB

  • Color colorL

  • Color colorR

  • Color colorTL

  • Color colorTR

  • Color colorBL

  • Color colorBR

  • String texture

  • Float u0

  • Float v0

  • Float u1

  • Float v1

text

  • Text text | Text[] textLines

  • Boolean shadow

  • Float scale

  • Color color

  • Boolean centered

  • Float lineSpacing

item

  • ItemStack item(支持 'itemid' 或原版 {id: 'item', Count: 4, tag: {...}} NBT 语法)

  • Boolean overlay

  • String customText

  • Float rotation

属性 (Properties)

  • Unit 是一个 Rhino Unit。它可以是数字、布尔值、颜色、方程式。每个 Float、Int、Boolean 和 Color 也是 Units,因此您可以在它们上使用方程式。

  • Int 是 int32 数字,任何整数值,例如 40

  • Float 是 float64 浮点数,例如 2.35

  • String 是字符串,例如 'example'。纹理通常需要资源位置 'namespace:path/to/texture.png'

  • Color 可以是 0xRRGGBB'#RRGGBB''#AARRGGBB''#58AD5B' 或聊天颜色,即 'red''dark_aqua' 等。RGBA color(Float, Float, Float, Float) 也受支持,其中 Float 是 0 到 1 之间的任何数字(支持 Units)。

  • Text 可以是字符串 'Example'Text.of('Red and italic string example').red().italic() 等格式化字符串。

可用的单位变量 (Available Unit Variables)

  • $screenW - 屏幕宽度

  • $screenH - 屏幕高度

  • $delta - 渲染增量

  • $mouseX - 鼠标X位置

  • $mouseY - 鼠标Y位置

可用的单位常量 (Available Unit Constants)

  • true - 布尔真值,等于 1

  • false - 布尔假值,等于 0

  • PI - 数字,等于 3.14159265358979323846

  • HALF_PI - 数字,等于 1.57079632679

  • TWO_PI - 数字,等于 6.28318530718

  • E - 数字,等于 2.7182818284590452354

示例 (Examples)

1.19.2+ 1.18.2及以下

PlayerEvents.loggedIn(event => {
  event.player.paint({
    example_rectangle: {
      type: 'rectangle',
      x: 10,
      y: 10,
      w: 50,
      h: 20,
      color: '#00FF00',
      draw: 'always'
    },
    last_message: {
      type: 'text',
      text: 'No last message',
      scale: 1.5,
      x: -4,
      y: -4,
      alignX: 'right',
      alignY: 'bottom',
      draw: 'always'
    }
  })
})

PlayerEvents.chat(event => {
  // 更新 example_rectangle x 值和 last_message 文本值为最后一条消息 + 来自事件的内容
  event.player.paint({example_rectangle: {x: '(sin((time() * 1.1)) * (($screenW - 32) / 2))', w: 32, h: 32, alignX: 'center', texture: 'kubejs:textures/item/diamond_ore.png'}})
  event.player.paint({last_message: {text: `Last message: ${event.message}`}})
  // 批量更新,这与上面2行代码相同,您可以使用任何您更喜欢的方式
  // event.player.paint({example_rectangle: {x: 120}, last_message: {text: `Last message: ${event.message}`}})
  event.player.paint({lava: {type: 'atlas_texture', texture: 'minecraft:block/lava_flow'}})
})
onEvent('player.logged_in', event => {
  event.player.paint({
    example_rectangle: {
      type: 'rectangle',
      x: 10,
      y: 10,
      w: 50,
      h: 20,
      color: '#00FF00',
      draw: 'always'
    },
    last_message: {
      type: 'text',
      text: 'No last message',
      scale: 1.5,
      x: -4,
      y: -4,
      alignX: 'right',
      alignY: 'bottom',
      draw: 'always'
    }
  })
})

onEvent('player.chat', event => {
	// 更新 example_rectangle x 值和 last_message 文本值为最后一条消息 + 来自事件的内容
	event.player.paint({example_rectangle: {x: '(sin((time() * 1.1)) * (($screenW - 32) / 2))', w: 32, h: 32, alignX: 'center', texture: 'kubejs:textures/item/diamond_ore.png'}})
	event.player.paint({last_message: {text: `Last message: ${event.message}`}})
	// 批量更新,这与上面2行代码相同,您可以使用任何您更喜欢的方式
	// event.player.paint({example_rectangle: {x: 120}, last_message: {text: `Last message: ${event.message}`}})
	event.player.paint({lava: {type: 'atlas_texture', texture: 'minecraft:block/lava_flow'}})
})