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);
}
}
}
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();
}
}
}
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;
}
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;
}
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;
}
Aggregations