Search in sources :

Example 6 with ItemType

use of net.glowstone.block.itemtype.ItemType in project Glowstone by GlowstoneMC.

the class UseItemHandler method handle.

@Override
public void handle(GlowSession session, UseItemMessage message) {
    GlowPlayer player = session.getPlayer();
    ItemStack holding = player.getItemInHand();
    if (holding != null) {
        ItemType type = ItemTable.instance().getItem(holding.getType());
        if (type != null) {
            if (type.canOnlyUseSelf()) {
                type.rightClickAir(player, holding);
            } else {
                if (holding.getType() == Material.WATER_BUCKET || holding.getType() == Material.LAVA_BUCKET) {
                    holding.setType(Material.BUCKET);
                }
            }
        }
    }
}
Also used : GlowPlayer(net.glowstone.entity.GlowPlayer) ItemType(net.glowstone.block.itemtype.ItemType) ItemStack(org.bukkit.inventory.ItemStack)

Example 7 with ItemType

use of net.glowstone.block.itemtype.ItemType in project Glowstone by GlowstoneMC.

the class BlockType method rightClickBlock.

@Override
public final void rightClickBlock(GlowPlayer player, GlowBlock against, BlockFace face, ItemStack holding, Vector clickedLoc) {
    GlowBlock target = against.getRelative(face);
    // prevent building above the height limit
    if (target.getLocation().getY() >= target.getWorld().getMaxHeight()) {
        player.sendMessage(ChatColor.RED + "The height limit for this world is " + target.getWorld().getMaxHeight() + " blocks");
        return;
    }
    // check whether the block clicked against should absorb the placement
    BlockType againstType = ItemTable.instance().getBlock(against.getTypeId());
    if (againstType != null) {
        if (againstType.canAbsorb(against, face, holding)) {
            target = against;
        } else if (!target.isEmpty()) {
            // air can always be overridden
            BlockType targetType = ItemTable.instance().getBlock(target.getTypeId());
            if (targetType != null && !targetType.canOverride(target, face, holding)) {
                return;
            }
        }
    }
    if (getMaterial().isSolid()) {
        BlockBoundingBox box = new BlockBoundingBox(target);
        List<Entity> entities = target.getWorld().getEntityManager().getEntitiesInside(box, null);
        for (Entity e : entities) {
            if (e instanceof LivingEntity) {
                return;
            }
        }
    }
    // call canBuild event
    boolean canBuild = canPlaceAt(target, face);
    BlockCanBuildEvent canBuildEvent = new BlockCanBuildEvent(target, getId(), canBuild);
    if (!EventFactory.callEvent(canBuildEvent).isBuildable()) {
        //revert(player, target);
        return;
    }
    // grab states and update block
    GlowBlockState oldState = target.getState(), newState = target.getState();
    ItemType itemType = ItemTable.instance().getItem(holding.getType());
    if (itemType.getPlaceAs() == null) {
        placeBlock(player, newState, face, holding, clickedLoc);
    } else {
        placeBlock(player, newState, face, new ItemStack(itemType.getPlaceAs().getMaterial(), holding.getAmount(), holding.getDurability()), clickedLoc);
    }
    newState.update(true);
    // call blockPlace event
    BlockPlaceEvent event = new BlockPlaceEvent(target, oldState, against, holding, player, canBuild);
    EventFactory.callEvent(event);
    if (event.isCancelled() || !event.canBuild()) {
        oldState.update(true);
        return;
    }
    // play the placement sound, except for the current player (handled by the client)
    SoundUtil.playSoundAtLocationExcept(target.getLocation(), getPlaceSound().getSound(), (getPlaceSound().getVolume() + 1F) / 2F, getPlaceSound().getPitch() * 0.8F, player);
    // do any after-place actions
    afterPlace(player, target, holding, oldState);
    // deduct from stack if not in creative mode
    if (player.getGameMode() != GameMode.CREATIVE) {
        holding.setAmount(holding.getAmount() - 1);
    }
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) BlockEntity(net.glowstone.block.entity.BlockEntity) GlowBlock(net.glowstone.block.GlowBlock) BlockCanBuildEvent(org.bukkit.event.block.BlockCanBuildEvent) GlowBlockState(net.glowstone.block.GlowBlockState) BlockPlaceEvent(org.bukkit.event.block.BlockPlaceEvent) ItemType(net.glowstone.block.itemtype.ItemType) BlockBoundingBox(net.glowstone.entity.physics.BlockBoundingBox) ItemStack(org.bukkit.inventory.ItemStack)

Example 8 with ItemType

use of net.glowstone.block.itemtype.ItemType in project Glowstone by GlowstoneMC.

the class ItemTypesTest method hasAllMaterials.

@Test
public void hasAllMaterials() {
    ItemType type = table.getItem(material);
    // special cases
    if (material == Material.AIR) {
        assertThat("ItemType exists for air: " + type, type, nullValue());
        return;
    }
    // check that it exists
    assertThat("ItemType does not exist for " + material, type, notNullValue());
    // check that its block status is correct
    assertThat("Block status mismatch between " + material + "(" + material.isBlock() + ") and " + type, (type instanceof BlockType), is(material.isBlock()));
    // check that material returned matches
    assertThat("ItemType returned wrong material", type.getMaterial(), is(material));
    // check that max stack size matches
    assertThat("Maximum stack size was incorrect", type.getMaxStackSize(), is(material.getMaxStackSize()));
}
Also used : BlockType(net.glowstone.block.blocktype.BlockType) ItemType(net.glowstone.block.itemtype.ItemType) Test(org.junit.Test)

Aggregations

ItemType (net.glowstone.block.itemtype.ItemType)8 GlowBlock (net.glowstone.block.GlowBlock)5 BlockType (net.glowstone.block.blocktype.BlockType)4 ItemStack (org.bukkit.inventory.ItemStack)4 GlowPlayer (net.glowstone.entity.GlowPlayer)3 BlockFace (org.bukkit.block.BlockFace)3 GlowBlockState (net.glowstone.block.GlowBlockState)2 BlockEntity (net.glowstone.block.entity.BlockEntity)2 GlowItem (net.glowstone.entity.objects.GlowItem)2 Block (org.bukkit.block.Block)2 Entity (org.bukkit.entity.Entity)2 Action (org.bukkit.event.block.Action)2 PlayerInteractEvent (org.bukkit.event.player.PlayerInteractEvent)2 MaterialData (org.bukkit.material.MaterialData)2 Vector (org.bukkit.util.Vector)2 Title (com.destroystokyo.paper.Title)1 Message (com.flowpowered.network.Message)1 ByteBufUtils (com.flowpowered.network.util.ByteBufUtils)1 Preconditions (com.google.common.base.Preconditions)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1