Search in sources :

Example 1 with GlowItem

use of net.glowstone.entity.objects.GlowItem 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 2 with GlowItem

use of net.glowstone.entity.objects.GlowItem in project Glowstone by GlowstoneMC.

the class BlockHopper method pullItems.

private void pullItems(GlowBlock block, HopperEntity hopper) {
    GlowBlock source = block.getRelative(BlockFace.UP);
    MaterialData data = source.getState().getData();
    if (!source.getType().isSolid() || (data instanceof Step && !((Step) data).isInverted()) || (data instanceof WoodenStep && !((WoodenStep) data).isInverted()) || (data instanceof Sign) || (data instanceof Rails)) {
        GlowItem item = getFirstDroppedItem(source.getLocation());
        if (item == null) {
            return;
        }
        ItemStack stack = item.getItemStack();
        HashMap<Integer, ItemStack> add = hopper.getInventory().addItem(stack);
        if (add.size() > 0) {
            item.setItemStack(add.get(0));
        } else {
            item.remove();
        }
    } else if (source.getBlockEntity() != null && source.getBlockEntity() instanceof ContainerEntity) {
        ContainerEntity sourceContainer = (ContainerEntity) source.getBlockEntity();
        if (sourceContainer.getInventory() == null || sourceContainer.getInventory().getContents().length == 0) {
            return;
        }
        ItemStack item = getFirstItem(sourceContainer);
        if (item == null) {
            return;
        }
        ItemStack clone = item.clone();
        clone.setAmount(1);
        if (hopper.getInventory().addItem(clone).size() > 0) {
            return;
        }
        if (item.getAmount() - 1 == 0) {
            sourceContainer.getInventory().remove(item);
        } else {
            item.setAmount(item.getAmount() - 1);
        }
    }
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) ContainerEntity(net.glowstone.block.entity.ContainerEntity) ItemStack(org.bukkit.inventory.ItemStack) GlowItem(net.glowstone.entity.objects.GlowItem)

Example 3 with GlowItem

use of net.glowstone.entity.objects.GlowItem in project Glowstone by GlowstoneMC.

the class DefaultDispenseBehavior method doDispense.

private void doDispense(GlowBlock block, ItemStack items, int power, BlockFace facing, Vector target) {
    double x = target.getX();
    double y = target.getY();
    double z = target.getZ();
    if (facing.getModY() != 0) {
        y -= 0.125;
    } else {
        y -= 0.15625;
    }
    double velocity = random.nextDouble() * 0.1 + 0.2;
    double velocityX = facing.getModX() * velocity;
    double velocityY = 0.2;
    double velocityZ = facing.getModZ() * velocity;
    velocityX += random.nextGaussian() * 0.0075 * power;
    velocityY += random.nextGaussian() * 0.0075 * power;
    velocityZ += random.nextGaussian() * 0.0075 * power;
    BlockDispenseEvent dispenseEvent = new BlockDispenseEvent(block, items, new Vector(velocityX, velocityY, velocityZ));
    EventFactory.callEvent(dispenseEvent);
    if (!dispenseEvent.isCancelled()) {
        GlowItem item = new GlowItem(new Location(block.getWorld(), x, y, z), dispenseEvent.getItem());
        item.setVelocity(dispenseEvent.getVelocity());
    }
}
Also used : BlockDispenseEvent(org.bukkit.event.block.BlockDispenseEvent) Vector(org.bukkit.util.Vector) GlowItem(net.glowstone.entity.objects.GlowItem) Location(org.bukkit.Location)

Example 4 with GlowItem

use of net.glowstone.entity.objects.GlowItem in project Glowstone by GlowstoneMC.

the class GlowHumanEntity method dropItemInHand.

/**
     * Drops the item this entity currently has in its hands and remove the
     * item from the HumanEntity's inventory.
     *
     * @param wholeStack True if the whole stack should be dropped
     */
public void dropItemInHand(boolean wholeStack) {
    ItemStack stack = getItemInHand();
    if (InventoryUtil.isEmpty(stack)) {
        return;
    }
    ItemStack dropping = stack.clone();
    if (!wholeStack) {
        dropping.setAmount(1);
    }
    GlowItem dropped = drop(dropping);
    if (dropped == null) {
        return;
    }
    if (stack.getAmount() == 1 || wholeStack) {
        setItemInHand(InventoryUtil.createEmptyStack());
    } else {
        ItemStack now = stack.clone();
        now.setAmount(now.getAmount() - 1);
        setItemInHand(now);
    }
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) GlowItem(net.glowstone.entity.objects.GlowItem)

Example 5 with GlowItem

use of net.glowstone.entity.objects.GlowItem in project Glowstone by GlowstoneMC.

the class GlowCow method entityInteract.

@Override
public boolean entityInteract(GlowPlayer player, InteractEntityMessage message) {
    super.entityInteract(player, message);
    if (player.getGameMode().equals(GameMode.CREATIVE) || player.getGameMode().equals(GameMode.SPECTATOR))
        return false;
    if (!isAdult())
        return false;
    if (!player.getItemInHand().getType().equals(Material.BUCKET))
        return false;
    if (player.getItemInHand().getAmount() > 1) {
        player.getItemInHand().setAmount(player.getItemInHand().getAmount() - 1);
    } else {
        player.getInventory().clear(player.getInventory().getHeldItemSlot());
    }
    if (player.getInventory().firstEmpty() == -1) {
        GlowItem item = player.getWorld().dropItem(player.getLocation().clone().add(0, 1, 0), new ItemStack(Material.MILK_BUCKET, 1));
        item.setVelocity(getLocation().add(0, -1, 0).clone().toVector().subtract(player.getLocation().clone().add(0, 1, 0).toVector()).multiply(0.3));
    } else {
        player.getInventory().addItem(new ItemStack(Material.MILK_BUCKET, 1));
    }
    return true;
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) GlowItem(net.glowstone.entity.objects.GlowItem)

Aggregations

GlowItem (net.glowstone.entity.objects.GlowItem)8 ItemStack (org.bukkit.inventory.ItemStack)4 Vector (org.bukkit.util.Vector)3 GlowBlock (net.glowstone.block.GlowBlock)2 Location (org.bukkit.Location)2 GlowWorld (net.glowstone.GlowWorld)1 GlowBlockState (net.glowstone.block.GlowBlockState)1 BlockContainer (net.glowstone.block.blocktype.BlockContainer)1 BlockType (net.glowstone.block.blocktype.BlockType)1 ContainerEntity (net.glowstone.block.entity.ContainerEntity)1 ItemTimedUsage (net.glowstone.block.itemtype.ItemTimedUsage)1 ItemType (net.glowstone.block.itemtype.ItemType)1 GlowPlayer (net.glowstone.entity.GlowPlayer)1 Block (org.bukkit.block.Block)1 BlockFace (org.bukkit.block.BlockFace)1 Action (org.bukkit.event.block.Action)1 BlockBreakEvent (org.bukkit.event.block.BlockBreakEvent)1 BlockDamageEvent (org.bukkit.event.block.BlockDamageEvent)1 BlockDispenseEvent (org.bukkit.event.block.BlockDispenseEvent)1 PlayerInteractEvent (org.bukkit.event.player.PlayerInteractEvent)1