Search in sources :

Example 26 with BlockState

use of org.spongepowered.api.block.BlockState in project SpongeCommon by SpongePowered.

the class MesaBiomeGenerationPopulator method performOnColumn.

public void performOnColumn(Random p_180622_2_, MutableBlockVolume p_180622_3_, World world, int p_180622_4_, int p_180622_5_, double p_180622_6_) {
    double d5 = 0.0D;
    int k;
    int l;
    if (this.hasHills) {
        k = (p_180622_4_ & -16) + (p_180622_5_ & 15);
        l = (p_180622_5_ & -16) + (p_180622_4_ & 15);
        double d1 = Math.min(Math.abs(p_180622_6_), this.noise1.getValue(k * 0.25D, l * 0.25D));
        if (d1 > 0.0D) {
            double d2 = 0.001953125D;
            double d3 = Math.abs(this.noise2.getValue(k * d2, l * d2));
            d5 = d1 * d1 * 2.5D;
            double d4 = Math.ceil(d3 * 50.0D) + 14.0D;
            if (d5 > d4) {
                d5 = d4;
            }
            d5 += 64.0D;
        }
    }
    k = p_180622_5_;
    l = p_180622_4_;
    int seaLevel = world.getSeaLevel();
    IBlockState iblockstate = Blocks.STAINED_HARDENED_CLAY.getDefaultState();
    IBlockState iblockstate3 = Blocks.STAINED_HARDENED_CLAY.getDefaultState();
    int i1 = (int) (p_180622_6_ / 3.0D + 3.0D + p_180622_2_.nextDouble() * 0.25D);
    boolean flag1 = Math.cos(p_180622_6_ / 3.0D * Math.PI) > 0.0D;
    int j1 = -1;
    boolean flag2 = false;
    for (int k1 = 255; k1 >= 0; --k1) {
        if (((IBlockState) p_180622_3_.getBlock(l, k1, k)).getMaterial() == Material.AIR && k1 < (int) d5) {
            p_180622_3_.setBlock(l, k1, k, (BlockState) Blocks.STONE.getDefaultState());
        }
        if (k1 <= p_180622_2_.nextInt(5)) {
            p_180622_3_.setBlock(l, k1, k, (BlockState) Blocks.BEDROCK.getDefaultState());
        } else {
            IBlockState iblockstate1 = (IBlockState) p_180622_3_.getBlock(l, k1, k);
            if (iblockstate1.getMaterial() == Material.AIR) {
                j1 = -1;
            } else if (iblockstate1.getBlock() == Blocks.STONE) {
                IBlockState iblockstate2;
                if (j1 == -1) {
                    flag2 = false;
                    if (i1 <= 0) {
                        iblockstate = null;
                        iblockstate3 = Blocks.STONE.getDefaultState();
                    } else if (k1 >= seaLevel - 4 && k1 <= seaLevel + 1) {
                        iblockstate = Blocks.STAINED_HARDENED_CLAY.getDefaultState();
                        iblockstate3 = Blocks.STAINED_HARDENED_CLAY.getDefaultState();
                    }
                    if (k1 < seaLevel && (iblockstate == null || iblockstate.getMaterial() == Material.AIR)) {
                        iblockstate = Blocks.WATER.getDefaultState();
                    }
                    j1 = i1 + Math.max(0, k1 - seaLevel);
                    if (k1 >= seaLevel - 1) {
                        if (this.hasTrees && k1 > 86 + i1 * 2) {
                            if (flag1) {
                                p_180622_3_.setBlock(l, k1, k, (BlockState) Blocks.DIRT.getDefaultState().withProperty(BlockDirt.VARIANT, BlockDirt.DirtType.COARSE_DIRT));
                            } else {
                                p_180622_3_.setBlock(l, k1, k, (BlockState) Blocks.GRASS.getDefaultState());
                            }
                        } else if (k1 > seaLevel + 3 + i1) {
                            if (k1 >= 64 && k1 <= 127) {
                                if (flag1) {
                                    iblockstate2 = Blocks.HARDENED_CLAY.getDefaultState();
                                } else {
                                    iblockstate2 = this.func_180629_a(p_180622_4_, k1, p_180622_5_);
                                }
                            } else {
                                iblockstate2 = Blocks.STAINED_HARDENED_CLAY.getDefaultState().withProperty(BlockColored.COLOR, EnumDyeColor.ORANGE);
                            }
                            p_180622_3_.setBlock(l, k1, k, (BlockState) iblockstate2);
                        } else {
                            p_180622_3_.setBlock(l, k1, k, (BlockState) Blocks.SAND.getDefaultState().withProperty(BlockSand.VARIANT, BlockSand.EnumType.RED_SAND));
                            flag2 = true;
                        }
                    } else {
                        p_180622_3_.setBlock(l, k1, k, (BlockState) iblockstate3);
                        if (iblockstate3.getBlock() == Blocks.STAINED_HARDENED_CLAY) {
                            p_180622_3_.setBlock(l, k1, k, (BlockState) iblockstate3.getBlock().getDefaultState().withProperty(BlockColored.COLOR, EnumDyeColor.ORANGE));
                        }
                    }
                } else if (j1 > 0) {
                    --j1;
                    if (flag2) {
                        IBlockState clay = Blocks.STAINED_HARDENED_CLAY.getDefaultState().withProperty(BlockColored.COLOR, EnumDyeColor.ORANGE);
                        p_180622_3_.setBlock(l, k1, k, (BlockState) clay);
                    } else {
                        iblockstate2 = this.func_180629_a(p_180622_4_, k1, p_180622_5_);
                        p_180622_3_.setBlock(l, k1, k, (BlockState) iblockstate2);
                    }
                }
            }
        }
    }
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) BlockState(org.spongepowered.api.block.BlockState) IBlockState(net.minecraft.block.state.IBlockState)

