Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

turbine overlay follow up #3550

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,89 +1,28 @@
package goodgenerator.blocks.regularBlock;

import net.minecraft.block.Block;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.common.util.ForgeDirection;

import goodgenerator.blocks.tileEntity.base.MTELargeTurbineBase;
import goodgenerator.client.render.BlockRenderHandler;
import goodgenerator.main.GoodGenerator;
import gregtech.api.enums.Textures;
import gregtech.api.interfaces.IIconContainer;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.render.TextureFactory;

public class BlockTurbineCasing extends BlockCasing implements ITextureBlock {

public static IIconContainer[][] turbineShape = new IIconContainer[3][9];
public IIconContainer base;

static {
for (int i = 0; i < 3; i++) for (int j = 1; j <= 9; j++)
turbineShape[i][j - 1] = new Textures.BlockIcons.CustomIcon("icons/turbines/TURBINE_" + i + j);
}

public BlockTurbineCasing(String name, String texture) {
super(name, new String[] { GoodGenerator.MOD_ID + ":" + texture });
base = new Textures.BlockIcons.CustomIcon("icons/" + texture);
}

private static int isTurbineControllerWithSide(IBlockAccess aWorld, int aX, int aY, int aZ, ForgeDirection side) {
TileEntity tTileEntity = aWorld.getTileEntity(aX, aY, aZ);
if (!(tTileEntity instanceof IGregTechTileEntity tTile)) return 0;
if (tTile.getMetaTileEntity() instanceof MTELargeTurbineBase && tTile.getFrontFacing() == side) {
if (tTile.isActive()) return 1;
return ((MTELargeTurbineBase) tTile.getMetaTileEntity()).hasTurbine() ? 2 : 3;
}
return 0;
}

public ITexture[] getTurbineCasing(int iconIndex, boolean active, boolean hasTurbine) {
int states = active ? 0 : hasTurbine ? 1 : 2;
return new ITexture[] { TextureFactory.of(base), TextureFactory.of(turbineShape[states][iconIndex]) };
}

@Override
public ITexture[] getTexture(Block aBlock, ForgeDirection side, IBlockAccess aWorld, int xCoord, int yCoord,
int zCoord) {
final int ordinalSide = side.ordinal();
int tInvertLeftRightMod = ordinalSide % 2 * 2 - 1;
switch (ordinalSide / 2) {
case 0:
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
if (i == 0 && j == 0) continue;
int tState;
if ((tState = isTurbineControllerWithSide(aWorld, xCoord + j, yCoord, zCoord + i, side)) != 0) {
return getTurbineCasing(4 - i * 3 - j, tState == 1, tState == 2);
}
}
}
break;
case 1:
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
if (i == 0 && j == 0) continue;
int tState;
if ((tState = isTurbineControllerWithSide(aWorld, xCoord + j, yCoord + i, zCoord, side)) != 0) {
return getTurbineCasing(4 + i * 3 - j * tInvertLeftRightMod, tState == 1, tState == 2);
}
}
}
break;
case 2:
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
if (i == 0 && j == 0) continue;
int tState;
if ((tState = isTurbineControllerWithSide(aWorld, xCoord, yCoord + i, zCoord + j, side)) != 0) {
return getTurbineCasing(4 + i * 3 + j * tInvertLeftRightMod, tState == 1, tState == 2);
}
}
}
break;
}
return getTexture(aBlock, side);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ public int getCasingTextureIndex() {
return 1538;
}

public boolean isNewStyleRendering() {
return true;
}

