|
| 1 | +package net.imprex.orebfuscator.nms.v1_21_R4; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Objects; |
| 8 | + |
| 9 | +import org.bukkit.Material; |
| 10 | +import org.bukkit.World; |
| 11 | +import org.bukkit.craftbukkit.v1_21_R4.CraftWorld; |
| 12 | +import org.bukkit.craftbukkit.v1_21_R4.block.data.CraftBlockData; |
| 13 | +import org.bukkit.craftbukkit.v1_21_R4.entity.CraftPlayer; |
| 14 | +import org.bukkit.entity.Player; |
| 15 | + |
| 16 | +import com.google.common.collect.ImmutableList; |
| 17 | + |
| 18 | +import it.unimi.dsi.fastutil.shorts.Short2ObjectLinkedOpenHashMap; |
| 19 | +import it.unimi.dsi.fastutil.shorts.Short2ObjectMap; |
| 20 | +import net.imprex.orebfuscator.config.Config; |
| 21 | +import net.imprex.orebfuscator.nms.AbstractNmsManager; |
| 22 | +import net.imprex.orebfuscator.nms.ReadOnlyChunk; |
| 23 | +import net.imprex.orebfuscator.util.BlockProperties; |
| 24 | +import net.imprex.orebfuscator.util.BlockStateProperties; |
| 25 | +import net.imprex.orebfuscator.util.NamespacedKey; |
| 26 | +import net.minecraft.core.BlockPos; |
| 27 | +import net.minecraft.core.SectionPos; |
| 28 | +import net.minecraft.core.registries.BuiltInRegistries; |
| 29 | +import net.minecraft.network.protocol.Packet; |
| 30 | +import net.minecraft.network.protocol.game.ClientGamePacketListener; |
| 31 | +import net.minecraft.network.protocol.game.ClientboundBlockUpdatePacket; |
| 32 | +import net.minecraft.network.protocol.game.ClientboundSectionBlocksUpdatePacket; |
| 33 | +import net.minecraft.resources.ResourceKey; |
| 34 | +import net.minecraft.server.level.ServerChunkCache; |
| 35 | +import net.minecraft.server.level.ServerLevel; |
| 36 | +import net.minecraft.server.level.ServerPlayer; |
| 37 | +import net.minecraft.world.level.block.Block; |
| 38 | +import net.minecraft.world.level.block.Blocks; |
| 39 | +import net.minecraft.world.level.block.entity.BlockEntity; |
| 40 | +import net.minecraft.world.level.block.state.BlockState; |
| 41 | +import net.minecraft.world.level.chunk.LevelChunk; |
| 42 | +import net.minecraft.world.level.chunk.LevelChunkSection; |
| 43 | + |
| 44 | +public class NmsManager extends AbstractNmsManager { |
| 45 | + |
| 46 | + private static final int BLOCK_ID_AIR = Block.getId(Blocks.AIR.defaultBlockState()); |
| 47 | + |
| 48 | + static int getBlockState(LevelChunk chunk, int x, int y, int z) { |
| 49 | + LevelChunkSection[] sections = chunk.getSections(); |
| 50 | + |
| 51 | + int sectionIndex = chunk.getSectionIndex(y); |
| 52 | + if (sectionIndex >= 0 && sectionIndex < sections.length) { |
| 53 | + LevelChunkSection section = sections[sectionIndex]; |
| 54 | + if (section != null && !section.hasOnlyAir()) { |
| 55 | + return Block.getId(section.getBlockState(x & 0xF, y & 0xF, z & 0xF)); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return BLOCK_ID_AIR; |
| 60 | + } |
| 61 | + |
| 62 | + private static ServerLevel level(World world) { |
| 63 | + return ((CraftWorld) world).getHandle(); |
| 64 | + } |
| 65 | + |
| 66 | + private static ServerPlayer player(Player player) { |
| 67 | + return ((CraftPlayer) player).getHandle(); |
| 68 | + } |
| 69 | + |
| 70 | + public NmsManager(Config config) { |
| 71 | + super(Block.BLOCK_STATE_REGISTRY.size(), new RegionFileCache(config.cache())); |
| 72 | + |
| 73 | + for (Map.Entry<ResourceKey<Block>, Block> entry : BuiltInRegistries.BLOCK.entrySet()) { |
| 74 | + NamespacedKey namespacedKey = NamespacedKey.fromString(entry.getKey().location().toString()); |
| 75 | + Block block = entry.getValue(); |
| 76 | + |
| 77 | + ImmutableList<BlockState> possibleBlockStates = block.getStateDefinition().getPossibleStates(); |
| 78 | + BlockProperties.Builder builder = BlockProperties.builder(namespacedKey); |
| 79 | + |
| 80 | + for (BlockState blockState : possibleBlockStates) { |
| 81 | + Material material = CraftBlockData.fromData(blockState).getMaterial(); |
| 82 | + |
| 83 | + BlockStateProperties properties = BlockStateProperties.builder(Block.getId(blockState)) |
| 84 | + .withIsAir(blockState.isAir()) |
| 85 | + // check if material is occluding and use blockData check for rare edge cases like barrier, spawner, slime_block, ... |
| 86 | + .withIsOccluding(material.isOccluding() && blockState.canOcclude()) |
| 87 | + .withIsBlockEntity(blockState.hasBlockEntity()) |
| 88 | + .withIsDefaultState(Objects.equals(block.defaultBlockState(), blockState)) |
| 89 | + .build(); |
| 90 | + |
| 91 | + builder.withBlockState(properties); |
| 92 | + } |
| 93 | + |
| 94 | + this.registerBlockProperties(builder.build()); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public ReadOnlyChunk getReadOnlyChunk(World world, int chunkX, int chunkZ) { |
| 100 | + ServerChunkCache serverChunkCache = level(world).getChunkSource(); |
| 101 | + LevelChunk chunk = serverChunkCache.getChunk(chunkX, chunkZ, true); |
| 102 | + return new ReadOnlyChunkWrapper(chunk); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public int getBlockState(World world, int x, int y, int z) { |
| 107 | + ServerChunkCache serverChunkCache = level(world).getChunkSource(); |
| 108 | + if (!serverChunkCache.isChunkLoaded(x >> 4, z >> 4)) { |
| 109 | + return BLOCK_ID_AIR; |
| 110 | + } |
| 111 | + |
| 112 | + LevelChunk chunk = serverChunkCache.getChunk(x >> 4, z >> 4, true); |
| 113 | + if (chunk == null) { |
| 114 | + return BLOCK_ID_AIR; |
| 115 | + } |
| 116 | + |
| 117 | + return getBlockState(chunk, x, y, z); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void sendBlockUpdates(World world, Iterable<net.imprex.orebfuscator.util.BlockPos> iterable) { |
| 122 | + ServerChunkCache serverChunkCache = level(world).getChunkSource(); |
| 123 | + BlockPos.MutableBlockPos position = new BlockPos.MutableBlockPos(); |
| 124 | + |
| 125 | + for (net.imprex.orebfuscator.util.BlockPos pos : iterable) { |
| 126 | + position.set(pos.x, pos.y, pos.z); |
| 127 | + serverChunkCache.blockChanged(position); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void sendBlockUpdates(Player player, Iterable<net.imprex.orebfuscator.util.BlockPos> iterable) { |
| 133 | + ServerPlayer serverPlayer = player(player); |
| 134 | + ServerLevel level = serverPlayer.serverLevel(); |
| 135 | + ServerChunkCache serverChunkCache = level.getChunkSource(); |
| 136 | + |
| 137 | + BlockPos.MutableBlockPos position = new BlockPos.MutableBlockPos(); |
| 138 | + Map<SectionPos, Short2ObjectMap<BlockState>> sectionPackets = new HashMap<>(); |
| 139 | + List<Packet<ClientGamePacketListener>> blockEntityPackets = new ArrayList<>(); |
| 140 | + |
| 141 | + for (net.imprex.orebfuscator.util.BlockPos pos : iterable) { |
| 142 | + if (!serverChunkCache.isChunkLoaded(pos.x >> 4, pos.z >> 4)) { |
| 143 | + continue; |
| 144 | + } |
| 145 | + |
| 146 | + position.set(pos.x, pos.y, pos.z); |
| 147 | + BlockState blockState = level.getBlockState(position); |
| 148 | + |
| 149 | + sectionPackets.computeIfAbsent(SectionPos.of(position), key -> new Short2ObjectLinkedOpenHashMap<>()) |
| 150 | + .put(SectionPos.sectionRelativePos(position), blockState); |
| 151 | + |
| 152 | + if (blockState.hasBlockEntity()) { |
| 153 | + BlockEntity blockEntity = level.getBlockEntity(position); |
| 154 | + if (blockEntity != null) { |
| 155 | + blockEntityPackets.add(blockEntity.getUpdatePacket()); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + for (Map.Entry<SectionPos, Short2ObjectMap<BlockState>> entry : sectionPackets.entrySet()) { |
| 161 | + Short2ObjectMap<BlockState> blockStates = entry.getValue(); |
| 162 | + if (blockStates.size() == 1) { |
| 163 | + Short2ObjectMap.Entry<BlockState> blockEntry = blockStates.short2ObjectEntrySet().iterator().next(); |
| 164 | + BlockPos blockPosition = entry.getKey().relativeToBlockPos(blockEntry.getShortKey()); |
| 165 | + serverPlayer.connection.send(new ClientboundBlockUpdatePacket(blockPosition, blockEntry.getValue())); |
| 166 | + } else { |
| 167 | + serverPlayer.connection.send(new ClientboundSectionBlocksUpdatePacket(entry.getKey(), |
| 168 | + blockStates.keySet(), blockStates.values().toArray(BlockState[]::new))); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + for (Packet<ClientGamePacketListener> packet : blockEntityPackets) { |
| 173 | + serverPlayer.connection.send(packet); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments