Search in sources :

Example 1 with TreeType

use of org.spongepowered.api.data.type.TreeType in project SpongeCommon by SpongePowered.

the class MixinBlockLeaves method getStateWithData.

@Override
public Optional<BlockState> getStateWithData(IBlockState blockState, ImmutableDataManipulator<?, ?> manipulator) {
    if (manipulator instanceof ImmutableTreeData) {
        final TreeType treeType = ((ImmutableTreeData) manipulator).type().get();
        final BlockPlanks.EnumType type = TreeTypeResolver.getFor(treeType);
        if (blockState.getBlock() instanceof BlockOldLeaf) {
            if (treeType.equals(TreeTypes.OAK) || treeType.equals(TreeTypes.BIRCH) || treeType.equals(TreeTypes.SPRUCE) || treeType.equals(TreeTypes.JUNGLE)) {
                return Optional.of((BlockState) blockState.withProperty(BlockOldLeaf.VARIANT, type));
            }
        } else if (blockState.getBlock() instanceof BlockNewLeaf) {
            if (treeType.equals(TreeTypes.ACACIA) || treeType.equals(TreeTypes.DARK_OAK)) {
                return Optional.of((BlockState) blockState.withProperty(BlockNewLeaf.VARIANT, type));
            }
        }
        return Optional.empty();
    }
    if (manipulator instanceof ImmutableDecayableData) {
        final boolean decayable = ((ImmutableDecayableData) manipulator).decayable().get();
        return Optional.of((BlockState) blockState.withProperty(BlockLeaves.DECAYABLE, decayable));
    }
    return super.getStateWithData(blockState, manipulator);
}
Also used : TreeType(org.spongepowered.api.data.type.TreeType) BlockState(org.spongepowered.api.block.BlockState) IBlockState(net.minecraft.block.state.IBlockState) BlockNewLeaf(net.minecraft.block.BlockNewLeaf) ImmutableDecayableData(org.spongepowered.api.data.manipulator.immutable.block.ImmutableDecayableData) ImmutableTreeData(org.spongepowered.api.data.manipulator.immutable.block.ImmutableTreeData) BlockOldLeaf(net.minecraft.block.BlockOldLeaf) BlockPlanks(net.minecraft.block.BlockPlanks)

Example 2 with TreeType

use of org.spongepowered.api.data.type.TreeType in project SpongeCommon by SpongePowered.

the class MixinBlockLog method getStateWithData.

@Override
public Optional<BlockState> getStateWithData(IBlockState blockState, ImmutableDataManipulator<?, ?> manipulator) {
    if (manipulator instanceof ImmutableTreeData) {
        final TreeType treeType = ((ImmutableTreeData) manipulator).type().get();
        final BlockPlanks.EnumType type = TreeTypeResolver.getFor(treeType);
        return processLogType(blockState, type, treeType);
    } else if (manipulator instanceof ImmutableLogAxisData) {
        final LogAxis logAxis = ((ImmutableLogAxisData) manipulator).type().get();
        return Optional.of((BlockState) blockState.withProperty(BlockLog.LOG_AXIS, (BlockLog.EnumAxis) (Object) logAxis));
    }
    return super.getStateWithData(blockState, manipulator);
}
Also used : TreeType(org.spongepowered.api.data.type.TreeType) BlockState(org.spongepowered.api.block.BlockState) IBlockState(net.minecraft.block.state.IBlockState) ImmutableTreeData(org.spongepowered.api.data.manipulator.immutable.block.ImmutableTreeData) LogAxis(org.spongepowered.api.data.type.LogAxis) ImmutableLogAxisData(org.spongepowered.api.data.manipulator.immutable.block.ImmutableLogAxisData) BlockPlanks(net.minecraft.block.BlockPlanks)

Example 3 with TreeType

use of org.spongepowered.api.data.type.TreeType in project SpongeCommon by SpongePowered.

the class MixinBlockLog method getTreeData.

