Search in sources :

Example 21 with Level

use of net.minecraft.world.level.Level in project SpongeCommon by SpongePowered.

the class CompassItemData method register.

static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(ItemStack.class).create(Keys.LODESTONE).get(stack -> {
        if (CompassItem.isLodestoneCompass(stack)) {
            final CompoundTag tag = stack.getOrCreateTag();
            final Optional<ResourceKey<Level>> dimension = CompassItem.getLodestoneDimension(tag);
            if (dimension.isPresent()) {
                return ServerLocation.of((ServerWorld) SpongeCommon.server().getLevel(dimension.get()), VecHelper.toVector3d(NbtUtils.readBlockPos(tag.getCompound("LodestonePos"))));
            }
        }
        return null;
    }).set((stack, location) -> {
        final CompoundTag tag = stack.getOrCreateTag();
        tag.put("LodestonePos", NbtUtils.writeBlockPos(VecHelper.toBlockPos(location)));
        Level.RESOURCE_KEY_CODEC.encodeStart(NbtOps.INSTANCE, ((net.minecraft.server.level.ServerLevel) location.world()).dimension()).resultOrPartial(SpongeCommon.logger()::error).ifPresent(dimension -> tag.put("LodestoneDimension", dimension));
        tag.putBoolean("LodestoneTracked", true);
    }).delete(stack -> {
        final CompoundTag tag = stack.getTag();
        if (tag != null) {
            tag.remove("LodestoneDimension");
            tag.remove("LodestonePos");
            tag.remove("LodestoneTracked");
        }
    });
}
Also used : ServerWorld(org.spongepowered.api.world.server.ServerWorld) NbtUtils(net.minecraft.nbt.NbtUtils) SpongeCommon(org.spongepowered.common.SpongeCommon) NbtOps(net.minecraft.nbt.NbtOps) ResourceKey(net.minecraft.resources.ResourceKey) CompassItem(net.minecraft.world.item.CompassItem) Keys(org.spongepowered.api.data.Keys) CompoundTag(net.minecraft.nbt.CompoundTag) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) VecHelper(org.spongepowered.common.util.VecHelper) Optional(java.util.Optional) ItemStack(net.minecraft.world.item.ItemStack) Level(net.minecraft.world.level.Level) ServerLocation(org.spongepowered.api.world.server.ServerLocation) CompoundTag(net.minecraft.nbt.CompoundTag) ResourceKey(net.minecraft.resources.ResourceKey)

Example 22 with Level

use of net.minecraft.world.level.Level in project Tropicraft by Tropicraft.

the class BoardwalkBlock method getStateForPlacement.

@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
    Level world = context.getLevel();
    BlockPos pos = context.getClickedPos();
    boolean tall = context.getClickLocation().y - pos.getY() > 0.5;
    BlockState state = this.defaultBlockState().setValue(AXIS, context.getHorizontalDirection().getAxis()).setValue(TYPE, tall ? Type.TALL : Type.SHORT).setValue(WATERLOGGED, world.getFluidState(pos).getType() == Fluids.WATER);
    state = this.applyConnections(state, world, pos);
    return state;
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos)

Example 23 with Level

use of net.minecraft.world.level.Level in project Tropicraft by Tropicraft.

the class BongoDrumBlock method onBlockLeftClick.

@SubscribeEvent
public static void onBlockLeftClick(PlayerInteractEvent.LeftClickBlock event) {
    final Level world = event.getWorld();
    final BlockState state = world.getBlockState(event.getPos());
    final Block block = state.getBlock();
    if (state.getBlock() instanceof BongoDrumBlock && event.getFace() == Direction.UP) {
        ((BongoDrumBlock) block).playBongoSound(world, event.getPos(), state);
    }
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) Block(net.minecraft.world.level.block.Block) Level(net.minecraft.world.level.Level) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 24 with Level

use of net.minecraft.world.level.Level in project Tropicraft by Tropicraft.

the class HugePlantBlock method getStateForPlacement.

@Override
@Nullable
public BlockState getStateForPlacement(BlockPlaceContext context) {
    Level world = context.getLevel();
    BlockPos pos = context.getClickedPos();
    for (BlockPos plantPos : Shape.fromSeed(this, pos)) {
        if (plantPos.equals(pos))
            continue;
        if (!world.getBlockState(plantPos).canBeReplaced(context)) {
            return null;
        }
    }
    return this.defaultBlockState().setValue(TYPE, Type.SEED);
}
Also used : Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) Nullable(javax.annotation.Nullable)

Example 25 with Level

use of net.minecraft.world.level.Level in project Tropicraft by Tropicraft.

the class AshenMaskItem method useOn.

/**
 * Called when this item is used when targetting a Block
 */
@Override
public InteractionResult useOn(UseOnContext context) {
    BlockPos pos = context.getClickedPos();
    Direction direction = context.getClickedFace();
    BlockPos offsetPos = pos.relative(direction);
    Player player = context.getPlayer();
    ItemStack itemStack = context.getItemInHand();
    if (player != null && !this.canPlace(player, direction, itemStack, offsetPos)) {
        return InteractionResult.FAIL;
    } else {
        Level world = context.getLevel();
        WallItemEntity wallItem = new WallItemEntity(world, offsetPos, direction);
        wallItem.setItem(itemStack);
        if (wallItem.survives()) {
            if (!world.isClientSide) {
                wallItem.playPlacementSound();
                world.addFreshEntity(wallItem);
            }
            itemStack.shrink(1);
        }
        return InteractionResult.SUCCESS;
    }
}
Also used : Player(net.minecraft.world.entity.player.Player) WallItemEntity(net.tropicraft.core.common.entity.placeable.WallItemEntity) BlockPos(net.minecraft.core.BlockPos) Level(net.minecraft.world.level.Level) ItemStack(net.minecraft.world.item.ItemStack) Direction(net.minecraft.core.Direction)

Aggregations

Level (net.minecraft.world.level.Level)53 BlockPos (net.minecraft.core.BlockPos)26 ServerLevel (net.minecraft.server.level.ServerLevel)19 ItemStack (net.minecraft.world.item.ItemStack)14 BlockState (net.minecraft.world.level.block.state.BlockState)13 IOException (java.io.IOException)10 Direction (net.minecraft.core.Direction)8 ChunkPos (net.minecraft.world.level.ChunkPos)6 Path (java.nio.file.Path)5 ExecutionException (java.util.concurrent.ExecutionException)5 Nullable (javax.annotation.Nullable)5 Entity (net.minecraft.world.entity.Entity)5 Player (net.minecraft.world.entity.player.Player)5 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)5 LevelStem (net.minecraft.world.level.dimension.LevelStem)5 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)5 Map (java.util.Map)4 Set (java.util.Set)4 CompoundTag (net.minecraft.nbt.CompoundTag)4 ResourceLocation (net.minecraft.resources.ResourceLocation)4