2016-12-18 17:28:29 +01:00
/ *
* This file ( " RenderBatteryBox.java " ) is part of the Actually Additions mod for Minecraft .
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http : //ellpeck.de/actaddlicense
* View the source code at https : //github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015 - 2017 Ellpeck
2016-12-18 17:28:29 +01:00
* /
package de.ellpeck.actuallyadditions.mod.blocks.render ;
2024-03-02 21:23:08 +01:00
import com.mojang.blaze3d.vertex.PoseStack ;
2024-03-03 01:20:53 +01:00
import com.mojang.math.Axis ;
2018-05-10 11:38:58 +02:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions ;
2016-12-18 17:28:29 +01:00
import de.ellpeck.actuallyadditions.mod.items.ItemBattery ;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBatteryBox ;
2023-01-15 17:56:02 +01:00
import de.ellpeck.actuallyadditions.mod.util.AssetUtil ;
2021-02-28 11:35:10 +01:00
import de.ellpeck.actuallyadditions.mod.util.Lang ;
2024-03-02 21:23:08 +01:00
import net.minecraft.Util ;
2016-12-18 17:28:29 +01:00
import net.minecraft.client.Minecraft ;
2024-03-02 21:23:08 +01:00
import net.minecraft.client.gui.Font ;
import net.minecraft.client.renderer.MultiBufferSource ;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer ;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider ;
2024-03-04 20:21:48 +01:00
import net.minecraft.core.registries.BuiltInRegistries ;
2024-03-12 21:31:29 +01:00
import net.minecraft.network.chat.Component ;
2024-03-02 21:23:08 +01:00
import net.minecraft.world.item.BlockItem ;
import net.minecraft.world.item.ItemStack ;
2024-03-04 20:21:48 +01:00
import net.neoforged.api.distmarker.Dist ;
import net.neoforged.api.distmarker.OnlyIn ;
import net.neoforged.neoforge.capabilities.Capabilities ;
2024-03-03 01:20:53 +01:00
import org.joml.Matrix4f ;
2016-12-18 17:28:29 +01:00
2024-03-04 20:21:48 +01:00
import java.util.Optional ;
2021-02-26 22:15:48 +01:00
@OnlyIn ( Dist . CLIENT )
2024-03-02 21:23:08 +01:00
public class RenderBatteryBox implements BlockEntityRenderer < TileEntityBatteryBox > {
public RenderBatteryBox ( BlockEntityRendererProvider . Context context ) {
2021-02-26 22:15:48 +01:00
}
2016-12-18 17:28:29 +01:00
2021-02-26 22:15:48 +01:00
@Override
2024-03-02 21:23:08 +01:00
public void render ( TileEntityBatteryBox tile , float partialTicks , PoseStack matrices , MultiBufferSource buffer , int combinedLight , int combinedOverlay ) {
2019-02-27 19:53:05 +01:00
ItemStack stack = tile . inv . getStackInSlot ( 0 ) ;
2021-02-28 11:35:10 +01:00
if ( stack . isEmpty ( ) | | ! ( stack . getItem ( ) instanceof ItemBattery ) ) {
return ;
}
2016-12-18 17:28:29 +01:00
2021-08-22 17:09:06 +02:00
matrices . pushPose ( ) ;
2021-02-28 11:35:10 +01:00
matrices . translate ( . 5f , . 35f , . 5f ) ;
2024-03-03 01:20:53 +01:00
matrices . mulPose ( Axis . ZP . rotationDegrees ( 180 ) ) ;
2016-12-18 17:28:29 +01:00
2021-08-22 17:09:06 +02:00
matrices . pushPose ( ) ;
2021-02-28 11:35:10 +01:00
matrices . scale ( 0 . 0075F , 0 . 0075F , 0 . 0075F ) ;
matrices . translate ( 0F , 0F , - 60F ) ;
2016-12-18 17:28:29 +01:00
2024-03-04 20:21:48 +01:00
Optional . ofNullable ( stack . getCapability ( Capabilities . EnergyStorage . ITEM ) ) . ifPresent ( cap - > {
2024-03-02 21:23:08 +01:00
Font font = Minecraft . getInstance ( ) . font ;
2016-12-18 17:28:29 +01:00
2021-02-28 11:35:10 +01:00
String energyTotal = Lang . cleanEnergyValues ( cap , false ) ;
2024-03-12 21:31:29 +01:00
Component energyName = Component . translatable ( " misc.actuallyadditions.power_name_long " ) ;
2024-03-03 01:20:53 +01:00
float backgroundOpacity = Minecraft . getInstance ( ) . options . getBackgroundOpacity ( 0 . 25F ) ;
int j = ( int ) ( backgroundOpacity * 255 . 0F ) < < 24 ;
2016-12-18 17:28:29 +01:00
2021-02-28 11:35:10 +01:00
for ( int i = 0 ; i < 4 ; i + + ) {
2024-03-07 00:35:55 +01:00
Matrix4f pose = matrices . last ( ) . pose ( ) ;
font . drawInBatch ( energyTotal , - font . width ( energyTotal ) / 2F , 10F , 0xFFFFFF , false , pose , buffer , Font . DisplayMode . NORMAL , j , combinedLight ) ;
font . drawInBatch ( energyName , - font . width ( energyName ) / 2F , 20F , 0xFFFFFF , false , pose , buffer , Font . DisplayMode . NORMAL , j , combinedLight ) ;
2016-12-18 17:28:29 +01:00
2021-02-28 11:35:10 +01:00
matrices . translate ( - 60F , 0F , 60F ) ;
2024-03-03 01:20:53 +01:00
matrices . mulPose ( Axis . YP . rotationDegrees ( 90 ) ) ;
2021-02-28 11:35:10 +01:00
}
} ) ;
2016-12-18 17:28:29 +01:00
2021-08-22 17:09:06 +02:00
matrices . popPose ( ) ; // text rotation
matrices . popPose ( ) ; // rotation + centering
2016-12-18 17:28:29 +01:00
2021-08-22 17:09:06 +02:00
double boop = Util . getMillis ( ) / 800D ;
2021-02-28 11:35:10 +01:00
float scale = stack . getItem ( ) instanceof BlockItem
? 0 . 85F
: 0 . 65F ;
2016-12-18 17:28:29 +01:00
2021-08-22 17:09:06 +02:00
matrices . pushPose ( ) ;
2021-02-28 11:35:10 +01:00
matrices . translate ( . 5f , 1f + Math . sin ( boop % ( 2 * Math . PI ) ) * 0 . 065 , . 5f ) ;
2024-03-03 01:20:53 +01:00
matrices . mulPose ( Axis . YP . rotationDegrees ( ( float ) ( boop * 40D % 360 ) ) ) ;
2021-02-28 11:35:10 +01:00
matrices . scale ( scale , scale , scale ) ;
2016-12-18 17:28:29 +01:00
2021-02-28 11:35:10 +01:00
try {
2023-01-15 17:56:02 +01:00
AssetUtil . renderItemInWorld ( stack , combinedLight , combinedOverlay , matrices , buffer ) ;
2021-02-28 11:35:10 +01:00
} catch ( Exception e ) {
2024-03-04 20:21:48 +01:00
ActuallyAdditions . LOGGER . error ( " Something went wrong trying to render an item in a battery box! The item is " + BuiltInRegistries . ITEM . getKey ( stack . getItem ( ) ) + " ! " , e ) ;
2016-12-18 17:28:29 +01:00
}
2021-02-28 11:35:10 +01:00
2021-08-22 17:09:06 +02:00
matrices . popPose ( ) ;
2016-12-18 17:28:29 +01:00
}
}