Example 27 with BlockState

use of org.spongepowered.api.block.BlockState in project SpongeCommon by SpongePowered.

the class SpongeBlockSnapshotBuilder method buildContent.

@Override
protected Optional<BlockSnapshot> buildContent(DataView container) throws InvalidDataException {
    if (!container.contains(DataQueries.BLOCK_STATE, Queries.WORLD_ID, DataQueries.SNAPSHOT_WORLD_POSITION)) {
        return Optional.empty();
    }
    checkDataExists(container, DataQueries.BLOCK_STATE);
    checkDataExists(container, Queries.WORLD_ID);
    final SpongeBlockSnapshotBuilder builder = new SpongeBlockSnapshotBuilder();
    final UUID worldUuid = UUID.fromString(container.getString(Queries.WORLD_ID).get());
    final Vector3i coordinate = DataUtil.getPosition3i(container);
    Optional<String> creatorUuid = container.getString(Queries.CREATOR_ID);
    Optional<String> notifierUuid = container.getString(Queries.NOTIFIER_ID);
    // We now reconstruct the custom data and all extra data.
    final BlockState blockState = container.getSerializable(DataQueries.BLOCK_STATE, BlockState.class).get();
    BlockState extendedState = null;
    if (container.contains(DataQueries.BLOCK_EXTENDED_STATE)) {
        extendedState = container.getSerializable(DataQueries.BLOCK_EXTENDED_STATE, BlockState.class).get();
    } else {
        extendedState = blockState;
    }
    builder.blockState(blockState).extendedState(extendedState).position(coordinate).worldId(worldUuid);
    if (creatorUuid.isPresent()) {
        builder.creator(UUID.fromString(creatorUuid.get()));
    }
    if (notifierUuid.isPresent()) {
        builder.notifier(UUID.fromString(notifierUuid.get()));
    }
    Optional<DataView> unsafeCompound = container.getView(DataQueries.UNSAFE_NBT);
    final NBTTagCompound compound = unsafeCompound.isPresent() ? NbtTranslator.getInstance().translateData(unsafeCompound.get()) : null;
    if (compound != null) {
        builder.unsafeNbt(compound);
    }
    if (container.contains(DataQueries.SNAPSHOT_TILE_DATA)) {
        final List<DataView> dataViews = container.getViewList(DataQueries.SNAPSHOT_TILE_DATA).get();
        DataUtil.deserializeImmutableManipulatorList(dataViews).stream().forEach(builder::add);
    }
    return Optional.of(new SpongeBlockSnapshot(builder));
}
Also used : DataView(org.spongepowered.api.data.DataView) BlockState(org.spongepowered.api.block.BlockState) Vector3i(com.flowpowered.math.vector.Vector3i) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) UUID(java.util.UUID)

