Search in sources :

Example 71 with CompoundTag

use of cn.nukkit.nbt.tag.CompoundTag in project Nukkit by Nukkit.

the class BanIpCommand method execute.

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    if (args.length == 0) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return false;
    }
    String value = args[0];
    String reason = "";
    for (int i = 1; i < args.length; i++) {
        reason += args[i] + " ";
    }
    if (reason.length() > 0) {
        reason = reason.substring(0, reason.length() - 1);
    }
    if (Pattern.matches("^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$", value)) {
        this.processIPBan(value, sender, reason);
        Command.broadcastCommandMessage(sender, new TranslationContainer("commands.banip.success", value));
    } else {
        Player player = sender.getServer().getPlayer(value);
        if (player != null) {
            this.processIPBan(player.getAddress(), sender, reason);
            Command.broadcastCommandMessage(sender, new TranslationContainer("commands.banip.success.players", new String[] { player.getAddress(), player.getName() }));
        } else {
            String name = value.toLowerCase();
            String path = sender.getServer().getDataPath() + "players/";
            File file = new File(path + name + ".dat");
            CompoundTag nbt = null;
            if (file.exists()) {
                try {
                    nbt = NBTIO.readCompressed(new FileInputStream(file));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (nbt != null && nbt.contains("lastIP") && Pattern.matches("^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$", (value = nbt.getString("lastIP")))) {
                this.processIPBan(value, sender, reason);
                Command.broadcastCommandMessage(sender, new TranslationContainer("commands.banip.success", value));
            } else {
                sender.sendMessage(new TranslationContainer("commands.banip.invalid"));
                return false;
            }
        }
    }
    return true;
}
Also used : Player(cn.nukkit.Player) TranslationContainer(cn.nukkit.lang.TranslationContainer) IOException(java.io.IOException) File(java.io.File) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) FileInputStream(java.io.FileInputStream)

Example 72 with CompoundTag

use of cn.nukkit.nbt.tag.CompoundTag in project Nukkit by Nukkit.

the class Anvil method generate.

public static void generate(String path, String name, long seed, Class<? extends Generator> generator, Map<String, String> options) throws IOException {
    if (!new File(path + "/region").exists()) {
        new File(path + "/region").mkdirs();
    }
    CompoundTag levelData = new CompoundTag("Data").putCompound("GameRules", new CompoundTag()).putLong("DayTime", 0).putInt("GameType", 0).putString("generatorName", Generator.getGeneratorName(generator)).putString("generatorOptions", options.containsKey("preset") ? options.get("preset") : "").putInt("generatorVersion", 1).putBoolean("hardcore", false).putBoolean("initialized", true).putLong("LastPlayed", System.currentTimeMillis() / 1000).putString("LevelName", name).putBoolean("raining", false).putInt("rainTime", 0).putLong("RandomSeed", seed).putInt("SpawnX", 128).putInt("SpawnY", 70).putInt("SpawnZ", 128).putBoolean("thundering", false).putInt("thunderTime", 0).putInt("version", 19133).putLong("Time", 0).putLong("SizeOnDisk", 0);
    NBTIO.writeGZIPCompressed(new CompoundTag().putCompound("Data", levelData), new FileOutputStream(path + "level.dat"), ByteOrder.BIG_ENDIAN);
}
Also used : FileOutputStream(java.io.FileOutputStream) File(java.io.File) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Example 73 with CompoundTag

use of cn.nukkit.nbt.tag.CompoundTag in project Nukkit by Nukkit.

the class Chunk method toBinary.

