Applied-Energistics-2-tiler.../src/main/java/appeng/client/ClientHelper.java

472 lines
14 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
2015-05-18 00:12:13 +02:00
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
2014-11-14 12:02:52 +01:00
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
2014-09-24 02:26:27 +02:00
package appeng.client;
2014-09-24 02:26:27 +02:00
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
2015-06-26 22:13:04 +02:00
import org.lwjgl.opengl.GL11;
2014-09-24 02:26:27 +02:00
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
2014-09-24 02:26:27 +02:00
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.renderer.color.IBlockColor;
import net.minecraft.client.renderer.color.ItemColors;
2014-09-24 02:26:27 +02:00
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
2015-06-16 02:44:59 +02:00
import net.minecraft.item.Item;
2014-09-24 02:26:27 +02:00
import net.minecraft.item.ItemStack;
2015-09-29 15:47:55 +02:00
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.IBlockAccess;
2014-09-24 02:26:27 +02:00
import net.minecraft.world.World;
2015-06-16 02:44:59 +02:00
import net.minecraftforge.client.event.ModelBakeEvent;
2014-09-24 02:26:27 +02:00
import net.minecraftforge.client.event.MouseEvent;
import net.minecraftforge.client.event.RenderLivingEvent;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.client.model.ModelLoaderRegistry;
2014-09-24 02:26:27 +02:00
import net.minecraftforge.common.MinecraftForge;
2015-06-16 02:44:59 +02:00
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
2014-09-24 02:26:27 +02:00
import appeng.api.parts.CableRenderMode;
import appeng.api.util.AEColor;
import appeng.block.AEBaseBlock;
import appeng.client.render.effects.AssemblerFX;
import appeng.client.render.effects.CraftingFx;
import appeng.client.render.effects.EnergyFx;
import appeng.client.render.effects.LightningArcFX;
import appeng.client.render.effects.LightningFX;
import appeng.client.render.effects.VibrantFX;
import appeng.client.render.model.GlassModelLoader;
import appeng.client.render.model.UVLModelLoader;
import appeng.client.render.textures.ParticleTextures;
2014-09-24 02:26:27 +02:00
import appeng.core.AEConfig;
import appeng.core.AELog;
import appeng.core.Api;
2014-09-24 02:26:27 +02:00
import appeng.core.CommonHelper;
import appeng.core.features.IAEFeature;
2014-09-24 02:26:27 +02:00
import appeng.core.sync.network.NetworkHandler;
import appeng.core.sync.packets.PacketAssemblerAnimation;
import appeng.core.sync.packets.PacketValueConfig;
import appeng.entity.EntityFloatingItem;
import appeng.entity.EntityTinyTNTPrimed;
import appeng.entity.RenderFloatingItem;
import appeng.entity.RenderTinyTNTPrimed;
import appeng.helpers.IMouseWheelItem;
import appeng.hooks.TickHandler;
import appeng.hooks.TickHandler.PlayerColor;
import appeng.items.misc.ItemPaintBall;
2014-09-24 02:26:27 +02:00
import appeng.server.ServerHelper;
import appeng.transformer.MissingCoreMod;
import appeng.util.Platform;
2014-09-24 02:26:27 +02:00
public class ClientHelper extends ServerHelper
{
2015-06-26 22:13:04 +02:00
@Override
public void preinit()
2014-09-24 02:26:27 +02:00
{
2015-06-26 22:13:04 +02:00
MinecraftForge.EVENT_BUS.register( this );
ModelLoaderRegistry.registerLoader( UVLModelLoader.INSTANCE );
ModelLoaderRegistry.registerLoader( new GlassModelLoader() );
for( IAEFeature feature : Api.INSTANCE.definitions().getFeatureRegistry().getRegisteredFeatures() )
{
feature.handler().registerStateMapper();
}
}
2014-09-24 02:26:27 +02:00
@Override
2015-06-26 22:13:04 +02:00
public void init()
{
2015-12-24 02:03:16 +01:00
// final Block fluixBlock = GameRegistry.findBlock( "appliedenergistics2", "fluix" );
// Item fluixItem = Item.getItemFromBlock( fluixBlock );
// ModelResourceLocation itemModelResourceLocation = new ModelResourceLocation( "appliedenergistics2:fluix",
// "inventory" );
// final int DEFAULT_ITEM_SUBTYPE = 0;
// final ItemModelMesher mesher = Minecraft.getMinecraft().getRenderItem().getItemModelMesher();
// // mesher.register( fluixItem, DEFAULT_ITEM_SUBTYPE, itemModelResourceLocation );
//
// final ResourceLocation resource = new ResourceLocation( "appliedenergistics2", "stair.fluix" );
// final ModelResourceLocation fluixStairModel = new ModelResourceLocation( resource, "inventory" );
// AELog.info( "FluixStairModel: " + fluixStairModel );
//
// final Set<Item> items = AEApi.instance().definitions().blocks().fluixStair().maybeItem().asSet();
// for( Item item : items )
// {
// AELog.info( "Registering with %s with unlocalized %s", item, item.getUnlocalizedName() );
// mesher.register( item, DEFAULT_ITEM_SUBTYPE, fluixStairModel );
// }
for( IAEFeature feature : Api.INSTANCE.definitions().getFeatureRegistry().getRegisteredFeatures() )
{
feature.handler().registerModel();
if( feature instanceof AEBaseBlock )
{
Minecraft.getMinecraft().getBlockColors().registerBlockColorHandler( new AEBaseBlockColor(), (Block) feature );
}
}
// Register color handling for paintball items
ItemColors itemColors = Minecraft.getMinecraft().getItemColors();
Item paintballItem = Api.INSTANCE.definitions().items().paintBall().maybeItem().get();
itemColors.registerItemColorHandler( ItemPaintBall::getColorFromItemstack, paintballItem );
2014-09-24 02:26:27 +02:00
}
@Override
public World getWorld()
2014-09-24 02:26:27 +02:00
{
if( Platform.isClient() )
2015-04-29 02:30:53 +02:00
{
return Minecraft.getMinecraft().theWorld;
2015-04-29 02:30:53 +02:00
}
else
2015-04-29 02:30:53 +02:00
{
return super.getWorld();
2015-04-29 02:30:53 +02:00
}
}
2014-09-24 02:26:27 +02:00
@Override
2015-09-30 14:24:40 +02:00
public void bindTileEntitySpecialRenderer( final Class<? extends TileEntity> tile, final AEBaseBlock blk )
{
}
2014-09-24 02:26:27 +02:00
@Override
public List<EntityPlayer> getPlayers()
{
if( Platform.isClient() )
{
2015-09-30 14:24:40 +02:00
final List<EntityPlayer> o = new ArrayList<>();
o.add( Minecraft.getMinecraft().thePlayer );
return o;
}
else
2015-04-29 02:30:53 +02:00
{
return super.getPlayers();
2015-04-29 02:30:53 +02:00
}
}
2014-09-24 02:26:27 +02:00
@Override
2015-09-30 14:24:40 +02:00
public void spawnEffect( final EffectType effect, final World worldObj, final double posX, final double posY, final double posZ, final Object o )
{
if( AEConfig.instance.enableEffects )
{
switch( effect )
{
case Assembler:
this.spawnAssembler( worldObj, posX, posY, posZ, o );
return;
case Vibrant:
this.spawnVibrant( worldObj, posX, posY, posZ );
return;
case Crafting:
this.spawnCrafting( worldObj, posX, posY, posZ );
return;
case Energy:
this.spawnEnergy( worldObj, posX, posY, posZ );
return;
case Lightning:
this.spawnLightning( worldObj, posX, posY, posZ );
return;
case LightningArc:
this.spawnLightningArc( worldObj, posX, posY, posZ, (Vec3d) o );
return;
default:
}
}
}
2014-09-24 02:26:27 +02:00
@Override
2015-09-30 14:24:40 +02:00
public boolean shouldAddParticles( final Random r )
{
2015-09-30 14:24:40 +02:00
final int setting = Minecraft.getMinecraft().gameSettings.particleSetting;
if( setting == 2 )
2015-04-29 02:30:53 +02:00
{
return false;
2015-04-29 02:30:53 +02:00
}
if( setting == 0 )
2015-04-29 02:30:53 +02:00
{
return true;
2015-04-29 02:30:53 +02:00
}
return r.nextInt( 2 * ( setting + 1 ) ) == 0;
}
@Override
public RayTraceResult getRTR()
{
return Minecraft.getMinecraft().objectMouseOver;
}
2014-09-24 02:26:27 +02:00
@Override
2015-09-30 14:24:40 +02:00
public void doRenderItem( final ItemStack itemstack, final World w )
2014-09-24 02:26:27 +02:00
{
if( itemstack != null )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final EntityItem entityitem = new EntityItem( w, 0.0D, 0.0D, 0.0D, itemstack );
2014-09-24 02:26:27 +02:00
entityitem.getEntityItem().stackSize = 1;
// set all this stuff and then do shit? meh?
entityitem.hoverStart = 0;
2015-06-16 02:44:59 +02:00
entityitem.setNoDespawn();
2014-09-24 02:26:27 +02:00
entityitem.rotationYaw = 0;
GL11.glPushMatrix();
GL11.glTranslatef( 0, -0.04F, 0 );
GL11.glColor4f( 1.0F, 1.0F, 1.0F, 1.0F );
// GL11.glDisable( GL11.GL_CULL_FACE );
2015-06-16 02:44:59 +02:00
// TODO RENDER ITEM FOR STORAGE MONITOR!
GL11.glPopMatrix();
}
}
@Override
public void postInit()
{
2015-12-24 02:03:16 +01:00
// RenderingRegistry.registerBlockHandler( WorldRender.INSTANCE );
2015-09-30 14:24:40 +02:00
final RenderManager inst = Minecraft.getMinecraft().getRenderManager();
2015-06-16 02:44:59 +02:00
2015-06-26 22:13:04 +02:00
inst.entityRenderMap.put( EntityTinyTNTPrimed.class, new RenderTinyTNTPrimed( inst ) );
inst.entityRenderMap.put( EntityFloatingItem.class, new RenderFloatingItem( inst ) );
2014-09-24 02:26:27 +02:00
}
2015-06-26 22:13:04 +02:00
@Override
public CableRenderMode getRenderMode()
{
if( Platform.isServer() )
2015-04-29 02:30:53 +02:00
{
return super.getRenderMode();
2015-04-29 02:30:53 +02:00
}
2015-09-30 14:24:40 +02:00
final Minecraft mc = Minecraft.getMinecraft();
final EntityPlayer player = mc.thePlayer;
return this.renderModeForPlayer( player );
}
@Override
public void triggerUpdates()
{
2015-09-30 14:24:40 +02:00
final Minecraft mc = Minecraft.getMinecraft();
if( mc == null || mc.thePlayer == null || mc.theWorld == null )
2015-04-29 02:30:53 +02:00
{
return;
2015-04-29 02:30:53 +02:00
}
2015-09-30 14:24:40 +02:00
final EntityPlayer player = mc.thePlayer;
2015-09-30 14:24:40 +02:00
final int x = (int) player.posX;
final int y = (int) player.posY;
final int z = (int) player.posZ;
2015-09-30 14:24:40 +02:00
final int range = 16 * 16;
mc.theWorld.markBlockRangeForRenderUpdate( x - range, y - range, z - range, x + range, y + range, z + range );
}
@Override
public void missingCoreMod()
{
throw new MissingCoreMod();
}
@SubscribeEvent
public void modelsBake( ModelBakeEvent event )
{
UVLModelLoader.INSTANCE.setLoader( event.getModelLoader() );
}
2015-06-26 22:13:04 +02:00
@SubscribeEvent
2015-09-30 14:24:40 +02:00
public void postPlayerRender( final RenderLivingEvent.Pre p )
2015-06-26 22:13:04 +02:00
{
final PlayerColor player = TickHandler.INSTANCE.getPlayerColors().get( p.getEntity().getEntityId() );
2015-06-26 22:13:04 +02:00
if( player != null )
{
2015-09-30 14:24:40 +02:00
final AEColor col = player.myColor;
2015-06-26 22:13:04 +02:00
2015-09-30 14:24:40 +02:00
final float r = 0xff & ( col.mediumVariant >> 16 );
final float g = 0xff & ( col.mediumVariant >> 8 );
final float b = 0xff & ( col.mediumVariant );
2015-06-26 22:13:04 +02:00
GL11.glColor3f( r / 255.0f, g / 255.0f, b / 255.0f );
}
}
2015-09-30 14:24:40 +02:00
private void spawnAssembler( final World worldObj, final double posX, final double posY, final double posZ, final Object o )
2015-06-26 22:13:04 +02:00
{
2015-09-30 14:24:40 +02:00
final PacketAssemblerAnimation paa = (PacketAssemblerAnimation) o;
2015-06-26 22:13:04 +02:00
2015-09-30 14:24:40 +02:00
final AssemblerFX fx = new AssemblerFX( Minecraft.getMinecraft().theWorld, posX, posY, posZ, 0.0D, 0.0D, 0.0D, paa.rate, paa.is );
2015-06-26 22:13:04 +02:00
Minecraft.getMinecraft().effectRenderer.addEffect( fx );
}
2015-09-30 14:24:40 +02:00
private void spawnVibrant( final World w, final double x, final double y, final double z )
2015-06-26 22:13:04 +02:00
{
if( CommonHelper.proxy.shouldAddParticles( Platform.getRandom() ) )
{
2015-09-30 14:24:40 +02:00
final double d0 = ( Platform.getRandomFloat() - 0.5F ) * 0.26D;
final double d1 = ( Platform.getRandomFloat() - 0.5F ) * 0.26D;
final double d2 = ( Platform.getRandomFloat() - 0.5F ) * 0.26D;
2015-06-26 22:13:04 +02:00
2015-09-30 14:24:40 +02:00
final VibrantFX fx = new VibrantFX( w, x + d0, y + d1, z + d2, 0.0D, 0.0D, 0.0D );
2015-06-26 22:13:04 +02:00
Minecraft.getMinecraft().effectRenderer.addEffect( fx );
}
}
2015-09-30 14:24:40 +02:00
private void spawnCrafting( final World w, final double posX, final double posY, final double posZ )
2015-06-26 22:13:04 +02:00
{
2015-09-30 14:24:40 +02:00
final float x = (float) ( ( ( Platform.getRandomInt() % 100 ) * 0.01 ) - 0.5 ) * 0.7f;
final float y = (float) ( ( ( Platform.getRandomInt() % 100 ) * 0.01 ) - 0.5 ) * 0.7f;
final float z = (float) ( ( ( Platform.getRandomInt() % 100 ) * 0.01 ) - 0.5 ) * 0.7f;
2015-06-26 22:13:04 +02:00
final CraftingFx fx = new CraftingFx( w, posX + x, posY + y, posZ + z, Items.DIAMOND );
2015-06-26 22:13:04 +02:00
fx.setMotionX( -x * 0.2f );
fx.setMotionY( -y * 0.2f );
fx.setMotionZ( -z * 0.2f );
2015-06-26 22:13:04 +02:00
Minecraft.getMinecraft().effectRenderer.addEffect( fx );
}
2015-09-30 14:24:40 +02:00
private void spawnEnergy( final World w, final double posX, final double posY, final double posZ )
2015-06-26 22:13:04 +02:00
{
2015-09-30 14:24:40 +02:00
final float x = (float) ( ( ( Platform.getRandomInt() % 100 ) * 0.01 ) - 0.5 ) * 0.7f;
final float y = (float) ( ( ( Platform.getRandomInt() % 100 ) * 0.01 ) - 0.5 ) * 0.7f;
final float z = (float) ( ( ( Platform.getRandomInt() % 100 ) * 0.01 ) - 0.5 ) * 0.7f;
2015-06-26 22:13:04 +02:00
final EnergyFx fx = new EnergyFx( w, posX + x, posY + y, posZ + z, Items.DIAMOND );
2015-06-26 22:13:04 +02:00
fx.setMotionX( -x * 0.1f );
fx.setMotionY( -y * 0.1f );
fx.setMotionZ( -z * 0.1f );
2015-06-26 22:13:04 +02:00
Minecraft.getMinecraft().effectRenderer.addEffect( fx );
}
2015-09-30 14:24:40 +02:00
private void spawnLightning( final World worldObj, final double posX, final double posY, final double posZ )
2015-06-26 22:13:04 +02:00
{
2015-09-30 14:24:40 +02:00
final LightningFX fx = new LightningFX( worldObj, posX, posY + 0.3f, posZ, 0.0f, 0.0f, 0.0f );
2015-06-26 22:13:04 +02:00
Minecraft.getMinecraft().effectRenderer.addEffect( fx );
}
private void spawnLightningArc( final World worldObj, final double posX, final double posY, final double posZ, final Vec3d second )
2015-06-26 22:13:04 +02:00
{
2015-09-30 14:24:40 +02:00
final LightningFX fx = new LightningArcFX( worldObj, posX, posY, posZ, second.xCoord, second.yCoord, second.zCoord, 0.0f, 0.0f, 0.0f );
2015-06-26 22:13:04 +02:00
Minecraft.getMinecraft().effectRenderer.addEffect( fx );
}
@SubscribeEvent
2015-09-30 14:24:40 +02:00
public void onModelBakeEvent( final ModelBakeEvent event )
2015-06-26 22:13:04 +02:00
{
for( IAEFeature feature : Api.INSTANCE.definitions().getFeatureRegistry().getRegisteredFeatures() )
{
feature.handler().registerCustomModelOverride(event.getModelRegistry());
}
2015-06-26 22:13:04 +02:00
}
2014-09-24 02:26:27 +02:00
@SubscribeEvent
2015-09-30 14:24:40 +02:00
public void wheelEvent( final MouseEvent me )
2014-09-24 02:26:27 +02:00
{
if( me.isCanceled() || me.getDwheel() == 0 )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
final Minecraft mc = Minecraft.getMinecraft();
final EntityPlayer player = mc.thePlayer;
if( player.isSneaking() )
2014-09-24 02:26:27 +02:00
{
final EnumHand hand;
if( player.getHeldItem( EnumHand.MAIN_HAND ) != null && player.getHeldItem( EnumHand.MAIN_HAND ).getItem() instanceof IMouseWheelItem )
{
hand = EnumHand.MAIN_HAND;
}
else if( player.getHeldItem( EnumHand.OFF_HAND ) != null && player.getHeldItem( EnumHand.OFF_HAND ).getItem() instanceof IMouseWheelItem )
{
hand = EnumHand.OFF_HAND;
}
else
{
return;
}
final ItemStack is = player.getHeldItem( hand );
2014-09-24 02:26:27 +02:00
try
{
NetworkHandler.instance.sendToServer( new PacketValueConfig( "Item", me.getDwheel() > 0 ? "WheelUp" : "WheelDown" ) );
2014-09-24 02:26:27 +02:00
me.setCanceled( true );
}
2015-09-30 14:24:40 +02:00
catch( final IOException e )
2014-09-24 02:26:27 +02:00
{
AELog.debug( e );
2014-09-24 02:26:27 +02:00
}
}
}
@SubscribeEvent
public void onTextureStitch( final TextureStitchEvent.Pre event )
{
ParticleTextures.registerSprite( event );
}
2015-06-26 22:13:04 +02:00
private static class IconReg
{
public final String name;
public final Object item;
public final int meta;
public final ModelResourceLocation loc;
2015-09-30 14:24:40 +02:00
public IconReg( final Object item2, final int meta2, final String name2 )
2015-06-26 22:13:04 +02:00
{
2015-09-30 14:26:54 +02:00
this.meta = meta2;
this.name = name2;
this.item = item2;
this.loc = null;
2015-06-26 22:13:04 +02:00
}
2015-06-16 02:44:59 +02:00
2015-09-30 14:24:40 +02:00
public IconReg( final Item item2, final int meta2, final String name2, final ModelResourceLocation res )
2015-06-26 22:13:04 +02:00
{
2015-09-30 14:26:54 +02:00
this.meta = meta2;
this.name = name2;
this.item = item2;
this.loc = res;
2015-06-26 22:13:04 +02:00
}
}
public static class AEBaseBlockColor implements IBlockColor
{
@Override
public int colorMultiplier( IBlockState state, IBlockAccess worldIn, BlockPos pos, int tintIndex )
{
return tintIndex;
}
}
}