如何获取结构方块内的数据

huangapple go评论76阅读模式
英文:

How to get the data inside a structure block

问题

我想从结构方块中获取方块数据,就像您执行以下命令一样:

/data get block 1 2 3
该命令返回1, 2, 3的方块数据如下:{metadata: "abc", mirror: "NONE", ignoreEntities: 1b, powered: 0b, seed: 0:, author: "myplayername", rotation:"NONE", posX: 0, mode: "DATA", posY: 1, sizeX: 0, posZ: 0, integrity: 1.0f, showair: 0b, x: 1, name:"", y: 2, z:3, id: "minecraft:structure_block", sizeY: 0, sizeZ: 0, showboundingbox: 1b}

具体而言,我需要“metadata”中的数据,即您在“Custom Data Tag Name”中找到的内容。我已经尝试过block.getBlockData()block.getMetadata("")block.getMetadata(null)以及block.getData()。我在Bukkit和Spigot文档以及帮助主题中搜索了一段时间,得到了刚刚尝试过的示例,而我在其他任何地方的搜索都没有相关的帮助。

提前感谢您的帮助。

英文:

I would like to get block data from structure blocks, in the same way if you would the following command:

/data get block 1 2 3
which gives 1, 2, 3 has the following block data: {metadata: "abc", mirror: "NONE", ignoreEntities: 1b, powered: 0b, seed: 0:, author: "myplayername", rotation:"NONE", posX: 0, mode: "DATA", posY: 1, sizeX: 0, posZ: 0, integrity: 1.0f, showair: 0b, x: 1, name:"", y: 2, z:3, id: "minecraft:structure_block", sizeY: 0, sizeZ: 0, showboundingbox: 1b}

Specifically I need the data in "metadata", ie the stuff you find if you just enter into "Custom Data Tag Name". I have tried block.getBlockData(), block.getMetadata(""), block.getMetadata(null) and block.getData(). Scouring the bukkit and spigot docs and help threads for a while yielded the examples I have just tried, and pretty much everywhere else I looked had no related help.

Thanks in advance.

答案1

得分: 1

翻译结果如下:

原来你需要将 Block 类型转换为 Structure 类型,以获取有关基本的 block.getType() 等无法提供的结构块更多信息。

要获取结构块元数据的原始字符串(与块元数据不同,例如此元数据在服务器重启后保留),请执行以下操作:

Structure structure_block = (Structure) block.getState();
String metadata_string = structure_block.getMetadata();

并且要检查是否完全正确:

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.BlockBreakEvent;

import org.bukkit.block.Structure;   //这部分是实现上述解决方案所需的

@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
    Player player = event.getPlayer();
    if(block.getType() == Material.STRUCTURE_BLOCK) {               //在尝试转换类型之前,请确保它是一个结构块
        event.setCancelled(true);                                   //这将使方块在您击打它时不会被破坏
        Structure structure_block = (Structure) block.getState();   //这部分是重要的转换操作
        player.sendMessage(structure_block.getMetadata());          //将原始字符串数据直接发送给玩家
    }
}

此代码包含了一个可复制粘贴的完整实现(希望如此),但与答案相关的重要部分已被注释说明。

英文:

It turns out you need to cast the Block type to the Structure type to get more info about the structure block that the basic block.getType() etc can't provide.

To get the raw string of structure block metadata (which is different to block metadata, for example this metadata remains after a server restart) do:

Structure structure_block = (Structure) block.getState();
String metadata_string = structure_block.getMetadata();

and to check that you've got it all right:

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.BlockBreakEvent;

import org.bukkit.block.Structure;   //this bit is required to implement the solution above

@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
    Player player = event.getPlayer();
    if(block.getType() == Material.STRUCTURE_BLOCK) {               //just make sure it is a structure block before trying to cast types
        event.setCancelled(true);                                   //this makes the block not break when you punch it
        Structure structure_block = (Structure) block.getState();   //this bit is the important casting operation
        player.sendMessage(structure_block.getMetadata());          //send the raw string data to the player directly
    }
}

This code contains a whole copy-pasteable implementation (hopefully), but the important bits relevant to the answer are commented as such.

答案2

得分: 0

看起来存储在元数据中的所有内容都是可以访问的,例如像这样:

block.getBiome();
block.getLocation();
block.getType();

您究竟想要对元数据做什么?

另外要注意,每次服务器重启后元数据都会被删除,因此它实际上不是可行的信息源,除非您确信它包含正确的数据,例如如果您从BlockPlaceEvent或类似事件中获取您的方块。

英文:

it seems all the stuff that is stored in the Metadata is just accesible, e.g. like so:

block.getBiome();
block.getLocation();
block.getType();

What exactly do you want to do with the metadata?

Also beware that the metadata is deleted every time the server restart's so it's not really a viable source of information , except you know for sure, that it contains proper data, e.g. if you get your block from a BlockPlaceEvent or something.

huangapple
  • 本文由 发表于 2020年10月9日 22:58:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64282530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定