Search in sources :

Example 6 with BlockType

use of net.glowstone.block.blocktype.BlockType in project Glowstone by GlowstoneMC.

the class BlockPlacementHandler method handle.

@Override
public void handle(GlowSession session, BlockPlacementMessage message) {
    //TODO: Hand handling instead of .getHeldItem()
    GlowPlayer player = session.getPlayer();
    if (player == null)
        return;
    //GlowServer.logger.info(session + ": " + message);
    /*
         * The client sends this packet for the following cases:
         * Right click air:
         * - Send direction=-1 packet for any non-null item
         * Right click block:
         * - Send packet with all values filled
         * - If client DOES NOT expect a block placement to result:
         *   - Send direction=-1 packet (unless item is null)
         *
         * Client will expect a block placement to result from blocks and from
         * certain items (e.g. sugarcane, sign). We *could* opt to trust the
         * client on this, but the server's view of events (particularly under
         * the Bukkit API, or custom ItemTypes) may differ from the client's.
         *
         * In order to avoid firing two events for one interact, the two
         * packet case must be handled here. Care must also be taken that a
         * right-click air of an expected-place item immediately after is
         * not considered part of the same action.
         */
    Action action = Action.RIGHT_CLICK_BLOCK;
    GlowBlock clicked = player.getWorld().getBlockAt(message.getX(), message.getY(), message.getZ());
    /*
         * Check if the message is a -1. If we *just* got a message with the
         * values filled, discard it, otherwise perform right-click-air.
         */
    if (message.getDirection() == -1) {
        BlockPlacementMessage previous = session.getPreviousPlacement();
        //   session.setPreviousPlacement(null);
        return;
    // }
    }
    // Set previous placement message
    session.setPreviousPlacement(message);
    // Get values from the message
    Vector clickedLoc = new Vector(message.getCursorX(), message.getCursorY(), message.getCursorZ());
    BlockFace face = convertFace(message.getDirection());
    ItemStack holding = player.getItemInHand();
    boolean rightClickedAir = false;
    // check that a block-click wasn't against air
    if (clicked == null || clicked.getType() == Material.AIR) {
        action = Action.RIGHT_CLICK_AIR;
        // inform the player their perception of reality is wrong
        if (holding.getType().isBlock()) {
            player.sendBlockChange(clicked.getLocation(), Material.AIR, (byte) 0);
            return;
        } else {
            rightClickedAir = true;
        }
    }
    // call interact event
    PlayerInteractEvent event = EventFactory.onPlayerInteract(player, action, rightClickedAir ? null : clicked, face);
    //GlowServer.logger.info("Interact: " + action + " " + clicked + " " + face);
    // attempt to use interacted block
    // DEFAULT is treated as ALLOW, and sneaking is always considered
    boolean useInteractedBlock = event.useInteractedBlock() != Result.DENY;
    if (useInteractedBlock && !rightClickedAir && (!player.isSneaking() || InventoryUtil.isEmpty(holding))) {
        BlockType blockType = ItemTable.instance().getBlock(clicked.getType());
        if (blockType != null) {
            useInteractedBlock = blockType.blockInteract(player, clicked, face, clickedLoc);
        } else {
            GlowServer.logger.info("Unknown clicked block, " + clicked.getType());
        }
    } else {
        useInteractedBlock = false;
    }
    // follows ALLOW/DENY: default to if no block was interacted with
    if (selectResult(event.useItemInHand(), !useInteractedBlock) && holding != null) {
        ItemType type = ItemTable.instance().getItem(holding.getType());
        if (!rightClickedAir && holding.getType() != Material.AIR && !type.canOnlyUseSelf()) {
            type.rightClickBlock(player, clicked, face, holding, clickedLoc);
        }
    }
    // in case something is unimplemented or otherwise screwy on our side
    if (!rightClickedAir) {
        revert(player, clicked);
        revert(player, clicked.getRelative(face));
    }
    // if there's been a change in the held item, make it valid again
    if (!InventoryUtil.isEmpty(holding)) {
        if (holding.getType().getMaxDurability() > 0 && holding.getDurability() > holding.getType().getMaxDurability()) {
            holding.setAmount(holding.getAmount() - 1);
            holding.setDurability((short) 0);
        }
        if (holding.getAmount() <= 0) {
            holding = null;
        }
    }
    player.setItemInHand(holding);
}
Also used : BlockPlacementMessage(net.glowstone.net.message.play.player.BlockPlacementMessage) Action(org.bukkit.event.block.Action) GlowBlock(net.glowstone.block.GlowBlock) BlockType(net.glowstone.block.blocktype.BlockType) GlowPlayer(net.glowstone.entity.GlowPlayer) BlockFace(org.bukkit.block.BlockFace) PlayerInteractEvent(org.bukkit.event.player.PlayerInteractEvent) ItemType(net.glowstone.block.itemtype.ItemType) ItemStack(org.bukkit.inventory.ItemStack) Vector(org.bukkit.util.Vector)

