Search in sources :

Example 1 with LanternBlockType

use of org.lanternpowered.server.block.LanternBlockType in project LanternServer by LanternPowered.

the class BlockRegistryModule method register.

@Override
public void register(int internalId, BlockType blockType) {
    LanternBlockType blockType0 = (LanternBlockType) checkNotNull(blockType, "blockType");
    checkState(blockType0.getBlockStateBase().getBlockStates().stream().filter(s -> !((LanternBlockState) s).isExtended()).count() <= 1, "You cannot register a blockType with more then one state with this method.");
    register0(internalId, blockType0, blockState -> (byte) 0);
}
Also used : BlockTypeBuilder(org.lanternpowered.server.block.BlockTypeBuilder) LanternBlockType(org.lanternpowered.server.block.LanternBlockType) LanternDirtType(org.lanternpowered.server.data.type.LanternDirtType) JukeboxInteractionBehavior(org.lanternpowered.server.block.behavior.vanilla.JukeboxInteractionBehavior) SnowyExtendedBlockStateProvider(org.lanternpowered.server.block.extended.SnowyExtendedBlockStateProvider) LanternBooleanTraits(org.lanternpowered.server.block.trait.LanternBooleanTraits) BoundingBoxes(org.lanternpowered.server.block.aabb.BoundingBoxes) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ChestPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.ChestPlacementBehavior) LanternTreeType(org.lanternpowered.server.data.type.LanternTreeType) RegistrationDependency(org.spongepowered.api.registry.util.RegistrationDependency) LanternStoneType(org.lanternpowered.server.data.type.LanternStoneType) KeyRegistryModule(org.lanternpowered.server.game.registry.type.data.KeyRegistryModule) PropertyProviders.replaceable(org.lanternpowered.server.block.provider.property.PropertyProviders.replaceable) ChestInteractionBehavior(org.lanternpowered.server.block.behavior.vanilla.ChestInteractionBehavior) NoteBlockInteractionBehavior(org.lanternpowered.server.block.behavior.vanilla.NoteBlockInteractionBehavior) ShulkerBoxInteractionBehavior(org.lanternpowered.server.block.behavior.vanilla.ShulkerBoxInteractionBehavior) Object2ShortOpenHashMap(it.unimi.dsi.fastutil.objects.Object2ShortOpenHashMap) LanternIntegerTraits(org.lanternpowered.server.block.trait.LanternIntegerTraits) Short2ObjectOpenHashMap(it.unimi.dsi.fastutil.shorts.Short2ObjectOpenHashMap) LanternQuartzType(org.lanternpowered.server.data.type.LanternQuartzType) LanternDyeColor(org.lanternpowered.server.data.type.LanternDyeColor) BlockTypes(org.spongepowered.api.block.BlockTypes) INSTANT_BROKEN(org.lanternpowered.server.block.provider.property.PropertyProviderCollections.INSTANT_BROKEN) LanternPlantType(org.lanternpowered.server.data.type.LanternPlantType) HopperPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.HopperPlacementBehavior) SimpleBlockDropsProviderBehavior(org.lanternpowered.server.block.behavior.simple.SimpleBlockDropsProviderBehavior) Preconditions.checkState(com.google.common.base.Preconditions.checkState) SimpleBreakBehavior(org.lanternpowered.server.block.behavior.simple.SimpleBreakBehavior) LanternPortionType(org.lanternpowered.server.data.type.LanternPortionType) EquipmentTypeRegistryModule(org.lanternpowered.server.game.registry.type.item.inventory.equipment.EquipmentTypeRegistryModule) BlockState(org.spongepowered.api.block.BlockState) SlabType(org.spongepowered.api.data.type.SlabType) Direction(org.spongepowered.api.util.Direction) SimplePlacementBehavior(org.lanternpowered.server.block.behavior.simple.SimplePlacementBehavior) QuartzLinesRotationPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.QuartzLinesRotationPlacementBehavior) Lantern(org.lanternpowered.server.game.Lantern) BlockSnapshotProviderPlaceBehavior(org.lanternpowered.server.block.behavior.simple.BlockSnapshotProviderPlaceBehavior) RotationPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.RotationPlacementBehavior) InstrumentTypeRegistryModule(org.lanternpowered.server.game.registry.type.data.InstrumentTypeRegistryModule) BlockType(org.spongepowered.api.block.BlockType) Object2ShortMap(it.unimi.dsi.fastutil.objects.Object2ShortMap) Optional(java.util.Optional) CraftingTableInteractionBehavior(org.lanternpowered.server.block.behavior.vanilla.CraftingTableInteractionBehavior) LanternSlabType(org.lanternpowered.server.data.type.LanternSlabType) LanternBedPart(org.lanternpowered.server.data.type.LanternBedPart) InventorySnapshot(org.lanternpowered.server.inventory.InventorySnapshot) BlockTypeBuilderImpl(org.lanternpowered.server.block.BlockTypeBuilderImpl) LanternKeys(org.lanternpowered.server.data.key.LanternKeys) Keys(org.spongepowered.api.data.key.Keys) EnderChestInteractionBehavior(org.lanternpowered.server.block.behavior.vanilla.EnderChestInteractionBehavior) OppositeFaceDirectionalPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.OppositeFaceDirectionalPlacementBehavior) LanternRailDirection(org.lanternpowered.server.data.type.LanternRailDirection) EnumTrait(org.spongepowered.api.block.trait.EnumTrait) Byte2ObjectOpenHashMap(it.unimi.dsi.fastutil.bytes.Byte2ObjectOpenHashMap) TranslationProvider(org.lanternpowered.server.block.TranslationProvider) Supplier(java.util.function.Supplier) ItemRegistryModule(org.lanternpowered.server.game.registry.type.item.ItemRegistryModule) TreeType(org.spongepowered.api.data.type.TreeType) PropertyProviders.equipmentType(org.lanternpowered.server.item.PropertyProviders.equipmentType) PropertyProviders.lightEmission(org.lanternpowered.server.block.provider.property.PropertyProviders.lightEmission) HorizontalRotationPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.HorizontalRotationPlacementBehavior) TorchPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.TorchPlacementBehavior) Byte2ObjectMap(it.unimi.dsi.fastutil.bytes.Byte2ObjectMap) TranslationHelper.tr(org.lanternpowered.server.text.translation.TranslationHelper.tr) LogAxisRotationPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.LogAxisRotationPlacementBehavior) PASSABLE(org.lanternpowered.server.block.provider.property.PropertyProviderCollections.PASSABLE) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) PropertyProviders.hardness(org.lanternpowered.server.block.provider.property.PropertyProviders.hardness) PropertyProviderCollections(org.lanternpowered.server.block.provider.property.PropertyProviderCollections) LanternSandType(org.lanternpowered.server.data.type.LanternSandType) LanternShrubType(org.lanternpowered.server.data.type.LanternShrubType) LanternBlockState(org.lanternpowered.server.block.state.LanternBlockState) SpongeTranslationProvider(org.lanternpowered.server.block.translation.SpongeTranslationProvider) LanternSandstoneType(org.lanternpowered.server.data.type.LanternSandstoneType) SlabItemInteractionBehavior(org.lanternpowered.server.item.behavior.vanilla.SlabItemInteractionBehavior) PropertyProviders.blastResistance(org.lanternpowered.server.block.provider.property.PropertyProviders.blastResistance) Short2ObjectMap(it.unimi.dsi.fastutil.shorts.Short2ObjectMap) TileEntityTypes(org.spongepowered.api.block.tileentity.TileEntityTypes) PropertyProviders.flammableInfo(org.lanternpowered.server.block.provider.property.PropertyProviders.flammableInfo) EquipmentTypes(org.spongepowered.api.item.inventory.equipment.EquipmentTypes) LanternEnumTraits(org.lanternpowered.server.block.trait.LanternEnumTraits) AdditionalPluginCatalogRegistryModule(org.lanternpowered.server.game.registry.AdditionalPluginCatalogRegistryModule) OpenableContainerInteractionBehavior(org.lanternpowered.server.block.behavior.vanilla.OpenableContainerInteractionBehavior) LanternBlockType(org.lanternpowered.server.block.LanternBlockType)

