2020-09-09 16:49:01 +02:00
package de.ellpeck.actuallyadditions.common.blocks.render ;
2016-12-18 17:28:29 +01:00
2020-09-07 20:33:27 +02:00
import com.mojang.blaze3d.matrix.MatrixStack ;
2020-09-09 16:49:01 +02:00
import de.ellpeck.actuallyadditions.common.ActuallyAdditions ;
import de.ellpeck.actuallyadditions.common.items.ItemBattery ;
import de.ellpeck.actuallyadditions.common.tile.TileEntityBatteryBox ;
import de.ellpeck.actuallyadditions.common.util.AssetUtil ;
import de.ellpeck.actuallyadditions.common.util.StackUtil ;
2016-12-18 17:28:29 +01:00
import net.minecraft.client.Minecraft ;
import net.minecraft.client.gui.FontRenderer ;
2020-09-07 20:33:27 +02:00
import net.minecraft.client.renderer.IRenderTypeBuffer ;
import net.minecraft.client.renderer.Quaternion ;
import net.minecraft.client.renderer.tileentity.TileEntityRenderer ;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher ;
import net.minecraft.item.BlockItem ;
2016-12-18 17:28:29 +01:00
import net.minecraft.item.ItemStack ;
2020-09-07 20:33:27 +02:00
import net.minecraft.util.Util ;
2016-12-18 17:28:29 +01:00
import net.minecraftforge.energy.CapabilityEnergy ;
2020-09-07 20:33:27 +02:00
import java.text.NumberFormat ;
2016-12-18 17:28:29 +01:00
2020-09-07 20:33:27 +02:00
// todo: migrate to client package
public class RenderBatteryBox extends TileEntityRenderer < TileEntityBatteryBox > {
2016-12-18 17:28:29 +01:00
2020-09-07 20:33:27 +02:00
public RenderBatteryBox ( TileEntityRendererDispatcher rendererDispatcherIn ) {
super ( rendererDispatcherIn ) ;
}
@Override
public void render ( TileEntityBatteryBox tile , float partialTicks , MatrixStack matrices , IRenderTypeBuffer bufferIn , int combinedLightIn , int combinedOverlayIn ) {
// BlockPos pos = tile.getPos();
2019-02-27 19:53:05 +01:00
ItemStack stack = tile . inv . getStackInSlot ( 0 ) ;
2019-05-02 09:10:29 +02:00
if ( StackUtil . isValid ( stack ) & & stack . getItem ( ) instanceof ItemBattery ) {
2020-09-07 20:33:27 +02:00
matrices . push ( ) ;
// matrices.translate((float) pos.getX() + 0.5F, (float) pos.getY() + 1F, (float) pos.getZ() + 0.5F);
2016-12-18 17:28:29 +01:00
2020-09-07 20:33:27 +02:00
matrices . push ( ) ;
2016-12-18 17:28:29 +01:00
2020-09-07 20:33:27 +02:00
matrices . scale ( 0 . 0075F , 0 . 0075F , 0 . 0075F ) ;
matrices . rotate ( new Quaternion ( 180F , 1F , 0F , 0F ) ) ;
matrices . translate ( 0F , 0F , - 50F ) ;
2016-12-18 17:28:29 +01:00
2020-09-07 20:33:27 +02:00
stack . getCapability ( CapabilityEnergy . ENERGY ) . ifPresent ( cap - > {
2016-12-18 17:28:29 +01:00
NumberFormat format = NumberFormat . getInstance ( ) ;
2020-09-07 20:33:27 +02:00
FontRenderer font = Minecraft . getInstance ( ) . fontRenderer ;
2016-12-18 17:28:29 +01:00
2019-05-02 09:10:29 +02:00
String s = format . format ( cap . getEnergyStored ( ) ) + " / " + format . format ( cap . getMaxEnergyStored ( ) ) ;
float lengthS = - font . getStringWidth ( s ) / 2F ;
2016-12-18 17:28:29 +01:00
String s2 = " Crystal Flux " ;
2019-05-02 09:10:29 +02:00
float lengthS2 = - font . getStringWidth ( s2 ) / 2F ;
2016-12-18 17:28:29 +01:00
2019-05-02 09:10:29 +02:00
for ( int i = 0 ; i < 4 ; i + + ) {
2020-09-07 20:33:27 +02:00
font . drawString ( s , lengthS , 10F , 0xFFFFFF ) ;
font . drawString ( s2 , lengthS2 , 20F , 0xFFFFFF ) ;
2016-12-18 17:28:29 +01:00
2020-09-07 20:33:27 +02:00
matrices . translate ( - 50F , 0F , 50F ) ;
matrices . rotate ( new Quaternion ( 90F , 0F , 1F , 0F ) ) ;
2016-12-18 17:28:29 +01:00
}
2020-09-07 20:33:27 +02:00
} ) ;
2016-12-18 17:28:29 +01:00
2020-09-07 20:33:27 +02:00
matrices . pop ( ) ;
2016-12-18 17:28:29 +01:00
2020-09-07 20:33:27 +02:00
double boop = Util . milliTime ( ) / 800D ;
matrices . translate ( 0D , Math . sin ( boop % ( 2 * Math . PI ) ) * 0 . 065 , 0D ) ;
matrices . rotate ( new Quaternion ( ( float ) ( boop * 40D % 360 ) , 0 , 1 , 0 ) ) ;
2016-12-18 17:28:29 +01:00
2020-09-07 20:33:27 +02:00
float scale = stack . getItem ( ) instanceof BlockItem ? 0 . 85F : 0 . 65F ;
matrices . scale ( scale , scale , scale ) ;
2019-05-02 09:10:29 +02:00
try {
2016-12-18 17:28:29 +01:00
AssetUtil . renderItemInWorld ( stack ) ;
2019-05-02 09:10:29 +02:00
} catch ( Exception e ) {
ActuallyAdditions . LOGGER . error ( " Something went wrong trying to render an item in a battery box! The item is " + stack . getItem ( ) . getRegistryName ( ) + " ! " , e ) ;
2016-12-18 17:28:29 +01:00
}
2020-09-07 20:33:27 +02:00
matrices . pop ( ) ;
2016-12-18 17:28:29 +01:00
}
}
}