Barteks2x

MixinWorld

May 10th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.18 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.lighting.LightingManager;
  27. import cubicchunks.util.AddressTools;
  28. import cubicchunks.world.ICubeCache;
  29. import cubicchunks.world.ICubicWorld;
  30. import cubicchunks.world.column.BlankColumn;
  31. import cubicchunks.world.column.Column;
  32. import cubicchunks.world.cube.Cube;
  33. import cubicchunks.worldgen.GeneratorStage;
  34. import net.minecraft.block.Block;
  35. import net.minecraft.block.state.IBlockState;
  36. import net.minecraft.entity.Entity;
  37. import net.minecraft.server.MinecraftServer;
  38. import net.minecraft.tileentity.TileEntity;
  39. import net.minecraft.util.math.BlockPos;
  40. import net.minecraft.world.EnumSkyBlock;
  41. import net.minecraft.world.GameRules;
  42. import net.minecraft.world.World;
  43. import net.minecraft.world.WorldProvider;
  44. import net.minecraft.world.WorldType;
  45. import net.minecraft.world.biome.BiomeGenBase;
  46. import net.minecraft.world.biome.BiomeProvider;
  47. import net.minecraft.world.chunk.IChunkProvider;
  48. import net.minecraft.world.storage.ISaveHandler;
  49. import net.minecraft.world.storage.WorldInfo;
  50. import org.spongepowered.asm.mixin.Final;
  51. import org.spongepowered.asm.mixin.Implements;
  52. import org.spongepowered.asm.mixin.Interface;
  53. import org.spongepowered.asm.mixin.Intrinsic;
  54. import org.spongepowered.asm.mixin.Mixin;
  55. import org.spongepowered.asm.mixin.Mutable;
  56. import org.spongepowered.asm.mixin.Shadow;
  57.  
  58. import java.util.Collection;
  59. import java.util.Random;
  60.  
  61. import static cubicchunks.util.Coords.blockToCube;
  62.  
  63. @Mixin(World.class)
  64. @Implements(@Interface(iface = ICubicWorld.class, prefix = "world$"))
  65. public abstract class MixinWorld implements ICubicWorld {
  66.  
  67.     @Shadow protected IChunkProvider chunkProvider;
  68.     @Shadow @Final public WorldProvider provider;
  69.     @Shadow @Final public Random rand;
  70.     @Shadow @Final public boolean isRemote;
  71.     @Shadow @Final @Mutable protected ISaveHandler saveHandler;
  72.  
  73.     protected LightingManager lightingManager;
  74.     protected boolean isCubicWorld;
  75.     protected int minHeight = 0, maxHeight = 256;
  76.     private boolean enableWorldGenPerfHack;
  77.  
  78.     @Shadow public abstract WorldType getWorldType();
  79.  
  80.     @Shadow public abstract void loadEntities(Collection<Entity> entities);
  81.  
  82.     @Shadow public abstract void addTileEntities(Collection<TileEntity> values);
  83.  
  84.     @Shadow public abstract void unloadEntities(Collection<Entity> entities);
  85.  
  86.     @Shadow public abstract void removeTileEntity(BlockPos pos);
  87.  
  88.     @Shadow public abstract long getTotalWorldTime();
  89.  
  90.     @Shadow public abstract void setTileEntity(BlockPos blockpos, TileEntity tileentity);
  91.  
  92.     @Shadow public abstract void markBlockRangeForRenderUpdate(BlockPos blockpos, BlockPos blockpos1);
  93.  
  94.     @Shadow public abstract boolean addTileEntity(TileEntity tileEntity);
  95.  
  96.     @Shadow
  97.     public abstract void markBlockRangeForRenderUpdate(int minBlockX, int minBlockY, int minBlockZ, int maxBlockX, int maxBlockY, int maxBlockZ);
  98.  
  99.     @Shadow public abstract long getSeed();
  100.  
  101.     @Shadow public abstract boolean checkLightFor(EnumSkyBlock sky, BlockPos pos);
  102.  
  103.     @Shadow public abstract ISaveHandler getSaveHandler();
  104.  
  105.     @Shadow public abstract MinecraftServer getMinecraftServer();
  106.  
  107.     @Shadow public abstract void addBlockEvent(BlockPos blockPos, Block i, int t, int p);
  108.  
  109.     @Shadow public abstract GameRules getGameRules();
  110.  
  111.     @Shadow public abstract WorldInfo getWorldInfo();
  112.  
  113.     @Shadow public abstract TileEntity getTileEntity(BlockPos pos);
  114.  
  115.     @Shadow public abstract boolean setBlockState(BlockPos blockPos, IBlockState blockState, int i);
  116.  
  117.     @Shadow public abstract IBlockState getBlockState(BlockPos pos);
  118.  
  119.     @Shadow public abstract boolean isAirBlock(BlockPos randomPos);
  120.  
  121.     @Shadow public abstract BiomeGenBase getBiomeGenForCoords(BlockPos cubeCenter);
  122.  
  123.     @Shadow public abstract BiomeProvider getBiomeProvider();
  124.  
  125.     @Shadow public abstract BlockPos getSpawnPoint();
  126.  
  127.     @Override public boolean isCubicWorld() {
  128.         return this.isCubicWorld;
  129.     }
  130.  
  131.     @Override public int getMinHeight() {
  132.         return this.minHeight;
  133.     }
  134.  
  135.     @Override public int getMaxHeight() {
  136.         return this.maxHeight;
  137.     }
  138.  
  139.     @Override public ICubeCache getCubeCache() {
  140.         return (ICubeCache) this.chunkProvider;
  141.     }
  142.  
  143.     @Override public LightingManager getLightingManager() {
  144.         return this.lightingManager;
  145.     }
  146.  
  147.     @Override
  148.     public boolean blocksExist(BlockPos pos, int dist, boolean allowEmptyCubes, GeneratorStage minStageAllowed) {
  149.         return blocksExist(
  150.                 pos.getX() - dist, pos.getY() - dist, pos.getZ() - dist,
  151.                 pos.getX() + dist, pos.getY() + dist, pos.getZ() + dist,
  152.                 allowEmptyCubes,
  153.                 minStageAllowed
  154.         );
  155.     }
  156.  
  157.     @Override
  158.     public boolean blocksExist(int minBlockX, int minBlockY, int minBlockZ, int maxBlockX, int maxBlockY, int maxBlockZ, boolean allowEmptyColumns, GeneratorStage minStageAllowed) {
  159.  
  160.         // convert block bounds to chunk bounds
  161.         int minCubeX = blockToCube(minBlockX);
  162.         int minCubeY = blockToCube(minBlockY);
  163.         int minCubeZ = blockToCube(minBlockZ);
  164.         int maxCubeX = blockToCube(maxBlockX);
  165.         int maxCubeY = blockToCube(maxBlockY);
  166.         int maxCubeZ = blockToCube(maxBlockZ);
  167.  
  168.         return cubesExist(minCubeX, minCubeY, minCubeZ, maxCubeX, maxCubeY, maxCubeZ, allowEmptyColumns, minStageAllowed);
  169.  
  170.     }
  171.  
  172.     @Override public boolean cubeAndNeighborsExist(Cube cube, boolean allowEmptyCubes, GeneratorStage minStageAllowed) {
  173.         return cubeAndNeighborsExist(cube.getX(), cube.getY(), cube.getZ(), allowEmptyCubes, minStageAllowed);
  174.     }
  175.  
  176.     @Override public Cube getCubeForAddress(long address) {
  177.         int x = AddressTools.getX(address);
  178.         int y = AddressTools.getY(address);
  179.         int z = AddressTools.getZ(address);
  180.  
  181.         return this.getCubeCache().getCube(x, y, z);
  182.     }
  183.  
  184.     @Override public Cube getCubeFromCubeCoords(int cubeX, int cubeY, int cubeZ) {
  185.         return this.getCubeCache().getCube(cubeX, cubeY, cubeZ);
  186.     }
  187.  
  188.     @Override public Cube getCubeFromBlockCoords(BlockPos pos) {
  189.         return this.getCubeFromCubeCoords(blockToCube(pos.getX()), blockToCube(pos.getY()), blockToCube(pos.getZ()));
  190.     }
  191.  
  192.     @Override public void setGeneratingWorld(boolean enable) {
  193.         this.enableWorldGenPerfHack = enable;
  194.     }
  195.  
  196.     private boolean cubeAndNeighborsExist(int cubeX, int cubeY, int cubeZ, boolean allowEmptyCubes, GeneratorStage minStageAllowed) {
  197.         return cubesExist(
  198.                 cubeX - 1, cubeY - 1, cubeZ - 1, cubeX + 1, cubeY + 1, cubeZ + 1, allowEmptyCubes, minStageAllowed);
  199.     }
  200.  
  201.     private boolean cubesExist(int minCubeX, int minCubeY, int minCubeZ, int maxCubeX, int maxCubeY, int maxCubeZ, boolean allowEmptyColumns, GeneratorStage minStageAllowed) {
  202.         if (this.enableWorldGenPerfHack) {
  203.             return true;
  204.         }
  205.         for (int cubeX = minCubeX; cubeX <= maxCubeX; cubeX++) {
  206.             for (int cubeY = minCubeY; cubeY <= maxCubeY; cubeY++) {
  207.                 for (int cubeZ = minCubeZ; cubeZ <= maxCubeZ; cubeZ++) {
  208.                     Cube cube = this.getCubeCache().getCube(cubeX, cubeY, cubeZ);
  209.                     if (cube == null) {
  210.                         return false;
  211.                     }
  212.                     Column column = cube.getColumn();
  213.                     if ((!allowEmptyColumns && column instanceof BlankColumn)
  214.                             || (minStageAllowed != null && cube.getGeneratorStage().isLessThan(minStageAllowed))) {
  215.                         return false;
  216.                     }
  217.                 }
  218.             }
  219.         }
  220.         return true;
  221.     }
  222.  
  223.     //vanilla field accessors
  224.  
  225.     @Override public WorldProvider getProvider() {
  226.         return this.provider;
  227.     }
  228.  
  229.     @Override public Random getRand() {
  230.         return this.rand;
  231.     }
  232.  
  233.     @Override public boolean isRemote() {
  234.         return this.isRemote;
  235.     }
  236.  
  237.     //vanilla methods
  238.  
  239.     @Intrinsic public void world$loadEntities(Collection<Entity> entities) {
  240.         this.loadEntities(entities);
  241.     }
  242.  
  243.     @Intrinsic public void world$addTileEntities(Collection<TileEntity> values) {
  244.         this.addTileEntities(values);
  245.     }
  246.  
  247.     @Intrinsic public void world$unloadEntities(Collection<Entity> entities) {
  248.         this.unloadEntities(entities);
  249.     }
  250.  
  251.     @Intrinsic public void world$removeTileEntity(BlockPos pos) {
  252.         this.removeTileEntity(pos);
  253.     }
  254.  
  255.     @Intrinsic public long world$getTotalWorldTime() {
  256.         return this.getTotalWorldTime();
  257.     }
  258.  
  259.     @Intrinsic public void world$setTileEntity(BlockPos blockpos, TileEntity tileentity) {
  260.         this.setTileEntity(blockpos, tileentity);
  261.     }
  262.  
  263.     @Intrinsic public void world$markBlockRangeForRenderUpdate(BlockPos blockpos, BlockPos blockpos1) {
  264.         this.markBlockRangeForRenderUpdate(blockpos, blockpos1);
  265.     }
  266.  
  267.     @Intrinsic public boolean world$addTileEntity(TileEntity tileEntity) {
  268.         return this.addTileEntity(tileEntity);
  269.     }
  270.  
  271.     @Intrinsic
  272.     public void world$markBlockRangeForRenderUpdate(int minBlockX, int minBlockY, int minBlockZ, int maxBlockX, int maxBlockY, int maxBlockZ) {
  273.         this.markBlockRangeForRenderUpdate(minBlockX, minBlockY, minBlockZ, maxBlockX, maxBlockY, maxBlockZ);
  274.     }
  275.  
  276.     @Intrinsic public long world$getSeed() {
  277.         return this.getSeed();
  278.     }
  279.  
  280.     @Intrinsic public boolean world$checkLightFor(EnumSkyBlock type, BlockPos pos) {
  281.         return this.checkLightFor(type, pos);
  282.     }
  283.  
  284.     @Intrinsic public ISaveHandler world$getSaveHandler() {
  285.         return this.getSaveHandler();
  286.     }
  287.  
  288.     @Intrinsic public MinecraftServer world$getMinecraftServer() {
  289.         return this.getMinecraftServer();
  290.     }
  291.  
  292.     @Intrinsic public void world$addBlockEvent(BlockPos blockPos, Block i, int t, int p) {
  293.         this.addBlockEvent(blockPos, i, t, p);
  294.     }
  295.  
  296.     @Intrinsic public GameRules world$getGameRules() {
  297.         return this.getGameRules();
  298.     }
  299.  
  300.     @Intrinsic public WorldInfo world$getWorldInfo() {
  301.         return this.getWorldInfo();
  302.     }
  303.  
  304.     @Intrinsic public TileEntity world$getTileEntity(BlockPos pos) {
  305.         return this.getTileEntity(pos);
  306.     }
  307.  
  308.     @Intrinsic public boolean world$setBlockState(BlockPos blockPos, IBlockState blockState, int i) {
  309.         return this.setBlockState(blockPos, blockState, i);
  310.     }
  311.  
  312.     @Intrinsic public IBlockState world$getBlockState(BlockPos pos) {
  313.         return this.getBlockState(pos);
  314.     }
  315.  
  316.     @Intrinsic public boolean world$isAirBlock(BlockPos pos) {
  317.         return this.isAirBlock(pos);
  318.     }
  319.  
  320.     @Intrinsic public BiomeGenBase world$getBiomeGenForCoords(BlockPos pos) {
  321.         return this.getBiomeGenForCoords(pos);
  322.     }
  323.  
  324.     @Intrinsic public BiomeProvider world$getBiomeProvider() {
  325.         return this.getBiomeProvider();
  326.     }
  327.  
  328.     @Intrinsic public BlockPos world$getSpawnPoint() {
  329.         return this.getSpawnPoint();
  330.     }
  331. }
Add Comment
Please, Sign In to add comment