use of net.minecraft.world.level.BlockGetter in project MysticTools by binary404.
the class BlockHelper method doNativeBreakBlock.
private static boolean doNativeBreakBlock(ServerLevel world, ServerPlayer player, BlockPos pos, BlockState stateBroken, ItemStack heldItem, boolean breakBlock, boolean ignoreHarvestRestrictions) {
int xp;
try {
boolean preCancelEvent = false;
if (!heldItem.isEmpty() && !heldItem.getItem().canAttackBlock(stateBroken, (Level) world, pos, (Player) player)) {
preCancelEvent = true;
}
BlockEvent.BreakEvent event = new BlockEvent.BreakEvent((Level) world, pos, stateBroken, (Player) player);
event.setCanceled(preCancelEvent);
MinecraftForge.EVENT_BUS.post((Event) event);
if (event.isCanceled()) {
return false;
}
xp = event.getExpToDrop();
} catch (Exception exc) {
return false;
}
if (xp == -1) {
return false;
}
if (heldItem.onBlockStartBreak(pos, (Player) player)) {
return false;
}
boolean harvestable = true;
try {
if (!ignoreHarvestRestrictions) {
harvestable = stateBroken.canHarvestBlock((BlockGetter) world, pos, (Player) player);
}
} catch (Exception exc) {
return false;
}
try {
heldItem.copy().mineBlock((Level) world, stateBroken, pos, (Player) player);
} catch (Exception exc) {
return false;
}
boolean wasCapturingStates = world.captureBlockSnapshots;
List<BlockSnapshot> previousCapturedStates = new ArrayList<>(world.capturedBlockSnapshots);
BlockEntity tileEntity = world.getBlockEntity(pos);
world.captureBlockSnapshots = true;
try {
if (breakBlock) {
if (!stateBroken.onDestroyedByPlayer((Level) world, pos, (Player) player, harvestable, Fluids.EMPTY.defaultFluidState())) {
restoreWorldState((Level) world, wasCapturingStates, previousCapturedStates);
return false;
}
} else {
stateBroken.getBlock().playerWillDestroy((Level) world, pos, stateBroken, (Player) player);
}
} catch (Exception exc) {
restoreWorldState((Level) world, wasCapturingStates, previousCapturedStates);
return false;
}
stateBroken.getBlock().destroy((LevelAccessor) world, pos, stateBroken);
if (harvestable) {
try {
stateBroken.getBlock().playerDestroy((Level) world, (Player) player, pos, stateBroken, tileEntity, heldItem.copy());
} catch (Exception exc) {
restoreWorldState((Level) world, wasCapturingStates, previousCapturedStates);
return false;
}
}
if (xp > 0) {
stateBroken.getBlock().popExperience(world, pos, xp);
}
BlockDropCaptureHelper.startCapturing();
try {
world.captureBlockSnapshots = false;
world.restoringBlockSnapshots = true;
world.capturedBlockSnapshots.forEach(s -> {
world.sendBlockUpdated(s.getPos(), s.getReplacedBlock(), s.getCurrentBlock(), s.getFlag());
s.getCurrentBlock().updateNeighbourShapes((LevelAccessor) world, s.getPos(), 11);
});
world.restoringBlockSnapshots = false;
} finally {
BlockDropCaptureHelper.getCapturedStacksAndStop();
world.capturedBlockSnapshots.clear();
world.captureBlockSnapshots = wasCapturingStates;
world.capturedBlockSnapshots.addAll(previousCapturedStates);
}
return true;
}
use of net.minecraft.world.level.BlockGetter in project Carpet-TCTC-Addition by The-Cat-Town-Craft.
the class FixCommand method fixChunk.
private static int fixChunk(CommandSourceStack source, ColumnPos pos) throws CommandSyntaxException {
ServerLevel level = source.getLevel();
ChunkPos chunkPos = new ChunkPos(pos.x >> 4, pos.z >> 4);
BlockGetter blockGetter = level.getChunkForCollisions(chunkPos.x, chunkPos.z);
if (!(blockGetter instanceof LevelChunk)) {
throw BlockPosArgument.ERROR_NOT_LOADED.create();
}
LevelChunk levelChunk = (LevelChunk) blockGetter;
Heightmap.primeHeightmaps(levelChunk, EnumSet.allOf(Heightmap.Types.class));
MessageUtil.sendMessage(source, new TextComponent(String.format(tr("carpet-tctc-addition.message.command.fix.fixing", "Fixing chunk [%s, %s]..."), chunkPos.x, chunkPos.z)).withStyle(ChatFormatting.GRAY, ChatFormatting.ITALIC));
((ThreadedLevelLightEngine) level.getLightEngine()).lightChunk(levelChunk, false).thenRun(() -> MessageUtil.sendMessage(source, new TextComponent(String.format(tr("carpet-tctc-addition.message.command.fix.fixed", "Fixed chunk [%s, %s]"), chunkPos.x, chunkPos.z)).withStyle(ChatFormatting.GRAY, ChatFormatting.ITALIC)));
return 1;
}
use of net.minecraft.world.level.BlockGetter in project Starlight by PaperMC.
the class StarLightEngine method performLightDecrease.
protected final void performLightDecrease(final LightChunkGetter lightAccess) {
final BlockGetter world = lightAccess.getLevel();
long[] queue = this.decreaseQueue;
long[] increaseQueue = this.increaseQueue;
int queueReadIndex = 0;
int queueLength = this.decreaseQueueInitialLength;
this.decreaseQueueInitialLength = 0;
int increaseQueueLength = this.increaseQueueInitialLength;
final int decodeOffsetX = -this.encodeOffsetX;
final int decodeOffsetY = -this.encodeOffsetY;
final int decodeOffsetZ = -this.encodeOffsetZ;
final int encodeOffset = this.coordinateOffset;
final int sectionOffset = this.chunkSectionIndexOffset;
final int emittedMask = this.emittedLightMask;
while (queueReadIndex < queueLength) {
final long queueValue = queue[queueReadIndex++];
final int posX = ((int) queueValue & 63) + decodeOffsetX;
final int posZ = (((int) queueValue >>> 6) & 63) + decodeOffsetZ;
final int posY = (((int) queueValue >>> 12) & ((1 << 16) - 1)) + decodeOffsetY;
final int propagatedLightLevel = (int) ((queueValue >>> (6 + 6 + 16)) & 0xF);
final AxisDirection[] checkDirections = OLD_CHECK_DIRECTIONS[(int) ((queueValue >>> (6 + 6 + 16 + 4)) & 63)];
if ((queueValue & FLAG_HAS_SIDED_TRANSPARENT_BLOCKS) == 0L) {
// we don't need to worry about our state here.
for (final AxisDirection propagate : checkDirections) {
final int offX = posX + propagate.x;
final int offY = posY + propagate.y;
final int offZ = posZ + propagate.z;
final int sectionIndex = (offX >> 4) + 5 * (offZ >> 4) + (5 * 5) * (offY >> 4) + sectionOffset;
final int localIndex = (offX & 15) | ((offZ & 15) << 4) | ((offY & 15) << 8);
final SWMRNibbleArray currentNibble = this.nibbleCache[sectionIndex];
final int lightLevel;
if (currentNibble == null || (lightLevel = currentNibble.getUpdating(localIndex)) == 0) {
// already at lowest (or unloaded), nothing we can do
continue;
}
final BlockState blockState = this.getBlockState(sectionIndex, localIndex);
if (blockState == null) {
continue;
}
final int opacityCached = ((ExtendedAbstractBlockState) blockState).getOpacityIfCached();
if (opacityCached != -1) {
final int targetLevel = Math.max(0, propagatedLightLevel - Math.max(1, opacityCached));
if (lightLevel > targetLevel) {
// it looks like another source propagated here, so re-propagate it
if (increaseQueueLength >= increaseQueue.length) {
increaseQueue = this.resizeIncreaseQueue();
}
increaseQueue[increaseQueueLength++] = ((offX + (offZ << 6) + (offY << 12) + encodeOffset) & ((1L << (6 + 6 + 16)) - 1)) | ((lightLevel & 0xFL) << (6 + 6 + 16)) | (((long) ALL_DIRECTIONS_BITSET) << (6 + 6 + 16 + 4)) | FLAG_RECHECK_LEVEL;
continue;
}
final int emittedLight = blockState.getLightEmission() & emittedMask;
if (emittedLight != 0) {
// note: do not set recheck level, or else the propagation will fail
if (increaseQueueLength >= increaseQueue.length) {
increaseQueue = this.resizeIncreaseQueue();
}
increaseQueue[increaseQueueLength++] = ((offX + (offZ << 6) + (offY << 12) + encodeOffset) & ((1L << (6 + 6 + 16)) - 1)) | ((emittedLight & 0xFL) << (6 + 6 + 16)) | (((long) ALL_DIRECTIONS_BITSET) << (6 + 6 + 16 + 4)) | (((ExtendedAbstractBlockState) blockState).isConditionallyFullOpaque() ? (FLAG_WRITE_LEVEL | FLAG_HAS_SIDED_TRANSPARENT_BLOCKS) : FLAG_WRITE_LEVEL);
}
currentNibble.set(localIndex, 0);
this.postLightUpdate(offX, offY, offZ);
if (targetLevel > 0) {
// we actually need to propagate 0 just in case we find a neighbour...
if (queueLength >= queue.length) {
queue = this.resizeDecreaseQueue();
}
queue[queueLength++] = ((offX + (offZ << 6) + (offY << 12) + encodeOffset) & ((1L << (6 + 6 + 16)) - 1)) | ((targetLevel & 0xFL) << (6 + 6 + 16)) | ((propagate.everythingButTheOppositeDirection) << (6 + 6 + 16 + 4));
continue;
}
continue;
} else {
this.mutablePos1.set(offX, offY, offZ);
long flags = 0;
if (((ExtendedAbstractBlockState) blockState).isConditionallyFullOpaque()) {
final VoxelShape cullingFace = blockState.getFaceOcclusionShape(world, this.mutablePos1, propagate.getOpposite().nms);
if (Shapes.faceShapeOccludes(Shapes.empty(), cullingFace)) {
continue;
}
flags |= FLAG_HAS_SIDED_TRANSPARENT_BLOCKS;
}
final int opacity = blockState.getLightBlock(world, this.mutablePos1);
final int targetLevel = Math.max(0, propagatedLightLevel - Math.max(1, opacity));
if (lightLevel > targetLevel) {
// it looks like another source propagated here, so re-propagate it
if (increaseQueueLength >= increaseQueue.length) {
increaseQueue = this.resizeIncreaseQueue();
}
increaseQueue[increaseQueueLength++] = ((offX + (offZ << 6) + (offY << 12) + encodeOffset) & ((1L << (6 + 6 + 16)) - 1)) | ((lightLevel & 0xFL) << (6 + 6 + 16)) | (((long) ALL_DIRECTIONS_BITSET) << (6 + 6 + 16 + 4)) | (FLAG_RECHECK_LEVEL | flags);
continue;
}
final int emittedLight = blockState.getLightEmission() & emittedMask;
if (emittedLight != 0) {
// note: do not set recheck level, or else the propagation will fail
if (increaseQueueLength >= increaseQueue.length) {
increaseQueue = this.resizeIncreaseQueue();
}
increaseQueue[increaseQueueLength++] = ((offX + (offZ << 6) + (offY << 12) + encodeOffset) & ((1L << (6 + 6 + 16)) - 1)) | ((emittedLight & 0xFL) << (6 + 6 + 16)) | (((long) ALL_DIRECTIONS_BITSET) << (6 + 6 + 16 + 4)) | (flags | FLAG_WRITE_LEVEL);
}
currentNibble.set(localIndex, 0);
this.postLightUpdate(offX, offY, offZ);
if (targetLevel > 0) {
if (queueLength >= queue.length) {
queue = this.resizeDecreaseQueue();
}
queue[queueLength++] = ((offX + (offZ << 6) + (offY << 12) + encodeOffset) & ((1L << (6 + 6 + 16)) - 1)) | ((targetLevel & 0xFL) << (6 + 6 + 16)) | ((propagate.everythingButTheOppositeDirection) << (6 + 6 + 16 + 4)) | flags;
}
continue;
}
}
} else {
// we actually need to worry about our state here
final BlockState fromBlock = this.getBlockState(posX, posY, posZ);
this.mutablePos2.set(posX, posY, posZ);
for (final AxisDirection propagate : checkDirections) {
final int offX = posX + propagate.x;
final int offY = posY + propagate.y;
final int offZ = posZ + propagate.z;
final int sectionIndex = (offX >> 4) + 5 * (offZ >> 4) + (5 * 5) * (offY >> 4) + sectionOffset;
final int localIndex = (offX & 15) | ((offZ & 15) << 4) | ((offY & 15) << 8);
final VoxelShape fromShape = (((ExtendedAbstractBlockState) fromBlock).isConditionallyFullOpaque()) ? fromBlock.getFaceOcclusionShape(world, this.mutablePos2, propagate.nms) : Shapes.empty();
if (fromShape != Shapes.empty() && Shapes.faceShapeOccludes(Shapes.empty(), fromShape)) {
continue;
}
final SWMRNibbleArray currentNibble = this.nibbleCache[sectionIndex];
final int lightLevel;
if (currentNibble == null || (lightLevel = currentNibble.getUpdating(localIndex)) == 0) {
// already at lowest (or unloaded), nothing we can do
continue;
}
final BlockState blockState = this.getBlockState(sectionIndex, localIndex);
if (blockState == null) {
continue;
}
final int opacityCached = ((ExtendedAbstractBlockState) blockState).getOpacityIfCached();
if (opacityCached != -1) {
final int targetLevel = Math.max(0, propagatedLightLevel - Math.max(1, opacityCached));
if (lightLevel > targetLevel) {
// it looks like another source propagated here, so re-propagate it
if (increaseQueueLength >= increaseQueue.length) {
increaseQueue = this.resizeIncreaseQueue();
}
increaseQueue[increaseQueueLength++] = ((offX + (offZ << 6) + (offY << 12) + encodeOffset) & ((1L << (6 + 6 + 16)) - 1)) | ((lightLevel & 0xFL) << (6 + 6 + 16)) | (((long) ALL_DIRECTIONS_BITSET) << (6 + 6 + 16 + 4)) | FLAG_RECHECK_LEVEL;
continue;
}
final int emittedLight = blockState.getLightEmission() & emittedMask;
if (emittedLight != 0) {
// note: do not set recheck level, or else the propagation will fail
if (increaseQueueLength >= increaseQueue.length) {
increaseQueue = this.resizeIncreaseQueue();
}
increaseQueue[increaseQueueLength++] = ((offX + (offZ << 6) + (offY << 12) + encodeOffset) & ((1L << (6 + 6 + 16)) - 1)) | ((emittedLight & 0xFL) << (6 + 6 + 16)) | (((long) ALL_DIRECTIONS_BITSET) << (6 + 6 + 16 + 4)) | (((ExtendedAbstractBlockState) blockState).isConditionallyFullOpaque() ? (FLAG_WRITE_LEVEL | FLAG_HAS_SIDED_TRANSPARENT_BLOCKS) : FLAG_WRITE_LEVEL);
}
currentNibble.set(localIndex, 0);
this.postLightUpdate(offX, offY, offZ);
if (targetLevel > 0) {
// we actually need to propagate 0 just in case we find a neighbour...
if (queueLength >= queue.length) {
queue = this.resizeDecreaseQueue();
}
queue[queueLength++] = ((offX + (offZ << 6) + (offY << 12) + encodeOffset) & ((1L << (6 + 6 + 16)) - 1)) | ((targetLevel & 0xFL) << (6 + 6 + 16)) | ((propagate.everythingButTheOppositeDirection) << (6 + 6 + 16 + 4));
continue;
}
continue;
} else {
this.mutablePos1.set(offX, offY, offZ);
long flags = 0;
if (((ExtendedAbstractBlockState) blockState).isConditionallyFullOpaque()) {
final VoxelShape cullingFace = blockState.getFaceOcclusionShape(world, this.mutablePos1, propagate.getOpposite().nms);
if (Shapes.faceShapeOccludes(fromShape, cullingFace)) {
continue;
}
flags |= FLAG_HAS_SIDED_TRANSPARENT_BLOCKS;
}
final int opacity = blockState.getLightBlock(world, this.mutablePos1);
final int targetLevel = Math.max(0, propagatedLightLevel - Math.max(1, opacity));
if (lightLevel > targetLevel) {
// it looks like another source propagated here, so re-propagate it
if (increaseQueueLength >= increaseQueue.length) {
increaseQueue = this.resizeIncreaseQueue();
}
increaseQueue[increaseQueueLength++] = ((offX + (offZ << 6) + (offY << 12) + encodeOffset) & ((1L << (6 + 6 + 16)) - 1)) | ((lightLevel & 0xFL) << (6 + 6 + 16)) | (((long) ALL_DIRECTIONS_BITSET) << (6 + 6 + 16 + 4)) | (FLAG_RECHECK_LEVEL | flags);
continue;
}
final int emittedLight = blockState.getLightEmission() & emittedMask;
if (emittedLight != 0) {
// note: do not set recheck level, or else the propagation will fail
if (increaseQueueLength >= increaseQueue.length) {
increaseQueue = this.resizeIncreaseQueue();
}
increaseQueue[increaseQueueLength++] = ((offX + (offZ << 6) + (offY << 12) + encodeOffset) & ((1L << (6 + 6 + 16)) - 1)) | ((emittedLight & 0xFL) << (6 + 6 + 16)) | (((long) ALL_DIRECTIONS_BITSET) << (6 + 6 + 16 + 4)) | (flags | FLAG_WRITE_LEVEL);
}
currentNibble.set(localIndex, 0);
this.postLightUpdate(offX, offY, offZ);
if (targetLevel > 0) {
// we actually need to propagate 0 just in case we find a neighbour...
if (queueLength >= queue.length) {
queue = this.resizeDecreaseQueue();
}
queue[queueLength++] = ((offX + (offZ << 6) + (offY << 12) + encodeOffset) & ((1L << (6 + 6 + 16)) - 1)) | ((targetLevel & 0xFL) << (6 + 6 + 16)) | ((propagate.everythingButTheOppositeDirection) << (6 + 6 + 16 + 4)) | flags;
}
continue;
}
}
}
}
// propagate sources we clobbered
this.increaseQueueInitialLength = increaseQueueLength;
this.performLightIncrease(lightAccess);
}
use of net.minecraft.world.level.BlockGetter in project Create by Creators-of-Create.
the class ToolboxBlock method getCloneItemStack.
@Override
public ItemStack getCloneItemStack(BlockGetter world, BlockPos pos, BlockState state) {
ItemStack item = new ItemStack(this);
Optional<ToolboxTileEntity> tileEntityOptional = getTileEntityOptional(world, pos);
CompoundTag tag = item.getOrCreateTag();
CompoundTag inv = tileEntityOptional.map(tb -> tb.inventory.serializeNBT()).orElse(new CompoundTag());
tag.put("Inventory", inv);
tileEntityOptional.map(tb -> tb.getUniqueId()).ifPresent(uid -> tag.putUUID("UniqueId", uid));
tileEntityOptional.map(ToolboxTileEntity::getCustomName).ifPresent(item::setHoverName);
return item;
}
use of net.minecraft.world.level.BlockGetter in project Cyclic by Lothrazar.
the class DarkGlassConnectedBlock method getStateForPlacement.
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
BlockGetter world = context.getLevel();
BlockPos pos = context.getClickedPos();
return super.getStateForPlacement(context).setValue(CONNECTED_DOWN, this.isSideConnectable(world, pos, Direction.DOWN)).setValue(CONNECTED_EAST, this.isSideConnectable(world, pos, Direction.EAST)).setValue(CONNECTED_NORTH, this.isSideConnectable(world, pos, Direction.NORTH)).setValue(CONNECTED_SOUTH, this.isSideConnectable(world, pos, Direction.SOUTH)).setValue(CONNECTED_UP, this.isSideConnectable(world, pos, Direction.UP)).setValue(CONNECTED_WEST, this.isSideConnectable(world, pos, Direction.WEST));
}
Aggregations