Barteks2x

MixinWorldServer

May 10th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.99 KB | None | 0 0
  1. /*
  2.  *  This file is part of Cubic Chunks Mod, licensed under the MIT License (MIT).
  3.  *
  4.  *  Copyright (c) 2015 contributors
  5.  *
  6.  *  Permission is hereby granted, free of charge, to any person obtaining a copy
  7.  *  of this software and associated documentation files (the "Software"), to deal
  8.  *  in the Software without restriction, including without limitation the rights
  9.  *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10.  *  copies of the Software, and to permit persons to whom the Software is
  11.  *  furnished to do so, subject to the following conditions:
  12.  *
  13.  *  The above copyright notice and this permission notice shall be included in
  14.  *  all copies or substantial portions of the Software.
  15.  *
  16.  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19.  *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21.  *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22.  *  THE SOFTWARE.
  23.  */
  24. package cubicchunks.asm.mixin.core;
  25.  
  26. import cubicchunks.CubicChunks;
  27. import cubicchunks.ICubicChunksWorldType;
  28. import cubicchunks.lighting.LightingManager;
  29. import cubicchunks.server.CubePlayerManager;
  30. import cubicchunks.server.ServerCubeCache;
  31. import cubicchunks.util.Coords;
  32. import cubicchunks.world.CubicChunksSaveHandlerWrapper;
  33. import cubicchunks.world.ICubicWorldServer;
  34. import cubicchunks.worldgen.GeneratorPipeline;
  35. import net.minecraft.server.management.PlayerManager;
  36. import net.minecraft.util.math.BlockPos;
  37. import net.minecraft.world.WorldServer;
  38. import net.minecraft.world.storage.ISaveHandler;
  39. import org.spongepowered.asm.mixin.Final;
  40. import org.spongepowered.asm.mixin.Mixin;
  41. import org.spongepowered.asm.mixin.Mutable;
  42. import org.spongepowered.asm.mixin.Shadow;
  43.  
  44. import static cubicchunks.server.ServerCubeCache.LoadType.LOAD_OR_GENERATE;
  45.  
  46. @Mixin(WorldServer.class)
  47. public abstract class MixinWorldServer extends MixinWorld implements ICubicWorldServer {
  48.     @Shadow @Mutable @Final private PlayerManager thePlayerManager;
  49.     @Shadow public boolean disableLevelSaving;
  50.  
  51.     private GeneratorPipeline generatorPipeline;
  52.  
  53.     @Override public void initCubicWorld() {
  54.         this.isCubicWorld = true;
  55.  
  56.         ServerCubeCache serverCubeCache = new ServerCubeCache(this);
  57.         this.chunkProvider = serverCubeCache;
  58.         this.thePlayerManager = new CubePlayerManager(this);
  59.  
  60.         this.lightingManager = new LightingManager(this);
  61.         this.generatorPipeline = new GeneratorPipeline(serverCubeCache);
  62.  
  63.         ICubicChunksWorldType type = (ICubicChunksWorldType) this.getWorldType();
  64.         type.registerWorldGen(this, this.generatorPipeline);
  65.         this.generatorPipeline.checkStages();
  66.  
  67.         this.maxHeight = type.getMaxHeight();
  68.         this.minHeight = type.getMinHeight();
  69.  
  70.         final ISaveHandler orig = this.saveHandler;
  71.         this.saveHandler = new CubicChunksSaveHandlerWrapper(this, orig);
  72.     }
  73.  
  74.     @Override public void generateWorld() {
  75.         ServerCubeCache serverCubeCache = this.getCubeCache();
  76.  
  77.         // load the cubes around the spawn point
  78.         CubicChunks.LOGGER.info("Loading cubes for spawn...");
  79.         final int spawnDistance = ServerCubeCache.WorldSpawnChunkDistance;
  80.         BlockPos spawnPoint = this.getSpawnPoint();
  81.         int spawnCubeX = Coords.blockToCube(spawnPoint.getX());
  82.         int spawnCubeY = Coords.blockToCube(spawnPoint.getY());
  83.         int spawnCubeZ = Coords.blockToCube(spawnPoint.getZ());
  84.         for (int cubeX = spawnCubeX - spawnDistance; cubeX <= spawnCubeX + spawnDistance; cubeX++) {
  85.             for (int cubeZ = spawnCubeZ - spawnDistance; cubeZ <= spawnCubeZ + spawnDistance; cubeZ++) {
  86.                 for (int cubeY = spawnCubeY + spawnDistance; cubeY >= spawnCubeY - spawnDistance; cubeY--) {
  87.                     serverCubeCache.loadCube(cubeX, cubeY, cubeZ, LOAD_OR_GENERATE);
  88.                 }
  89.             }
  90.         }
  91.  
  92.         // wait for the cubes to be loaded
  93.         GeneratorPipeline pipeline = this.getGeneratorPipeline();
  94.         int numCubesTotal = pipeline.getNumCubes();
  95.         if (numCubesTotal > 0) {
  96.             long timeStart = System.currentTimeMillis();
  97.             //CubicChunks.LOGGER.info("Generating {} cubes for spawn at block ({},{},{}) cube ({},{},{})...",
  98.             //      numCubesTotal, spawnPoint.getX(), spawnPoint.getY(), spawnPoint.getZ(), spawnCubeX, spawnCubeY,
  99.             //      spawnCubeZ);
  100.             pipeline.generateAll();
  101.             long timeDiff = System.currentTimeMillis() - timeStart;
  102.             //CubicChunks.LOGGER.info("Done in {} ms", timeDiff);
  103.         }
  104.  
  105.         // don't save cubes here. Vanilla doesn't do that.
  106.         // and saving chunks here would be extremely slow
  107.         // serverCubeCache.saveAllChunks();
  108.     }
  109.  
  110.     @Override public ServerCubeCache getCubeCache() {
  111.         return (ServerCubeCache) this.chunkProvider;
  112.     }
  113.  
  114.     @Override public GeneratorPipeline getGeneratorPipeline() {
  115.         return this.generatorPipeline;
  116.     }
  117.  
  118.     //vanilla field accessors
  119.  
  120.     @Override public boolean getDisableLevelSaving() {
  121.         return this.disableLevelSaving;
  122.     }
  123.  
  124. }
Add Comment
Please, Sign In to add comment