protected ImmutableTreeData getTreeData(IBlockState blockState) {
    BlockPlanks.EnumType type;
    if (blockState.getBlock() instanceof BlockOldLog) {
        type = blockState.getValue(BlockOldLog.VARIANT);
    } else if (blockState.getBlock() instanceof BlockNewLog) {
        type = blockState.getValue(BlockNewLog.VARIANT);
    } else {
        type = BlockPlanks.EnumType.OAK;
    }
    final TreeType treeType = TreeTypeResolver.getFor(type);
    return ImmutableDataCachingUtil.getManipulator(ImmutableSpongeTreeData.class, treeType);
}
Also used : TreeType(org.spongepowered.api.data.type.TreeType) BlockOldLog(net.minecraft.block.BlockOldLog) BlockNewLog(net.minecraft.block.BlockNewLog) BlockPlanks(net.minecraft.block.BlockPlanks)

Example 4 with TreeType

use of org.spongepowered.api.data.type.TreeType in project SpongeCommon by SpongePowered.

the class MixinBlockLog method getStateWithValue.

@Override
public <E> Optional<BlockState> getStateWithValue(IBlockState blockState, Key<? extends BaseValue<E>> key, E value) {
    if (key.equals(Keys.TREE_TYPE)) {
        final TreeType treeType = (TreeType) value;
        final BlockPlanks.EnumType type = TreeTypeResolver.getFor(treeType);
        return processLogType(blockState, type, treeType);
    } else if (key.equals(Keys.LOG_AXIS)) {
        return Optional.of((BlockState) blockState.withProperty(BlockLog.LOG_AXIS, (BlockLog.EnumAxis) value));
    }
    return super.getStateWithValue(blockState, key, value);
}
Also used : TreeType(org.spongepowered.api.data.type.TreeType) BlockState(org.spongepowered.api.block.BlockState) IBlockState(net.minecraft.block.state.IBlockState) BlockPlanks(net.minecraft.block.BlockPlanks)

Example 5 with TreeType

use of org.spongepowered.api.data.type.TreeType in project modules-extra by CubeEngine.

the class ChopListener method onChop.