@Override
protected MultiblockTooltipBuilder createTooltip() {
final MultiblockTooltipBuilder tt = new MultiblockTooltipBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,40 @@
import static com.gtnewhorizon.structurelib.structure.StructureUtility.ofBlock;
import static com.gtnewhorizon.structurelib.structure.StructureUtility.transpose;
import static gregtech.api.enums.HatchElement.*;
import static gregtech.api.enums.Textures.BlockIcons.*;
import static gregtech.api.util.GTStructureUtility.*;
import static gregtech.api.util.GTUtility.validMTEList;

import java.util.ArrayList;

import net.minecraft.block.Block;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.StatCollector;
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidStack;

import org.jetbrains.annotations.NotNull;

import com.gtnewhorizon.structurelib.alignment.constructable.ISurvivalConstructable;
import com.gtnewhorizon.structurelib.structure.IStructureDefinition;
import com.gtnewhorizon.structurelib.structure.IStructureElementCheckOnly;
import com.gtnewhorizon.structurelib.structure.ISurvivalBuildEnvironment;
import com.gtnewhorizon.structurelib.structure.StructureDefinition;

import gregtech.api.interfaces.IHatchElement;
import gregtech.api.interfaces.IIconContainer;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.items.MetaGeneratedTool;
import gregtech.api.metatileentity.implementations.MTEEnhancedMultiBlockBase;
import gregtech.api.metatileentity.implementations.MTEHatchDynamo;
import gregtech.api.recipe.check.CheckRecipeResult;
import gregtech.api.recipe.check.CheckRecipeResultRegistry;
import gregtech.api.util.GTUtility;
import gregtech.api.util.GTUtilityClient;
import gregtech.api.util.TurbineStatCalculator;
import gregtech.api.util.shutdown.ShutDownReasonRegistry;
import gregtech.common.items.MetaGeneratedTool01;
Expand All @@ -52,26 +56,20 @@ protected IStructureDefinition<MTELargeTurbineBase> computeValue(Class<?> type)
.addShape(
STRUCTURE_PIECE_MAIN,
transpose(
new String[][] { { " ", "xxxxx", "xxxxx", "xxxxx", "xxxxx", },
{ " --- ", "xcccx", "xchcx", "xchcx", "xcccx", },
{ " --- ", "xc~cx", "xh-hx", "xh-hx", "xcdcx", },
{ " --- ", "xcccx", "xchcx", "xchcx", "xcccx", },
{ " ", "xxxxx", "xxxxx", "xxxxx", "xxxxx", }, }))
new String[][] { { " ", " ", " ", " ", " ", },
{ " --- ", " ccc ", " hhh ", " hhh ", " hhh ", },
{ " --- ", " c~c ", " h-h ", " h-h ", " hdh ", },
{ " --- ", " ccc ", " hhh ", " hhh ", " hhh ", },
{ " ", " ", " ", " ", " ", }, }))
.addElement('c', lazy(t -> ofBlock(t.getCasingBlock(), t.getCasingMeta())))
.addElement('d', lazy(t -> Dynamo.newAny(t.getCasingTextureIndex(), 1)))
.addElement(
'h',
lazy(
t -> buildHatchAdder(MTELargeTurbineBase.class)
.atLeast(Maintenance, InputHatch, OutputHatch, OutputBus, InputBus, Muffler)
t -> buildHatchAdder(MTELargeTurbineBase.class).atLeast(t.getHatchElements())
.casingIndex(t.getCasingTextureIndex())
.dot(2)
.buildAndChain(t.getCasingBlock(), t.getCasingMeta())))
.addElement('x', (IStructureElementCheckOnly<MTELargeTurbineBase>) (aContext, aWorld, aX, aY, aZ) -> {
TileEntity tTile = aWorld.getTileEntity(aX, aY, aZ);
return !(tTile instanceof IGregTechTileEntity)
|| !(((IGregTechTileEntity) tTile).getMetaTileEntity() instanceof MTELargeTurbineBase);
})
.build();
}
};
Expand All @@ -85,6 +83,12 @@ protected IStructureDefinition<MTELargeTurbineBase> computeValue(Class<?> type)
protected int overflowMultiplier = 0;
protected long maxPower = 0;

// client side stuff
protected boolean mHasTurbine;
// mMachine got overwritten by StructureLib extended facing query response
// so we use a separate field for this
protected boolean mFormed;

public MTELargeTurbineBase(int aID, String aName, String aNameRegional) {
super(aID, aName, aNameRegional);
}
Expand All @@ -103,6 +107,25 @@ public IStructureDefinition<MTELargeTurbineBase> getStructureDefinition() {
return STRUCTURE_DEFINITION.get(getClass());
}

@SuppressWarnings("unchecked")
protected IHatchElement<? super MTELargeTurbineBase>[] getHatchElements() {
if (getPollutionPerTick(null) == 0)
return new IHatchElement[] { Maintenance, InputHatch, OutputHatch, OutputBus, InputBus };
return new IHatchElement[] { Maintenance, InputHatch, OutputHatch, OutputBus, InputBus, Muffler };
}

@Override
public boolean checkStructure(boolean aForceReset, IGregTechTileEntity aBaseMetaTileEntity) {
boolean f = super.checkStructure(aForceReset, aBaseMetaTileEntity);
if (f && getBaseMetaTileEntity().isServerSide()) {
// while is this a client side field, blockrenderer will reuse the server world for client side rendering
// so we must set it as well...
mFormed = true;
return true;
}
return f;
}

