Search in sources :

Example 36 with GlowBlock

use of net.glowstone.block.GlowBlock in project Glowstone by GlowstoneMC.

the class AnvilChunkIoService method read.

/**
     * Reads a chunk from its region file.
     *
     * @param chunk The GlowChunk to read into.
     * @return Whether the
     * @throws IOException if an I/O error occurs.
     */
@Override
public boolean read(GlowChunk chunk) throws IOException {
    int x = chunk.getX(), z = chunk.getZ();
    RegionFile region = cache.getRegionFile(x, z);
    int regionX = x & REGION_SIZE - 1;
    int regionZ = z & REGION_SIZE - 1;
    if (!region.hasChunk(regionX, regionZ)) {
        return false;
    }
    DataInputStream in = region.getChunkDataInputStream(regionX, regionZ);
    CompoundTag levelTag;
    try (NBTInputStream nbt = new NBTInputStream(in, false)) {
        CompoundTag root = nbt.readCompound();
        levelTag = root.getCompound("Level");
    }
    // read the vertical sections
    List<CompoundTag> sectionList = levelTag.getCompoundList("Sections");
    ChunkSection[] sections = new ChunkSection[GlowChunk.SEC_COUNT];
    for (CompoundTag sectionTag : sectionList) {
        int y = sectionTag.getByte("Y");
        if (sections[y] != null) {
            GlowServer.logger.log(Level.WARNING, "Multiple chunk sections at y " + y + " in " + chunk + "!");
            continue;
        }
        if (y < 0 || y > GlowChunk.SEC_COUNT) {
            GlowServer.logger.log(Level.WARNING, "Out of bounds chunk section at y " + y + " in " + chunk + "!");
            continue;
        }
        sections[y] = ChunkSection.fromNBT(sectionTag);
    }
    // initialize the chunk
    chunk.initializeSections(sections);
    chunk.setPopulated(levelTag.getBool("TerrainPopulated"));
    // read biomes
    if (levelTag.isByteArray("Biomes")) {
        chunk.setBiomes(levelTag.getByteArray("Biomes"));
    }
    // read height map
    if (levelTag.isIntArray("HeightMap")) {
        chunk.setHeightMap(levelTag.getIntArray("HeightMap"));
    } else {
        chunk.automaticHeightMap();
    }
    // read entities
    if (levelTag.isList("Entities", TagType.COMPOUND)) {
        for (CompoundTag entityTag : levelTag.getCompoundList("Entities")) {
            try {
                // note that creating the entity is sufficient to add it to the world
                EntityStorage.loadEntity(chunk.getWorld(), entityTag);
            } catch (Exception e) {
                String id = entityTag.isString("id") ? entityTag.getString("id") : "<missing>";
                if (e.getMessage() != null && e.getMessage().startsWith("Unknown entity type to load:")) {
                    GlowServer.logger.warning("Unknown entity in " + chunk + ": " + id);
                } else {
                    GlowServer.logger.log(Level.WARNING, "Error loading entity in " + chunk + ": " + id, e);
                }
            }
        }
    }
    // read block entities
    List<CompoundTag> storedBlockEntities = levelTag.getCompoundList("TileEntities");
    BlockEntity blockEntity;
    for (CompoundTag blockEntityTag : storedBlockEntities) {
        int tx = blockEntityTag.getInt("x");
        int ty = blockEntityTag.getInt("y");
        int tz = blockEntityTag.getInt("z");
        blockEntity = chunk.createEntity(tx & 0xf, ty, tz & 0xf, chunk.getType(tx & 0xf, tz & 0xf, ty));
        if (blockEntity != null) {
            try {
                blockEntity.loadNbt(blockEntityTag);
            } catch (Exception ex) {
                String id = blockEntityTag.isString("id") ? blockEntityTag.getString("id") : "<missing>";
                GlowServer.logger.log(Level.SEVERE, "Error loading block entity at " + blockEntity.getBlock() + ": " + id, ex);
            }
        } else {
            String id = blockEntityTag.isString("id") ? blockEntityTag.getString("id") : "<missing>";
            GlowServer.logger.warning("Unknown block entity at " + chunk.getWorld().getName() + "," + tx + "," + ty + "," + tz + ": " + id);
        }
    }
    if (levelTag.isList("TileTicks", TagType.COMPOUND)) {
        List<CompoundTag> tileTicks = levelTag.getCompoundList("TileTicks");
        for (CompoundTag tileTick : tileTicks) {
            int tileX = tileTick.getInt("x");
            int tileY = tileTick.getInt("y");
            int tileZ = tileTick.getInt("z");
            String id = tileTick.getString("i");
            if (id.startsWith("minecraft:")) {
                id = id.replace("minecraft:", "");
                if (id.startsWith("flowing_")) {
                    id = id.replace("flowing_", "");
                } else if (id.equals("water") || id.equals("lava")) {
                    id = "stationary_" + id;
                }
            }
            Material material = Material.getMaterial(id.toUpperCase());
            GlowBlock block = chunk.getBlock(tileX, tileY, tileZ);
            if (material != block.getType()) {
                continue;
            }
            // TODO tick delay: tileTick.getInt("t");
            // TODO ordering: tileTick.getInt("p");
            BlockType type = ItemTable.instance().getBlock(material);
            if (type == null) {
                continue;
            }
            block.getWorld().requestPulse(block);
        }
    }
    return true;
}
Also used : Material(org.bukkit.Material) DataInputStream(java.io.DataInputStream) IOException(java.io.IOException) GlowBlock(net.glowstone.block.GlowBlock) BlockType(net.glowstone.block.blocktype.BlockType) NBTInputStream(net.glowstone.util.nbt.NBTInputStream) ChunkSection(net.glowstone.chunk.ChunkSection) CompoundTag(net.glowstone.util.nbt.CompoundTag) BlockEntity(net.glowstone.block.entity.BlockEntity)

