Search in sources :

Example 26 with GlowBlock

use of net.glowstone.block.GlowBlock 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 27 with GlowBlock

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

the class GlowFurnaceInventory method setItem.

@Override
public void setItem(int index, ItemStack item) {
    super.setItem(index, item);
    GlowBlock block = (GlowBlock) getHolder().getBlock();
    block.getWorld().requestPulse(block);
}
Also used : GlowBlock(net.glowstone.block.GlowBlock)

Example 28 with GlowBlock

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

the class LavaDecorator method decorate.

@Override
public void decorate(World world, Random random, Chunk source) {
    int sourceX = (source.getX() << 4) + random.nextInt(16);
    int sourceZ = (source.getZ() << 4) + random.nextInt(16);
    int sourceY = flowing ? 4 + random.nextInt(120) : 10 + random.nextInt(108);
    Block block = world.getBlockAt(sourceX, sourceY, sourceZ);
    if ((block.getType() == Material.NETHERRACK || block.isEmpty()) && block.getRelative(BlockFace.UP).getType() == Material.NETHERRACK) {
        int netherrackBlockCount = 0;
        for (BlockFace face : SIDES) {
            if (block.getRelative(face).getType() == Material.NETHERRACK) {
                netherrackBlockCount++;
            }
        }
        int airBlockCount = 0;
        for (BlockFace face : SIDES) {
            if (block.getRelative(face).isEmpty()) {
                airBlockCount++;
            }
        }
        if (netherrackBlockCount == 5 || flowing && airBlockCount == 1 && netherrackBlockCount == 4) {
            BlockState state = block.getState();
            state.setType(Material.LAVA);
            state.update(true);
            new PulseTask((GlowBlock) block, true, 1, true).startPulseTask();
        }
    }
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) BlockState(org.bukkit.block.BlockState) BlockFace(org.bukkit.block.BlockFace) GlowBlock(net.glowstone.block.GlowBlock) Block(org.bukkit.block.Block) PulseTask(net.glowstone.scheduler.PulseTask)

Example 29 with GlowBlock

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

the class ItemItemFrame method rightClickBlock.

@Override
public void rightClickBlock(GlowPlayer player, GlowBlock target, BlockFace face, ItemStack holding, Vector clickedLoc) {
    GlowItemFrame entity = new GlowItemFrame(player, target.getLocation().getBlock().getRelative(face).getLocation(), face);
    List<Message> spawnMessage = entity.createSpawnMessage();
    entity.getWorld().getRawPlayers().stream().filter(p -> p.canSeeEntity(entity)).forEach(p -> p.getSession().sendAll(spawnMessage.toArray(new Message[spawnMessage.size()])));
}
Also used : Vector(org.bukkit.util.Vector) GlowBlock(net.glowstone.block.GlowBlock) List(java.util.List) Message(com.flowpowered.network.Message) GlowPlayer(net.glowstone.entity.GlowPlayer) BlockFace(org.bukkit.block.BlockFace) GlowItemFrame(net.glowstone.entity.objects.GlowItemFrame) ItemStack(org.bukkit.inventory.ItemStack) Message(com.flowpowered.network.Message) GlowItemFrame(net.glowstone.entity.objects.GlowItemFrame)

Example 30 with GlowBlock

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

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