Search in sources :

Example 36 with BlockType

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

the class PlayerInteractionHandler method handleBrokenBlock.

private void handleBrokenBlock() {
    final Location<World> location = new Location<>(this.player.getWorld(), this.diggingBlock);
    final CauseStack causeStack = CauseStack.current();
    try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
        frame.pushCause(this.player);
        // Add context
        frame.addContext(EventContextKeys.PLAYER, this.player);
        frame.addContext(ContextKeys.INTERACTION_LOCATION, location);
        frame.addContext(ContextKeys.BLOCK_LOCATION, location);
        final BehaviorContextImpl context = new BehaviorContextImpl(causeStack);
        final BlockState blockState = location.getBlock();
        final LanternBlockType blockType = (LanternBlockType) blockState.getType();
        if (context.process(blockType.getPipeline().pipeline(BreakBlockBehavior.class), (ctx, behavior) -> behavior.tryBreak(blockType.getPipeline(), ctx)).isSuccess()) {
            context.accept();
            this.diggingBlock = null;
            this.diggingBlockType = null;
        } else {
            context.revert();
            // TODO: Resend tile entity data, action data, ... ???
            this.player.sendBlockChange(this.diggingBlock, blockState);
        }
        if (this.lastBreakState != -1) {
            sendBreakUpdate(-1);
        }
    }
}
Also used : MessagePlayOutEntityAnimation(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityAnimation) LanternKeys(org.lanternpowered.server.data.key.LanternKeys) Keys(org.spongepowered.api.data.key.Keys) EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) LanternBlockType(org.lanternpowered.server.block.LanternBlockType) MessagePlayInPlayerSwingArm(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerSwingArm) BehaviorContextImpl(org.lanternpowered.server.behavior.BehaviorContextImpl) Vector3d(com.flowpowered.math.vector.Vector3d) MessagePlayInOutFinishUsingItem(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutFinishUsingItem) MessagePlayInPlayerDigging(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerDigging) HandType(org.spongepowered.api.data.type.HandType) MinimumUseDurationProperty(org.lanternpowered.server.item.property.MinimumUseDurationProperty) GameModes(org.spongepowered.api.entity.living.player.gamemode.GameModes) BreakBlockBehavior(org.lanternpowered.server.block.behavior.types.BreakBlockBehavior) ItemStack(org.spongepowered.api.item.inventory.ItemStack) BehaviorContext(org.lanternpowered.server.behavior.BehaviorContext) CauseStack(org.lanternpowered.server.event.CauseStack) InteractWithItemBehavior(org.lanternpowered.server.item.behavior.types.InteractWithItemBehavior) MessagePlayOutBlockBreakAnimation(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutBlockBreakAnimation) BehaviorResult(org.lanternpowered.server.behavior.BehaviorResult) LanternWorld(org.lanternpowered.server.world.LanternWorld) InteractWithBlockBehavior(org.lanternpowered.server.block.behavior.types.InteractWithBlockBehavior) Nullable(javax.annotation.Nullable) ContextKeys(org.lanternpowered.server.behavior.ContextKeys) FinishUsingItemBehavior(org.lanternpowered.server.item.behavior.types.FinishUsingItemBehavior) Location(org.spongepowered.api.world.Location) MaximumUseDurationProperty(org.lanternpowered.server.item.property.MaximumUseDurationProperty) LanternItemType(org.lanternpowered.server.item.LanternItemType) MessagePlayInPlayerUseItem(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerUseItem) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) BlockTypes(org.spongepowered.api.block.BlockTypes) SwingHandEntityEvent(org.lanternpowered.server.entity.event.SwingHandEntityEvent) Set(java.util.Set) LanternGame(org.lanternpowered.server.game.LanternGame) BlockState(org.spongepowered.api.block.BlockState) Objects(java.util.Objects) Direction(org.spongepowered.api.util.Direction) DualWieldProperty(org.lanternpowered.server.item.property.DualWieldProperty) Lantern(org.lanternpowered.server.game.Lantern) Vector3i(com.flowpowered.math.vector.Vector3i) World(org.spongepowered.api.world.World) BlockType(org.spongepowered.api.block.BlockType) HandTypes(org.spongepowered.api.data.type.HandTypes) Optional(java.util.Optional) MessagePlayInPlayerBlockPlacement(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerBlockPlacement) CauseStack(org.lanternpowered.server.event.CauseStack) BehaviorContextImpl(org.lanternpowered.server.behavior.BehaviorContextImpl) BlockState(org.spongepowered.api.block.BlockState) LanternWorld(org.lanternpowered.server.world.LanternWorld) World(org.spongepowered.api.world.World) LanternBlockType(org.lanternpowered.server.block.LanternBlockType) Location(org.spongepowered.api.world.Location)

Example 37 with BlockType

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

the class PlayerInteractionHandler method handleDigging.

/**
 * Handles the {@link MessagePlayInPlayerDigging}.
 *
 * @param message The message
 */
