use of cn.nukkit.nbt.tag.CompoundTag in project Nukkit by Nukkit.
the class BlockChest method place.
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
BlockEntityChest chest = null;
int[] faces = { 2, 5, 3, 4 };
this.setDamage(faces[player != null ? player.getDirection().getHorizontalIndex() : 0]);
for (int side = 2; side <= 5; ++side) {
if ((this.getDamage() == 4 || this.getDamage() == 5) && (side == 4 || side == 5)) {
continue;
} else if ((this.getDamage() == 3 || this.getDamage() == 2) && (side == 2 || side == 3)) {
continue;
}
Block c = this.getSide(BlockFace.fromIndex(side));
if (c instanceof BlockChest && c.getDamage() == this.getDamage()) {
BlockEntity blockEntity = this.getLevel().getBlockEntity(c);
if (blockEntity instanceof BlockEntityChest && !((BlockEntityChest) blockEntity).isPaired()) {
chest = (BlockEntityChest) blockEntity;
break;
}
}
}
this.getLevel().setBlock(block, this, true, true);
CompoundTag nbt = new CompoundTag("").putList(new ListTag<>("Items")).putString("id", BlockEntity.CHEST).putInt("x", (int) this.x).putInt("y", (int) this.y).putInt("z", (int) this.z);
if (item.hasCustomName()) {
nbt.putString("CustomName", item.getCustomName());
}
if (item.hasCustomBlockData()) {
Map<String, Tag> customData = item.getCustomBlockData().getTags();
for (Map.Entry<String, Tag> tag : customData.entrySet()) {
nbt.put(tag.getKey(), tag.getValue());
}
}
BlockEntity blockEntity = new BlockEntityChest(this.getLevel().getChunk((int) (this.x) >> 4, (int) (this.z) >> 4), nbt);
if (chest != null) {
chest.pairWith(((BlockEntityChest) blockEntity));
((BlockEntityChest) blockEntity).pairWith(chest);
}
return true;
}
use of cn.nukkit.nbt.tag.CompoundTag in project Nukkit by Nukkit.
the class BlockEnchantingTable method onActivate.
@Override
public boolean onActivate(Item item, Player player) {
if (player != null) {
BlockEntity t = this.getLevel().getBlockEntity(this);
BlockEntityEnchantTable enchantTable;
if (t instanceof BlockEntityEnchantTable) {
enchantTable = (BlockEntityEnchantTable) t;
} else {
CompoundTag nbt = new CompoundTag().putList(new ListTag<>("Items")).putString("id", BlockEntity.ENCHANT_TABLE).putInt("x", (int) this.x).putInt("y", (int) this.y).putInt("z", (int) this.z);
enchantTable = new BlockEntityEnchantTable(this.getLevel().getChunk((int) (this.x) >> 4, (int) (this.z) >> 4), nbt);
}
if (enchantTable.namedTag.contains("Lock") && enchantTable.namedTag.get("Lock") instanceof StringTag) {
if (!enchantTable.namedTag.getString("Lock").equals(item.getCustomName())) {
return true;
}
}
player.addWindow(new EnchantInventory(this.getLocation()), Player.ENCHANT_WINDOW_ID);
}
return true;
}
use of cn.nukkit.nbt.tag.CompoundTag in project Nukkit by Nukkit.
the class Server method getOfflinePlayerData.
public CompoundTag getOfflinePlayerData(String name) {
name = name.toLowerCase();
String path = this.getDataPath() + "players/";
File file = new File(path + name + ".dat");
if (this.shouldSavePlayerData() && file.exists()) {
try {
return NBTIO.readCompressed(new FileInputStream(file));
} catch (Exception e) {
file.renameTo(new File(path + name + ".dat.bak"));
this.logger.notice(this.getLanguage().translateString("nukkit.data.playerCorrupted", name));
}
} else {
this.logger.notice(this.getLanguage().translateString("nukkit.data.playerNotFound", name));
}
Position spawn = this.getDefaultLevel().getSafeSpawn();
CompoundTag nbt = new CompoundTag().putLong("firstPlayed", System.currentTimeMillis() / 1000).putLong("lastPlayed", System.currentTimeMillis() / 1000).putList(new ListTag<DoubleTag>("Pos").add(new DoubleTag("0", spawn.x)).add(new DoubleTag("1", spawn.y)).add(new DoubleTag("2", spawn.z))).putString("Level", this.getDefaultLevel().getName()).putList(new ListTag<>("Inventory")).putCompound("Achievements", new CompoundTag()).putInt("playerGameType", this.getGamemode()).putList(new ListTag<DoubleTag>("Motion").add(new DoubleTag("0", 0)).add(new DoubleTag("1", 0)).add(new DoubleTag("2", 0))).putList(new ListTag<FloatTag>("Rotation").add(new FloatTag("0", 0)).add(new FloatTag("1", 0))).putFloat("FallDistance", 0).putShort("Fire", 0).putShort("Air", 300).putBoolean("OnGround", true).putBoolean("Invulnerable", false).putString("NameTag", name);
this.saveOfflinePlayerData(name, nbt);
return nbt;
}
use of cn.nukkit.nbt.tag.CompoundTag in project Nukkit by Nukkit.
the class EntityHuman method initEntity.
@Override
protected void initEntity() {
this.setDataFlag(DATA_PLAYER_FLAGS, DATA_PLAYER_FLAG_SLEEP, false);
this.setDataFlag(DATA_FLAGS, DATA_FLAG_GRAVITY);
this.setDataProperty(new IntPositionEntityData(DATA_PLAYER_BED_POSITION, 0, 0, 0), false);
if (!(this instanceof Player)) {
if (this.namedTag.contains("NameTag")) {
this.setNameTag(this.namedTag.getString("NameTag"));
}
if (this.namedTag.contains("Skin") && this.namedTag.get("Skin") instanceof CompoundTag) {
if (!this.namedTag.getCompound("Skin").contains("Transparent")) {
this.namedTag.getCompound("Skin").putBoolean("Transparent", false);
}
this.setSkin(new Skin(this.namedTag.getCompound("Skin").getByteArray("Data"), this.namedTag.getCompound("Skin").getString("ModelId")));
}
this.uuid = Utils.dataToUUID(String.valueOf(this.getId()).getBytes(StandardCharsets.UTF_8), this.getSkin().getData(), this.getNameTag().getBytes(StandardCharsets.UTF_8));
}
super.initEntity();
}
use of cn.nukkit.nbt.tag.CompoundTag in project Nukkit by Nukkit.
the class EntityHumanType method initEntity.
@Override
protected void initEntity() {
this.inventory = new PlayerInventory(this);
if (this.namedTag.contains("Inventory") && this.namedTag.get("Inventory") instanceof ListTag) {
ListTag<CompoundTag> inventoryList = this.namedTag.getList("Inventory", CompoundTag.class);
for (CompoundTag item : inventoryList.getAll()) {
int slot = item.getByte("Slot");
if (slot >= 0 && slot < 9) {
// hotbar
// Old hotbar saving stuff, remove it (useless now)
inventoryList.remove(item);
} else if (slot >= 100 && slot < 104) {
this.inventory.setItem(this.inventory.getSize() + slot - 100, NBTIO.getItemHelper(item));
} else {
this.inventory.setItem(slot - 9, NBTIO.getItemHelper(item));
}
}
}
this.enderChestInventory = new PlayerEnderChestInventory(this);
if (this.namedTag.contains("EnderItems") && this.namedTag.get("EnderItems") instanceof ListTag) {
ListTag<CompoundTag> inventoryList = this.namedTag.getList("EnderItems", CompoundTag.class);
for (CompoundTag item : inventoryList.getAll()) {
this.enderChestInventory.setItem(item.getByte("Slot"), NBTIO.getItemHelper(item));
}
}
super.initEntity();
}
Aggregations