Example 2 with LanternBlockType

use of org.lanternpowered.server.block.LanternBlockType 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 3 with LanternBlockType

use of org.lanternpowered.server.block.LanternBlockType in project LanternServer by LanternPowered.

the class LanternChunk method createSnapshot.

@Override
public BlockSnapshot createSnapshot(int x, int y, int z) {
    final BlockState state = getBlock(x, y, z);
    final Location<World> loc = new Location<>(this.world, x, y, z);
    // TODO: Tile entity data
    return new LanternBlockSnapshot(loc, state, ((LanternBlockType) state.getType()).getExtendedBlockStateProvider().get(state, loc, null), getCreator(x, y, z), getNotifier(x, y, z), ImmutableMap.of());
}
Also used : LanternBlockSnapshot(org.lanternpowered.server.block.LanternBlockSnapshot) BlockState(org.spongepowered.api.block.BlockState) World(org.spongepowered.api.world.World) LanternWorld(org.lanternpowered.server.world.LanternWorld) LanternBlockType(org.lanternpowered.server.block.LanternBlockType) Location(org.spongepowered.api.world.Location)

Example 4 with LanternBlockType

use of org.lanternpowered.server.block.LanternBlockType in project LanternServer by LanternPowered.

the class LanternChunk method getBlockSelectionBox.

