|
| 1 | +/* |
| 2 | + * Copyright (c) 2019-2024 GeyserMC. http://geysermc.org |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | + * THE SOFTWARE. |
| 21 | + * |
| 22 | + * @author GeyserMC |
| 23 | + * @link https://github.com/GeyserMC/Geyser |
| 24 | + */ |
| 25 | + |
| 26 | +package org.geysermc.geyser.adapters.paper.v768; |
| 27 | + |
| 28 | +import it.unimi.dsi.fastutil.ints.IntArrayList; |
| 29 | +import it.unimi.dsi.fastutil.ints.IntList; |
| 30 | +import net.minecraft.core.Registry; |
| 31 | +import net.minecraft.core.registries.Registries; |
| 32 | +import net.minecraft.resources.ResourceLocation; |
| 33 | +import net.minecraft.server.MinecraftServer; |
| 34 | +import net.minecraft.world.level.biome.Biome; |
| 35 | +import net.minecraft.world.level.block.Block; |
| 36 | +import net.minecraft.world.level.block.state.BlockState; |
| 37 | +import net.minecraft.world.level.chunk.LevelChunk; |
| 38 | +import net.minecraft.world.level.chunk.LevelChunkSection; |
| 39 | +import org.bukkit.World; |
| 40 | +import org.bukkit.craftbukkit.CraftWorld; |
| 41 | +import org.geysermc.geyser.adapters.paper.PaperWorldAdapter; |
| 42 | + |
| 43 | +import java.util.ArrayList; |
| 44 | +import java.util.List; |
| 45 | +import java.util.stream.Stream; |
| 46 | + |
| 47 | +public class WorldAdapter_v768 extends PaperWorldAdapter { |
| 48 | + @Override |
| 49 | + public int getBlockAt(World world, int x, int y, int z) { |
| 50 | + if (y < world.getMinHeight()) { |
| 51 | + return 0; |
| 52 | + } |
| 53 | + |
| 54 | + LevelChunk chunk = ((CraftWorld) world).getHandle().getChunkIfLoaded(x >> 4, z >> 4); |
| 55 | + if (chunk == null) { // should never happen but just to be on the safe side |
| 56 | + return 0; |
| 57 | + } |
| 58 | + int worldOffset = world.getMinHeight() >> 4; |
| 59 | + int chunkOffset = (y >> 4) - worldOffset; |
| 60 | + if (chunkOffset < chunk.getSections().length) { |
| 61 | + LevelChunkSection section = chunk.getSections()[chunkOffset]; |
| 62 | + if (section != null && !section.hasOnlyAir()) { |
| 63 | + return Block.getId(section.getBlockState(x & 15, y & 15, z & 15)); |
| 64 | + } |
| 65 | + } |
| 66 | + return 0; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public IntList getAllBlockStates() { |
| 71 | + IntList blockStates = new IntArrayList(); |
| 72 | + for (BlockState block : Block.BLOCK_STATE_REGISTRY) { |
| 73 | + blockStates.add(Block.getId(block)); |
| 74 | + } |
| 75 | + return blockStates; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public String[] getBiomeSuggestions(boolean tags) { |
| 80 | + Registry<Biome> registry = MinecraftServer.getServer() |
| 81 | + .registryAccess() |
| 82 | + .lookupOrThrow(Registries.BIOME); |
| 83 | + if (!tags) { |
| 84 | + return getBiomes(registry).toArray(String[]::new); |
| 85 | + } |
| 86 | + |
| 87 | + List<String> keys = new ArrayList<>(registry.getTags().map(tag -> "#" + tag.key().location()).toList()); |
| 88 | + keys.addAll(getBiomes(registry).toList()); |
| 89 | + return keys.toArray(new String[0]); |
| 90 | + } |
| 91 | + |
| 92 | + private Stream<String> getBiomes(Registry<Biome> registry) { |
| 93 | + return registry.keySet().stream().map(ResourceLocation::toString); |
| 94 | + } |
| 95 | +} |
0 commit comments