Search in sources :

Example 51 with BlockState

use of org.spongepowered.api.block.BlockState in project LanternServer by LanternPowered.

the class LanternBlockVolumeWorker method merge.

@Override
public void merge(BlockVolume second, BlockVolumeMerger merger, MutableBlockVolume destination) {
    final Vector3i offsetSecond = align(second);
    final int xOffsetSecond = offsetSecond.getX();
    final int yOffsetSecond = offsetSecond.getY();
    final int zOffsetSecond = offsetSecond.getZ();
    final Vector3i offsetDestination = align(destination);
    final int xOffsetDestination = offsetDestination.getX();
    final int yOffsetDestination = offsetDestination.getY();
    final int zOffsetDestination = offsetDestination.getZ();
    final UnmodifiableBlockVolume firstUnmodifiableVolume = this.volume.getUnmodifiableBlockView();
    final int xMin = firstUnmodifiableVolume.getBlockMin().getX();
    final int yMin = firstUnmodifiableVolume.getBlockMin().getY();
    final int zMin = firstUnmodifiableVolume.getBlockMin().getZ();
    final int xMax = firstUnmodifiableVolume.getBlockMax().getX();
    final int yMax = firstUnmodifiableVolume.getBlockMax().getY();
    final int zMax = firstUnmodifiableVolume.getBlockMax().getZ();
    final UnmodifiableBlockVolume secondUnmodifiableVolume = second.getUnmodifiableBlockView();
    for (int z = zMin; z <= zMax; z++) {
        for (int y = yMin; y <= yMax; y++) {
            for (int x = xMin; x <= xMax; x++) {
                final BlockState block = merger.merge(firstUnmodifiableVolume, x, y, z, secondUnmodifiableVolume, x + xOffsetSecond, y + yOffsetSecond, z + zOffsetSecond);
                destination.setBlock(x + xOffsetDestination, y + yOffsetDestination, z + zOffsetDestination, block);
            }
        }
    }
}
Also used : BlockState(org.spongepowered.api.block.BlockState) Vector3i(com.flowpowered.math.vector.Vector3i) UnmodifiableBlockVolume(org.spongepowered.api.world.extent.UnmodifiableBlockVolume)

Example 52 with BlockState

use of org.spongepowered.api.block.BlockState in project LanternServer by LanternPowered.

the class LanternBlockVolumeWorker method map.

@Override
public void map(BlockVolumeMapper mapper, MutableBlockVolume destination) {
    final Vector3i offset = align(destination);
    final int xOffset = offset.getX();
    final int yOffset = offset.getY();
    final int zOffset = offset.getZ();
    final UnmodifiableBlockVolume unmodifiableVolume = this.volume.getUnmodifiableBlockView();
    final int xMin = unmodifiableVolume.getBlockMin().getX();
    final int yMin = unmodifiableVolume.getBlockMin().getY();
    final int zMin = unmodifiableVolume.getBlockMin().getZ();
    final int xMax = unmodifiableVolume.getBlockMax().getX();
    final int yMax = unmodifiableVolume.getBlockMax().getY();
    final int zMax = unmodifiableVolume.getBlockMax().getZ();
    for (int z = zMin; z <= zMax; z++) {
        for (int y = yMin; y <= yMax; y++) {
            for (int x = xMin; x <= xMax; x++) {
                final BlockState block = mapper.map(unmodifiableVolume, x, y, z);
                destination.setBlock(x + xOffset, y + yOffset, z + zOffset, block);
            }
        }
    }
}
Also used : BlockState(org.spongepowered.api.block.BlockState) Vector3i(com.flowpowered.math.vector.Vector3i) UnmodifiableBlockVolume(org.spongepowered.api.world.extent.UnmodifiableBlockVolume)

Example 53 with BlockState

use of org.spongepowered.api.block.BlockState in project Nucleus by NucleusPowered.

the class SellCommand method executeCommand.

@Override
public CommandResult executeCommand(final Player src, CommandContext args) throws Exception {
    // Get the item in the hand.
    ItemStack is = src.getItemInHand(HandTypes.MAIN_HAND).orElseThrow(() -> new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.generalerror.handempty")));
    String id;
    Optional<BlockState> blockState = is.get(Keys.ITEM_BLOCKSTATE);
    id = blockState.map(blockState1 -> blockState1.getId().toLowerCase()).orElseGet(() -> is.getType().getId());
    ItemDataNode node = itemDataService.getDataForItem(id);
    final double sellPrice = node.getServerSellPrice();
    if (sellPrice < 0) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.itemsell.notforselling"));
        return CommandResult.empty();
    }
    // Get the cost.
    final int amt = is.getQuantity();
    final double overallCost = sellPrice * amt;
    if (econHelper.depositInPlayer(src, overallCost, false)) {
        src.setItemInHand(HandTypes.MAIN_HAND, null);
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.itemsell.summary", Text.of(amt), Text.of(is), Text.of(econHelper.getCurrencySymbol(overallCost))));
        return CommandResult.success();
    }
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.itemsell.error", Text.of(is)));
    return CommandResult.empty();
}
Also used : BlockState(org.spongepowered.api.block.BlockState) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ItemDataNode(io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode)

Example 54 with BlockState

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

the class SpongeBlockSnapshot method restore.