@Override
public Optional<AABB> getBlockSelectionBox(int x, int y, int z) {
    final BlockState block = getBlock(x, y, z);
    if (block.getType() == BlockTypes.AIR) {
        return Optional.empty();
    }
    final ObjectProvider<AABB> aabbObjectProvider = ((LanternBlockType) block.getType()).getBoundingBoxProvider();
    if (aabbObjectProvider == null) {
        return Optional.empty();
    }
    final AABB aabb;
    if (aabbObjectProvider instanceof ConstantObjectProvider || aabbObjectProvider instanceof CachedSimpleObjectProvider || aabbObjectProvider instanceof SimpleObjectProvider) {
        aabb = aabbObjectProvider.get(block, null, null);
    } else {
        aabb = aabbObjectProvider.get(block, new Location<>(this.world, x, y, z), null);
    }
    return aabb == null ? Optional.empty() : Optional.of(aabb.offset(x, y, z));
}
Also used : BlockState(org.spongepowered.api.block.BlockState) ConstantObjectProvider(org.lanternpowered.server.block.provider.ConstantObjectProvider) CachedSimpleObjectProvider(org.lanternpowered.server.block.provider.CachedSimpleObjectProvider) LanternBlockType(org.lanternpowered.server.block.LanternBlockType) AABB(org.spongepowered.api.util.AABB) CachedSimpleObjectProvider(org.lanternpowered.server.block.provider.CachedSimpleObjectProvider) SimpleObjectProvider(org.lanternpowered.server.block.provider.SimpleObjectProvider) Location(org.spongepowered.api.world.Location)

Example 5 with LanternBlockType

use of org.lanternpowered.server.block.LanternBlockType in project LanternServer by LanternPowered.

the class LanternChunk method setBlock.

