Search in sources :

Example 36 with BlockPos

use of net.minecraft.core.BlockPos in project Denizen-For-Bukkit by DenizenScript.

the class DenizenNetworkManagerImpl method processShowFakeForPacket.

public boolean processShowFakeForPacket(Packet<?> packet, GenericFutureListener<? extends Future<? super Void>> genericfuturelistener) {
    if (FakeBlock.blocks.isEmpty()) {
        return false;
    }
    try {
        if (packet instanceof ClientboundLevelChunkWithLightPacket) {
            FakeBlock.FakeBlockMap map = FakeBlock.blocks.get(player.getUUID());
            if (map == null) {
                return false;
            }
            int chunkX = ((ClientboundLevelChunkWithLightPacket) packet).getX();
            int chunkZ = ((ClientboundLevelChunkWithLightPacket) packet).getZ();
            ChunkCoordinate chunkCoord = new ChunkCoordinate(chunkX, chunkZ, player.getLevel().getWorld().getName());
            List<FakeBlock> blocks = FakeBlock.getFakeBlocksFor(player.getUUID(), chunkCoord);
            if (blocks == null || blocks.isEmpty()) {
                return false;
            }
            ClientboundLevelChunkWithLightPacket newPacket = FakeBlockHelper.handleMapChunkPacket(player.getBukkitEntity().getWorld(), (ClientboundLevelChunkWithLightPacket) packet, chunkX, chunkZ, blocks);
            oldManager.send(newPacket, genericfuturelistener);
            return true;
        } else if (packet instanceof ClientboundSectionBlocksUpdatePacket) {
            FakeBlock.FakeBlockMap map = FakeBlock.blocks.get(player.getUUID());
            if (map == null) {
                return false;
            }
            SectionPos coord = (SectionPos) SECTIONPOS_MULTIBLOCKCHANGE.get(packet);
            ChunkCoordinate coordinateDenizen = new ChunkCoordinate(coord.getX(), coord.getZ(), player.getLevel().getWorld().getName());
            if (!map.byChunk.containsKey(coordinateDenizen)) {
                return false;
            }
            ClientboundSectionBlocksUpdatePacket newPacket = new ClientboundSectionBlocksUpdatePacket(copyPacket(packet));
            LocationTag location = new LocationTag(player.getLevel().getWorld(), 0, 0, 0);
            short[] originalOffsetArray = (short[]) OFFSETARRAY_MULTIBLOCKCHANGE.get(newPacket);
            BlockState[] originalDataArray = (BlockState[]) BLOCKARRAY_MULTIBLOCKCHANGE.get(newPacket);
            short[] offsetArray = Arrays.copyOf(originalOffsetArray, originalOffsetArray.length);
            BlockState[] dataArray = Arrays.copyOf(originalDataArray, originalDataArray.length);
            OFFSETARRAY_MULTIBLOCKCHANGE.set(newPacket, offsetArray);
            BLOCKARRAY_MULTIBLOCKCHANGE.set(newPacket, dataArray);
            for (int i = 0; i < offsetArray.length; i++) {
                short offset = offsetArray[i];
                BlockPos pos = coord.relativeToBlockPos(offset);
                location.setX(pos.getX());
                location.setY(pos.getY());
                location.setZ(pos.getZ());
                FakeBlock block = map.byLocation.get(location);
                if (block != null) {
                    dataArray[i] = FakeBlockHelper.getNMSState(block);
                }
            }
            oldManager.send(newPacket, genericfuturelistener);
            return true;
        } else if (packet instanceof ClientboundBlockUpdatePacket) {
            BlockPos pos = ((ClientboundBlockUpdatePacket) packet).getPos();
            LocationTag loc = new LocationTag(player.getLevel().getWorld(), pos.getX(), pos.getY(), pos.getZ());
            FakeBlock block = FakeBlock.getFakeBlockFor(player.getUUID(), loc);
            if (block != null) {
                ClientboundBlockUpdatePacket newPacket = new ClientboundBlockUpdatePacket(((ClientboundBlockUpdatePacket) packet).getPos(), FakeBlockHelper.getNMSState(block));
                oldManager.send(newPacket, genericfuturelistener);
                return true;
            }
        } else if (packet instanceof ClientboundBlockBreakAckPacket) {
            ClientboundBlockBreakAckPacket origPack = (ClientboundBlockBreakAckPacket) packet;
            BlockPos pos = origPack.pos();
            LocationTag loc = new LocationTag(player.getLevel().getWorld(), pos.getX(), pos.getY(), pos.getZ());
            FakeBlock block = FakeBlock.getFakeBlockFor(player.getUUID(), loc);
            if (block != null) {
                ClientboundBlockBreakAckPacket newPacket = new ClientboundBlockBreakAckPacket(origPack.pos(), FakeBlockHelper.getNMSState(block), origPack.action(), false);
                oldManager.send(newPacket, genericfuturelistener);
                return true;
            }
        }
    } catch (Throwable ex) {
        Debug.echoError(ex);
    }
    return false;
}
Also used : ChunkCoordinate(com.denizenscript.denizen.utilities.blocks.ChunkCoordinate) FakeBlock(com.denizenscript.denizen.utilities.blocks.FakeBlock) LocationTag(com.denizenscript.denizen.objects.LocationTag) BlockState(net.minecraft.world.level.block.state.BlockState) BlockPos(net.minecraft.core.BlockPos) SectionPos(net.minecraft.core.SectionPos)

