|
| 1 | +package me.outspending.biomesapi; |
| 2 | + |
| 3 | +import com.google.common.base.Preconditions; |
| 4 | +import me.outspending.biomesapi.unsafe.UnsafeNMS; |
| 5 | +import net.minecraft.core.Holder; |
| 6 | +import net.minecraft.resources.Identifier; |
| 7 | +import net.minecraft.world.level.biome.Biome; |
| 8 | +import org.bukkit.Chunk; |
| 9 | + |
| 10 | +import java.util.Optional; |
| 11 | +import java.util.concurrent.CompletableFuture; |
| 12 | + |
| 13 | +import net.minecraft.core.MappedRegistry; |
| 14 | +import net.minecraft.core.Registry; |
| 15 | +import net.minecraft.core.registries.Registries; |
| 16 | +import net.minecraft.network.protocol.game.ClientboundLevelChunkWithLightPacket; |
| 17 | +import net.minecraft.resources.ResourceKey; |
| 18 | +import net.minecraft.server.dedicated.DedicatedServer; |
| 19 | +import net.minecraft.world.level.chunk.LevelChunk; |
| 20 | +import net.minecraft.world.level.chunk.status.ChunkStatus; |
| 21 | +import net.minecraft.world.level.lighting.LevelLightEngine; |
| 22 | +import org.bukkit.Bukkit; |
| 23 | +import org.bukkit.Location; |
| 24 | +import org.bukkit.NamespacedKey; |
| 25 | +import org.bukkit.craftbukkit.CraftChunk; |
| 26 | +import org.bukkit.craftbukkit.CraftServer; |
| 27 | +import org.bukkit.craftbukkit.entity.CraftPlayer; |
| 28 | +import org.bukkit.entity.Player; |
| 29 | +import org.bukkit.plugin.Plugin; |
| 30 | +import org.jetbrains.annotations.NotNull; |
| 31 | +import org.jetbrains.annotations.Nullable; |
| 32 | + |
| 33 | +import java.lang.reflect.Field; |
| 34 | +import java.lang.reflect.InvocationTargetException; |
| 35 | +import java.lang.reflect.Method; |
| 36 | +import java.util.List; |
| 37 | +import java.util.function.Supplier; |
| 38 | + |
| 39 | +/** |
| 40 | + * Implementation of UnsafeNMS for Minecraft version 26.1.1. |
| 41 | + * This class provides methods to interact with the Minecraft server's internal mechanics, |
| 42 | + * such as updating chunks and managing the biome registry. |
| 43 | + * |
| 44 | + * @author Jsinco |
| 45 | + * @version 1.2.1 |
| 46 | + * @since 1.2.1 |
| 47 | + */ |
| 48 | +public class UnsafeNMS_v26_1_1 implements UnsafeNMS { |
| 49 | + |
| 50 | + /** |
| 51 | + * Retrieves the registry for a given key. |
| 52 | + * |
| 53 | + * @param key The key for the registry to retrieve. |
| 54 | + * @return The registry associated with the given key. |
| 55 | + */ |
| 56 | + private static <T> MappedRegistry<@NotNull T> getRegistry(ResourceKey<@NotNull Registry<@NotNull T>> key) { |
| 57 | + DedicatedServer server = ((CraftServer) Bukkit.getServer()).getServer(); |
| 58 | + return (MappedRegistry<@NotNull T>) server.registryAccess().lookup(key).orElseThrow(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Updates a list of chunks asynchronously. |
| 63 | + * For each chunk, it creates a packet with the chunk's data and sends it to all players within the chunk's view distance. |
| 64 | + * |
| 65 | + * @param chunks The chunks to update. |
| 66 | + */ |
| 67 | + @Override |
| 68 | + public void updateChunks(@NotNull List<CompletableFuture<Chunk>> chunks, @Nullable Plugin plugin) { |
| 69 | + CompletableFuture.runAsync(() -> { |
| 70 | + for (CompletableFuture<Chunk> chunkFuture : chunks) { |
| 71 | + chunkFuture.thenAccept(chunk -> { |
| 72 | + if (plugin != null) { |
| 73 | + Bukkit.getRegionScheduler().run(plugin, chunk.getWorld(), chunk.getX(), chunk.getZ(), task -> { |
| 74 | + doUpdateChunk(chunk); |
| 75 | + }); |
| 76 | + } else { |
| 77 | + doUpdateChunk(chunk); |
| 78 | + } |
| 79 | + }).exceptionally(ex -> { |
| 80 | + ex.printStackTrace(); |
| 81 | + return null; |
| 82 | + }); |
| 83 | + } |
| 84 | + }).exceptionally(ex -> { |
| 85 | + ex.printStackTrace(); |
| 86 | + return null; |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + private void doUpdateChunk(Chunk chunk) { |
| 91 | + LevelChunk levelChunk = (LevelChunk) ((CraftChunk) chunk).getHandle(ChunkStatus.BIOMES); |
| 92 | + LevelLightEngine levelLightEngine = levelChunk.getLevel().getLightEngine(); |
| 93 | + |
| 94 | + ClientboundLevelChunkWithLightPacket packet = new ClientboundLevelChunkWithLightPacket(levelChunk, levelLightEngine, null, null, true); |
| 95 | + for (Player player : getPlayersInDistance(chunk)) { |
| 96 | + ((CraftPlayer) player).getHandle().connection.send(packet); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Locks or unlocks the biome registry. |
| 102 | + * It uses reflection to access the private boolean field in the registry class and sets its value. |
| 103 | + * |
| 104 | + * @param lock true to lock the biome registry, false to unlock it. |
| 105 | + */ |
| 106 | + @Override |
| 107 | + public void biomeRegistryLock(boolean lock) { |
| 108 | + MappedRegistry<@NotNull Biome> biomes = getRegistry(Registries.BIOME); |
| 109 | + try { |
| 110 | + Class<?> registryClass = biomes.getClass(); |
| 111 | + Field field = registryClass.getDeclaredField("frozen"); |
| 112 | + field.setAccessible(true); |
| 113 | + field.setBoolean(biomes, lock); |
| 114 | + } catch (NoSuchFieldException | IllegalAccessException e) { |
| 115 | + throw new RuntimeException("Failed to change biome registry lock state", e); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Unlocks the registry, performs an operation supplied by the supplier, and then freezes the registry. |
| 121 | + * This method is useful for performing operations on the registry that require it to be unlocked. |
| 122 | + * |
| 123 | + * @param supplier The supplier that provides the operation to perform on the unlocked registry. |
| 124 | + */ |
| 125 | + @Override |
| 126 | + public void unlockRegistry(@NotNull Supplier<?> supplier) { |
| 127 | + MappedRegistry<@NotNull Biome> registry = getRegistry(Registries.BIOME); |
| 128 | + biomeRegistryLock(false); |
| 129 | + supplier.get(); |
| 130 | + |
| 131 | + try { |
| 132 | + // Use reflection to set the 'allTags' field to an unbound TagSet |
| 133 | + Class<?> registryClass = registry.getClass(); |
| 134 | + Field field = registryClass.getDeclaredField("allTags"); |
| 135 | + field.setAccessible(true); |
| 136 | + Class<?> tagSetClass = Class.forName("net.minecraft.core.MappedRegistry$TagSet"); |
| 137 | + Method unboundMethod = tagSetClass.getDeclaredMethod("unbound"); |
| 138 | + unboundMethod.setAccessible(true); |
| 139 | + Object unboundTagSet = unboundMethod.invoke(null); |
| 140 | + field.set(registry, unboundTagSet); |
| 141 | + } catch (NoSuchFieldException | IllegalAccessException | ClassNotFoundException | InvocationTargetException | |
| 142 | + NoSuchMethodException e) { |
| 143 | + e.printStackTrace(); |
| 144 | + } |
| 145 | + registry.freeze(); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Retrieves the biome registry from the Minecraft server. |
| 150 | + * This method gets the server instance from the Bukkit API, accesses the registry of the server, |
| 151 | + * and retrieves the biome registry. If the biome registry cannot be retrieved, it throws a RuntimeException. |
| 152 | + * |
| 153 | + * @return The biome registry from the Minecraft server. |
| 154 | + * @throws RuntimeException if the biome registry cannot be retrieved. |
| 155 | + */ |
| 156 | + @Override |
| 157 | + public @NotNull Registry<@NotNull Biome> getRegistry() { |
| 158 | + |
| 159 | + return ((CraftServer) Bukkit.getServer()).getServer() |
| 160 | + .registryAccess() |
| 161 | + .lookup(Registries.BIOME) |
| 162 | + .orElseThrow(() -> new RuntimeException("Could not retrieve biome registry")); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public void updateBiome(@NotNull Location minLoc, @NotNull Location maxLoc, @NotNull NamespacedKey namespacedKey) { |
| 167 | + CompletableFuture.runAsync(() -> { |
| 168 | + |
| 169 | + String namespace = namespacedKey.getNamespace(); |
| 170 | + String path = namespacedKey.getKey(); |
| 171 | + |
| 172 | + ResourceKey<@NotNull Biome> biomeKey = ResourceKey.create(Registries.BIOME, Identifier.fromNamespaceAndPath(namespace, path)); |
| 173 | + Optional<Holder.Reference<@NotNull Biome>> biomeOptional = getRegistry().get(biomeKey); |
| 174 | + |
| 175 | + Preconditions.checkArgument(biomeOptional.isPresent(), "Biome with namespace " + namespace + ":" + path + " does not exist"); |
| 176 | + |
| 177 | + Holder<@NotNull Biome> biome = biomeOptional.orElseThrow(); |
| 178 | + |
| 179 | + for (int x = minLoc.getBlockX(); x <= maxLoc.getBlockX(); x++) { |
| 180 | + for (int y = minLoc.getBlockY(); y <= maxLoc.getBlockY(); y++) { |
| 181 | + for (int z = minLoc.getBlockZ(); z <= maxLoc.getBlockZ(); z++) { |
| 182 | + final int finalX = x; |
| 183 | + final int finalY = y; |
| 184 | + final int finalZ = z; |
| 185 | + |
| 186 | + minLoc.getWorld().getChunkAtAsync(x, z).thenAccept(chunk -> { |
| 187 | + LevelChunk levelChunk = (LevelChunk) ((CraftChunk) chunk).getHandle(ChunkStatus.BIOMES); |
| 188 | + levelChunk.setNoiseBiome(finalX, finalY, finalZ, biome); |
| 189 | + }); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + }); |
| 195 | + } |
| 196 | + |
| 197 | +} |
| 198 | + |
0 commit comments