Search in sources :

Example 11 with BlockType

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

the class MixinChunk_Tracker method addTrackedBlockPosition.

@Override
public void addTrackedBlockPosition(Block block, BlockPos pos, User user, PlayerTracker.Type trackerType) {
    if (this.world.isRemote) {
        return;
    }
    if (PhaseTracker.getInstance().getCurrentState().ignoresBlockTracking()) {
        // Don't track chunk gen
        return;
    }
    // Don't track fake players
    if (user instanceof EntityPlayerMP && SpongeImplHooks.isFakePlayer((EntityPlayerMP) user)) {
        return;
    }
    if (!SpongeHooks.getActiveConfig((WorldServer) this.world).getConfig().getBlockTracking().getBlockBlacklist().contains(((BlockType) block).getId())) {
        SpongeHooks.logBlockTrack(this.world, block, pos, user, true);
    } else {
        SpongeHooks.logBlockTrack(this.world, block, pos, user, false);
    }
    final IMixinWorldInfo worldInfo = (IMixinWorldInfo) this.world.getWorldInfo();
    final int indexForUniqueId = worldInfo.getIndexForUniqueId(user.getUniqueId());
    if (pos.getY() <= 255) {
        short blockPos = blockPosToShort(pos);
        final PlayerTracker playerTracker = this.trackedShortBlockPositions.get(blockPos);
        if (playerTracker != null) {
            if (trackerType == PlayerTracker.Type.OWNER) {
                playerTracker.ownerIndex = indexForUniqueId;
                playerTracker.notifierIndex = indexForUniqueId;
            } else {
                playerTracker.notifierIndex = indexForUniqueId;
            }
        } else {
            this.trackedShortBlockPositions.put(blockPos, new PlayerTracker(indexForUniqueId, trackerType));
        }
    } else {
        int blockPos = blockPosToInt(pos);
        final PlayerTracker playerTracker = this.trackedIntBlockPositions.get(blockPos);
        if (playerTracker != null) {
            if (trackerType == PlayerTracker.Type.OWNER) {
                playerTracker.ownerIndex = indexForUniqueId;
            } else {
                playerTracker.notifierIndex = indexForUniqueId;
            }
        } else {
            this.trackedIntBlockPositions.put(blockPos, new PlayerTracker(indexForUniqueId, trackerType));
        }
    }
}
Also used : BlockType(org.spongepowered.api.block.BlockType) PlayerTracker(org.spongepowered.common.entity.PlayerTracker) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) WorldServer(net.minecraft.world.WorldServer)

Example 12 with BlockType

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

the class SpongeItemStackBuilder method fromBlockSnapshot.

@Override
public ItemStack.Builder fromBlockSnapshot(BlockSnapshot blockSnapshot) {
    checkNotNull(blockSnapshot, "The snapshot was null!");
    reset();
    final BlockType blockType = blockSnapshot.getState().getType();
    final Optional<ItemType> itemType = blockType.getItem();
    itemType(itemType.orElseThrow(() -> new IllegalArgumentException("ItemType not found for block type: " + blockType.getId())));
    quantity(1);
    if (blockSnapshot instanceof SpongeBlockSnapshot) {
        final Block block = (Block) blockType;
        this.damageValue = block.damageDropped((IBlockState) blockSnapshot.getState());
        final Optional<NBTTagCompound> compound = ((SpongeBlockSnapshot) blockSnapshot).getCompound();
        if (compound.isPresent()) {
            this.compound = new NBTTagCompound();
            this.compound.setTag(NbtDataUtil.BLOCK_ENTITY_TAG, compound.get());
        }
    // todo probably needs more testing, but this'll do donkey...
    } else {
        // TODO handle through the API specifically handling the rest of the data stuff
        blockSnapshot.getContainers().forEach(this::itemData);
    }
    return this;
}
Also used : SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) IBlockState(net.minecraft.block.state.IBlockState) BlockType(org.spongepowered.api.block.BlockType) ItemType(org.spongepowered.api.item.ItemType) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Block(net.minecraft.block.Block)

Example 13 with BlockType

