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