Example 37 with BlockPos

use of net.minecraft.core.BlockPos in project Denizen-For-Bukkit by DenizenScript.

the class EntityHelperImpl method forceInteraction.

@Override
public void forceInteraction(Player player, Location location) {
    CraftPlayer craftPlayer = (CraftPlayer) player;
    BlockPos pos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
    ((CraftBlock) location.getBlock()).getNMS().use(((CraftWorld) location.getWorld()).getHandle(), craftPlayer != null ? craftPlayer.getHandle() : null, InteractionHand.MAIN_HAND, new BlockHitResult(new Vec3(0, 0, 0), null, pos, false));
}
Also used : Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult)

Example 38 with BlockPos

use of net.minecraft.core.BlockPos in project Denizen-For-Bukkit by DenizenScript.

the class ItemHelperImpl method renderFullMap.

/**
 * Copied from MapItem.update, redesigned slightly to render totally rather than just relative to a player.
 * Some variables manually renamed for readability.
 * Also contains reflection fixes for Spigot's FluidState bug.
 */
public static void renderFullMap(MapItemSavedData worldmap, int xMin, int zMin, int xMax, int zMax) {
    Level world = ((CraftWorld) worldmap.mapView.getWorld()).getHandle();
    int scale = 1 << worldmap.scale;
    int mapX = worldmap.x;
    int mapZ = worldmap.z;
    for (int x = xMin; x < xMax; x++) {
        double d0 = 0.0D;
        for (int z = zMin; z < zMax; z++) {
            int k2 = (mapX / scale + x - 64) * scale;
            int l2 = (mapZ / scale + z - 64) * scale;
            Multiset<MaterialColor> multiset = LinkedHashMultiset.create();
            LevelChunk chunk = world.getChunkAt(new BlockPos(k2, 0, l2));
            if (!chunk.isEmpty()) {
                ChunkPos chunkcoordintpair = chunk.getPos();
                int i3 = k2 & 15;
                int j3 = l2 & 15;
                int k3 = 0;
                double d1 = 0.0D;
                if (world.dimensionType().hasCeiling()) {
                    int l3 = k2 + l2 * 231871;
                    l3 = l3 * l3 * 31287121 + l3 * 11;
                    if ((l3 >> 20 & 1) == 0) {
                        multiset.add(Blocks.DIRT.defaultBlockState().getMapColor(world, BlockPos.ZERO), 10);
                    } else {
                        multiset.add(Blocks.STONE.defaultBlockState().getMapColor(world, BlockPos.ZERO), 100);
                    }
                    d1 = 100.0D;
                } else {
                    BlockPos.MutableBlockPos blockposition_mutableblockposition = new BlockPos.MutableBlockPos();
                    BlockPos.MutableBlockPos blockposition_mutableblockposition1 = new BlockPos.MutableBlockPos();
                    for (int i4 = 0; i4 < scale; ++i4) {
                        for (int j4 = 0; j4 < scale; ++j4) {
                            int k4 = chunk.getHeight(Heightmap.Types.WORLD_SURFACE, i4 + i3, j4 + j3) + 1;
                            BlockState iblockdata;
                            if (k4 <= world.getMinBuildHeight() + 1) {
                                iblockdata = Blocks.BEDROCK.defaultBlockState();
                            } else {
                                do {
                                    --k4;
                                    blockposition_mutableblockposition.set(chunkcoordintpair.getMinBlockX() + i4 + i3, k4, chunkcoordintpair.getMinBlockZ() + j4 + j3);
                                    iblockdata = chunk.getBlockState(blockposition_mutableblockposition);
                                } while (iblockdata.getMapColor(world, blockposition_mutableblockposition) == MaterialColor.NONE && k4 > world.getMinBuildHeight());
                                if (k4 > world.getMinBuildHeight() && !blockStateFluidIsEmpty(iblockdata)) {
                                    int l4 = k4 - 1;
                                    blockposition_mutableblockposition1.set(blockposition_mutableblockposition);
                                    BlockState iblockdata1;
                                    do {
                                        blockposition_mutableblockposition1.setY(l4--);
                                        iblockdata1 = chunk.getBlockState(blockposition_mutableblockposition1);
                                        k3++;
                                    } while (l4 > world.getMinBuildHeight() && !blockStateFluidIsEmpty(iblockdata1));
                                    iblockdata = getCorrectStateForFluidBlock(world, iblockdata, blockposition_mutableblockposition);
                                }
                            }
                            worldmap.checkBanners(world, chunkcoordintpair.getMinBlockX() + i4 + i3, chunkcoordintpair.getMinBlockZ() + j4 + j3);
                            d1 += (double) k4 / (double) (scale * scale);
                            multiset.add(iblockdata.getMapColor(world, blockposition_mutableblockposition));
                        }
                    }
                }
                k3 /= scale * scale;
                double d2 = (d1 - d0) * 4.0D / (double) (scale + 4) + ((double) (x + z & 1) - 0.5D) * 0.4D;
                byte b0 = 1;
                if (d2 > 0.6D) {
                    b0 = 2;
                }
                if (d2 < -0.6D) {
                    b0 = 0;
                }
                MaterialColor materialmapcolor = Iterables.getFirst(Multisets.copyHighestCountFirst(multiset), MaterialColor.NONE);
                if (materialmapcolor == MaterialColor.WATER) {
                    d2 = (double) k3 * 0.1D + (double) (x + z & 1) * 0.2D;
                    b0 = 1;
                    if (d2 < 0.5D) {
                        b0 = 2;
                    }
                    if (d2 > 0.9D) {
                        b0 = 0;
                    }
                }
                d0 = d1;
                worldmap.updateColor(x, z, (byte) (materialmapcolor.id * 4 + b0));
            }
        }
    }
}
Also used : MaterialColor(net.minecraft.world.level.material.MaterialColor) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) BlockState(net.minecraft.world.level.block.state.BlockState) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) ChunkPos(net.minecraft.world.level.ChunkPos) CraftWorld(org.bukkit.craftbukkit.v1_18_R1.CraftWorld)