use of org.spongepowered.api.block.BlockType 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 14 with BlockType

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

the class AbstractTileBuilder method buildContent.

@SuppressWarnings("unchecked")
@Override
protected Optional<T> buildContent(DataView container) throws InvalidDataException {
    checkNotNull(container);
    if (!container.contains(DataQueries.TILE_TYPE, DataQueries.WORLD, Queries.POSITION_X, Queries.POSITION_Y, Queries.POSITION_Z)) {
        return Optional.empty();
    }
    String worldName = container.getString(DataQueries.WORLD).get();
    Optional<World> worldOptional = Sponge.getGame().getServer().getWorld(worldName);
    if (!worldOptional.isPresent()) {
        throw new InvalidDataException("The provided container references a world that does not exist!");
    }
    Class<? extends TileEntity> clazz = TileEntity.REGISTRY.getObject(new ResourceLocation(container.getString(DataQueries.TILE_TYPE).get()));
    if (clazz == null) {
        // basically we didn't manage to find the class and the class isn't even registered with MC
        return Optional.empty();
    }
    BlockType type = classToTypeMap.get(clazz);
    if (type == null) {
        // TODO throw exception maybe?
        return Optional.empty();
    }
    // Now we should be ready to actually translate the TileEntity with the right block.
    final int x = container.getInt(DataQueries.X_POS).get();
    final int y = container.getInt(DataQueries.Y_POS).get();
    final int z = container.getInt(DataQueries.Z_POS).get();
    worldOptional.get().getLocation(x, y, z).setBlockType(type);
    BlockPos pos = new BlockPos(x, y, z);
    TileEntity tileEntity = ((net.minecraft.world.World) worldOptional.get()).getTileEntity(pos);
    if (tileEntity == null) {
        // TODO throw exception maybe?
        return Optional.empty();
    }
    // We really need to validate only after the implementing class deems it ready...
    tileEntity.invalidate();
    return Optional.of((T) tileEntity);
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) BlockType(org.spongepowered.api.block.BlockType) ResourceLocation(net.minecraft.util.ResourceLocation) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) BlockPos(net.minecraft.util.math.BlockPos) World(org.spongepowered.api.world.World)

Example 15 with BlockType

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

the class AntiRailDupeListener method onPistonMove.

@Listener
public void onPistonMove(ChangeBlockEvent event, @Named(NamedCause.SOURCE) Piston piston) {
    event.getTransactions().stream().map(Transaction::getFinal).forEach(block -> {
        BlockType finalType = block.getState().getType();
        if (RAIL_BLOCKS.contains(finalType)) {
            Location<World> location = block.getLocation().get();
            Task.builder().execute(() -> {
                location.setBlockType(BlockTypes.AIR, Cause.source(SkreePlugin.container()).build());
            }).delayTicks(1).submit(SkreePlugin.inst());
        }
    });
}
Also used : BlockType(org.spongepowered.api.block.BlockType) World(org.spongepowered.api.world.World) Listener(org.spongepowered.api.event.Listener)

Aggregations

BlockType (org.spongepowered.api.block.BlockType)91 World (org.spongepowered.api.world.World)34 BlockState (org.spongepowered.api.block.BlockState)23 Listener (org.spongepowered.api.event.Listener)21 Location (org.spongepowered.api.world.Location)17 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)16 ItemStack (org.spongepowered.api.item.inventory.ItemStack)14 Vector3i (com.flowpowered.math.vector.Vector3i)12 Optional (java.util.Optional)12 Player (org.spongepowered.api.entity.living.player.Player)12 List (java.util.List)10 Set (java.util.Set)10 Direction (org.spongepowered.api.util.Direction)10 Vector3d (com.flowpowered.math.vector.Vector3d)9 ArrayList (java.util.ArrayList)9 Keys (org.spongepowered.api.data.key.Keys)9 Sponge (org.spongepowered.api.Sponge)8 LanternBlockType (org.lanternpowered.server.block.LanternBlockType)7 Text (org.spongepowered.api.text.Text)7 HashMap (java.util.HashMap)6