Example 28 with BlockState

use of org.spongepowered.api.block.BlockState in project SpongeCommon by SpongePowered.

the class SpongeTileEntityArchetype method apply.

@Override
public Optional<org.spongepowered.api.block.tileentity.TileEntity> apply(Location<World> location) {
    final BlockState currentState = location.getBlock();
    final Block currentBlock = BlockUtil.toBlock(currentState);
    final Block newBlock = BlockUtil.toBlock(this.blockState);
    final net.minecraft.world.World minecraftWorld = (net.minecraft.world.World) location.getExtent();
    BlockPos blockpos = VecHelper.toBlockPos(location);
    if (currentBlock != newBlock) {
        ((World) minecraftWorld).setBlock(blockpos.getX(), blockpos.getY(), blockpos.getZ(), this.blockState, BlockChangeFlags.ALL);
    }
    final NBTTagCompound compound = this.data.copy();
    TileEntity tileEntity = minecraftWorld.getTileEntity(blockpos);
    if (tileEntity == null) {
        return Optional.empty();
    }
    compound.setInteger("x", blockpos.getX());
    compound.setInteger("y", blockpos.getY());
    compound.setInteger("z", blockpos.getZ());
    tileEntity.readFromNBT(compound);
    tileEntity.markDirty();
    return Optional.of((org.spongepowered.api.block.tileentity.TileEntity) tileEntity);
}
Also used : NBTTagCompound(net.minecraft.nbt.NBTTagCompound) World(org.spongepowered.api.world.World) TileEntity(net.minecraft.tileentity.TileEntity) BlockState(org.spongepowered.api.block.BlockState) Block(net.minecraft.block.Block) BlockPos(net.minecraft.util.math.BlockPos)

Example 29 with BlockState

use of org.spongepowered.api.block.BlockState in project SpongeCommon by SpongePowered.

the class SpongeBlockStateMetaContentUpdater method update.

@SuppressWarnings("deprecation")
@Override
public DataView update(DataView content) {
    // Right, so we have to get the block type id
    final String blockTypeId = content.getString(DataQueries.BLOCK_TYPE).get();
    // Check if it's there....
    final Optional<BlockType> blockType = Sponge.getRegistry().getType(BlockType.class, blockTypeId);
    if (!blockType.isPresent()) {
        // sure, throw an exception to throw down the chain
        throw new InvalidDataException("Could not find a block type for the given id: " + blockTypeId);
    }
    // Get the meta, if it wasn't available, just default to the default block state.
    final int meta = content.getInt(DataQueries.BLOCK_STATE_UNSAFE_META).orElse(0);
    // Get the block type
    final BlockType type = blockType.get();
    // Cast to internal and get the block state from damage value, this is purely
    // implementation of minecraft, mods may change this in the future, not really known how
    // they will handle it?
    final IBlockState blockState = ((Block) type).getStateFromMeta(meta);
    // Now that we have the actual block state, delete the old data
    content.remove(DataQueries.BLOCK_TYPE);
    content.remove(DataQueries.BLOCK_STATE_UNSAFE_META);
    // Cast to the API state to get the id
    final BlockState apiState = (BlockState) blockState;
    // set the id
    content.set(DataQueries.BLOCK_STATE, apiState.getId());
    // set the version!!
    content.set(Queries.CONTENT_VERSION, 2);
    // Presto!
    return content;
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) BlockState(org.spongepowered.api.block.BlockState) IBlockState(net.minecraft.block.state.IBlockState) BlockType(org.spongepowered.api.block.BlockType) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) Block(net.minecraft.block.Block)

Example 30 with BlockState

use of org.spongepowered.api.block.BlockState in project Skree by Skelril.

the class GoldRushListener method onPlayerInteractEvent.

