本文整理汇总了PHP中pocketmine\nbt\NBT::parseJSON方法的典型用法代码示例。如果您正苦于以下问题:PHP NBT::parseJSON方法的具体用法?PHP NBT::parseJSON怎么用?PHP NBT::parseJSON使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pocketmine\nbt\NBT
的用法示例。
在下文中一共展示了NBT::parseJSON方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: execute
public function execute(CommandSender $sender, $currentAlias, array $args)
{
if (!$this->testPermission($sender)) {
return true;
}
if (count($args) < 2) {
$sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage]));
return true;
}
$player = $sender->getServer()->getPlayer($args[0]);
$item = Item::fromString($args[1]);
if (!isset($args[2])) {
$item->setCount($item->getMaxStackSize());
} else {
$item->setCount((int) $args[2]);
}
if (isset($args[3])) {
$tags = $exception = null;
$data = implode(" ", array_slice($args, 3));
try {
$tags = NBT::parseJSON($data);
} catch (\Throwable $ex) {
$exception = $ex;
}
if (!$tags instanceof CompoundTag or $exception !== null) {
$sender->sendMessage(new TranslationContainer("commands.give.tagError", [$exception !== null ? $exception->getMessage() : "Invalid tag conversion"]));
return true;
}
$item->setNamedTag($tags);
}
if ($player instanceof Player) {
if ($item->getId() === 0) {
$sender->sendMessage(new TranslationContainer(TextFormat::RED . "%commands.give.item.notFound", [$args[1]]));
return true;
}
//TODO: overflow
$player->getInventory()->addItem(clone $item);
} else {
$sender->sendMessage(new TranslationContainer(TextFormat::RED . "%commands.generic.player.notFound"));
return true;
}
Command::broadcastCommandMessage($sender, new TranslationContainer("%commands.give.success", [$item->getName() . " (" . $item->getId() . ":" . $item->getDamage() . ")", (string) $item->getCount(), $player->getName()]));
return true;
}
示例2: execute
public function execute(CommandSender $sender, $currentAlias, array $args)
{
if (!$this->testPermission($sender)) {
return true;
}
if (count($args) != 1 and count($args) != 4 and count($args) != 5) {
$sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage]));
return true;
}
$x = 0;
$y = 0;
$z = 0;
if (count($args) == 4 or count($args) == 5) {
//position is set
//TODO:simpilify them to one piece of code
//Code for setting $x
if (is_numeric($args[1])) {
//x is given directly
$x = $args[1];
} elseif (strcmp($args[1], "~") >= 0) {
//x is given with a "~"
$offset_x = trim($args[1], "~");
if ($sender instanceof Player) {
//using in-game
$x = is_numeric($offset_x) ? $sender->x + $offset_x : $sender->x;
} else {
//using in console
$sender->sendMessage(TextFormat::RED . "You must specify a position where the entity is spawned to when using in console");
return false;
}
} else {
//other circumstances
$sender->sendMessage(TextFormat::RED . "Argument error");
return false;
}
//Code for setting $y
if (is_numeric($args[2])) {
//y is given directly
$y = $args[2];
} elseif (strcmp($args[2], "~") >= 0) {
//y is given with a "~"
$offset_y = trim($args[2], "~");
if ($sender instanceof Player) {
//using in-game
$y = is_numeric($offset_y) ? $sender->y + $offset_y : $sender->y;
$y = min(128, max(0, $y));
} else {
//using in console
$sender->sendMessage(TextFormat::RED . "You must specify a position where the entity is spawned to when using in console");
return false;
}
} else {
//other circumstances
$sender->sendMessage(TextFormat::RED . "Argument error");
return false;
}
//Code for setting $z
if (is_numeric($args[3])) {
//z is given directly
$z = $args[3];
} elseif (strcmp($args[3], "~") >= 0) {
//z is given with a "~"
$offset_z = trim($args[3], "~");
if ($sender instanceof Player) {
//using in-game
$z = is_numeric($offset_z) ? $sender->z + $offset_z : $sender->z;
} else {
//using in console
$sender->sendMessage(TextFormat::RED . "You must specify a position where the entity is spawned to when using in console");
return false;
}
} else {
//other circumstances
$sender->sendMessage(TextFormat::RED . "Argument error");
return false;
}
}
//finish setting the location
if (count($args) == 1) {
if ($sender instanceof Player) {
$x = $sender->x;
$y = $sender->y;
$z = $sender->z;
} else {
$sender->sendMessage(TextFormat::RED . "You must specify a position where the entity is spawned to when using in console");
return false;
}
}
//finish setting the location
$entity = null;
$type = $args[0];
$level = $sender instanceof Player ? $sender->getLevel() : $sender->getServer()->getDefaultLevel();
$chunk = $level->getChunk(round($x) >> 4, round($z) >> 4);
$nbt = new CompoundTag("", ["Pos" => new ListTag("Pos", [new DoubleTag("", $x), new DoubleTag("", $y), new DoubleTag("", $z)]), "Motion" => new ListTag("Motion", [new DoubleTag("", 0), new DoubleTag("", 0), new DoubleTag("", 0)]), "Rotation" => new ListTag("Rotation", [new FloatTag("", lcg_value() * 360), new FloatTag("", 0)])]);
if (count($args) == 5 and $args[4][0] == "{") {
//Tags are found
$nbtExtra = NBT::parseJSON($args[4]);
$nbt = NBT::combineCompoundTags($nbt, $nbtExtra, true);
}
$entity = Entity::createEntity($type, $chunk, $nbt);
//.........这里部分代码省略.........