Divided the Block Wrapper in two different class

This commit is contained in:
Leonardo
2021-10-26 12:31:06 +02:00
parent 12a32b9fb4
commit bde14012d9
5 changed files with 220 additions and 163 deletions
@@ -19,8 +19,6 @@
package com.seibel.lod.builders.lodBuilding;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -38,7 +36,8 @@ import com.seibel.lod.util.LodThreadFactory;
import com.seibel.lod.util.LodUtil;
import com.seibel.lod.util.ThreadMapUtil;
import com.seibel.lod.wrappers.Block.BlockPosWrapper;
import com.seibel.lod.wrappers.Block.BlockWrapper;
import com.seibel.lod.wrappers.Block.BlockColorWrapper;
import com.seibel.lod.wrappers.Block.BlockShapeWrapper;
import com.seibel.lod.wrappers.Chunk.ChunkPosWrapper;
import com.seibel.lod.wrappers.Chunk.ChunkWrapper;
import com.seibel.lod.wrappers.MinecraftWrapper;
@@ -192,8 +191,6 @@ public class LodBuilder
{
startX = detail.startX[i];
startZ = detail.startZ[i];
endX = detail.endX[i];
endZ = detail.endZ[i];
long[] data;
long[] dataToMergeVertical = createVerticalDataToMerge(detail, chunk, config, startX, startZ);
@@ -466,7 +463,7 @@ public class LodBuilder
{
int blockColor;
int colorOfBlock;
int colorInt;
int xRel = blockPos.getX() - chunk.getPos().getMinBlockX();
@@ -476,31 +473,39 @@ public class LodBuilder
int z = blockPos.getZ();
//Biome biome = chunk.getBiomes().getNoiseBiome(xRel >> 2, y >> 2, zRel >> 2);
BlockWrapper block;
if (chunk.isWaterLogged(blockPos))
block = BlockWrapper.getBlockWrapper(Blocks.WATER);
else
block = chunk.getBlock(blockPos);
BlockColorWrapper blockColorWrapper;
BlockShapeWrapper blockShapeWrapper;
if (block.isToAvoid())
if (chunk.isWaterLogged(blockPos))
{
blockColorWrapper = BlockColorWrapper.WATER_COLOR;
blockShapeWrapper = BlockShapeWrapper.WATER_SHAPE;
}
else
{
blockColorWrapper = chunk.getBlockColorWrapper(blockPos);
blockShapeWrapper = chunk.getBlockShapeWrapper(blockPos);
}
if (blockShapeWrapper.isToAvoid())
{
return 0;
}
blockColor = block.getColor();
colorOfBlock = blockColorWrapper.getColor();
if (block.hasTint())
if (blockColorWrapper.hasTint())
{
BiomeWrapper biome = chunk.getBiome(xRel, y, zRel);
int tintValue;
if (block.hasGrassTint())
if (blockColorWrapper.hasGrassTint())
{
// grass and green plants
tintValue = biome.getGrassTint(x, z);
}
else if (block.hasFolliageTint())
else if (blockColorWrapper.hasFolliageTint())
{
tintValue = biome.getFolliageTint();
}
@@ -510,10 +515,10 @@ public class LodBuilder
//tintValue = BiomeColors.getAverageFoliageColor(serverWorld, blockPos);
tintValue = biome.getWaterTint();
}
colorInt = ColorUtil.multiplyRGBcolors(tintValue | 0xFF000000, blockColor);
colorInt = ColorUtil.multiplyRGBcolors(tintValue | 0xFF000000, colorOfBlock);
}
else
colorInt = blockColor;
colorInt = colorOfBlock;
return colorInt;
}
@@ -529,7 +534,7 @@ public class LodBuilder
boolean nonFullAvoidance = LodConfig.CLIENT.worldGenerator.blockToAvoid.get().nonFull;
boolean noCollisionAvoidance = LodConfig.CLIENT.worldGenerator.blockToAvoid.get().noCollision;
BlockWrapper block = chunk.getBlock(blockPos);
BlockShapeWrapper block = chunk.getBlockShapeWrapper(blockPos);
return block != null
&& !block.isToAvoid()
&& !(nonFullAvoidance && block.isNonFull())
@@ -13,24 +13,23 @@ import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.world.IBlockReader;
import net.minecraftforge.client.model.data.ModelDataMap;
import java.util.*;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
//This class wraps the minecraft Block class
public class BlockWrapper
public class BlockColorWrapper
{
//set of block which require tint
public static final ConcurrentMap<Block, BlockWrapper> blockWrapperMap = new ConcurrentHashMap<>();
public static final ConcurrentMap<Block, BlockColorWrapper> blockColorWrapperMap = new ConcurrentHashMap<>();
public static final ModelDataMap dataMap = new ModelDataMap.Builder().build();
public static Random random = new Random(0);
public static BlockColorWrapper WATER_COLOR = getBlockColorWrapper(Blocks.WATER);
private Block block;
private boolean toAvoid;
private boolean nonFull;
private boolean noCollision;
private int color;
private boolean isColored;
private boolean toTint;
@@ -40,10 +39,8 @@ public class BlockWrapper
/**Constructor only require for the block instance we are wrapping**/
public BlockWrapper(Block block)
public BlockColorWrapper(Block block)
{
this.nonFull = false;
this.noCollision = false;
this.color = 0;
this.isColored = true;
this.toTint = false;
@@ -51,58 +48,25 @@ public class BlockWrapper
setupColorAndTint();
}
/**Constructor only require for the block instance we are wrapping**/
public BlockWrapper(Block block, ChunkWrapper chunkWrapper, BlockPosWrapper blockPosWrapper)
{
this.nonFull = true;
this.noCollision = true;
this.color = 0;
this.isColored = true;
this.toTint = false;
this.block = block;
setupColorAndTint();
setupShapes(chunkWrapper, blockPosWrapper);
}
/**
* this return a wrapper of the block in input
* @param block Block object to wrap
*/
static public BlockWrapper getBlockWrapper(Block block)
static public BlockColorWrapper getBlockColorWrapper(Block block)
{
//first we check if the block has already been wrapped
if (blockWrapperMap.containsKey(block) && blockWrapperMap.get(block) != null)
return blockWrapperMap.get(block);
if (blockColorWrapperMap.containsKey(block) && blockColorWrapperMap.get(block) != null)
return blockColorWrapperMap.get(block);
//if it hasn't been created yet, we create it and save it in the map
BlockWrapper blockWrapper = new BlockWrapper(block);
blockWrapperMap.put(block, blockWrapper);
BlockColorWrapper blockWrapper = new BlockColorWrapper(block);
blockColorWrapperMap.put(block, blockWrapper);
//we return the newly created wrapper
return blockWrapper;
}
/**
* this return a wrapper of the block in input
* @param block Block object to wrap
*/
static public BlockWrapper getBlockWrapper(Block block, ChunkWrapper chunkWrapper, BlockPosWrapper blockPosWrapper)
{
//first we check if the block has already been wrapped
if (blockWrapperMap.containsKey(block) && blockWrapperMap.get(block) != null)
return blockWrapperMap.get(block);
//if it hasn't been created yet, we create it and save it in the map
BlockWrapper blockWrapper = new BlockWrapper(block, chunkWrapper, blockPosWrapper);
blockWrapperMap.put(block, blockWrapper);
//we return the newly created wrapper
return blockWrapper;
}
/**
* Generate the color of the given block from its texture
* and store it for later use.
@@ -213,13 +177,13 @@ public class BlockWrapper
this.toTint = true;
// we check which kind of tint we need to apply
if (grassInstance() && this.toTint)
if (grassInstance())
this.grassTint = true;
if (leavesInstance() && this.toTint)
if (leavesInstance())
this.folliageTint = true;
if (waterIstance() && this.toTint)
if (waterIstance())
this.waterTint = true;
color = tempColor;
@@ -249,56 +213,7 @@ public class BlockWrapper
{
return block == Blocks.WATER;
}
private void setupShapes(ChunkWrapper chunkWrapper, BlockPosWrapper blockPosWrapper)
{
IBlockReader chunk = chunkWrapper.getChunk();
BlockPos blockPos = blockPosWrapper.getBlockPos();
boolean noCollisionSetted = false;
boolean nonFullSetted = false;
if (!block.defaultBlockState().getFluidState().isEmpty() || block instanceof SixWayBlock)
{
noCollisionSetted = true;
nonFullSetted = true;
noCollision = false;
nonFull = false;
}
if (!nonFullSetted)
{
VoxelShape voxelShape = block.defaultBlockState().getShape(chunk, blockPos);
if (!voxelShape.isEmpty())
{
AxisAlignedBB bbox = voxelShape.bounds();
double xWidth = (bbox.maxX - bbox.minX);
double yWidth = (bbox.maxY - bbox.minY);
double zWidth = (bbox.maxZ - bbox.minZ);
if (xWidth < 1 && zWidth < 1 && yWidth < 1)
nonFull = true;
else
nonFull = false;
}
else
{
nonFull = false;
}
}
if (!noCollisionSetted)
{
VoxelShape collisionShape = block.defaultBlockState().getCollisionShape(chunk, blockPos);
noCollision = collisionShape.isEmpty();
}
toAvoid = ofBlockToAvoid(block);
}
private boolean ofBlockToAvoid(Block block)
{
return block == Blocks.AIR
|| block != Blocks.CAVE_AIR
|| block != Blocks.BARRIER;
}
//--------------//
//Colors getters//
//--------------//
@@ -342,36 +257,15 @@ public class BlockWrapper
}
//-----------------//
//Avoidance getters//
//-----------------//
public boolean isNonFull()
{
return nonFull;
}
public boolean hasNoCollision()
{
return noCollision;
}
public boolean isToAvoid()
{
return folliageTint;
}
@Override public boolean equals(Object o)
{
if (this == o)
return true;
if (!(o instanceof BlockWrapper))
if (!(o instanceof BlockColorWrapper))
return false;
BlockWrapper that = (BlockWrapper) o;
BlockColorWrapper that = (BlockColorWrapper) o;
return Objects.equals(block, that.block);
}
@@ -381,3 +275,4 @@ public class BlockWrapper
}
}
@@ -0,0 +1,157 @@
package com.seibel.lod.wrappers.Block;
import com.seibel.lod.util.ColorUtil;
import com.seibel.lod.wrappers.Chunk.ChunkWrapper;
import com.seibel.lod.wrappers.MinecraftWrapper;
import net.minecraft.block.*;
import net.minecraft.client.renderer.model.BakedQuad;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.util.Direction;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.world.IBlockReader;
import net.minecraftforge.client.model.data.ModelDataMap;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
//This class wraps the minecraft Block class
public class BlockShapeWrapper
{
//set of block which require tint
public static final ConcurrentMap<Block, BlockShapeWrapper> blockShapeWrapperMap = new ConcurrentHashMap<>();
public static BlockShapeWrapper WATER_SHAPE = new BlockShapeWrapper();
private Block block;
private boolean toAvoid;
private boolean nonFull;
private boolean noCollision;
/**Constructor only require for the block instance we are wrapping**/
public BlockShapeWrapper(Block block, ChunkWrapper chunkWrapper, BlockPosWrapper blockPosWrapper)
{
this.block = block;
this.nonFull = false;
this.noCollision = false;
this.toAvoid = false;
setupShapes(chunkWrapper, blockPosWrapper);
}
private BlockShapeWrapper()
{
this.block = Blocks.WATER;
this.nonFull = false;
this.noCollision = false;
this.toAvoid = false;
}
/**
* this return a wrapper of the block in input
* @param block Block object to wrap
*/
static public BlockShapeWrapper getBlockShapeWrapper(Block block, ChunkWrapper chunkWrapper, BlockPosWrapper blockPosWrapper)
{
//first we check if the block has already been wrapped
if (blockShapeWrapperMap.containsKey(block) && blockShapeWrapperMap.get(block) != null)
return blockShapeWrapperMap.get(block);
//if it hasn't been created yet, we create it and save it in the map
BlockShapeWrapper blockWrapper = new BlockShapeWrapper(block, chunkWrapper, blockPosWrapper);
blockShapeWrapperMap.put(block, blockWrapper);
//we return the newly created wrapper
return blockWrapper;
}
private void setupShapes(ChunkWrapper chunkWrapper, BlockPosWrapper blockPosWrapper)
{
IBlockReader chunk = chunkWrapper.getChunk();
BlockPos blockPos = blockPosWrapper.getBlockPos();
boolean noCollisionSetted = false;
boolean nonFullSetted = false;
if (!block.defaultBlockState().getFluidState().isEmpty() || block instanceof SixWayBlock)
{
noCollisionSetted = true;
nonFullSetted = true;
noCollision = false;
nonFull = false;
}
if (!nonFullSetted)
{
VoxelShape voxelShape = block.defaultBlockState().getShape(chunk, blockPos);
if (!voxelShape.isEmpty())
{
AxisAlignedBB bbox = voxelShape.bounds();
double xWidth = (bbox.maxX - bbox.minX);
double yWidth = (bbox.maxY - bbox.minY);
double zWidth = (bbox.maxZ - bbox.minZ);
if (xWidth < 1 && zWidth < 1 && yWidth < 1)
nonFull = true;
else
nonFull = false;
}
else
{
nonFull = false;
}
}
if (!noCollisionSetted)
{
VoxelShape collisionShape = block.defaultBlockState().getCollisionShape(chunk, blockPos);
noCollision = collisionShape.isEmpty();
}
toAvoid = ofBlockToAvoid(block);
}
private boolean ofBlockToAvoid(Block block)
{
return block == Blocks.AIR
|| block != Blocks.CAVE_AIR
|| block != Blocks.BARRIER;
}
//-----------------//
//Avoidance getters//
//-----------------//
public boolean isNonFull()
{
return nonFull;
}
public boolean hasNoCollision()
{
return noCollision;
}
public boolean isToAvoid()
{
return toAvoid;
}
@Override public boolean equals(Object o)
{
if (this == o)
return true;
if (!(o instanceof BlockShapeWrapper))
return false;
BlockShapeWrapper that = (BlockShapeWrapper) o;
return Objects.equals(block, that.block);
}
@Override public int hashCode()
{
return Objects.hash(block);
}
}
@@ -1,20 +1,16 @@
package com.seibel.lod.wrappers.Chunk;
import com.seibel.lod.util.LodUtil;
import com.seibel.lod.wrappers.Block.BlockWrapper;
import com.seibel.lod.wrappers.Block.BlockColorWrapper;
import com.seibel.lod.wrappers.Block.BlockPosWrapper;
import com.seibel.lod.wrappers.Block.BlockShapeWrapper;
import com.seibel.lod.wrappers.World.BiomeWrapper;
import com.sun.javafx.scene.control.behavior.OptionalBoolean;
import net.minecraft.block.BlockState;
import net.minecraft.block.ILiquidContainer;
import net.minecraft.block.IWaterLoggable;
import net.minecraft.loot.conditions.BlockStateProperty;
import net.minecraft.state.Property;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.world.chunk.IChunk;
import java.util.Optional;
public class ChunkWrapper
{
@@ -49,11 +45,15 @@ public class ChunkWrapper
return BiomeWrapper.getBiomeWrapper(chunk.getBiomes().getNoiseBiome(xRel >> 2, yAbs >> 2, zRel >> 2));
}
public BlockWrapper getBlock(BlockPosWrapper blockPos)
public BlockColorWrapper getBlockColorWrapper(BlockPosWrapper blockPos)
{
return BlockWrapper.getBlockWrapper(chunk.getBlockState(blockPos.getBlockPos()).getBlock(), this, blockPos);
return BlockColorWrapper.getBlockColorWrapper(chunk.getBlockState(blockPos.getBlockPos()).getBlock());
}
public BlockShapeWrapper getBlockShapeWrapper(BlockPosWrapper blockPos)
{
return BlockShapeWrapper.getBlockShapeWrapper(chunk.getBlockState(blockPos.getBlockPos()).getBlock(), this, blockPos);
}
public ChunkWrapper(IChunk chunk)
{
@@ -1,7 +1,7 @@
package com.seibel.lod.wrappers.World;
import com.seibel.lod.util.ColorUtil;
import com.seibel.lod.wrappers.Block.BlockWrapper;
import com.seibel.lod.wrappers.Block.BlockColorWrapper;
import net.minecraft.block.Blocks;
import net.minecraft.world.biome.Biome;
@@ -49,32 +49,32 @@ public class BiomeWrapper
{
case NETHER:
colorInt = BlockWrapper.getBlockWrapper(Blocks.NETHERRACK).getColor();
colorInt = BlockColorWrapper.getBlockColorWrapper(Blocks.NETHERRACK).getColor();
break;
case THEEND:
colorInt = BlockWrapper.getBlockWrapper(Blocks.END_STONE).getColor();
colorInt = BlockColorWrapper.getBlockColorWrapper(Blocks.END_STONE).getColor();
break;
case BEACH:
case DESERT:
colorInt = BlockWrapper.getBlockWrapper(Blocks.SAND).getColor();
colorInt = BlockColorWrapper.getBlockColorWrapper(Blocks.SAND).getColor();
break;
case EXTREME_HILLS:
colorInt = BlockWrapper.getBlockWrapper(Blocks.STONE).getColor();
colorInt = BlockColorWrapper.getBlockColorWrapper(Blocks.STONE).getColor();
break;
case MUSHROOM:
colorInt = BlockWrapper.getBlockWrapper(Blocks.MYCELIUM).getColor();
colorInt = BlockColorWrapper.getBlockColorWrapper(Blocks.MYCELIUM).getColor();
break;
case ICY:
colorInt = BlockWrapper.getBlockWrapper(Blocks.SNOW).getColor();
colorInt = BlockColorWrapper.getBlockColorWrapper(Blocks.SNOW).getColor();
break;
case MESA:
colorInt = BlockWrapper.getBlockWrapper(Blocks.RED_SAND).getColor();
colorInt = BlockColorWrapper.getBlockColorWrapper(Blocks.RED_SAND).getColor();
break;
case OCEAN:
@@ -84,19 +84,19 @@ public class BiomeWrapper
case SWAMP:
case FOREST:
color = BlockWrapper.getBlockWrapper(Blocks.OAK_LEAVES).getColor();
color = BlockColorWrapper.getBlockColorWrapper(Blocks.OAK_LEAVES).getColor();
tint = biome.getFoliageColor();
colorInt = ColorUtil.multiplyRGBcolors(color, tint);
break;
case TAIGA:
color = BlockWrapper.getBlockWrapper(Blocks.SPRUCE_LEAVES).getColor();
color = BlockColorWrapper.getBlockColorWrapper(Blocks.SPRUCE_LEAVES).getColor();
tint = biome.getFoliageColor();
colorInt = ColorUtil.multiplyRGBcolors(color, tint);
break;
case JUNGLE:
color = BlockWrapper.getBlockWrapper(Blocks.JUNGLE_LEAVES).getColor();
color = BlockColorWrapper.getBlockColorWrapper(Blocks.JUNGLE_LEAVES).getColor();
tint = biome.getFoliageColor();
colorInt = ColorUtil.multiplyRGBcolors(color, tint);
break;
@@ -105,7 +105,7 @@ public class BiomeWrapper
case NONE:
case PLAINS:
case SAVANNA:
color = BlockWrapper.getBlockWrapper(Blocks.GRASS_BLOCK).getColor();
color = BlockColorWrapper.getBlockColorWrapper(Blocks.GRASS_BLOCK).getColor();
tint = biome.getGrassColor(x,z);
colorInt = ColorUtil.multiplyRGBcolors(color, tint);
break;