Example 37 with GlowBlock

use of net.glowstone.block.GlowBlock in project Dragonet-Legacy by DragonetMC.

the class RemoveBlockPacketTranslator method handleSpecific.

@Override
public Message[] handleSpecific(RemoveBlockPacket packet) {
    if (!(this.getSession().getPlayer() instanceof Player)) {
        return null;
    }
    if (getSession().getPlayer().getGameMode().equals(GameMode.CREATIVE)) {
        final GlowPlayer player = getSession().getPlayer();
        GlowWorld world = player.getWorld();
        GlowBlock block = world.getBlockAt(packet.x, packet.y, packet.z);
        PlayerInteractEvent interactEvent = EventFactory.onPlayerInteract(player, Action.LEFT_CLICK_BLOCK, block, BlockFace.UP);
        if (interactEvent.isCancelled()) {
            player.sendBlockChange(block.getLocation(), block.getType(), block.getData());
            return null;
        }
        block.setType(Material.AIR);
    } else {
        //Not Creative
        DiggingMessage msgFinishBreak = new DiggingMessage(DiggingMessage.FINISH_DIGGING, packet.x, packet.y, packet.z, 1);
        return new Message[] { msgFinishBreak };
    }
    return null;
}
Also used : GlowPlayer(net.glowstone.entity.GlowPlayer) Player(org.bukkit.entity.Player) GlowBlock(net.glowstone.block.GlowBlock) DiggingMessage(net.glowstone.net.message.play.player.DiggingMessage) Message(com.flowpowered.networking.Message) DiggingMessage(net.glowstone.net.message.play.player.DiggingMessage) GlowPlayer(net.glowstone.entity.GlowPlayer) PlayerInteractEvent(org.bukkit.event.player.PlayerInteractEvent) GlowWorld(net.glowstone.GlowWorld)

Example 38 with GlowBlock

use of net.glowstone.block.GlowBlock in project Glowstone by GlowstoneMC.

the class BlockChest method placeBlock.

@Override
public void placeBlock(GlowPlayer player, GlowBlockState state, BlockFace face, ItemStack holding, Vector clickedLoc) {
    super.placeBlock(player, state, face, holding, clickedLoc);
    MaterialData data = state.getData();
    if (data instanceof Chest) {
        Chest chest = (Chest) data;
        GlowBlock chestBlock = state.getBlock();
        BlockFace normalFacing = getOppositeBlockFace(player.getLocation(), false);
        Collection<BlockFace> attachedChests = searchChests(chestBlock);
        if (attachedChests.isEmpty()) {
            chest.setFacingDirection(normalFacing);
            state.setData(chest);
            return;
        } else if (attachedChests.size() > 1) {
            GlowServer.logger.warning("Chest placed near two other chests!");
            return;
        }
        BlockFace otherPart = attachedChests.iterator().next();
        GlowBlock otherPartBlock = chestBlock.getRelative(otherPart);
        if (getAttachedChest(otherPartBlock) != null) {
            GlowServer.logger.warning("Chest placed near already attached chest!");
            return;
        }
        BlockState otherPartState = otherPartBlock.getState();
        MaterialData otherPartData = otherPartState.getData();
        if (otherPartData instanceof Chest) {
            Chest otherChest = (Chest) otherPartData;
            BlockFace facing = getFacingDirection(normalFacing, otherChest.getFacing(), otherPart, player);
            chest.setFacingDirection(facing);
            state.setData(chest);
            otherChest.setFacingDirection(facing);
            otherPartState.setData(otherChest);
            otherPartState.update();
        } else {
            warnMaterialData(Chest.class, otherPartData);
        }
    } else {
        warnMaterialData(Chest.class, data);
    }
}
Also used : Chest(org.bukkit.material.Chest) GlowBlock(net.glowstone.block.GlowBlock) BlockState(org.bukkit.block.BlockState) GlowBlockState(net.glowstone.block.GlowBlockState) BlockFace(org.bukkit.block.BlockFace) MaterialData(org.bukkit.material.MaterialData)

