作者:barnseyminesu
项目:Small-ZC-Plugin
public function saveMacro($name, Macro $macro)
{
$tag = new tag\Compound();
$tag["author"] = new tag\String("author", $macro->getAuthor());
$tag["description"] = new tag\String("description", $macro->getDescription());
$tag["ops"] = new tag\Enum("ops");
foreach ($macro->getOperations() as $i => $log) {
$tag["ops"][$i] = $log->toTag();
}
$nbt = new NBT();
$nbt->setData($tag);
$file = $this->getFile($name);
$stream = fopen($file, "wb");
if (!is_resource($stream)) {
throw new \RuntimeException("Unable to open stream. Maybe the macro name is not a valid filename?");
}
$compression = $this->getMain()->getConfig()->getAll()["data providers"]["macro"]["mcr"]["compression"];
if ($compression === 0) {
$data = $nbt->write();
} else {
$data = $nbt->writeCompressed($compression);
}
fwrite($stream, chr($compression) . $data);
fclose($stream);
}
作者:TylerGame
项目:PocketMine-M
public function generateChunk($x, $z)
{
$nbt = new Compound("Level", []);
$nbt->xPos = new Int("xPos", $this->getX() * 32 + $x);
$nbt->zPos = new Int("zPos", $this->getZ() * 32 + $z);
$nbt->LastUpdate = new Long("LastUpdate", 0);
$nbt->LightPopulated = new Byte("LightPopulated", 0);
$nbt->TerrainPopulated = new Byte("TerrainPopulated", 0);
$nbt->V = new Byte("V", self::VERSION);
$nbt->InhabitedTime = new Long("InhabitedTime", 0);
$biomes = str_repeat(Binary::writeByte(-1), 256);
$nbt->Biomes = new ByteArray("Biomes", $biomes);
$nbt->BiomeColors = new IntArray("BiomeColors", array_fill(0, 156, Binary::readInt("…²J")));
$nbt->HeightMap = new IntArray("HeightMap", array_fill(0, 256, 127));
$nbt->Sections = new Enum("Sections", []);
$nbt->Sections->setTagType(NBT::TAG_Compound);
$nbt->Entities = new Enum("Entities", []);
$nbt->Entities->setTagType(NBT::TAG_Compound);
$nbt->TileEntities = new Enum("TileEntities", []);
$nbt->TileEntities->setTagType(NBT::TAG_Compound);
$nbt->TileTicks = new Enum("TileTicks", []);
$nbt->TileTicks->setTagType(NBT::TAG_Compound);
$writer = new NBT(NBT::BIG_ENDIAN);
$nbt->setName("Level");
$writer->setData(new Compound("", ["Level" => $nbt]));
$chunkData = $writer->writeCompressed(ZLIB_ENCODING_DEFLATE, RegionLoader::$COMPRESSION_LEVEL);
$this->saveChunk($x, $z, $chunkData);
}
作者:robske11
项目:ClearSk
public function requestChunkTask($x, $z)
{
$chunk = $this->getChunk($x, $z, false);
if (!$chunk instanceof Chunk) {
throw new ChunkException("Invalid Chunk sent");
}
$tiles = "";
if (count($chunk->getTiles()) > 0) {
$nbt = new NBT(NBT::LITTLE_ENDIAN);
$list = [];
foreach ($chunk->getTiles() as $tile) {
if ($tile instanceof Spawnable) {
$list[] = $tile->getSpawnCompound();
}
}
$nbt->setData($list);
$tiles = $nbt->write(true);
}
$extraData = new BinaryStream();
$extraData->putLInt(count($chunk->getBlockExtraDataArray()));
foreach ($chunk->getBlockExtraDataArray() as $key => $value) {
$extraData->putLInt($key);
$extraData->putLShort($value);
}
$ordered = $chunk->getBlockIdArray() . $chunk->getBlockDataArray() . $chunk->getBlockSkyLightArray() . $chunk->getBlockLightArray() . pack("C*", ...$chunk->getHeightMapArray()) . pack("N*", ...$chunk->getBiomeColorArray()) . $extraData->getBuffer() . $tiles;
$this->getLevel()->chunkRequestCallback($x, $z, $ordered, FullChunkDataPacket::ORDER_LAYERED);
return null;
}
作者:ClearSkyTea
项目:ClearSk
public function write(NBT $nbt, bool $network = false)
{
foreach ($this as $tag) {
if ($tag instanceof Tag and !$tag instanceof EndTag) {
$nbt->writeTag($tag, $network);
}
}
$nbt->writeTag(new EndTag(), $network);
}
作者:Edwardthedog
项目:Steadfast
public function write(NBT $nbt)
{
foreach ($this as $tag) {
if ($tag instanceof Tag and !$tag instanceof End) {
$nbt->writeTag($tag);
}
}
$nbt->writeTag(new End());
}
作者:organizatio
项目:SpawningPoo
/**
*
* @param string $name
*
* @return CompoundTag
*/
public function getOfflinePlayerData($name)
{
$name = strtolower($name);
$path = $this->datapath . "players/";
if (file_exists($path . "{$name}.dat")) {
try {
$nbt = new NBT(NBT::BIG_ENDIAN);
$nbt->readCompressed(file_get_contents($path . "{$name}.dat"));
return $nbt->getData();
} catch (\Throwable $e) {
// zlib decode error / corrupt data
rename($path . "{$name}.dat", $path . "{$name}.dat.bak");
}
}
$spawn = explode(':', $this->spawn);
$nbt = new CompoundTag("", [new LongTag("firstPlayed", floor(microtime(true) * 1000)), new LongTag("lastPlayed", floor(microtime(true) * 1000)), new ListTag("Pos", [new DoubleTag(0, $spawn[0]), new DoubleTag(1, $spawn[1]), new DoubleTag(2, $spawn[2])]), new StringTag("Level", $spawn[3]), new ListTag("Inventory", []), new CompoundTag("Achievements", []), new IntTag("playerGameType", $this->gamemode), new ListTag("Motion", [new DoubleTag(0, 0.0), new DoubleTag(1, 0.0), new DoubleTag(2, 0.0)]), new ListTag("Rotation", [new FloatTag(0, 0.0), new FloatTag(1, 0.0)]), new FloatTag("FallDistance", 0.0), new ShortTag("Fire", 0), new ShortTag("Air", 300), new ByteTag("OnGround", 1), new ByteTag("Invulnerable", 0), new StringTag("NameTag", $name)]);
$nbt->Pos->setTagType(NBT::TAG_Double);
$nbt->Inventory->setTagType(NBT::TAG_Compound);
$nbt->Motion->setTagType(NBT::TAG_Double);
$nbt->Rotation->setTagType(NBT::TAG_Float);
if (file_exists($path . "{$name}.yml")) {
// Importing old PocketMine-MP files
$data = new Config($path . "{$name}.yml", Config::YAML, []);
$nbt["playerGameType"] = (int) $data->get("gamemode");
$nbt["Level"] = $data->get("position")["level"];
$nbt["Pos"][0] = $data->get("position")["x"];
$nbt["Pos"][1] = $data->get("position")["y"];
$nbt["Pos"][2] = $data->get("position")["z"];
$nbt["SpawnLevel"] = $data->get("spawn")["level"];
$nbt["SpawnX"] = (int) $data->get("spawn")["x"];
$nbt["SpawnY"] = (int) $data->get("spawn")["y"];
$nbt["SpawnZ"] = (int) $data->get("spawn")["z"];
foreach ($data->get("inventory") as $slot => $item) {
if (count($item) === 3) {
$nbt->Inventory[$slot + 9] = new CompoundTag("", [new ShortTag("id", $item[0]), new ShortTag("Damage", $item[1]), new ByteTag("Count", $item[2]), new ByteTag("Slot", $slot + 9), new ByteTag("TrueSlot", $slot + 9)]);
}
}
foreach ($data->get("hotbar") as $slot => $itemSlot) {
if (isset($nbt->Inventory[$itemSlot + 9])) {
$item = $nbt->Inventory[$itemSlot + 9];
$nbt->Inventory[$slot] = new CompoundTag("", [new ShortTag("id", $item["id"]), new ShortTag("Damage", $item["Damage"]), new ByteTag("Count", $item["Count"]), new ByteTag("Slot", $slot), new ByteTag("TrueSlot", $item["TrueSlot"])]);
}
}
foreach ($data->get("armor") as $slot => $item) {
if (count($item) === 2) {
$nbt->Inventory[$slot + 100] = new CompoundTag("", [new ShortTag("id", $item[0]), new ShortTag("Damage", $item[1]), new ByteTag("Count", 1), new ByteTag("Slot", $slot + 100)]);
}
}
foreach ($data->get("achievements") as $achievement => $status) {
$nbt->Achievements[$achievement] = new ByteTag($achievement, $status == true ? 1 : 0);
}
unlink($path . "{$name}.yml");
}
return $nbt;
}
作者:Edwardthedog
项目:Steadfast
public function spawnTo(Player $player)
{
if ($this->closed) {
return \false;
}
$nbt = new NBT(NBT::LITTLE_ENDIAN);
$nbt->setData($this->getSpawnCompound());
$pk = new TileEntityDataPacket();
$pk->x = $this->x;
$pk->y = $this->y;
$pk->z = $this->z;
$pk->namedtag = $nbt->write();
$player->dataPacket($pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
return \true;
}
作者:TylerAndre
项目:Steadfast
public function spawnTo(Player $player)
{
if ($this->closed) {
return false;
}
$nbt = new NBT(NBT::LITTLE_ENDIAN);
$nbt->setData($this->getSpawnCompound());
$pk = new TileEntityDataPacket();
$pk->x = $this->x;
$pk->y = $this->y;
$pk->z = $this->z;
$pk->namedtag = $nbt->write();
$player->dataPacket($pk);
return true;
}
作者:robske11
项目:ClearSk
public function __construct(Human $player, $contents = null)
{
$this->hotbar = range(0, $this->getHotbarSize() - 1, 1);
parent::__construct($player, InventoryType::get(InventoryType::PLAYER));
if ($contents !== null) {
if ($contents instanceof ListTag) {
//Saved data to be loaded into the inventory
foreach ($contents as $item) {
if ($item["Slot"] >= 0 and $item["Slot"] < $this->getHotbarSize()) {
//Hotbar
if (isset($item["TrueSlot"])) {
//Valid slot was found, change the linkage to this slot
if (0 <= $item["TrueSlot"] and $item["TrueSlot"] < $this->getSize()) {
$this->hotbar[$item["Slot"]] = $item["TrueSlot"];
} elseif ($item["TrueSlot"] < 0) {
//Link to an empty slot (empty hand)
$this->hotbar[$item["Slot"]] = -1;
}
}
/* If TrueSlot is not set, leave the slot index as its default which was filled in above
* This only overwrites slot indexes for valid links */
} elseif ($item["Slot"] >= 100 and $item["Slot"] < 104) {
//Armor
$this->setItem($this->getSize() + $item["Slot"] - 100, NBT::getItemHelper($item), false);
} else {
$this->setItem($item["Slot"] - $this->getHotbarSize(), NBT::getItemHelper($item), false);
}
}
} else {
throw new \InvalidArgumentException("Expecting ListTag, received " . gettype($contents));
}
}
}
作者:iTXTec
项目:Genisy
public function __construct(Level $level, Chunk $chunk)
{
$this->levelId = $level->getId();
$this->chunk = $chunk->toFastBinary();
$this->chunkX = $chunk->getX();
$this->chunkZ = $chunk->getZ();
$tiles = "";
$nbt = new NBT(NBT::LITTLE_ENDIAN);
foreach ($chunk->getTiles() as $tile) {
if ($tile instanceof Spawnable) {
$nbt->setData($tile->getSpawnCompound());
$tiles .= $nbt->write();
}
}
$this->tiles = $tiles;
}
作者:ClearSkyTea
项目:ClearSk
public function setItem(Item $item, bool $setChanged = true)
{
$nbtItem = NBT::putItemHelper($item);
$nbtItem->setName("Item");
$this->namedtag->Item = $nbtItem;
if ($setChanged) {
$this->setChanged();
}
}
作者:ClearSkyTea
项目:ClearSk
/**
* Return the defined game rules as NBT.
*/
public function writeToNBT()
{
$compoundarray = [];
foreach ($this->theGameRules as $key => $value) {
$compoundarray[] = NBT::fromArrayGuesser($key, $value);
}
$nbttagcompound = new CompoundTag("GameRules", $compoundarray);
return $nbttagcompound;
}
作者:beito12
项目:PocketMine-MP-Plugin
public function setItem(Item $item)
{
$tag = NBT::putItemHelper($item);
$tag->setName("Item");
$this->namedtag->Item = $tag;
$this->spawnToAll();
if ($this->chunk) {
$this->chunk->setChanged();
$this->level->clearChunkCache($this->chunk->getX(), $this->chunk->getZ());
}
}
作者:rry
项目:PocketMine-M
public function __construct(Anvil $level, $levelId, $chunkX, $chunkZ)
{
$this->levelId = $levelId;
$this->chunkX = $chunkX;
$this->chunkZ = $chunkZ;
$chunk = $level->getChunk($chunkX, $chunkZ, false);
if (!$chunk instanceof Chunk) {
throw new ChunkException("Invalid Chunk sent");
}
$this->biomeIds = $chunk->getBiomeIdArray();
$this->biomeColors = $chunk->getBiomeColorArray();
$this->sections = $chunk->getSections();
$tiles = "";
$nbt = new NBT(NBT::LITTLE_ENDIAN);
foreach ($chunk->getTiles() as $tile) {
if ($tile instanceof Spawnable) {
$nbt->setData($tile->getSpawnCompound());
$tiles .= $nbt->write();
}
}
$this->tiles = $tiles;
$this->compressionLevel = Level::$COMPRESSION_LEVEL;
}
作者:DWW
项目:pocketmine-plugin
public function onRun($currentTicks)
{
$this->getOwner()->updateVars();
foreach ($this->getOwner()->getServer()->getLevels() as $lv) {
if (count($lv->getPlayers()) == 0) {
continue;
}
foreach ($lv->getTiles() as $tile) {
if (!$tile instanceof Sign) {
continue;
}
$sign = $tile->getText();
$text = $this->getOwner()->getLiveSign($sign);
if ($text == null) {
continue;
}
$pk = new TileEntityDataPacket();
$data = $tile->getSpawnCompound();
$data->Text1 = new String("Text1", $text[0]);
$data->Text2 = new String("Text2", $text[1]);
$data->Text3 = new String("Text3", $text[2]);
$data->Text4 = new String("Text4", $text[3]);
$nbt = new NBT(NBT::LITTLE_ENDIAN);
$nbt->setData($data);
$pk->x = $tile->getX();
$pk->y = $tile->getY();
$pk->z = $tile->getZ();
$pk->namedtag = $nbt->write();
foreach ($lv->getPlayers() as $pl) {
$pl->dataPacket($pk);
}
//foreach Players
}
//foreach Tiles
}
// foreach Levels
}
作者:Edwardthedog
项目:Steadfast
public function read(NBT $nbt)
{
$this->value = [];
$size = $nbt->endianness === 1 ? \PHP_INT_SIZE === 8 ? \unpack("N", $nbt->get(4))[1] << 32 >> 32 : \unpack("N", $nbt->get(4))[1] : (\PHP_INT_SIZE === 8 ? \unpack("V", $nbt->get(4))[1] << 32 >> 32 : \unpack("V", $nbt->get(4))[1]);
$value = \unpack($nbt->endianness === NBT::LITTLE_ENDIAN ? "V*" : "N*", $nbt->get($size * 4));
foreach ($value as $i => $v) {
$this->value[$i - 1] = $v;
}
}
作者:barnseyminesu
项目:Small-ZC-Plugin
public function saveArea(Area $area)
{
$nbt = new NBT(NBT::BIG_ENDIAN);
$data = new Compound();
$data->CaseName = new String("CaseName", $area->getName());
$data->SerializedShape = new String("SerializedShape", serialize($area->getShape()));
$data->UserFlags = new Int("UserFlags", $area->getUserFlags());
$data->NonUserFlags = new Int("NonUserFlags", $area->getNonUserFlags());
$data->Owner = new String("Owner", $area->getOwner());
$data->Users = new Enum("Users", array_map(function ($user) {
return new String("", $user);
}, $area->getUsers()));
$file = str_replace('$${areaname}', strtolower($area->getName()), $this->areaFile);
file_put_contents($file, $nbt->writeCompressed());
}
作者:iTXTec
项目:Genisy
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;
}
作者:Guillaume35
项目:Katan
/**
* This method should not be used by plugins, use the Inventory
*
* @param int $index
* @param Item $item
*
* @return bool
*/
public function setItem($index, Item $item)
{
$i = $this->getSlotIndex($index);
$d = NBT::putItemHelper($item, $index);
if ($item->getId() === Item::AIR or $item->getCount() <= 0) {
if ($i >= 0) {
unset($this->namedtag->Items[$i]);
}
} elseif ($i < 0) {
for ($i = 0; $i <= $this->getSize(); ++$i) {
if (!isset($this->namedtag->Items[$i])) {
break;
}
}
$this->namedtag->Items[$i] = $d;
} else {
$this->namedtag->Items[$i] = $d;
}
return true;
}
作者:beito12
项目:PocketMine-MP-Plugin
public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null)
{
if ($face > 1) {
$faces = [2 => 3, 3 => 2, 4 => 1, 5 => 4];
$itemTag = NBT::putItemHelper(Item::get(Item::AIR));
$itemTag->setName("Item");
$nbt = new CompoundTag("", [new StringTag("id", MainClass::TILE_ITEM_FRAME), new IntTag("x", $block->x), new IntTag("y", $block->y), new IntTag("z", $block->z), new CompoundTag("Item", $itemTag->getValue()), new FloatTag("ItemDropChance", 1.0), new ByteTag("ItemRotation", 0)]);
if ($item->hasCustomBlockData()) {
foreach ($item->getCustomBlockData() as $key => $value) {
//Reformat: support "Item" tag by give command
if ($key === "Item" and $value instanceof CompoundTag) {
$value = NBT::putItemHelper(NBT::getItemHelper($value));
$value->setName("Item");
}
$nbt->{$key} = $value;
}
}
//var_dump($nbt);//debug
Tile::createTile("ItemFrame", $this->getLevel()->getChunk($this->x >> 4, $this->z >> 4), $nbt);
$this->getLevel()->setBlock($block, Block::get(MainClass::BLOCK_ITEM_FRAME, $faces[$face]), true, true);
return true;
}
return false;
}