Skip to content

Commit

Permalink
Surface rocks (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
TechLord22 authored Dec 26, 2021
1 parent 6c0b975 commit c510bcd
Show file tree
Hide file tree
Showing 10 changed files with 289 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.JsonObject;
import gregtech.api.unification.OreDictUnifier;
import gregtech.api.unification.material.Material;
import gregtech.api.unification.material.properties.PropertyKey;
import gregtech.api.unification.stack.UnificationEntry;
import gregtech.api.util.GTLog;
import gregtech.api.worldgen.config.OreConfigUtils;
Expand Down Expand Up @@ -58,7 +59,10 @@ private Set<Material> findUndergroundMaterials(Collection<IBlockState> generated
} else {
ItemStack itemStack = new ItemStack(blockState.getBlock(), 1, blockState.getBlock().damageDropped(blockState));
UnificationEntry entry = OreDictUnifier.getUnificationEntry(itemStack);
resultMaterial = entry == null ? null : entry.material;
if (entry != null && entry.material != null && entry.material.hasProperty(PropertyKey.ORE))
resultMaterial = entry.material;
else
resultMaterial = null;
}
if (resultMaterial != null) {
result.add(resultMaterial);
Expand All @@ -68,9 +72,11 @@ private Set<Material> findUndergroundMaterials(Collection<IBlockState> generated
}

private void setStoneBlock(World world, BlockPos blockPos, Collection<Material> undergroundMaterials) {
boolean surfaceRockPlaced = world.setBlockState(blockPos, MetaBlocks.SURFACE_ROCK.getDefaultState());
if (!surfaceRockPlaced) {
failedGenerationCounter++;
for (Material material : undergroundMaterials) {
boolean surfaceRockPlaced = world.setBlockState(blockPos, MetaBlocks.SURFACE_ROCK.get(material).getBlock(material));
if (!surfaceRockPlaced) {
failedGenerationCounter++;
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/gregtech/client/ClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ public class ClientProxy extends CommonProxy {
public static final IBlockColor FOAM_BLOCK_COLOR = (IBlockState state, IBlockAccess worldIn, BlockPos pos, int tintIndex) ->
state.getValue(BlockColored.COLOR).colorValue;

public static final IBlockColor SURFACE_ROCK_BLOCK_COLOR = (IBlockState state, IBlockAccess worldIn, BlockPos pos, int tintIndex) ->
state.getValue(((BlockSurfaceRock) state.getBlock()).variantProperty).getMaterialRGB();

public void onPreLoad() {
super.onPreLoad();

Expand Down Expand Up @@ -167,6 +170,7 @@ public static void registerSprites(TextureStitchEvent.Pre event) {
}
MetaBlocks.COMPRESSED.values().stream().distinct().forEach(c -> c.onTextureStitch(event));
MetaBlocks.FRAMES.values().stream().distinct().forEach(f -> f.onTextureStitch(event));
MetaBlocks.SURFACE_ROCK.values().stream().distinct().forEach(c -> c.onTextureStitch(event));
MetaBlocks.ORES.forEach(o -> o.onTextureStitch(event));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public void onModelsBake(ModelBakeEvent event) {
event.getModelRegistry().putObject(BlockOre.MODEL_LOCATION, OreBakedModel.INSTANCE);
event.getModelRegistry().putObject(BlockCompressed.MODEL_LOCATION, CompressedBlockBakedModel.INSTANCE);
event.getModelRegistry().putObject(BlockFrame.MODEL_LOCATION, FrameBakedModel.INSTANCE);
event.getModelRegistry().putObject(BlockSurfaceRock.MODEL_LOCATION, SurfaceRockBakedModel.INSTANCE);
}

private static class ModelBuiltInRenderer implements IBakedModel {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package gregtech.client.model.modelfactories;

import gregtech.api.unification.material.Material;
import gregtech.api.unification.material.info.MaterialIconSet;
import gregtech.api.unification.material.info.MaterialIconType;
import gregtech.client.model.ModelFactory;
import gregtech.common.blocks.BlockSurfaceRock;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.*;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraftforge.client.model.ModelLoader;
import org.apache.commons.lang3.tuple.Pair;
import org.lwjgl.util.vector.Vector3f;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.vecmath.Matrix4f;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SurfaceRockBakedModel implements IBakedModel {

public static final SurfaceRockBakedModel INSTANCE = new SurfaceRockBakedModel();

private final Map<MaterialIconSet, Map<EnumFacing, BakedQuad>> materialFaces;
private final ThreadLocal<TextureAtlasSprite> particle;

private SurfaceRockBakedModel() {
this.materialFaces = new Object2ObjectOpenHashMap<>();
this.particle = ThreadLocal.withInitial(() -> Minecraft.getMinecraft().getTextureMapBlocks().getMissingSprite());
}

@Override
@Nonnull
public List<BakedQuad> getQuads(@Nullable IBlockState state, @Nullable EnumFacing side, long rand) {
List<BakedQuad> quads = new ArrayList<>();
if (side == null) return quads;
if (state != null) {
Material material = state.getValue(((BlockSurfaceRock) state.getBlock()).variantProperty);
Map<EnumFacing, BakedQuad> materialFace = materialFaces.get(material.getMaterialIconSet());
if (materialFace == null) {
materialFaces.put(material.getMaterialIconSet(), materialFace = new Object2ObjectOpenHashMap<>());
}
BakedQuad materialFaceQuad = materialFace.get(side);
if (materialFaceQuad == null) {
materialFace.put(side, materialFaceQuad = ModelFactory.getBakery().makeBakedQuad(
new Vector3f(0F, 0F, 0F),
new Vector3f(16F, 16F, 16F),
new BlockPartFace(side, 1, "", new BlockFaceUV(new float[] { 0.0F, 0.0F, 16.0F, 16.0F, 0.0F, 0.0F, 16.0F, 16.0F }, 0)),
ModelLoader.defaultTextureGetter().apply(MaterialIconType.block.getBlockPath(material.getMaterialIconSet())),
side,
ModelRotation.X0_Y0,
null,
true,
true));
}
quads.add(materialFaceQuad);
particle.set(materialFaceQuad.getSprite());
} else {
ItemStack stack = SurfaceRockBlockItemOverride.INSTANCE.stack.get();
if (!stack.isEmpty()) {
BlockSurfaceRock rock = (BlockSurfaceRock) ((ItemBlock) stack.getItem()).getBlock();
IBlockState compressedState = rock.getDefaultState().withProperty(rock.variantProperty, rock.variantProperty.getAllowedValues().get(stack.getMetadata()));
for (EnumFacing face : EnumFacing.VALUES) {
quads.addAll(getQuads(compressedState, face, rand));
}
}
}
return quads;
}

@Override
public boolean isAmbientOcclusion() {
return true;
}

@Override
public boolean isGui3d() {
return true;
}

@Override
public boolean isBuiltInRenderer() {
return false;
}

@Override
public TextureAtlasSprite getParticleTexture() {
return particle.get();
}

@Override
public Pair<? extends IBakedModel, Matrix4f> handlePerspective(ItemCameraTransforms.TransformType cameraTransformType) {
return Pair.of(this, ModelFactory.getBlockTransform(cameraTransformType).getMatrix());
}

@Override
public ItemOverrideList getOverrides() {
return SurfaceRockBlockItemOverride.INSTANCE;
}

private static class SurfaceRockBlockItemOverride extends ItemOverrideList {

private static final SurfaceRockBlockItemOverride INSTANCE = new SurfaceRockBlockItemOverride();

private final ThreadLocal<ItemStack> stack = ThreadLocal.withInitial(() -> ItemStack.EMPTY);

@Override
public IBakedModel handleItemState(IBakedModel originalModel, ItemStack stack, @Nullable World world, @Nullable EntityLivingBase entity) {
this.stack.set(stack);
return originalModel;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import codechicken.lib.render.pipeline.ColourMultiplier;
import codechicken.lib.render.pipeline.IVertexOperation;
import codechicken.lib.render.pipeline.attribute.ColourAttribute;
import codechicken.lib.texture.TextureUtils;
import codechicken.lib.vec.Cuboid6;
import codechicken.lib.vec.Matrix4;
import codechicken.lib.vec.TransformationList;
Expand All @@ -20,6 +19,7 @@
import gregtech.api.util.Position;
import gregtech.api.util.PositionedRect;
import gregtech.api.util.Size;
import gregtech.client.renderer.texture.Textures;
import gregtech.common.blocks.BlockSurfaceRock;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.BufferBuilder;
Expand Down Expand Up @@ -72,21 +72,26 @@ private static List<IndexedCuboid6> generateCuboidList(Random random) {
ArrayList<IndexedCuboid6> result = new ArrayList<>();
List<PositionedRect> occupiedAreas = new ArrayList<>();
int stonePlaceAttempts = 64;
int maxStones = 8;
int maxStones = 1;
int stonesPlaced = 0;
for (int i = 0; i < stonePlaceAttempts && stonesPlaced < maxStones; i++) {
int sizeX = 2 + random.nextInt(3);
int sizeZ = 2 + random.nextInt(3);
int stoneHeight = 4 + random.nextInt(4);
int posX = random.nextInt(16 - sizeX);
int posZ = random.nextInt(16 - sizeZ);
// int sizeX = 2 + random.nextInt(14);
// int sizeZ = 2 + random.nextInt(14);
// int stoneHeight = 1 + random.nextInt(4);
int sizeX = 6;
int sizeZ = 6;
int stoneHeight = 2;
// int posX = random.nextInt(16 - sizeX);
// int posZ = random.nextInt(16 - sizeZ);
int posX = 6;
int posZ = 6;
PositionedRect rect = new PositionedRect(new Position(posX, posZ), new Size(sizeX, sizeZ));
if (occupiedAreas.stream().noneMatch(rect::intersects)) {
Vector3 minVector = new Vector3(posX / 16.0, 0 / 16.0, posZ / 16.0);
Cuboid6 bounds = new Cuboid6(minVector, minVector.copy());
bounds.max.add(sizeX / 16.0, stoneHeight / 16.0, sizeZ / 16.0);
int brightness = 100 + random.nextInt(130);
result.add(new IndexedCuboid6(brightness, bounds));
result.add(new IndexedCuboid6(255, bounds));
occupiedAreas.add(rect);
stonesPlaced++;
}
Expand All @@ -106,8 +111,9 @@ public boolean renderBlock(IBlockAccess world, BlockPos pos, IBlockState state,
renderState.bind(buffer);
Matrix4 translation = new Matrix4();
translation.translate(pos.getX(), pos.getY(), pos.getZ());
TextureAtlasSprite stoneSprite = TextureUtils.getBlockTexture("stone");
Material material = ((BlockSurfaceRock) state.getBlock()).getStoneMaterial(world, pos, state);
// TextureAtlasSprite stoneSprite = TextureUtils.getBlockTexture("stone");
TextureAtlasSprite stoneSprite = Textures.SURFACE_ROCK_TEXTURE.getParticleSprite();
Material material = state.getValue(((BlockSurfaceRock) state.getBlock()).variantProperty);
int renderingColor = GTUtility.convertRGBtoOpaqueRGBA_CL(material.getMaterialRGB());
IVertexOperation[] operations = new IVertexOperation[]{
new IconTransformation(stoneSprite),
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/gregtech/client/renderer/texture/Textures.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ public class Textures {
public static final SimpleOverlayRenderer COVER_INTERFACE_PROXY = new SimpleOverlayRenderer("cover/cover_interface_proxy");
public static final SimpleOverlayRenderer COVER_INTERFACE_WIRELESS = new SimpleOverlayRenderer("cover/cover_interface_wireless");

public static final SimpleOverlayRenderer SURFACE_ROCK_TEXTURE = new SimpleOverlayRenderer("stones/surface_rock_stone");

@SideOnly(Side.CLIENT)
public static ThreadLocal<CubeRendererState> RENDER_STATE;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gregtech/common/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ public static void registerBlocks(RegistryEvent.Register<Block> event) {
registry.register(LEAVES);
registry.register(SAPLING);
registry.register(PLANKS);
registry.register(SURFACE_ROCK);

COMPRESSED.values().stream().distinct().forEach(registry::register);
FRAMES.values().stream().distinct().forEach(registry::register);
SURFACE_ROCK.values().stream().distinct().forEach(registry::register);
ORES.forEach(registry::register);
}

Expand Down
Loading

0 comments on commit c510bcd

Please sign in to comment.