001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.core.util; 020 021import com.plotsquared.core.PlotSquared; 022import com.plotsquared.core.configuration.caption.Caption; 023import com.plotsquared.core.location.Location; 024import com.plotsquared.core.player.PlotPlayer; 025import com.plotsquared.core.plot.Plot; 026import com.plotsquared.core.util.task.RunnableVal; 027import com.sk89q.jnbt.CompoundTag; 028import com.sk89q.jnbt.IntTag; 029import com.sk89q.jnbt.NBTInputStream; 030import com.sk89q.jnbt.NBTOutputStream; 031import com.sk89q.jnbt.Tag; 032import com.sk89q.worldedit.math.BlockVector2; 033import com.sk89q.worldedit.regions.CuboidRegion; 034import com.sk89q.worldedit.world.World; 035import com.sk89q.worldedit.world.biome.BiomeType; 036import com.sk89q.worldedit.world.block.BlockState; 037import com.sk89q.worldedit.world.block.BlockType; 038import com.sk89q.worldedit.world.entity.EntityType; 039import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; 040import org.checkerframework.checker.index.qual.NonNegative; 041import org.checkerframework.checker.nullness.qual.NonNull; 042import org.checkerframework.checker.nullness.qual.Nullable; 043 044import java.io.ByteArrayOutputStream; 045import java.io.File; 046import java.io.FileInputStream; 047import java.io.IOException; 048import java.io.OutputStream; 049import java.net.URL; 050import java.util.Collection; 051import java.util.HashMap; 052import java.util.HashSet; 053import java.util.Map; 054import java.util.Set; 055import java.util.UUID; 056import java.util.function.Consumer; 057import java.util.function.IntConsumer; 058import java.util.zip.GZIPInputStream; 059import java.util.zip.GZIPOutputStream; 060import java.util.zip.ZipEntry; 061import java.util.zip.ZipOutputStream; 062 063public abstract class WorldUtil { 064 065 /** 066 * Set the biome in a region 067 * 068 * @param world World name 069 * @param region Region 070 * @param biome Biome 071 * @since 6.6.0 072 */ 073 public static void setBiome(String world, final CuboidRegion region, BiomeType biome) { 074 PlotSquared.platform().worldUtil().setBiomes(world, region, biome); 075 } 076 077 /** 078 * Check if a given world name corresponds to a real world 079 * 080 * @param worldName World name 081 * @return {@code true} if there exists a world with the given world name, 082 * {@code false} if not 083 */ 084 public abstract boolean isWorld(@NonNull String worldName); 085 086 /** 087 * @param location Sign location 088 * @return Sign content (or an empty string array if the block is not a sign) 089 * @deprecated May result in synchronous chunk loading 090 */ 091 @Deprecated 092 public @NonNull 093 abstract String[] getSignSynchronous(@NonNull Location location); 094 095 /** 096 * Get the world spawn location 097 * 098 * @param world World name 099 * @return World spawn location 100 */ 101 public @NonNull 102 abstract Location getSpawn(@NonNull String world); 103 104 /** 105 * Set the world spawn location 106 * 107 * @param location New spawn 108 */ 109 public abstract void setSpawn(@NonNull Location location); 110 111 /** 112 * Save a world 113 * 114 * @param world World name 115 */ 116 public abstract void saveWorld(@NonNull String world); 117 118 /** 119 * Get a string comparison with the closets block state matching a given string 120 * 121 * @param name Block name 122 * @return Comparison result containing the closets matching block 123 */ 124 public @NonNull 125 abstract StringComparison<BlockState>.ComparisonResult getClosestBlock(@NonNull String name); 126 127 /** 128 * Set the block at the specified location to a sign, with given text 129 * 130 * @param location Block location 131 * @param lines Sign text 132 * @param replacements Text replacements 133 */ 134 public abstract void setSign( 135 @NonNull Location location, 136 @NonNull Caption[] lines, 137 @NonNull TagResolver... replacements 138 ); 139 140 /** 141 * Get the biome in a given chunk, asynchronously 142 * 143 * @param world World 144 * @param x Chunk X coordinate 145 * @param z Chunk Z coordinate 146 * @param result Result consumer 147 */ 148 public abstract void getBiome(@NonNull String world, int x, int z, @NonNull Consumer<BiomeType> result); 149 150 /** 151 * Get the biome in a given chunk, asynchronously 152 * 153 * @param world World 154 * @param x Chunk X coordinate 155 * @param z Chunk Z coordinate 156 * @return Biome 157 * @deprecated Use {@link #getBiome(String, int, int, Consumer)} 158 */ 159 @Deprecated 160 public @NonNull 161 abstract BiomeType getBiomeSynchronous(@NonNull String world, int x, int z); 162 163 /** 164 * Get the block at a given location (asynchronously) 165 * 166 * @param location Block location 167 * @param result Result consumer 168 */ 169 public abstract void getBlock(@NonNull Location location, @NonNull Consumer<BlockState> result); 170 171 /** 172 * Get the block at a given location (synchronously) 173 * 174 * @param location Block location 175 * @return Result 176 * @deprecated Use {@link #getBlock(Location, Consumer)} 177 */ 178 @Deprecated 179 public @NonNull 180 abstract BlockState getBlockSynchronous(@NonNull Location location); 181 182 /** 183 * Get the Y coordinate of the highest non-air block in the world, asynchronously 184 * 185 * @param world World name 186 * @param x X coordinate 187 * @param z Z coordinate 188 * @param result Result consumer 189 */ 190 public abstract void getHighestBlock(@NonNull String world, int x, int z, @NonNull IntConsumer result); 191 192 /** 193 * Get the Y coordinate of the highest non-air block in the world, synchronously 194 * 195 * @param world World name 196 * @param x X coordinate 197 * @param z Z coordinate 198 * @return Result 199 * @deprecated Use {@link #getHighestBlock(String, int, int, IntConsumer)} 200 */ 201 @Deprecated 202 @NonNegative 203 public abstract int getHighestBlockSynchronous(@NonNull String world, int x, int z); 204 205 /** 206 * Set the biome in a region 207 * 208 * @param worldName World name 209 * @param region Region 210 * @param biome New biome 211 */ 212 public void setBiomes(@NonNull String worldName, @NonNull CuboidRegion region, @NonNull BiomeType biome) { 213 final World world = getWeWorld(worldName); 214 region.forEach(bv -> world.setBiome(bv, biome)); 215 } 216 217 /** 218 * Get the WorldEdit {@link com.sk89q.worldedit.world.World} corresponding to a world name 219 * 220 * @param world World name 221 * @return World object 222 */ 223 public abstract com.sk89q.worldedit.world.@NonNull World getWeWorld(@NonNull String world); 224 225 /** 226 * Refresh (resend) chunk to player. Usually after setting the biome 227 * 228 * @param x Chunk x location 229 * @param z Chunk z location 230 * @param world World of the chunk 231 */ 232 public abstract void refreshChunk(int x, int z, String world); 233 234 /** 235 * The legacy web interface is deprecated for removal in favor of Arkitektonika. 236 */ 237 @Deprecated(forRemoval = true, since = "6.11.0") 238 public void upload( 239 final @NonNull Plot plot, 240 final @Nullable UUID uuid, 241 final @Nullable String file, 242 final @NonNull RunnableVal<URL> whenDone 243 ) { 244 plot.getHome(home -> SchematicHandler.upload(uuid, file, "zip", new RunnableVal<>() { 245 @Override 246 public void run(OutputStream output) { 247 try (final ZipOutputStream zos = new ZipOutputStream(output)) { 248 File dat = getDat(plot.getWorldName()); 249 Location spawn = getSpawn(plot.getWorldName()); 250 if (dat != null) { 251 ZipEntry ze = new ZipEntry("world" + File.separator + dat.getName()); 252 zos.putNextEntry(ze); 253 try (NBTInputStream nis = new NBTInputStream(new GZIPInputStream(new FileInputStream(dat)))) { 254 Map<String, Tag> tag = ((CompoundTag) nis.readNamedTag().getTag()).getValue(); 255 Map<String, Tag> newMap = new HashMap<>(); 256 for (Map.Entry<String, Tag> entry : tag.entrySet()) { 257 if (!entry.getKey().equals("Data")) { 258 newMap.put(entry.getKey(), entry.getValue()); 259 continue; 260 } 261 Map<String, Tag> data = new HashMap<>(((CompoundTag) entry.getValue()).getValue()); 262 data.put("SpawnX", new IntTag(home.getX())); 263 data.put("SpawnY", new IntTag(home.getY())); 264 data.put("SpawnZ", new IntTag(home.getZ())); 265 newMap.put("Data", new CompoundTag(data)); 266 } 267 try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { 268 try (NBTOutputStream out = new NBTOutputStream(new GZIPOutputStream(baos, true))) { 269 //TODO Find what this should be called 270 out.writeNamedTag("Schematic????", new CompoundTag(newMap)); 271 } 272 zos.write(baos.toByteArray()); 273 } 274 } 275 } 276 setSpawn(spawn); 277 byte[] buffer = new byte[1024]; 278 Set<BlockVector2> added = new HashSet<>(); 279 for (Plot current : plot.getConnectedPlots()) { 280 Location bot = current.getBottomAbs(); 281 Location top = current.getTopAbs(); 282 int brx = bot.getX() >> 9; 283 int brz = bot.getZ() >> 9; 284 int trx = top.getX() >> 9; 285 int trz = top.getZ() >> 9; 286 Set<BlockVector2> files = getChunkChunks(bot.getWorldName()); 287 for (BlockVector2 mca : files) { 288 if (mca.getX() >= brx && mca.getX() <= trx && mca.getZ() >= brz && mca.getZ() <= trz && !added.contains( 289 mca)) { 290 final File file = getMcr(plot.getWorldName(), mca.getX(), mca.getZ()); 291 if (file != null) { 292 //final String name = "r." + (x - cx) + "." + (z - cz) + ".mca"; 293 String name = file.getName(); 294 final ZipEntry ze = new ZipEntry("world" + File.separator + "region" + File.separator + name); 295 zos.putNextEntry(ze); 296 added.add(mca); 297 try (FileInputStream in = new FileInputStream(file)) { 298 int len; 299 while ((len = in.read(buffer)) > 0) { 300 zos.write(buffer, 0, len); 301 } 302 } 303 zos.closeEntry(); 304 } 305 } 306 } 307 } 308 zos.closeEntry(); 309 zos.flush(); 310 zos.finish(); 311 } catch (IOException e) { 312 e.printStackTrace(); 313 } 314 } 315 }, whenDone)); 316 } 317 318 final @Nullable File getDat(final @NonNull String world) { 319 File file = new File(PlotSquared.platform().worldContainer() + File.separator + world + File.separator + "level.dat"); 320 if (file.exists()) { 321 return file; 322 } 323 return null; 324 } 325 326 @Nullable 327 private File getMcr(final @NonNull String world, final int x, final int z) { 328 final File file = 329 new File( 330 PlotSquared.platform().worldContainer(), 331 world + File.separator + "region" + File.separator + "r." + x + '.' + z + ".mca" 332 ); 333 if (file.exists()) { 334 return file; 335 } 336 return null; 337 } 338 339 340 public Set<BlockVector2> getChunkChunks(String world) { 341 File folder = new File(PlotSquared.platform().worldContainer(), world + File.separator + "region"); 342 File[] regionFiles = folder.listFiles(); 343 if (regionFiles == null) { 344 throw new RuntimeException("Could not find worlds folder: " + folder + " ? (no read access?)"); 345 } 346 HashSet<BlockVector2> chunks = new HashSet<>(); 347 for (File file : regionFiles) { 348 String name = file.getName(); 349 if (name.endsWith("mca")) { 350 String[] split = name.split("\\."); 351 try { 352 int x = Integer.parseInt(split[1]); 353 int z = Integer.parseInt(split[2]); 354 BlockVector2 loc = BlockVector2.at(x, z); 355 chunks.add(loc); 356 } catch (NumberFormatException ignored) { 357 } 358 } 359 } 360 return chunks; 361 } 362 363 /** 364 * Check if two blocks are the same type) 365 * 366 * @param block1 First block 367 * @param block2 Second block 368 * @return {@code true} if the blocks have the same type, {@code false} if not 369 */ 370 public abstract boolean isBlockSame(@NonNull BlockState block1, @NonNull BlockState block2); 371 372 /** 373 * Get the player health 374 * 375 * @param player Player 376 * @return Non-negative health 377 */ 378 @NonNegative 379 public abstract double getHealth(@NonNull PlotPlayer<?> player); 380 381 /** 382 * Set the player health 383 * 384 * @param player Player health 385 * @param health Non-negative health 386 */ 387 public abstract void setHealth(@NonNull PlotPlayer<?> player, @NonNegative double health); 388 389 /** 390 * Get the player food level 391 * 392 * @param player Player 393 * @return Non-negative food level 394 */ 395 @NonNegative 396 public abstract int getFoodLevel(@NonNull PlotPlayer<?> player); 397 398 /** 399 * Set the player food level 400 * 401 * @param player Player food level 402 * @param foodLevel Non-negative food level 403 */ 404 public abstract void setFoodLevel(@NonNull PlotPlayer<?> player, @NonNegative int foodLevel); 405 406 /** 407 * Get all entity types belonging to an entity category 408 * 409 * @param category Entity category 410 * @return Set containing all entities belonging to the given category 411 */ 412 public @NonNull 413 abstract Set<EntityType> getTypesInCategory(@NonNull String category); 414 415 /** 416 * Get all recognized tile entity types 417 * 418 * @return Collection containing all known tile entity types 419 */ 420 public @NonNull 421 abstract Collection<BlockType> getTileEntityTypes(); 422 423 /** 424 * Get the tile entity count in a chunk 425 * 426 * @param world World 427 * @param chunk Chunk coordinates 428 * @return Tile entity count 429 */ 430 @NonNegative 431 public abstract int getTileEntityCount(@NonNull String world, @NonNull BlockVector2 chunk); 432 433}