@Override
public boolean checkMachine(IGregTechTileEntity aBaseMetaTileEntity, ItemStack aStack) {
maxPower = 0;
Expand All @@ -120,6 +143,47 @@ public boolean checkMachine(IGregTechTileEntity aBaseMetaTileEntity, ItemStack a

public abstract int getCasingTextureIndex();

public boolean isNewStyleRendering() {
return false;
}

public IIconContainer[] getTurbineTextureActive() {
return TURBINE_NEW_ACTIVE;
}

public IIconContainer[] getTurbineTextureFull() {
return TURBINE_NEW;
}

public IIconContainer[] getTurbineTextureEmpty() {
return TURBINE_NEW_EMPTY;
}

@Override
public boolean renderInWorld(IBlockAccess aWorld, int aX, int aY, int aZ, Block aBlock, RenderBlocks aRenderer) {
if (!isNewStyleRendering() || !mFormed) return false;

IIconContainer[] tTextures;
if (getBaseMetaTileEntity().isActive()) tTextures = getTurbineTextureActive();
else if (hasTurbine()) tTextures = getTurbineTextureFull();
else tTextures = getTurbineTextureEmpty();
GTUtilityClient
.renderTurbineOverlay(aWorld, aX, aY, aZ, aRenderer, getExtendedFacing(), getCasingBlock(), tTextures);
return false;
}

@Override
public void onValueUpdate(byte aValue) {
mHasTurbine = (aValue & 0x1) != 0;
mFormed = (aValue & 0x2) != 0;
super.onValueUpdate(aValue);
}

@Override
public byte getUpdateData() {
return (byte) ((hasTurbine() ? 1 : 0) | (mMachine ? 2 : 0));
}

@Override
public boolean addToMachineList(IGregTechTileEntity tTileEntity, int aBaseCasingIndex) {
return addMaintenanceToMachineList(tTileEntity, getCasingTextureIndex())
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/gregtech/api/util/GTUtilityClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@
import java.lang.reflect.Field;
import java.util.List;

import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.common.util.ForgeDirection;

import com.google.common.collect.Lists;
import com.gtnewhorizon.structurelib.alignment.enumerable.ExtendedFacing;

import cpw.mods.fml.relauncher.ReflectionHelper;
import gregtech.api.enums.Dyes;
import gregtech.api.interfaces.IIconContainer;
import gregtech.common.render.GTRenderUtil;

public class GTUtilityClient {

Expand Down Expand Up @@ -49,4 +57,53 @@ public static List<String> getTooltip(ItemStack aStack, boolean aGuiStyle) {
return Lists.newArrayList(aStack.getDisplayName());
}
}

public static void renderTurbineOverlay(IBlockAccess aWorld, int aX, int aY, int aZ, RenderBlocks aRenderer,
ExtendedFacing tExtendedFacing, Block tBlockOverride, IIconContainer[] tTextures) {
int[] tABCCoord = new int[] { -1, -1, 0 };
int[] tXYZOffset = new int[3];
final ForgeDirection tDirection = tExtendedFacing.getDirection();
final LightingHelper tLighting = new LightingHelper(aRenderer);

// for some reason +x and -z need this field set to true, but not any other sides
if (tDirection == ForgeDirection.NORTH || tDirection == ForgeDirection.EAST) aRenderer.field_152631_f = true;

for (int i = 0; i < 9; i++) {
tExtendedFacing.getWorldOffset(tABCCoord, tXYZOffset);
// since structure check passed, we can assume it is turbine casing
int tX = tXYZOffset[0] + aX;
int tY = tXYZOffset[1] + aY;
int tZ = tXYZOffset[2] + aZ;
Block tBlock;
if (tBlockOverride == null) {
tBlock = aWorld.getBlock(aX + tDirection.offsetX, tY + tDirection.offsetY, aZ + tDirection.offsetZ);
} else {
tBlock = tBlockOverride;
}
// we skip the occlusion test, as we always require a working turbine to have a block of air before it
// so the front face cannot be occluded whatsoever in the most cases.
Tessellator.instance.setBrightness(
tBlock.getMixedBrightnessForBlock(
aWorld,
aX + tDirection.offsetX,
tY + tDirection.offsetY,
aZ + tDirection.offsetZ));
tLighting.setupLighting(tBlock, tX, tY, tZ, tDirection)
.setupColor(tDirection, Dyes._NULL.mRGBa);
GTRenderUtil.renderBlockIcon(
aRenderer,
tBlock,
tX + tDirection.offsetX * 0.001,
tY + tDirection.offsetY * 0.001,
tZ + tDirection.offsetZ * 0.001,
tTextures[i].getIcon(),
tDirection);
if (++tABCCoord[0] == 2) {
tABCCoord[0] = -1;
tABCCoord[1]++;
}
}

aRenderer.field_152631_f = false;
}
}
Loading