@Listener
public void onChop(final ChangeBlockEvent.Break event, @First Player player) {
    if (!player.getItemInHand(HandTypes.MAIN_HAND).isPresent() || event.getCause().getContext().containsKey(EventContextKeys.PLAYER_SIMULATED)) {
        return;
    }
    ItemStack axe = player.getItemInHand(HandTypes.MAIN_HAND).orElse(null);
    if (axe == null || axe.getType() != DIAMOND_AXE || axe.get(Keys.ITEM_DURABILITY).orElse(0) <= 0 || !axe.get(Keys.ITEM_ENCHANTMENTS).orElse(emptyList()).contains(Enchantment.builder().type(EnchantmentTypes.PUNCH).level(5).build())) {
        return;
    }
    if (!module.use.check(player)) {
        return;
    }
    Sponge.getCauseStackManager().addContext(EventContextKeys.PLAYER_SIMULATED, player.getProfile());
    for (Transaction<BlockSnapshot> transaction : event.getTransactions()) {
        BlockType type = transaction.getOriginal().getState().getType();
        Location<World> orig = transaction.getOriginal().getLocation().get();
        BlockType belowType = orig.getBlockRelative(DOWN).getBlockType();
        if (isLog(type) && isSoil(belowType)) {
            TreeType treeType = transaction.getOriginal().get(TREE_TYPE).get();
            Set<Location<World>> treeBlocks = findTreeBlocks(orig, treeType);
            if (treeBlocks.isEmpty()) {
                return;
            }
            int logs = 0;
            int leaves = 0;
            Set<Location> saplings = new HashSet<>();
            for (Location<World> block : treeBlocks) {
                if (isLog(block.getBlockType())) {
                    if (!block.equals(orig)) {
                        block.getExtent().playSound(SoundTypes.BLOCK_WOOD_STEP, block.getPosition(), 1);
                    }
                    logs++;
                    block.setBlockType(BlockTypes.AIR);
                    BlockType belowTyp = block.getBlockRelative(DOWN).getBlockType();
                    if (isSoil(belowTyp)) {
                        saplings.add(block);
                    }
                }
                if (block.getBlockType() == LEAVES || block.getBlockType() == LEAVES2) {
                    block.setBlockType(BlockTypes.AIR);
                    // TODO leaves sound?
                    block.getExtent().playSound(SoundTypes.BLOCK_GRASS_STEP, block.getPosition(), 1);
                    leaves++;
                }
            }
            ItemStack log = ItemStack.builder().itemType(type.getItem().get()).quantity(logs).build();
            log.offer(TREE_TYPE, treeType);
            int apples = 0;
            if (treeType == JUNGLE) {
                leaves = leaves / 40;
            } else {
                if (treeType == DARK_OAK || treeType == OAK) {
                    apples = leaves / 200;
                }
                leaves = leaves / 20;
            }
            if (leaves == 0) {
                leaves = 1;
            }
            BlockState sapState = BlockTypes.SAPLING.getDefaultState().with(TREE_TYPE, treeType).get();
            if (this.module.autoplant.check(player)) {
                leaves -= saplings.size();
                leaves = Math.max(0, leaves);
                transaction.setCustom(sapState.snapshotFor(transaction.getOriginal().getLocation().get()));
                saplings.forEach(l -> l.setBlock(sapState));
            }
            final int uses = axe.get(Keys.ITEM_DURABILITY).get() - logs;
            axe.offer(Keys.ITEM_DURABILITY, uses);
            player.setItemInHand(HandTypes.MAIN_HAND, axe);
            World world = player.getWorld();
            Entity itemEntity;
            Sponge.getCauseStackManager().removeContext(EventContextKeys.PLAYER_SIMULATED);
            Sponge.getCauseStackManager().pushCause(player);
            if (apples > 0) {
                ItemStack apple = ItemStack.builder().itemType(APPLE).quantity(apples).build();
                itemEntity = world.createEntity(ITEM, orig.getPosition());
                itemEntity.offer(REPRESENTED_ITEM, apple.createSnapshot());
                world.spawnEntity(itemEntity);
            }
            if (leaves > 0) {
                ItemStack sap = ItemStack.builder().fromBlockState(sapState).build();
                sap.setQuantity(leaves);
                itemEntity = world.createEntity(ITEM, orig.getPosition());
                itemEntity.offer(REPRESENTED_ITEM, sap.createSnapshot());
                world.spawnEntity(itemEntity);
            }
            itemEntity = world.createEntity(ITEM, orig.getPosition());
            itemEntity.offer(REPRESENTED_ITEM, log.createSnapshot());
            world.spawnEntity(itemEntity);
            return;
        }
    }
}
Also used : TreeType(org.spongepowered.api.data.type.TreeType) Entity(org.spongepowered.api.entity.Entity) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) BlockState(org.spongepowered.api.block.BlockState) BlockType(org.spongepowered.api.block.BlockType) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Location(org.spongepowered.api.world.Location) HashSet(java.util.HashSet) Listener(org.spongepowered.api.event.Listener)

Aggregations

TreeType (org.spongepowered.api.data.type.TreeType)10 BlockPlanks (net.minecraft.block.BlockPlanks)6 BlockState (org.spongepowered.api.block.BlockState)6 IBlockState (net.minecraft.block.state.IBlockState)4 BlockNewLeaf (net.minecraft.block.BlockNewLeaf)3 BlockOldLeaf (net.minecraft.block.BlockOldLeaf)3 BlockType (org.spongepowered.api.block.BlockType)2 ImmutableTreeData (org.spongepowered.api.data.manipulator.immutable.block.ImmutableTreeData)2 LogAxis (org.spongepowered.api.data.type.LogAxis)2 Vector3d (com.flowpowered.math.vector.Vector3d)1 Vector3i (com.flowpowered.math.vector.Vector3i)1 TypeToken (com.google.common.reflect.TypeToken)1 Field (java.lang.reflect.Field)1 Instant (java.time.Instant)1 HashSet (java.util.HashSet)1 List (java.util.List)1 UUID (java.util.UUID)1 BlockNewLog (net.minecraft.block.BlockNewLog)1 BlockOldLog (net.minecraft.block.BlockOldLog)1 LanternKeyFactory.makeImmutableBoundedValueKey (org.lanternpowered.server.data.key.LanternKeyFactory.makeImmutableBoundedValueKey)1