@Override
public boolean restore(final boolean force, final BlockChangeFlag flag) {
    // TODO - rewrite with the PhaseTracker being the hook or use SpongeImplHooks to do the restore.
    final Optional<ServerLevel> optionalWorld = Optional.ofNullable(this.world.get());
    if (!optionalWorld.isPresent()) {
        return false;
    }
    final ServerLevel world = optionalWorld.get();
    // this way we guarantee an exit.
    try (final PhaseContext<?> context = BlockPhase.State.RESTORING_BLOCKS.createPhaseContext(PhaseTracker.SERVER)) {
        context.buildAndSwitch();
        final BlockPos pos = VecHelper.toBlockPos(this.pos);
        if (!net.minecraft.world.level.Level.isInWorldBounds(pos)) {
            // Invalid position. Inline this check
            return false;
        }
        final net.minecraft.world.level.block.state.BlockState current = world.getBlockState(pos);
        final net.minecraft.world.level.block.state.BlockState replaced = (net.minecraft.world.level.block.state.BlockState) this.blockState;
        if (!force && (current.getBlock() != replaced.getBlock() || current != replaced)) {
            return false;
        }
        // being created during block change removals
        if (!current.is(((net.minecraft.world.level.block.state.BlockState) this.blockState).getBlock())) {
            world.removeBlockEntity(pos);
        }
        world.setBlock(pos, replaced, BlockChangeFlagManager.andNotifyClients(flag).getRawFlag());
        if (this.compound != null) {
            @Nullable BlockEntity te = world.getBlockEntity(pos);
            if (te != null) {
                te.load((net.minecraft.world.level.block.state.BlockState) this.blockState, this.compound);
            } else {
                // In cases like this, we need to directly just say "fuck it" and deserialize from the compound directly.
                try {
                    te = BlockEntity.loadStatic((net.minecraft.world.level.block.state.BlockState) this.blockState, this.compound);
                    if (te != null) {
                        world.getChunk(pos).setBlockEntity(pos, te);
                    }
                } catch (final Exception e) {
                    // Seriously? The mod should be broken then.
                    final PrettyPrinter printer = new PrettyPrinter(60).add("Unable to restore").centre().hr().add("A mod is not correctly deserializing a TileEntity that is being restored. ").addWrapped(60, "Note that this is not the fault of Sponge. Sponge is understanding that " + "a block is supposed to have a TileEntity, but the mod is breaking the contract" + "on how to re-create the tile entity. Please open an issue with the offending mod.").add("Here's the provided compound:");
                    printer.add();
                    try {
                        printer.addWrapped(80, "%s : %s", "This compound", this.compound);
                    } catch (final Throwable error) {
                        printer.addWrapped(80, "Unable to get the string of this compound. Printing out some of the entries to better assist");
                    }
                    printer.add().add("Desired World: " + this.worldKey).add("Position: " + this.pos).add("Desired BlockState: " + this.blockState);
                    printer.add();
                    printer.log(SpongeCommon.logger(), Level.ERROR);
                    // I mean, I guess. the block was set up, but not the tile entity.
                    return true;
                }
            }
            if (te != null) {
                te.setChanged();
            }
        }
        // Finally, mark the location as being updated.
        world.getChunkSource().blockChanged(pos);
        return true;
    }
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) PrettyPrinter(org.spongepowered.common.util.PrettyPrinter) BlockState(org.spongepowered.api.block.BlockState) BlockPos(net.minecraft.core.BlockPos) Nullable(org.checkerframework.checker.nullness.qual.Nullable) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 55 with BlockState

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

the class SpongeBlockEntityArchetype method apply.

@Override
public Optional<BlockEntity> apply(final ServerLocation location) {
    final BlockState currentState = location.block();
    final Block currentBlock = ((net.minecraft.world.level.block.state.BlockState) currentState).getBlock();
    final Block newBlock = ((net.minecraft.world.level.block.state.BlockState) this.blockState).getBlock();
    final Level minecraftWorld = (net.minecraft.world.level.Level) location.world();
    final BlockPos blockpos = VecHelper.toBlockPos(location);
    if (currentBlock != newBlock) {
        ((org.spongepowered.api.world.World) minecraftWorld).setBlock(blockpos.getX(), blockpos.getY(), blockpos.getZ(), this.blockState, BlockChangeFlags.ALL);
    }
    final CompoundTag compound = this.compound.copy();
    final net.minecraft.world.level.block.entity.@Nullable BlockEntity tileEntity = minecraftWorld.getBlockEntity(blockpos);
    if (tileEntity == null) {
        return Optional.empty();
    }
    compound.putInt(Constants.TileEntity.X_POS, blockpos.getX());
    compound.putInt(Constants.TileEntity.Y_POS, blockpos.getY());
    compound.putInt(Constants.TileEntity.Z_POS, blockpos.getZ());
    tileEntity.load((net.minecraft.world.level.block.state.BlockState) currentState, compound);
    tileEntity.clearCache();
    return Optional.of((org.spongepowered.api.block.entity.BlockEntity) tileEntity);
}
Also used : BlockState(org.spongepowered.api.block.BlockState) Block(net.minecraft.world.level.block.Block) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) CompoundTag(net.minecraft.nbt.CompoundTag) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

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