public byte[] toBinary(boolean saveExtra) {
    try {
        LevelProvider provider = this.getProvider();
        if (saveExtra && provider instanceof LevelDB) {
            List<CompoundTag> entities = new ArrayList<>();
            for (Entity entity : this.getEntities().values()) {
                if (!(entity instanceof Player) && !entity.closed) {
                    entity.saveNBT();
                    entities.add(entity.namedTag);
                }
            }
            EntitiesKey entitiesKey = EntitiesKey.create(this.getX(), this.getZ());
            if (!entities.isEmpty()) {
                ((LevelDB) provider).getDatabase().put(entitiesKey.toArray(), NBTIO.write(entities));
            } else {
                ((LevelDB) provider).getDatabase().delete(entitiesKey.toArray());
            }
            List<CompoundTag> tiles = new ArrayList<>();
            for (BlockEntity blockEntity : this.getBlockEntities().values()) {
                if (!blockEntity.closed) {
                    blockEntity.saveNBT();
                    entities.add(blockEntity.namedTag);
                }
            }
            TilesKey tilesKey = TilesKey.create(this.getX(), this.getZ());
            if (!tiles.isEmpty()) {
                ((LevelDB) provider).getDatabase().put(tilesKey.toArray(), NBTIO.write(tiles));
            } else {
                ((LevelDB) provider).getDatabase().delete(tilesKey.toArray());
            }
            ExtraDataKey extraDataKey = ExtraDataKey.create(this.getX(), this.getZ());
            if (!this.getBlockExtraDataArray().isEmpty()) {
                BinaryStream extraData = new BinaryStream();
                Map<Integer, Integer> extraDataArray = this.getBlockExtraDataArray();
                extraData.putInt(extraDataArray.size());
                for (Integer key : extraDataArray.keySet()) {
                    extraData.putInt(key);
                    extraData.putShort(extraDataArray.get(key));
                }
                ((LevelDB) provider).getDatabase().put(extraDataKey.toArray(), extraData.getBuffer());
            } else {
                ((LevelDB) provider).getDatabase().delete(extraDataKey.toArray());
            }
        }
        byte[] heightMap = this.getHeightMapArray();
        byte[] biomeColors = new byte[this.getBiomeColorArray().length * 4];
        for (int i = 0; i < this.getBiomeColorArray().length; i++) {
            byte[] bytes = Binary.writeInt(this.getBiomeColorArray()[i]);
            biomeColors[i * 4] = bytes[0];
            biomeColors[i * 4 + 1] = bytes[1];
            biomeColors[i * 4 + 2] = bytes[2];
            biomeColors[i * 4 + 3] = bytes[3];
        }
        return Binary.appendBytes(Binary.writeLInt(this.getX()), Binary.writeLInt(this.getZ()), this.getBlockIdArray(), this.getBlockDataArray(), this.getBlockSkyLightArray(), this.getBlockLightArray(), heightMap, biomeColors, new byte[] { (byte) (((this.isLightPopulated ? 0x04 : 0) | (this.isPopulated() ? 0x02 : 0) | (this.isGenerated() ? 0x01 : 0)) & 0xff) });
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ExtraDataKey(cn.nukkit.level.format.leveldb.key.ExtraDataKey) Entity(cn.nukkit.entity.Entity) BlockEntity(cn.nukkit.blockentity.BlockEntity) Player(cn.nukkit.Player) LevelProvider(cn.nukkit.level.format.LevelProvider) BinaryStream(cn.nukkit.utils.BinaryStream) EntitiesKey(cn.nukkit.level.format.leveldb.key.EntitiesKey) IOException(java.io.IOException) TilesKey(cn.nukkit.level.format.leveldb.key.TilesKey) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 74 with CompoundTag

use of cn.nukkit.nbt.tag.CompoundTag in project Nukkit by Nukkit.

the class Anvil method requestChunkTask.

@Override
public AsyncTask requestChunkTask(int x, int z) throws ChunkException {
    Chunk chunk = (Chunk) this.getChunk(x, z, false);
    if (chunk == null) {
        throw new ChunkException("Invalid Chunk Set");
    }
    long timestamp = chunk.getChanges();
    byte[] blockEntities = new byte[0];
    if (!chunk.getBlockEntities().isEmpty()) {
        List<CompoundTag> tagList = new ArrayList<>();
        for (BlockEntity blockEntity : chunk.getBlockEntities().values()) {
            if (blockEntity instanceof BlockEntitySpawnable) {
                tagList.add(((BlockEntitySpawnable) blockEntity).getSpawnCompound());
            }
        }
        try {
            blockEntities = NBTIO.write(tagList, ByteOrder.LITTLE_ENDIAN, true);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    Map<Integer, Integer> extra = chunk.getBlockExtraDataArray();
    BinaryStream extraData;
    if (!extra.isEmpty()) {
        extraData = new BinaryStream();
        extraData.putVarInt(extra.size());
        for (Map.Entry<Integer, Integer> entry : extra.entrySet()) {
            extraData.putVarInt(entry.getKey());
            extraData.putLShort(entry.getValue());
        }
    } else {
        extraData = null;
    }
    BinaryStream stream = ThreadCache.binaryStream.get().reset();
    int count = 0;
    cn.nukkit.level.format.ChunkSection[] sections = chunk.getSections();
    for (int i = sections.length - 1; i >= 0; i--) {
        if (!sections[i].isEmpty()) {
            count = i + 1;
            break;
        }
    }
    stream.putByte((byte) count);
    for (int i = 0; i < count; i++) {
        stream.putByte((byte) 0);
        stream.put(sections[i].getBytes());
    }
    for (byte height : chunk.getHeightMapArray()) {
        stream.putByte(height);
    }
    stream.put(PAD_256);
    stream.put(chunk.getBiomeIdArray());
    stream.putByte((byte) 0);
    if (extraData != null) {
        stream.put(extraData.getBuffer());
    } else {
        stream.putVarInt(0);
    }
    stream.put(blockEntities);
    this.getLevel().chunkRequestCallback(timestamp, x, z, stream.getBuffer());
    return null;
}
Also used : BlockEntitySpawnable(cn.nukkit.blockentity.BlockEntitySpawnable) BinaryStream(cn.nukkit.utils.BinaryStream) IOException(java.io.IOException) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) FullChunk(cn.nukkit.level.format.FullChunk) ChunkException(cn.nukkit.utils.ChunkException) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Aggregations

CompoundTag (cn.nukkit.nbt.tag.CompoundTag)74 ListTag (cn.nukkit.nbt.tag.ListTag)28 BlockEntity (cn.nukkit.blockentity.BlockEntity)13 DoubleTag (cn.nukkit.nbt.tag.DoubleTag)13 FloatTag (cn.nukkit.nbt.tag.FloatTag)13 Tag (cn.nukkit.nbt.tag.Tag)13 StringTag (cn.nukkit.nbt.tag.StringTag)12 Map (java.util.Map)8 Entity (cn.nukkit.entity.Entity)7 IOException (java.io.IOException)7 Player (cn.nukkit.Player)5 BinaryStream (cn.nukkit.utils.BinaryStream)5 ArrayList (java.util.ArrayList)5 BlockRail (cn.nukkit.block.BlockRail)4 FullChunk (cn.nukkit.level.format.FullChunk)4 BaseFullChunk (cn.nukkit.level.format.generic.BaseFullChunk)4 Rail (cn.nukkit.utils.Rail)4 File (java.io.File)4 BlockEntityChest (cn.nukkit.blockentity.BlockEntityChest)3 BlockEntitySpawnable (cn.nukkit.blockentity.BlockEntitySpawnable)3