Example 39 with GlowBlock

use of net.glowstone.block.GlowBlock in project Glowstone by GlowstoneMC.

the class BlockCrops method getGrowthRateModifier.

protected float getGrowthRateModifier(GlowBlock block) {
    float modifier = 1;
    // check for soil around (increase the chance modifier to 10 in the best conditions)
    for (int x = -1; x <= 1; x++) {
        for (int z = -1; z <= 1; z++) {
            GlowBlock b = block.getWorld().getBlockAt(block.getX() + x, block.getY() - 1, block.getZ() + z);
            float soilBonus = 0;
            if (b.getType() == Material.SOIL) {
                soilBonus = 1;
                // check if soil is wet for more bonus
                if (b.getData() > 0) {
                    soilBonus = 3;
                }
                // more chances if the soil the crop is planted on is wet
                if (x != 0 || z != 0) {
                    soilBonus /= 4.0F;
                }
            }
            // this will add 0.25 points for dry soil around, 0.75 points for wet soil around
            // and 1 point for dry soil under the stem, 3 points for wet soil under stem
            modifier += soilBonus;
        }
    }
    // check for crops around, decrease chances by 50% if a crop of the same type is found on
    // both NS and EW axis, or a crop is found on a diagonal block
    boolean cropOnDiagonalBlock = false;
    boolean cropOnNorthOrSouth = false;
    boolean cropOnEastOrWest = false;
    for (int x = -1; x <= 1; x++) {
        for (int z = -1; z <= 1; z++) {
            if (x != 0 || z != 0) {
                if (block.getWorld().getBlockAt(block.getX() + x, block.getY(), block.getZ() + z).getType() == getMaterial()) {
                    if (x != 0 && z != 0) {
                        cropOnDiagonalBlock = true;
                    } else if (x == 0) {
                        cropOnNorthOrSouth = true;
                    } else {
                        cropOnEastOrWest = true;
                    }
                }
            }
        }
    }
    if (cropOnNorthOrSouth && cropOnEastOrWest || cropOnDiagonalBlock) {
        return modifier / 2.0F;
    }
    return modifier;
}
Also used : GlowBlock(net.glowstone.block.GlowBlock)

Example 40 with GlowBlock

use of net.glowstone.block.GlowBlock in project Glowstone by GlowstoneMC.

the class BlockButton method extraUpdate.

private void extraUpdate(GlowBlock block) {
    Button button = (Button) block.getState().getData();
    ItemTable itemTable = ItemTable.instance();
    GlowBlock target = block.getRelative(button.getAttachedFace());
    if (target.getType().isSolid()) {
        for (BlockFace face2 : ADJACENT) {
            GlowBlock target2 = target.getRelative(face2);
            BlockType notifyType = itemTable.getBlock(target2.getTypeId());
            if (notifyType != null) {
                if (target2.getFace(block) == null) {
                    notifyType.onNearBlockChanged(target2, BlockFace.SELF, block, block.getType(), block.getData(), block.getType(), block.getData());
                }
                notifyType.onRedstoneUpdate(target2);
            }
        }
    }
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) Button(org.bukkit.material.Button) ItemTable(net.glowstone.block.ItemTable) BlockFace(org.bukkit.block.BlockFace)

Aggregations

GlowBlock (net.glowstone.block.GlowBlock)72 BlockFace (org.bukkit.block.BlockFace)23 GlowBlockState (net.glowstone.block.GlowBlockState)21 Block (org.bukkit.block.Block)16 GlowWorld (net.glowstone.GlowWorld)14 MaterialData (org.bukkit.material.MaterialData)13 ItemTable (net.glowstone.block.ItemTable)10 BlockType (net.glowstone.block.blocktype.BlockType)7 ItemType (net.glowstone.block.itemtype.ItemType)7 Material (org.bukkit.Material)7 Vector (org.bukkit.util.Vector)7 BlockEntity (net.glowstone.block.entity.BlockEntity)6 ItemStack (org.bukkit.inventory.ItemStack)6 BlockVector (org.bukkit.util.BlockVector)5 Message (com.flowpowered.network.Message)4 IOException (java.io.IOException)4 GlowPlayer (net.glowstone.entity.GlowPlayer)4 Title (com.destroystokyo.paper.Title)3 ByteBufUtils (com.flowpowered.network.util.ByteBufUtils)3 Preconditions (com.google.common.base.Preconditions)3