Example 7 with BlockType

use of net.glowstone.block.blocktype.BlockType in project Glowstone by GlowstoneMC.

the class DiggingHandler method handle.

@Override
public void handle(GlowSession session, DiggingMessage message) {
    //Todo: Implement SHOOT_ARROW_FINISH_EATING
    //Todo: Implement SWAP_ITEM_IN_HAND
    GlowPlayer player = session.getPlayer();
    GlowWorld world = player.getWorld();
    GlowBlock block = world.getBlockAt(message.getX(), message.getY(), message.getZ());
    BlockFace face = BlockPlacementHandler.convertFace(message.getFace());
    ItemStack holding = player.getItemInHand();
    if (block.getRelative(face).getType() == Material.FIRE) {
        block.getRelative(face).breakNaturally();
        // returns to avoid breaking block in creative
        return;
    }
    boolean blockBroken = false;
    boolean revert = false;
    if (message.getState() == DiggingMessage.START_DIGGING) {
        // call interact event
        Action action = Action.LEFT_CLICK_BLOCK;
        Block eventBlock = block;
        if (player.getLocation().distanceSquared(block.getLocation()) > 36 || block.getTypeId() == 0) {
            action = Action.LEFT_CLICK_AIR;
            eventBlock = null;
        }
        PlayerInteractEvent interactEvent = EventFactory.onPlayerInteract(player, action, eventBlock, face);
        // attempt to use item in hand, that is, dig up the block
        if (!BlockPlacementHandler.selectResult(interactEvent.useItemInHand(), true)) {
            // the event was cancelled, get out of here
            revert = true;
        } else if (player.getGameMode() != GameMode.SPECTATOR) {
            player.setDigging(null);
            // emit damage event - cancel by default if holding a sword
            boolean instaBreak = player.getGameMode() == GameMode.CREATIVE || block.getMaterialValues().getHardness() == 0;
            BlockDamageEvent damageEvent = new BlockDamageEvent(player, block, player.getItemInHand(), instaBreak);
            if (player.getGameMode() == GameMode.CREATIVE && holding != null && EnchantmentTarget.WEAPON.includes(holding.getType())) {
                damageEvent.setCancelled(true);
            }
            EventFactory.callEvent(damageEvent);
            // follow orders
            if (damageEvent.isCancelled()) {
                revert = true;
            } else {
                // in creative, break even if denied in the event, or the block
                // can never be broken (client does not send DONE_DIGGING).
                blockBroken = damageEvent.getInstaBreak();
                if (!blockBroken) {
                    /// TODO: add a delay here based on hardness
                    player.setDigging(block);
                }
            }
        }
    } else if (message.getState() == DiggingMessage.CANCEL_DIGGING) {
        player.setDigging(null);
    } else if (message.getState() == DiggingMessage.FINISH_DIGGING) {
        // shouldn't happen in creative mode
        // todo: verification against malicious clients
        blockBroken = block.equals(player.getDigging());
        if (blockBroken && holding.getType().getMaxDurability() != 0 && holding.getType() != Material.AIR && holding.getDurability() != holding.getType().getMaxDurability()) {
            switch(block.getType()) {
                case GRASS:
                case DIRT:
                case SAND:
                case GRAVEL:
                case MYCEL:
                case SOUL_SAND:
                    switch(holding.getType()) {
                        case WOOD_SPADE:
                        case STONE_SPADE:
                        case IRON_SPADE:
                        case GOLD_SPADE:
                        case DIAMOND_SPADE:
                            holding.setDurability((short) (holding.getDurability() + 1));
                            break;
                        default:
                            holding.setDurability((short) (holding.getDurability() + 2));
                            break;
                    }
                    break;
                case LOG:
                case LOG_2:
                case WOOD:
                case CHEST:
                    switch(holding.getType()) {
                        case WOOD_AXE:
                        case STONE_AXE:
                        case IRON_AXE:
                        case GOLD_AXE:
                        case DIAMOND_AXE:
                            holding.setDurability((short) (holding.getDurability() + 1));
                            break;
                        default:
                            holding.setDurability((short) (holding.getDurability() + 2));
                            break;
                    }
                    break;
                case STONE:
                case COBBLESTONE:
                    switch(holding.getType()) {
                        case WOOD_PICKAXE:
                        case STONE_PICKAXE:
                        case IRON_PICKAXE:
                        case GOLD_PICKAXE:
                        case DIAMOND_PICKAXE:
                            holding.setDurability((short) (holding.getDurability() + 1));
                            break;
                        default:
                            holding.setDurability((short) (holding.getDurability() + 2));
                            break;
                    }
                    break;
                default:
                    holding.setDurability((short) (holding.getDurability() + 2));
                    break;
            }
            if (holding.getType().getMaxDurability() != 0 && holding.getDurability() >= holding.getType().getMaxDurability()) {
                player.getItemInHand().setType(Material.AIR);
            }
        }
        player.setDigging(null);
    } else if (message.getState() == DiggingMessage.STATE_DROP_ITEM) {
        player.dropItemInHand(false);
        return;
    } else if (message.getState() == DiggingMessage.STATE_DROP_ITEMSTACK) {
        player.dropItemInHand(true);
        return;
    } else if (message.getState() == DiggingMessage.STATE_SHOT_ARROW_FINISH_EATING && player.getUsageItem() != null) {
        if (Objects.equals(player.getUsageItem(), holding)) {
            ItemType type = ItemTable.instance().getItem(player.getUsageItem().getType());
            if (type != null && type instanceof ItemTimedUsage) {
                ((ItemTimedUsage) type).endUse(player, player.getUsageItem());
            } else {
            // todo: inform the player that this item cannot be consumed/used
            }
        } else {
        // todo: verification against malicious clients
        // todo: inform player their item is wrong
        }
        return;
    } else if (message.getState() == DiggingMessage.SWAP_ITEM_IN_HAND) {
        ItemStack main = player.getInventory().getItemInMainHand();
        ItemStack off = player.getInventory().getItemInOffHand();
        player.getInventory().setItemInOffHand(main);
        player.getInventory().setItemInMainHand(off);
        player.updateInventory();
        return;
    } else {
        return;
    }
    if (blockBroken && !revert) {
        // fire the block break event
        BlockBreakEvent breakEvent = EventFactory.callEvent(new BlockBreakEvent(block, player));
        if (breakEvent.isCancelled()) {
            BlockPlacementHandler.revert(player, block);
            return;
        }
        MaterialData data = block.getState().getData();
        if (data instanceof DoublePlant) {
            if (((DoublePlant) data).getSpecies() == DoublePlantSpecies.PLANT_APEX && block.getRelative(BlockFace.DOWN).getState().getData() instanceof DoublePlant) {
                block = block.getRelative(BlockFace.DOWN);
            }
        }
        BlockType blockType = ItemTable.instance().getBlock(block.getType());
        if (blockType != null) {
            blockType.blockDestroy(player, block, face);
        }
        // destroy the block
        if (!block.isEmpty() && !block.isLiquid() && (player.getGameMode() != GameMode.CREATIVE || blockType instanceof BlockContainer) && world.getGameRuleMap().getBoolean("doTileDrops")) {
            Collection<ItemStack> drops = blockType.getDrops(block, holding);
            if (blockType instanceof BlockContainer && player.getGameMode() == GameMode.CREATIVE) {
                drops = ((BlockContainer) blockType).getContentDrops(block);
            }
            for (ItemStack drop : drops) {
                GlowItem item = world.dropItemNaturally(block.getLocation(), drop);
                item.setPickupDelay(30);
                item.setBias(player);
            }
        }
        player.addExhaustion(0.005f);
        // STEP_SOUND actually is the block break particles
        world.playEffectExceptTo(block.getLocation(), Effect.STEP_SOUND, block.getTypeId(), 64, player);
        GlowBlockState state = block.getState();
        block.setType(Material.AIR);
        if (blockType != null) {
            blockType.afterDestroy(player, block, face, state);
        }
    } else if (revert) {
        // replace the block that wasn't really dug
        BlockPlacementHandler.revert(player, block);
    } else if (block.getType() != Material.AIR) {
        BlockType blockType = ItemTable.instance().getBlock(block.getType());
        blockType.leftClickBlock(player, block, holding);
    }
}
Also used : ItemTimedUsage(net.glowstone.block.itemtype.ItemTimedUsage) Action(org.bukkit.event.block.Action) GlowPlayer(net.glowstone.entity.GlowPlayer) BlockFace(org.bukkit.block.BlockFace) BlockContainer(net.glowstone.block.blocktype.BlockContainer) GlowBlockState(net.glowstone.block.GlowBlockState) PlayerInteractEvent(org.bukkit.event.player.PlayerInteractEvent) ItemType(net.glowstone.block.itemtype.ItemType) GlowItem(net.glowstone.entity.objects.GlowItem) GlowBlock(net.glowstone.block.GlowBlock) BlockType(net.glowstone.block.blocktype.BlockType) BlockDamageEvent(org.bukkit.event.block.BlockDamageEvent) GlowWorld(net.glowstone.GlowWorld) GlowBlock(net.glowstone.block.GlowBlock) Block(org.bukkit.block.Block) BlockBreakEvent(org.bukkit.event.block.BlockBreakEvent) MaterialData(org.bukkit.material.MaterialData) ItemStack(org.bukkit.inventory.ItemStack) DoublePlant(org.bukkit.material.DoublePlant)