@Listener(order = Order.FIRST)
public void onPlayerInteractEvent(InteractBlockEvent.Secondary.MainHand event, @Root Player player) {
    Optional<GoldRushInstance> optInst = manager.getApplicableZone(player);
    if (!optInst.isPresent()) {
        return;
    }
    GoldRushInstance inst = optInst.get();
    BlockSnapshot snapshot = event.getTargetBlock();
    BlockState state = snapshot.getState();
    if (!snapshot.getLocation().isPresent()) {
        return;
    }
    Location<World> targetBlock = snapshot.getLocation().get();
    if (state.getType() == BlockTypes.WALL_SIGN && inst.getLockLocations().contains(targetBlock)) {
        Optional<TileEntity> optTileEnt = snapshot.getLocation().get().getTileEntity();
        if (!optTileEnt.isPresent()) {
            return;
        }
        TileEntity tileEntity = optTileEnt.get();
        Optional<List<Text>> optTexts = tileEntity.get(Keys.SIGN_LINES);
        if (!optTexts.isPresent()) {
            return;
        }
        List<Text> texts = optTexts.get();
        boolean unlocked = false;
        String text = texts.get(1).toPlain().toLowerCase();
        NonNullList<ItemStack> itemStacks = tf(player).inventory.mainInventory;
        for (int i = 0; i < itemStacks.size(); ++i) {
            ItemStack is = itemStacks.get(i);
            if (is == ItemStack.EMPTY || is.getItem() != CustomItemTypes.GOLD_RUSH_KEY) {
                continue;
            }
            if (text.contains("blue")) {
                if (is.getItemDamage() != 1) {
                    continue;
                }
            } else if (text.contains("red")) {
                if (is.getItemDamage() != 0) {
                    continue;
                }
            } else {
                continue;
            }
            unlocked = true;
            itemStacks.set(i, ItemStack.EMPTY);
            break;
        }
        if (unlocked) {
            tf(player).inventoryContainer.detectAndSendChanges();
            texts.set(2, Text.of("Locked"));
            texts.set(3, Text.of("- Unlocked -"));
            tileEntity.offer(Keys.SIGN_LINES, texts);
        }
    } else if (state.getType() == BlockTypes.LEVER) {
        Task.builder().execute(() -> {
            if (inst.checkLevers()) {
                inst.completeGame();
            }
        }).delayTicks(1).submit(SkreePlugin.inst());
    } else if (targetBlock.equals(inst.getRewardChestLoc()) && inst.isComplete()) {
        event.setUseItemResult(Tristate.FALSE);
        event.setUseBlockResult(Tristate.FALSE);
        player.sendMessage(Text.of(TextColors.YELLOW, "You have successfully robbed the bank!"));
        inst.payPlayer(player);
    } else if (!inst.isLocked()) {
        if (state.getType() == BlockTypes.STONE_BUTTON) {
            inst.tryToStart();
        }
    }
}
Also used : BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) Text(org.spongepowered.api.text.Text) World(org.spongepowered.api.world.World) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) BlockState(org.spongepowered.api.block.BlockState) NonNullList(net.minecraft.util.NonNullList) ItemStack(net.minecraft.item.ItemStack) Listener(org.spongepowered.api.event.Listener)

Aggregations

BlockState (org.spongepowered.api.block.BlockState)133 World (org.spongepowered.api.world.World)39 IBlockState (net.minecraft.block.state.IBlockState)29 BlockType (org.spongepowered.api.block.BlockType)27 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)22 Direction (org.spongepowered.api.util.Direction)21 Optional (java.util.Optional)20 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)20 Vector3i (com.flowpowered.math.vector.Vector3i)19 Location (org.spongepowered.api.world.Location)18 ItemStack (org.spongepowered.api.item.inventory.ItemStack)17 LocatableBlock (org.spongepowered.api.world.LocatableBlock)14 Sponge (org.spongepowered.api.Sponge)13 ItemType (org.spongepowered.api.item.ItemType)13 ArrayList (java.util.ArrayList)12 Player (org.spongepowered.api.entity.living.player.Player)12 List (java.util.List)11 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)11 Vector3d (com.flowpowered.math.vector.Vector3d)10 Listener (org.spongepowered.api.event.Listener)10