@Override
public boolean setBlock(int x, int y, int z, BlockState block, BlockChangeFlag flag) {
    checkNotNull(block, "block");
    checkNotNull(flag, "flag");
    checkVolumeBounds(x, y, z);
    if (!this.loaded) {
        return false;
    }
    final short type = BlockRegistryModule.get().getStateInternalIdAndData(block);
    final short type1;
    // Air doesn't have metadata values
    if (type >> 4 == 0 && type != 0) {
        type1 = 0;
    } else {
        type1 = type;
    }
    final BlockState[] changeData = new BlockState[1];
    final int rx = x & 0xf;
    final int rz = z & 0xf;
    this.chunkSections.work(y >> 4, section -> {
        if (section == null) {
            // so we can fail fast
            if (type1 == 0) {
                return section;
            }
            // Create a new section
            section = new ChunkSection();
        }
        final int index = ChunkSection.index(rx, y & 0xf, rz);
        final short oldType = section.types[index];
        if (oldType == type1) {
            return section;
        }
        if (oldType != 0) {
            short count = section.typesCountMap.get(oldType);
            if (count > 0) {
                if (--count <= 0) {
                    section.typesCountMap.remove(oldType);
                } else {
                    section.typesCountMap.put(oldType, count);
                }
            }
        }
        if (type1 != 0) {
            section.typesCountMap.put(type1, (short) (section.typesCountMap.get(type1) + 1));
            if (oldType == 0) {
                section.nonAirCount++;
            }
        } else {
            section.nonAirCount--;
        }
        final BlockState oldState = BlockRegistryModule.get().getStateByInternalIdAndData(oldType).get();
        changeData[0] = oldState;
        // The section is empty, destroy it
        if (section.nonAirCount <= 0) {
            return null;
        }
        final LanternTileEntity tileEntity = section.tileEntities.get((short) index);
        boolean remove = false;
        boolean refresh = false;
        final Optional<TileEntityProvider> tileEntityProvider = ((LanternBlockType) block.getType()).getTileEntityProvider();
        if (tileEntity != null) {
            if (oldType == 0 || type1 == 0) {
                remove = true;
            } else if (tileEntity instanceof ITileEntityRefreshBehavior) {
                if (((ITileEntityRefreshBehavior) tileEntity).shouldRefresh(oldState, block)) {
                    remove = true;
                    refresh = true;
                }
            } else if (oldType >> 4 != type1 >> 4) {
                // The default behavior will only refresh if the
                // block type is changed and not the block state
                remove = true;
                refresh = true;
            }
            if (refresh && !tileEntityProvider.isPresent()) {
                refresh = false;
            }
        } else if (tileEntityProvider.isPresent()) {
            refresh = true;
        }
        if (remove) {
            tileEntity.setValid(false);
        }
        if (refresh) {
            final Location<World> location = tileEntity != null ? tileEntity.getLocation() : new Location<>(this.world, x, y, z);
            final LanternTileEntity newTileEntity = (LanternTileEntity) tileEntityProvider.get().get(block, location, null);
            section.tileEntities.put((short) index, newTileEntity);
            newTileEntity.setLocation(location);
            newTileEntity.setValid(true);
        } else if (remove) {
            section.tileEntities.remove((short) index);
        }
        section.types[index] = type1;
        return section;
    });
    final int index = rz << 4 | rx;
    long stamp = this.heightMapLock.writeLock();
    try {
        // TODO: Check first and then use the write lock?
        if (type != 0 && (this.heightMap[index] & 0xff) < y) {
            this.heightMap[index] = (byte) y;
            this.heightMapUpdateFlags.clear(index);
        } else if (type == 0 && (this.heightMap[index] & 0xff) == y) {
            this.heightMapUpdateFlags.set(index);
        }
    } finally {
        this.heightMapLock.unlock(stamp);
    }
    if (changeData[0] != null) {
        this.world.getEventListener().onBlockChange(x, y, z, changeData[0], block);
    }
    return true;
}
Also used : TileEntityProvider(org.lanternpowered.server.block.TileEntityProvider) World(org.spongepowered.api.world.World) LanternWorld(org.lanternpowered.server.world.LanternWorld) LanternBlockType(org.lanternpowered.server.block.LanternBlockType) BlockState(org.spongepowered.api.block.BlockState) ITileEntityRefreshBehavior(org.lanternpowered.server.block.tile.ITileEntityRefreshBehavior) LanternTileEntity(org.lanternpowered.server.block.tile.LanternTileEntity)

Aggregations

LanternBlockType (org.lanternpowered.server.block.LanternBlockType)11 BlockState (org.spongepowered.api.block.BlockState)10 Location (org.spongepowered.api.world.Location)9 World (org.spongepowered.api.world.World)9 Optional (java.util.Optional)8 Keys (org.spongepowered.api.data.key.Keys)8 Direction (org.spongepowered.api.util.Direction)8 ContextKeys (org.lanternpowered.server.behavior.ContextKeys)7 BlockType (org.spongepowered.api.block.BlockType)7 Lantern (org.lanternpowered.server.game.Lantern)6 GameModes (org.spongepowered.api.entity.living.player.gamemode.GameModes)6 Vector3d (com.flowpowered.math.vector.Vector3d)5 Vector3i (com.flowpowered.math.vector.Vector3i)5 Set (java.util.Set)5 Supplier (java.util.function.Supplier)5 Nullable (javax.annotation.Nullable)5 Behavior (org.lanternpowered.server.behavior.Behavior)5 BehaviorContextImpl (org.lanternpowered.server.behavior.BehaviorContextImpl)5 BehaviorPipeline (org.lanternpowered.server.behavior.pipeline.BehaviorPipeline)5 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)4