Example 8 with BlockType

use of net.glowstone.block.blocktype.BlockType in project Glowstone by GlowstoneMC.

the class ItemBanner method rightClickBlock.

@Override
public void rightClickBlock(GlowPlayer player, GlowBlock target, BlockFace face, ItemStack holding, Vector clickedLoc) {
    BlockType placeAs;
    if (face == BlockFace.UP) {
        placeAs = ItemTable.instance().getBlock(Material.STANDING_BANNER);
    } else if (face == BlockFace.DOWN) {
        return;
    } else {
        placeAs = ItemTable.instance().getBlock(Material.WALL_BANNER);
    }
    placeAs.rightClickBlock(player, target, face, holding, clickedLoc);
}
Also used : BlockType(net.glowstone.block.blocktype.BlockType)

Example 9 with BlockType

use of net.glowstone.block.blocktype.BlockType in project Glowstone by GlowstoneMC.

the class ItemFilledBucket method rightClickBlock.

@Override
public void rightClickBlock(GlowPlayer player, GlowBlock against, BlockFace face, ItemStack holding, Vector clickedLoc) {
    GlowBlock target = against.getRelative(face);
    BlockType againstBlockType = ItemTable.instance().getBlock(against.getType());
    if (againstBlockType.canAbsorb(target, face, holding)) {
        target = against;
    } else if (!target.isEmpty()) {
        BlockType targetType = ItemTable.instance().getBlock(target.getTypeId());
        if (!targetType.canOverride(target, face, holding)) {
            return;
        }
    }
    GlowBlockState newState = target.getState();
    PlayerBucketEmptyEvent event = EventFactory.callEvent(new PlayerBucketEmptyEvent(player, target, face, holding.getType(), holding));
    if (event.isCancelled()) {
        return;
    }
    liquid.placeBlock(player, newState, face, holding, clickedLoc);
    // perform the block change
    newState.update(true);
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) BlockType(net.glowstone.block.blocktype.BlockType) GlowBlockState(net.glowstone.block.GlowBlockState) PlayerBucketEmptyEvent(org.bukkit.event.player.PlayerBucketEmptyEvent)