Example 39 with BlockPos

use of net.minecraft.core.BlockPos in project Denizen-For-Bukkit by DenizenScript.

the class BiomeNMSImpl method setTo.

@Override
public void setTo(Block block) {
    if (((CraftWorld) block.getWorld()).getHandle() != this.world) {
        NMSHandler.getInstance().getBiomeNMS(block.getWorld(), getName()).setTo(block);
        return;
    }
    // Based on CraftWorld source
    BlockPos pos = new BlockPos(block.getX(), 0, block.getZ());
    if (world.hasChunkAt(pos)) {
        LevelChunk chunk = world.getChunkAt(pos);
        if (chunk != null) {
            chunk.setBiome(block.getX() >> 2, block.getY() >> 2, block.getZ() >> 2, biomeBase);
            chunk.setUnsaved(true);
        }
    }
}
Also used : LevelChunk(net.minecraft.world.level.chunk.LevelChunk) BlockPos(net.minecraft.core.BlockPos)

Example 40 with BlockPos

use of net.minecraft.core.BlockPos in project Denizen-For-Bukkit by DenizenScript.

the class BlockLightImpl method checkIfLightsBrokenByPacket.

public static void checkIfLightsBrokenByPacket(ClientboundBlockUpdatePacket packet, Level world) {
    try {
        BlockPos pos = packet.getPos();
        int chunkX = pos.getX() >> 4;
        int chunkZ = pos.getZ() >> 4;
        Bukkit.getScheduler().scheduleSyncDelayedTask(NMSHandler.getJavaPlugin(), () -> {
            LevelChunk chunk = world.getChunk(chunkX, chunkZ);
            boolean any = false;
            for (Vector vec : RELATIVE_CHUNKS) {
                ChunkAccess other = world.getChunk(chunkX + vec.getBlockX(), chunkZ + vec.getBlockZ(), ChunkStatus.FULL, false);
                if (other instanceof LevelChunk) {
                    List<BlockLight> lights = lightsByChunk.get(new ChunkCoordinate(((LevelChunk) other).bukkitChunk));
                    if (lights != null) {
                        any = true;
                        for (BlockLight light : lights) {
                            Bukkit.getScheduler().scheduleSyncDelayedTask(NMSHandler.getJavaPlugin(), () -> light.update(light.intendedLevel, false), 1);
                        }
                    }
                }
            }
            if (any) {
                Bukkit.getScheduler().scheduleSyncDelayedTask(NMSHandler.getJavaPlugin(), () -> sendNearbyChunkUpdates(chunk), 3);
            }
        }, 1);
    } catch (Exception ex) {
        Debug.echoError(ex);
    }
}
Also used : ChunkAccess(net.minecraft.world.level.chunk.ChunkAccess) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) ChunkCoordinate(com.denizenscript.denizen.utilities.blocks.ChunkCoordinate) BlockLight(com.denizenscript.denizen.nms.abstracts.BlockLight) BlockPos(net.minecraft.core.BlockPos) Vector(org.bukkit.util.Vector)

