Search in sources :

Example 11 with GlowPlayer

use of net.glowstone.entity.GlowPlayer in project Glowstone by GlowstoneMC.

the class Explosion method damageEntities.

/////////////////////////////////////////
// Damage entities
private Collection<GlowPlayer> damageEntities() {
    float power = this.power;
    this.power *= 2;
    Collection<GlowPlayer> affectedPlayers = new ArrayList<>();
    LivingEntity[] entities = getNearbyEntities();
    for (LivingEntity entity : entities) {
        // refine area to sphere, instead of box
        if (distanceToSquared(entity) > power * power) {
            continue;
        }
        double exposure = world.rayTrace(location, (GlowEntity) entity);
        double impact = (1 - (distanceTo(entity) / power / 2)) * exposure;
        double damage = (impact * impact + impact) * 8 * power + 1;
        int epf = getProtectionFactor(entity);
        double reduction = calculateEnchantedReduction(epf);
        damage = damage * reduction;
        exposure -= exposure * epf * 0.15;
        DamageCause damageCause;
        if (source == null || source.getType() == EntityType.PRIMED_TNT) {
            damageCause = DamageCause.BLOCK_EXPLOSION;
        } else {
            damageCause = DamageCause.ENTITY_EXPLOSION;
        }
        entity.damage(damage, source, damageCause);
        if (entity instanceof GlowPlayer) {
            affectedPlayers.add((GlowPlayer) entity);
            if (((GlowPlayer) entity).isFlying()) {
                continue;
            }
        }
        Vector rayLength = RayUtil.getVelocityRay(distanceToHead(entity));
        rayLength.multiply(exposure);
        Vector currentVelocity = entity.getVelocity();
        currentVelocity.add(rayLength);
        entity.setVelocity(currentVelocity);
    }
    return affectedPlayers;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) GlowPlayer(net.glowstone.entity.GlowPlayer) DamageCause(org.bukkit.event.entity.EntityDamageEvent.DamageCause) BlockVector(org.bukkit.util.BlockVector) Vector(org.bukkit.util.Vector)

Example 12 with GlowPlayer

use of net.glowstone.entity.GlowPlayer in project Glowstone by GlowstoneMC.

the class Explosion method explodeWithEvent.

public boolean explodeWithEvent() {
    if (power < 0.1f)
        return true;
    Set<BlockVector> droppedBlocks = calculateBlocks();
    EntityExplodeEvent event = EventFactory.callEvent(new EntityExplodeEvent(source, location, toBlockList(droppedBlocks), yield));
    if (event.isCancelled())
        return false;
    yield = event.getYield();
    playOutSoundAndParticles();
    List<Block> blocks = toBlockList(droppedBlocks);
    for (Block block : blocks) {
        handleBlockExplosion((GlowBlock) block);
    }
    if (incendiary) {
        for (Block block : blocks) {
            setBlockOnFire((GlowBlock) block);
        }
    }
    Collection<GlowPlayer> affectedPlayers = damageEntities();
    for (GlowPlayer player : affectedPlayers) {
        playOutExplosion(player, droppedBlocks);
    }
    return true;
}
Also used : EntityExplodeEvent(org.bukkit.event.entity.EntityExplodeEvent) GlowPlayer(net.glowstone.entity.GlowPlayer) GlowBlock(net.glowstone.block.GlowBlock) Block(org.bukkit.block.Block) BlockVector(org.bukkit.util.BlockVector)

Example 13 with GlowPlayer

use of net.glowstone.entity.GlowPlayer 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 14 with GlowPlayer

use of net.glowstone.entity.GlowPlayer 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 15 with GlowPlayer

use of net.glowstone.entity.GlowPlayer in project Glowstone by GlowstoneMC.

the class SteerVehicleHandler method handle.

@Override
public void handle(GlowSession session, SteerVehicleMessage message) {
    GlowServer.logger.info(session + ": " + message);
    GlowPlayer player = session.getPlayer();
    if (message.isUnmount() && player.isInsideVehicle()) {
        player.leaveVehicle();
    }
// todo
}
Also used : GlowPlayer(net.glowstone.entity.GlowPlayer)

Aggregations

GlowPlayer (net.glowstone.entity.GlowPlayer)39 ItemStack (org.bukkit.inventory.ItemStack)10 GlowBlock (net.glowstone.block.GlowBlock)5 Player (org.bukkit.entity.Player)5 Message (com.flowpowered.network.Message)4 Location (org.bukkit.Location)4 GlowWorld (net.glowstone.GlowWorld)3 ItemType (net.glowstone.block.itemtype.ItemType)3 UserListItemMessage (net.glowstone.net.message.play.game.UserListItemMessage)3 BlockPlacementMessage (net.glowstone.net.message.play.player.BlockPlacementMessage)3 Material (org.bukkit.Material)3 Block (org.bukkit.block.Block)3 BlockFace (org.bukkit.block.BlockFace)3 Vector (org.bukkit.util.Vector)3 ChunkLocation (org.dragonet.ChunkLocation)3 PEPacket (org.dragonet.net.packet.minecraft.PEPacket)3 AsyncableMessage (com.flowpowered.network.AsyncableMessage)2 Message (com.flowpowered.networking.Message)2 ArrayList (java.util.ArrayList)2 BlockType (net.glowstone.block.blocktype.BlockType)2