public void handleDigging(MessagePlayInPlayerDigging message) {
    final MessagePlayInPlayerDigging.Action action = message.getAction();
    final Vector3i blockPos = message.getPosition();
    if (action == MessagePlayInPlayerDigging.Action.START) {
        // Check if the block is within the players reach
        if (this.player.getPosition().distanceSquared(blockPos.toDouble().add(0.5, 2.0, 0.5)) > 6.0 * 6.0) {
            return;
        }
        if (this.diggingBlock != null) {
            Lantern.getLogger().warn("{} started breaking a block without finishing the last one.", this.player.getName());
        }
        final BlockType blockType = this.player.getWorld().getBlockType(blockPos);
        if (blockType == BlockTypes.AIR) {
            return;
        }
        this.diggingBlock = blockPos;
        this.diggingBlockType = blockType;
        this.diggingDuration = getDiggingDuration(blockPos);
        // The client won't send a finish message
        if (this.diggingDuration == 0) {
            handleBrokenBlock();
        } else {
            this.diggingEndTime = this.diggingDuration == -1 ? -1 : System.nanoTime() + this.diggingDuration;
        }
    } else if (action == MessagePlayInPlayerDigging.Action.CANCEL) {
        if (this.diggingBlock == null || !this.diggingBlock.equals(blockPos)) {
            return;
        }
        if (this.lastBreakState != -1) {
            sendBreakUpdate(-1);
        }
        this.diggingBlock = null;
        this.diggingBlockType = null;
    } else {
        if (this.diggingBlock == null) {
            return;
        }
        final BlockType blockType = this.player.getWorld().getBlockType(blockPos);
        if (blockType != this.diggingBlockType) {
            return;
        }
        if (this.diggingEndTime == -1) {
            Lantern.getLogger().warn("{} attempted to break a unbreakable block.", this.player.getName());
        } else {
            final long deltaTime = System.nanoTime() - this.diggingEndTime;
            if (deltaTime < 0) {
                Lantern.getLogger().warn("{} finished breaking a block too early, {}ms too fast.", this.player.getName(), -(deltaTime / 1000));
            }
            handleBrokenBlock();
        }
    }
}
Also used : LanternBlockType(org.lanternpowered.server.block.LanternBlockType) BlockType(org.spongepowered.api.block.BlockType) Vector3i(com.flowpowered.math.vector.Vector3i) MessagePlayInPlayerDigging(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerDigging)

Example 38 with BlockType

use of org.spongepowered.api.block.BlockType in project modules-extra by CubeEngine.

the class Observe method blockCause.

public static Map<String, Object> blockCause(BlockSnapshot block) {
    BlockType type = block.getState().getType();
    Map<String, Object> data = new HashMap<>();
    data.put(CAUSE_TYPE, CAUSE_BLOCK.toString());
    data.put(CAUSE_NAME, type.getId());
    return data;
}
Also used : BlockType(org.spongepowered.api.block.BlockType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 39 with BlockType

use of org.spongepowered.api.block.BlockType in project modules-extra by CubeEngine.

the class Recall method cause.

private static Text cause(Map<String, Object> source, Text text, CauseType type) {
    Object causeName = source.get(CAUSE_NAME);
    switch(type) {
        case CAUSE_PLAYER:
            text = Text.of(DARK_GREEN, causeName).toBuilder().onHover(TextActions.showText(Text.of(YELLOW, source.get(CAUSE_PLAYER_UUID)))).build();
            break;
        case CAUSE_BLOCK:
            Optional<BlockType> bType = Sponge.getRegistry().getType(BlockType.class, causeName.toString());
            if (!bType.isPresent()) {
                // TODO translate
                text = Text.of(TextColors.GOLD, "unknown Block");
            } else {
                if (bType.get() == BlockTypes.LAVA || bType.get() == BlockTypes.FLOWING_LAVA || bType.get() == BlockTypes.FIRE) {
                    text = Text.of(TextColors.RED, bType.get().getTranslation());
                } else {
                    text = Text.of(TextColors.GOLD, bType.get().getTranslation());
                }
            }
            break;
        case CAUSE_TNT:
            // TODO translatable
            text = Text.of(TextColors.RED, "TNT");
            if (source.get(CAUSE_PLAYER_UUID) == null) {
                text = text.toBuilder().append(Text.of(" (", Text.of(TextColors.GOLD, causeName), ")")).build();
            } else {
                text = text.toBuilder().append(Text.of(" (", Text.of(DARK_GREEN, causeName).toBuilder().onHover(TextActions.showText(Text.of(YELLOW, source.get(CAUSE_PLAYER_UUID)))).build(), ")")).build();
            }
            break;
        case CAUSE_DAMAGE:
            text = Text.of(TextColors.GOLD, causeName);
            break;
        case CAUSE_ENTITY:
            text = Text.of(TextColors.GOLD, Sponge.getRegistry().getType(EntityType.class, causeName.toString()).map(EntityType::getTranslation).map(Translation::get).orElse(causeName.toString()));
            // TODO translation
            if (source.containsKey(CAUSE_TARGET)) {
                Map<String, Object> sourceTarget = ((Map<String, Object>) source.get(CAUSE_TARGET));
                CauseType targetType = CauseType.valueOf(sourceTarget.get(CAUSE_TYPE).toString());
                text = text.toBuilder().append(Text.of("…").toBuilder().onHover(TextActions.showText(Text.of(text, "◎", cause(sourceTarget, Text.of("?"), targetType)))).build()).build();
            }
            break;
    }
    return text;
}
Also used : EntityType(org.spongepowered.api.entity.EntityType) BlockType(org.spongepowered.api.block.BlockType) CauseType(org.cubeengine.module.vigil.report.block.BlockReport.CauseType)

Example 40 with BlockType

use of org.spongepowered.api.block.BlockType in project modules-extra by CubeEngine.

the class RepairBlockManager method attachRepairBlock.

/**
 * Attaches a repair block to a block
 *
 * @param block the block to attach to
 * @return true on success
 */
public boolean attachRepairBlock(Location<World> block) {
    BlockType material = block.getBlockType();
    if (!this.isRepairBlock(block)) {
        if (this.repairBlocks.containsKey(material)) {
            this.blockMap.put(block, material);
            this.persister.storeBlock(block, this.db.getDSL().newRecord(TABLE_REPAIR_BLOCK).newRepairBlock(block));
            return true;
        }
    }
    return false;
}
Also used : BlockType(org.spongepowered.api.block.BlockType)

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