Aggregations

BlockPos (net.minecraft.core.BlockPos)54 BlockState (net.minecraft.world.level.block.state.BlockState)10 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)8 LevelChunk (net.minecraft.world.level.chunk.LevelChunk)8 Level (net.minecraft.world.level.Level)7 Direction (net.minecraft.core.Direction)6 Vec3 (net.minecraft.world.phys.Vec3)6 Nonnull (javax.annotation.Nonnull)5 ResourceLocation (net.minecraft.resources.ResourceLocation)5 ItemStack (net.minecraft.world.item.ItemStack)5 LocationTag (com.denizenscript.denizen.objects.LocationTag)4 ChunkCoordinate (com.denizenscript.denizen.utilities.blocks.ChunkCoordinate)4 FakeBlock (com.denizenscript.denizen.utilities.blocks.FakeBlock)4 Nullable (javax.annotation.Nullable)3 GameTest (net.minecraft.gametest.framework.GameTest)3 ChunkPos (net.minecraft.world.level.ChunkPos)3 CraftWorld (org.bukkit.craftbukkit.v1_18_R1.CraftWorld)3 BlockLight (com.denizenscript.denizen.nms.abstracts.BlockLight)2 CompoundTagBuilder (com.denizenscript.denizen.nms.util.jnbt.CompoundTagBuilder)2 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)2