Example 10 with BlockType

use of net.glowstone.block.blocktype.BlockType 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)

Aggregations

BlockType (net.glowstone.block.blocktype.BlockType)17 GlowBlock (net.glowstone.block.GlowBlock)8 Material (org.bukkit.Material)5 ItemType (net.glowstone.block.itemtype.ItemType)4 BlockFace (org.bukkit.block.BlockFace)4 Block (org.bukkit.block.Block)3 ItemStack (org.bukkit.inventory.ItemStack)3 GlowWorld (net.glowstone.GlowWorld)2 GlowBlockState (net.glowstone.block.GlowBlockState)2 ItemTable (net.glowstone.block.ItemTable)2 BlockLiquid (net.glowstone.block.blocktype.BlockLiquid)2 BlockEntity (net.glowstone.block.entity.BlockEntity)2 GlowPlayer (net.glowstone.entity.GlowPlayer)2 Action (org.bukkit.event.block.Action)2 PlayerInteractEvent (org.bukkit.event.player.PlayerInteractEvent)2 Test (org.junit.Test)2 DataInputStream (java.io.DataInputStream)1 IOException (java.io.IOException)1 BlockContainer (net.glowstone.block.blocktype.BlockContainer)1 ItemTimedUsage (net.glowstone.block.itemtype.ItemTimedUsage)1