Advertisement
Guest User

Untitled

a guest
Jan 5th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 33.55 KB | None | 0 0
  1. package fr.ph1823.MyLife;
  2.  
  3. /**
  4. * Created by ph1823 - Minecraft on 04/01/2017.
  5. */
  6.  
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.Arrays;
  10. import java.util.List;
  11.  
  12. import javax.vecmath.AxisAngle4d;
  13. import javax.vecmath.Matrix4f;
  14. import javax.vecmath.Quat4f;
  15. import javax.vecmath.Vector3d;
  16. import javax.vecmath.Vector4f;
  17.  
  18. import erc._core.ERC_CONST;
  19. import net.minecraft.block.Block;
  20. import net.minecraft.block.ITileEntityProvider;
  21. import net.minecraft.block.material.Material;
  22. import net.minecraft.block.properties.IProperty;
  23. import net.minecraft.block.properties.PropertyBool;
  24. import net.minecraft.block.properties.PropertyDirection;
  25. import net.minecraft.block.state.BlockState;
  26. import net.minecraft.block.state.IBlockState;
  27. import net.minecraft.client.Minecraft;
  28. import net.minecraft.client.resources.model.IBakedModel;
  29. import net.minecraft.client.resources.model.ModelResourceLocation;
  30. import net.minecraft.creativetab.CreativeTabs;
  31. import net.minecraft.entity.EntityLivingBase;
  32. import net.minecraft.entity.player.EntityPlayer;
  33. import net.minecraft.item.Item;
  34. import net.minecraft.tileentity.TileEntity;
  35. import net.minecraft.util.BlockPos;
  36. import net.minecraft.util.ChatComponentText;
  37. import net.minecraft.util.EnumFacing;
  38. import net.minecraft.util.ITickable;
  39. import net.minecraft.util.MathHelper;
  40. import net.minecraft.util.ResourceLocation;
  41. import net.minecraft.world.IBlockAccess;
  42. import net.minecraft.world.World;
  43. import net.minecraftforge.client.model.IModel;
  44. import net.minecraftforge.client.model.ModelLoader;
  45. import net.minecraftforge.client.model.ModelLoaderRegistry;
  46. import net.minecraftforge.client.model.TRSRTransformation;
  47. import net.minecraftforge.client.model.b3d.B3DLoader;
  48. import net.minecraftforge.client.model.obj.OBJLoader;
  49. import net.minecraftforge.client.model.obj.OBJModel;
  50. import net.minecraftforge.client.model.obj.OBJModel.OBJBakedModel;
  51. import net.minecraftforge.common.property.ExtendedBlockState;
  52. import net.minecraftforge.common.property.IExtendedBlockState;
  53. import net.minecraftforge.common.property.IUnlistedProperty;
  54. import net.minecraftforge.fml.common.FMLLog;
  55. import net.minecraftforge.fml.common.Mod;
  56. import net.minecraftforge.fml.common.Mod.EventHandler;
  57. import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
  58. import net.minecraftforge.fml.common.registry.GameRegistry;
  59. import net.minecraftforge.fml.relauncher.Side;
  60. import net.minecraftforge.fml.relauncher.SideOnly;
  61.  
  62. import com.google.common.collect.ImmutableList;
  63. import com.google.common.collect.Lists;
  64.  
  65. @Mod(modid = ModelLoaderRegistryDebug.MODID, version = ModelLoaderRegistryDebug.VERSION)
  66. public class ModelLoaderRegistryDebug
  67. {
  68.    public static final String MODID = "ForgeDebugModelLoaderRegistry";
  69.    public static final String VERSION = "1.0";
  70.  
  71.    @EventHandler
  72.    public void preInit(FMLPreInitializationEvent event)
  73.    {
  74.        GameRegistry.registerBlock(CustomModelBlock.instance, CustomModelBlock.name);
  75.        GameRegistry.registerBlock(OBJTesseractBlock.instance, OBJTesseractBlock.name);
  76.        GameRegistry.registerBlock(OBJVertexColoring1.instance, OBJVertexColoring1.name);
  77.        GameRegistry.registerBlock(OBJDirectionEye.instance, OBJDirectionEye.name);
  78.        GameRegistry.registerBlock(OBJVertexColoring2.instance, OBJVertexColoring2.name);
  79.        GameRegistry.registerBlock(OBJDirectionBlock.instance, OBJDirectionBlock.name);
  80.        GameRegistry.registerBlock(OBJCustomDataBlock.instance, OBJCustomDataBlock.name);
  81.        GameRegistry.registerBlock(OBJDynamicEye.instance, OBJDynamicEye.name);
  82.        GameRegistry.registerTileEntity(OBJTesseractTileEntity.class, OBJTesseractBlock.name);
  83.        GameRegistry.registerTileEntity(OBJVertexColoring2TileEntity.class, OBJVertexColoring2.name);
  84.        GameRegistry.registerTileEntity(OBJDynamicEyeTileEntity.class, OBJDynamicEye.name);
  85.        if (event.getSide() == Side.CLIENT)
  86.            clientPreInit();
  87.    }
  88.  
  89.    private void clientPreInit()
  90.    {
  91.        B3DLoader.instance.addDomain(MODID.toLowerCase());
  92.        Item item = Item.getItemFromBlock(CustomModelBlock.instance);
  93.        ModelLoader.setCustomModelResourceLocation(item, 0, new ModelResourceLocation(MODID.toLowerCase() + ":" + CustomModelBlock.name, "inventory"));
  94.  
  95.        OBJLoader.instance.addDomain(MODID.toLowerCase());
  96.  
  97.        OBJLoader.instance.addDomain(ERC_CONST.DOMAIN);
  98.        Item item2 = Item.getItemFromBlock(OBJTesseractBlock.instance);
  99.        ModelLoader.setCustomModelResourceLocation(item2, 0, new ModelResourceLocation(MODID.toLowerCase() + ":" + OBJTesseractBlock.name, "inventory"));
  100.  
  101.        Item item3 = Item.getItemFromBlock(OBJVertexColoring1.instance);
  102.        ModelLoader.setCustomModelResourceLocation(item3, 0, new ModelResourceLocation(MODID.toLowerCase() + ":" + OBJVertexColoring1.name, "inventory"));
  103.  
  104.        Item item4 = Item.getItemFromBlock(OBJDirectionEye.instance);
  105.        ModelLoader.setCustomModelResourceLocation(item4, 0, new ModelResourceLocation(MODID.toLowerCase() + ":" + OBJDirectionEye.name, "inventory"));
  106.  
  107.        Item item5 = Item.getItemFromBlock(OBJVertexColoring2.instance);
  108.        ModelLoader.setCustomModelResourceLocation(item5, 0, new ModelResourceLocation(MODID.toLowerCase() + ":" + OBJVertexColoring2.name, "inventory"));
  109.  
  110.        Item item6 = Item.getItemFromBlock(OBJDirectionBlock.instance);
  111.        ModelLoader.setCustomModelResourceLocation(item6, 0, new ModelResourceLocation(MODID.toLowerCase() + ":" + OBJDirectionBlock.name, "inventory"));
  112.  
  113.        Item item7 = Item.getItemFromBlock(OBJCustomDataBlock.instance);
  114.        ModelLoader.setCustomModelResourceLocation(item7, 0, new ModelResourceLocation(MODID.toLowerCase() + ":" + OBJCustomDataBlock.name, "inventory"));
  115.  
  116.        Item item8 = Item.getItemFromBlock(OBJDynamicEye.instance);
  117.        ModelLoader.setCustomModelResourceLocation(item8, 0, new ModelResourceLocation(MODID.toLowerCase() + ":" + OBJDynamicEye.name, "inventory"));
  118.    }
  119.  
  120.    public static class CustomModelBlock extends Block
  121.    {
  122.        public static final PropertyDirection FACING = PropertyDirection.create("facing");
  123.        public static final CustomModelBlock instance = new CustomModelBlock();
  124.        public static final String name = "CustomModelBlock";
  125.        private int counter = 1;
  126.        public ExtendedBlockState state = new ExtendedBlockState(this, new IProperty[]{FACING}, new IUnlistedProperty[]{B3DLoader.B3DFrameProperty.instance});
  127.  
  128.        private CustomModelBlock()
  129.        {
  130.            super(Material.iron);
  131.            this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
  132.            setCreativeTab(CreativeTabs.tabBlock);
  133.            setUnlocalizedName(MODID + ":" + name);
  134.        }
  135.  
  136.        @Override
  137.        public boolean isOpaqueCube() { return false; }
  138.  
  139.        @Override
  140.        public boolean isFullCube() { return false; }
  141.  
  142.        @Override
  143.        public boolean isVisuallyOpaque() { return false; }
  144.  
  145.        @Override
  146.        public IBlockState onBlockPlaced(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
  147.        {
  148.            return this.getDefaultState().withProperty(FACING, getFacingFromEntity(world, pos, placer));
  149.        }
  150.  
  151.        @Override
  152.        public IBlockState getStateFromMeta(int meta)
  153.        {
  154.            return this.getDefaultState().withProperty(FACING, EnumFacing.getFront(meta));
  155.        }
  156.  
  157.        @Override
  158.        public int getMetaFromState(IBlockState state)
  159.        {
  160.            return ((EnumFacing) state.getValue(FACING)).getIndex();
  161.        }
  162.  
  163.        @Override
  164.        public IBlockState getExtendedState(IBlockState state, IBlockAccess world, BlockPos pos)
  165.        {
  166.            //Only return an IExtendedBlockState from this method and createState(), otherwise block placement might break!
  167.            B3DLoader.B3DState newState = new B3DLoader.B3DState(null, counter);
  168.            return ((IExtendedBlockState) state).withProperty(B3DLoader.B3DFrameProperty.instance, newState);
  169.        }
  170.  
  171.        @Override
  172.        public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ)
  173.        {
  174.            if(world.isRemote)
  175.            {
  176.                System.out.println("click " + counter);
  177.                if(player.isSneaking()) counter--;
  178.                else counter++;
  179.                //if(counter >= model.getNode().getKeys().size()) counter = 0;
  180.                world.markBlockRangeForRenderUpdate(pos, pos);
  181.            }
  182.            return false;
  183.        }
  184.  
  185.        @Override
  186.        public BlockState createBlockState()
  187.        {
  188.            return new ExtendedBlockState(this, new IProperty[]{FACING}, new IUnlistedProperty[]{B3DLoader.B3DFrameProperty.instance});
  189.        }
  190.  
  191.        public static EnumFacing getFacingFromEntity(World worldIn, BlockPos clickedBlock, EntityLivingBase entityIn)
  192.        {
  193.            if (MathHelper.abs((float)entityIn.posX - (float)clickedBlock.getX()) < 2.0F && MathHelper.abs((float)entityIn.posZ - (float)clickedBlock.getZ()) < 2.0F)
  194.            {
  195.                double d0 = entityIn.posY + (double)entityIn.getEyeHeight();
  196.  
  197.                if (d0 - (double)clickedBlock.getY() > 2.0D)
  198.                {
  199.                    return EnumFacing.UP;
  200.                }
  201.  
  202.                if ((double)clickedBlock.getY() - d0 > 0.0D)
  203.                {
  204.                    return EnumFacing.DOWN;
  205.                }
  206.            }
  207.  
  208.            return entityIn.getHorizontalFacing().getOpposite();
  209.        }
  210.    }
  211.    /**
  212.     * This block is intended to demonstrate how to change the visibility of a group(s)
  213.     * from within the block's class.
  214.     * By right clicking on this block the player increments an integer value in the tile entity
  215.     * for this block, which is then added to a list of strings and passed into the constructor
  216.     * for OBJState. NOTE: this trick only works if your groups are named '1', '2', '3', etc.,
  217.     * otherwise they must be added by name.
  218.     * Holding shift decrements the value in the tile entity.
  219.     * @author shadekiller666
  220.     *
  221.     */
  222.    public static class OBJTesseractBlock extends Block implements ITileEntityProvider
  223.    {
  224.        public static final OBJTesseractBlock instance = new OBJTesseractBlock();
  225.        public static final String name = "OBJTesseractBlock";
  226.        private ExtendedBlockState state = new ExtendedBlockState(this, new IProperty[0], new IUnlistedProperty[]{OBJModel.OBJProperty.instance});
  227.  
  228.        private OBJTesseractBlock()
  229.        {
  230.            super(Material.iron);
  231.            setCreativeTab(CreativeTabs.tabBlock);
  232.            setUnlocalizedName(MODID + ":" + name);
  233.        }
  234.  
  235.        @Override
  236.        public TileEntity createNewTileEntity(World worldIn, int meta)
  237.        {
  238.            return new OBJTesseractTileEntity();
  239.        }
  240.  
  241.        @Override
  242.        public boolean isOpaqueCube() { return false; }
  243.  
  244.        @Override
  245.        public boolean isFullCube() { return false; }
  246.  
  247.        @Override
  248.        public boolean isVisuallyOpaque() { return false; }
  249.  
  250.        @Override
  251.        public IBlockState getExtendedState(IBlockState state, IBlockAccess world, BlockPos pos)
  252.        {
  253.            OBJTesseractTileEntity tileEntity = (OBJTesseractTileEntity) world.getTileEntity(pos);
  254.            OBJModel.OBJState retState = new OBJModel.OBJState(tileEntity == null ? Lists.newArrayList(OBJModel.Group.ALL) : tileEntity.visible, true);
  255.            return ((IExtendedBlockState) this.state.getBaseState()).withProperty(OBJModel.OBJProperty.instance, retState);
  256.        }
  257.  
  258.        @Override
  259.        public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ)
  260.        {
  261.            if (world.getTileEntity(pos) == null) world.setTileEntity(pos, new OBJTesseractTileEntity());
  262.            OBJTesseractTileEntity tileEntity = (OBJTesseractTileEntity) world.getTileEntity(pos);
  263.            IModel model = ModelLoaderRegistry.getMissingModel();
  264.            try
  265.            {
  266.                model = ModelLoaderRegistry.getModel(new ResourceLocation(ERC_CONST.DOMAIN+ ":" + "models/coaster.b3d"));
  267.            }
  268.            catch (IOException e)
  269.            {
  270.                model = ModelLoaderRegistry.getMissingModel();
  271.            }
  272.  
  273.            if (player.isSneaking())
  274.            {
  275.                tileEntity.decrement();
  276.            }
  277.            else
  278.            {
  279.                if (model != ModelLoaderRegistry.getMissingModel())
  280.                {
  281.                    tileEntity.setMax(((OBJModel) model).getMatLib().getGroups().keySet().size() - 1);
  282.                    tileEntity.increment();
  283.                }
  284.            }
  285.  
  286.            if (world.isRemote)
  287.            {
  288.                OBJBakedModel objBaked = (OBJBakedModel) Minecraft.getMinecraft().getBlockRendererDispatcher().getModelFromBlockState(state, world, pos);
  289.                objBaked.scheduleRebake();  //not necessarily needed for this specific case, but is available
  290.            }
  291.            world.markBlockRangeForRenderUpdate(pos, pos);
  292.            return false;
  293.        }
  294.  
  295.        @Override
  296.        public boolean hasTileEntity(IBlockState state)
  297.        {
  298.            return true;
  299.        }
  300.    }
  301.  
  302.    public static class OBJTesseractTileEntity extends TileEntity
  303.    {
  304.        private int counter = 1;
  305.        private int max = 2;
  306.        public List<String> visible = new ArrayList<String>();
  307.  
  308.        public OBJTesseractTileEntity()
  309.        {
  310.            this.visible.add(OBJModel.Group.ALL);
  311.        }
  312.  
  313.        public void increment()
  314.        {
  315.            if (this.visible.contains(OBJModel.Group.ALL)) this.visible.remove(OBJModel.Group.ALL);
  316.            if (this.counter == max)
  317.            {
  318.                this.counter = 0;
  319.                this.visible.clear();
  320.            }
  321.            this.counter++;
  322.            this.visible.add(Integer.toString(this.counter));
  323.            ChatComponentText text = new ChatComponentText("" + this.counter);
  324.            if (this.worldObj.isRemote) Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(text);
  325.        }
  326.  
  327.        public void decrement()
  328.        {
  329.            if (this.visible.contains(OBJModel.Group.ALL)) this.visible.remove(OBJModel.Group.ALL);
  330.            if (this.counter == 1)
  331.            {
  332.                this.counter = max + 1;
  333.                for (int i = 1; i < max; i++) this.visible.add(Integer.toString(i));
  334.            }
  335.            this.visible.remove(Integer.toString(this.counter));
  336.            this.counter--;
  337.            ChatComponentText text = new ChatComponentText("" + this.counter);
  338.            if (this.worldObj.isRemote) Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(text);
  339.        }
  340.  
  341.        public void reset()
  342.        {
  343.            this.counter = 1;
  344.            this.max = 2;
  345.            this.visible.clear();
  346.            this.visible.add(Integer.toString(this.counter));
  347.        }
  348.  
  349.        public int getMax()
  350.        {
  351.            return this.max;
  352.        }
  353.  
  354.        public void setMax(int max)
  355.        {
  356.            this.max = max;
  357.        }
  358.  
  359.        public void setToMax()
  360.        {
  361.            this.counter = this.max;
  362.        }
  363.    }
  364.  
  365.    /**
  366.     * This block demonstrates how to utilize the vertex coloring feature
  367.     * of the OBJ loader. See 'vertex_coloring.obj' and 'vertex_coloring.mtl' in
  368.     * 'test/resources/assets/forgedebugmodelloaderregistry/models/block/', to properly
  369.     * utilize this feature an obj file must have 1 'usemtl' key before every vertex as shown,
  370.     * having less 'usemtl' lines than 'v' lines will result in the faces having that material's
  371.     * color instead of each vertex.
  372.     * @author shadekiller666
  373.     *
  374.     */
  375.    public static class OBJVertexColoring1 extends Block
  376.    {
  377.        public static final OBJVertexColoring1 instance = new OBJVertexColoring1();
  378.        public static final String name = "OBJVertexColoring1";
  379.  
  380.        private OBJVertexColoring1()
  381.        {
  382.            super(Material.iron);
  383.            setCreativeTab(CreativeTabs.tabBlock);
  384.            setUnlocalizedName(name);
  385.        }
  386.  
  387.        @Override
  388.        public boolean isOpaqueCube() { return false; }
  389.  
  390.        @Override
  391.        public boolean isFullCube() { return false; }
  392.  
  393.        @Override
  394.        public boolean isVisuallyOpaque() { return false; }
  395.    }
  396.  
  397.    /**
  398.     * This block demonstrates how to use IProperties and IUnlistedProperties together
  399.     * in the same ExtendedBlockState. Similar to pistons, this block will face the player
  400.     * when placed. Unlike pistons, however; this block's model is an eyeball, because
  401.     * the OBJ loader can load spheres.
  402.     * @author shadekiller666
  403.     *
  404.     */
  405.    public static class OBJDirectionEye extends Block
  406.    {
  407.        public static final PropertyDirection FACING = PropertyDirection.create("facing");
  408.        public static final OBJDirectionEye instance = new OBJDirectionEye();
  409.        public static final String name = "OBJDirectionEye";
  410.        private ExtendedBlockState state = new ExtendedBlockState(this, new IProperty[] {FACING}, new IUnlistedProperty[]{OBJModel.OBJProperty.instance});
  411.  
  412.        private OBJDirectionEye()
  413.        {
  414.            super(Material.iron);
  415.            setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
  416.            setCreativeTab(CreativeTabs.tabBlock);
  417.            setUnlocalizedName(name);
  418.        }
  419.  
  420.        @Override
  421.        public IBlockState onBlockPlaced(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
  422.        {
  423.            return this.getDefaultState().withProperty(FACING, getFacingFromEntity(world, pos, placer));
  424.        }
  425.  
  426.        @Override
  427.        public IBlockState getStateFromMeta(int meta)
  428.        {
  429.            return this.getDefaultState().withProperty(FACING, EnumFacing.getFront(meta));
  430.        }
  431.  
  432.        @Override
  433.        public int getMetaFromState(IBlockState state)
  434.        {
  435.            return ((EnumFacing) state.getValue(FACING)).getIndex();
  436.        }
  437.  
  438.        @Override
  439.        @SideOnly(Side.CLIENT)
  440.        public IBlockState getStateForEntityRender(IBlockState state)
  441.        {
  442.            return this.getDefaultState().withProperty(FACING, EnumFacing.NORTH);
  443.        }
  444.  
  445.        @Override
  446.        public BlockState createBlockState()
  447.        {
  448.            return new ExtendedBlockState(this, new IProperty[] {FACING}, new IUnlistedProperty[] {OBJModel.OBJProperty.instance});
  449.        }
  450.  
  451.        @Override
  452.        public boolean isOpaqueCube() { return false; }
  453.  
  454.        @Override
  455.        public boolean isFullCube() { return false; }
  456.  
  457.        @Override
  458.        public boolean isVisuallyOpaque() { return false; }
  459.  
  460.        @Override
  461.        public IBlockState getExtendedState(IBlockState state, IBlockAccess world, BlockPos pos)
  462.        {
  463.            EnumFacing facing = (EnumFacing) state.getValue(FACING);
  464.            TRSRTransformation transform = new TRSRTransformation(facing);
  465.            OBJModel.OBJState retState = new OBJModel.OBJState(Arrays.asList(new String[]{OBJModel.Group.ALL}), true, transform);
  466.            return ((IExtendedBlockState) state).withProperty(OBJModel.OBJProperty.instance, retState);
  467.        }
  468.  
  469.        public static EnumFacing getFacingFromEntity(World worldIn, BlockPos clickedBlock, EntityLivingBase entityIn)
  470.        {
  471.            if (MathHelper.abs((float)entityIn.posX - (float)clickedBlock.getX()) < 2.0F && MathHelper.abs((float)entityIn.posZ - (float)clickedBlock.getZ()) < 2.0F)
  472.            {
  473.                double d0 = entityIn.posY + (double)entityIn.getEyeHeight();
  474.  
  475.                if (d0 - (double)clickedBlock.getY() > 2.0D)
  476.                {
  477.                    return EnumFacing.DOWN;
  478.                }
  479.  
  480.                if ((double)clickedBlock.getY() - d0 > 0.0D)
  481.                {
  482.                    return EnumFacing.UP;
  483.                }
  484.            }
  485.  
  486.            return entityIn.getHorizontalFacing();
  487.        }
  488.    }
  489.  
  490.    /**
  491.     * This block uses the same model as CustomModelBlock3 does, but
  492.     * this class allows the player to cycle the colors of each vertex to black
  493.     * and then back to the original color when right clicking on the block.
  494.     * @author shadekiller666
  495.     *
  496.     */
  497.    public static class OBJVertexColoring2 extends Block implements ITileEntityProvider
  498.    {
  499.        public static final OBJVertexColoring2 instance = new OBJVertexColoring2();
  500.        public static final String name = "OBJVertexColoring2";
  501.  
  502.        private OBJVertexColoring2()
  503.        {
  504.            super(Material.iron);
  505.            setCreativeTab(CreativeTabs.tabBlock);
  506.            setUnlocalizedName(name);
  507.        }
  508.  
  509.        @Override
  510.        public TileEntity createNewTileEntity(World worldIn, int meta)
  511.        {
  512.            return new OBJVertexColoring2TileEntity();
  513.        }
  514.  
  515.        @Override
  516.        public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ)
  517.        {
  518.            if (world.getTileEntity(pos) != null && world.getTileEntity(pos) instanceof OBJVertexColoring2TileEntity)
  519.            {
  520.                ((OBJVertexColoring2TileEntity) world.getTileEntity(pos)).cycleColors();
  521.            }
  522.            return false;
  523.        }
  524.    }
  525.  
  526.    public static class OBJVertexColoring2TileEntity extends TileEntity
  527.    {
  528.        private int index = 0;
  529.        private int maxIndex = 1;
  530.        private List<Vector4f> colorList = new ArrayList<Vector4f>();
  531.        private boolean hasFilledList = false;
  532.        private boolean shouldIncrement = true;
  533.  
  534.        public OBJVertexColoring2TileEntity() {}
  535.  
  536.        public void cycleColors()
  537.        {
  538.            if (this.worldObj.isRemote)
  539.            {
  540.                FMLLog.info("%b", shouldIncrement);
  541.                IBakedModel bakedModel = Minecraft.getMinecraft().getBlockRendererDispatcher().getModelFromBlockState(this.worldObj.getBlockState(this.pos), this.worldObj, this.pos);
  542.                if (bakedModel != null && bakedModel instanceof OBJBakedModel)
  543.                {
  544.                    OBJBakedModel objBaked = (OBJBakedModel) bakedModel;
  545.                    ImmutableList<String> materialNames = objBaked.getModel().getMatLib().getMaterialNames();
  546.                    if (!hasFilledList)
  547.                    {
  548.                        for (String name : materialNames)
  549.                        {
  550.                            if (!name.equals(OBJModel.Material.WHITE_NAME))
  551.                            {
  552.                                colorList.add(objBaked.getModel().getMatLib().getMaterial(name).getColor());
  553.                            }
  554.                        }
  555.                        hasFilledList = true;
  556.                    }
  557.                    maxIndex = materialNames.size();
  558.                    if (this.shouldIncrement && index + 1 < maxIndex)
  559.                    {
  560.                        FMLLog.info("incrementing");
  561.                        String name = materialNames.get(index);
  562.                        objBaked.getModel().getMatLib().changeMaterialColor(name, 0xFF000000);
  563.                        objBaked.scheduleRebake();
  564.                        index++;
  565.                    }
  566.                    else if (index - 1 >= 0)
  567.                    {
  568.                        index--;
  569.                        this.shouldIncrement = index == 0;
  570.                        int color = 0;
  571.                        color |= (int) (colorList.get(index).getW() * 255) << 24;
  572.                        color |= (int) (colorList.get(index).getX() * 255) << 16;
  573.                        color |= (int) (colorList.get(index).getY() * 255) << 8;
  574.                        color |= (int) (colorList.get(index).getZ() * 255);
  575.                        String name = materialNames.get(index);
  576.                        if (!name.equals(OBJModel.Material.WHITE_NAME))
  577.                        {
  578.                            objBaked.getModel().getMatLib().changeMaterialColor(name, color);
  579.                            objBaked.scheduleRebake();
  580.                        }
  581.                    }
  582.                    this.worldObj.markBlockRangeForRenderUpdate(this.pos, this.pos);
  583.                }
  584.            }
  585.        }
  586.    }
  587.  
  588.    /**
  589.     * This block is a debug block that faces the player when placed, like a piston.
  590.     * @author shadekiller666
  591.     *
  592.     */
  593.    public static class OBJDirectionBlock extends Block
  594.    {
  595.        public static final PropertyDirection FACING = PropertyDirection.create("facing");
  596.        public static final OBJDirectionBlock instance = new OBJDirectionBlock();
  597.        public static final String name = "OBJDirectionBlock";
  598.        public ExtendedBlockState state = new ExtendedBlockState(this, new IProperty[]{FACING}, new IUnlistedProperty[]{OBJModel.OBJProperty.instance});
  599.  
  600.        private OBJDirectionBlock()
  601.        {
  602.            super(Material.iron);
  603.            this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
  604.            setCreativeTab(CreativeTabs.tabBlock);
  605.            setUnlocalizedName(MODID + ":" + name);
  606.        }
  607.  
  608.        @Override
  609.        public boolean isOpaqueCube() { return false; }
  610.  
  611.        @Override
  612.        public boolean isFullCube() { return false; }
  613.  
  614.        @Override
  615.        public boolean isVisuallyOpaque() { return false; }
  616.  
  617.        @Override
  618.        public IBlockState onBlockPlaced(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
  619.        {
  620.            return this.getDefaultState().withProperty(FACING, getFacingFromEntity(world, pos, placer));
  621.        }
  622.  
  623.        @Override
  624.        public IBlockState getStateFromMeta(int meta)
  625.        {
  626.            return this.getDefaultState().withProperty(FACING, EnumFacing.getFront(meta));
  627.        }
  628.  
  629.        @Override
  630.        public int getMetaFromState(IBlockState state)
  631.        {
  632.            return ((EnumFacing) state.getValue(FACING)).getIndex();
  633.        }
  634.  
  635.        @Override
  636.        public IBlockState getExtendedState(IBlockState state, IBlockAccess world, BlockPos pos)
  637.        {
  638.            //Only return an IExtendedBlockState from this method and createState(), otherwise block placement will break!
  639.            EnumFacing facing = (EnumFacing) state.getValue(FACING);
  640.            TRSRTransformation transform = new TRSRTransformation(facing);
  641.            OBJModel.OBJState newState = new OBJModel.OBJState(Lists.newArrayList(OBJModel.Group.ALL), true, transform);
  642.            return ((IExtendedBlockState) state).withProperty(OBJModel.OBJProperty.instance, newState);
  643.        }
  644.  
  645.        @Override
  646.        public BlockState createBlockState()
  647.        {
  648.            return new ExtendedBlockState(this, new IProperty[]{FACING}, new IUnlistedProperty[]{OBJModel.OBJProperty.instance});
  649.        }
  650.  
  651.        public static EnumFacing getFacingFromEntity(World worldIn, BlockPos clickedBlock, EntityLivingBase entityIn)
  652.        {
  653.            if (MathHelper.abs((float)entityIn.posX - (float)clickedBlock.getX()) < 2.0F && MathHelper.abs((float)entityIn.posZ - (float)clickedBlock.getZ()) < 2.0F)
  654.            {
  655.                double d0 = entityIn.posY + (double)entityIn.getEyeHeight();
  656.  
  657.                if (d0 - (double)clickedBlock.getY() > 2.0D)
  658.                {
  659.                    return EnumFacing.UP;
  660.                }
  661.  
  662.                if ((double)clickedBlock.getY() - d0 > 0.0D)
  663.                {
  664.                    return EnumFacing.DOWN;
  665.                }
  666.            }
  667.  
  668.            return entityIn.getHorizontalFacing().getOpposite();
  669.        }
  670.    }
  671.  
  672.    /**
  673.     * This block is a testing block that will be used to test the use
  674.     * of "custom" data defined in a forge blockstate json. WIP, ignore for now.
  675.     * @author shadekiller666
  676.     *
  677.     */
  678.    public static class OBJCustomDataBlock extends Block
  679.    {
  680.        public static final PropertyBool NORTH = PropertyBool.create("north");
  681.        public static final PropertyBool SOUTH = PropertyBool.create("south");
  682.        public static final PropertyBool WEST = PropertyBool.create("west");
  683.        public static final PropertyBool EAST = PropertyBool.create("east");
  684.        public static final OBJCustomDataBlock instance = new OBJCustomDataBlock();
  685.        public static final String name = "OBJCustomDataBlock";
  686.  
  687.        private OBJCustomDataBlock()
  688.        {
  689.            super(Material.iron);
  690.            this.setDefaultState(this.blockState.getBaseState().withProperty(NORTH, false).withProperty(SOUTH, false).withProperty(WEST, false).withProperty(EAST, false));
  691.            setCreativeTab(CreativeTabs.tabBlock);
  692.            setUnlocalizedName(MODID + ":" + name);
  693.        }
  694.  
  695.        @Override
  696.        public boolean isOpaqueCube()
  697.        {
  698.            return false;
  699.        }
  700.  
  701.        @Override
  702.        public boolean isFullCube()
  703.        {
  704.            return false;
  705.        }
  706.  
  707.        @Override
  708.        public int getMetaFromState(IBlockState state)
  709.        {
  710.            return 0;
  711.        }
  712.  
  713.        public boolean canConnectTo(IBlockAccess world, BlockPos pos)
  714.        {
  715.            Block block = world.getBlockState(pos).getBlock();
  716.            return block instanceof OBJCustomDataBlock;
  717.        }
  718.  
  719.        @Override
  720.        public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos pos)
  721.        {
  722.            return state.withProperty(NORTH, this.canConnectTo(world, pos.north())).withProperty(SOUTH, this.canConnectTo(world, pos.south())).withProperty(WEST, this.canConnectTo(world, pos.west())).withProperty(EAST, this.canConnectTo(world, pos.east()));
  723.        }
  724.  
  725.        @Override
  726.        public BlockState createBlockState()
  727.        {
  728.            return new BlockState(this, new IProperty[]{NORTH, SOUTH, WEST, EAST});
  729.        }
  730.    }
  731.  
  732.    /**
  733.     * This block uses the same model as CustomModelBlock4, but instead of facing the
  734.     * player when placed, this one ALWAYS faces the player. I know, creepy right?
  735.     * @author shadekiller666
  736.     *
  737.     */
  738.    public static class OBJDynamicEye extends Block implements ITileEntityProvider
  739.    {
  740.        public static final OBJDynamicEye instance = new OBJDynamicEye();
  741.        public static final String name = "OBJDynamicEye";
  742.        public ExtendedBlockState state = new ExtendedBlockState(this, new IProperty[0], new IUnlistedProperty[] {OBJModel.OBJProperty.instance});
  743.        private OBJDynamicEye()
  744.        {
  745.            super(Material.iron);
  746.            setCreativeTab(CreativeTabs.tabBlock);
  747.            setUnlocalizedName(MODID + ":" + name);
  748.        }
  749.  
  750.        @Override
  751.        public TileEntity createNewTileEntity(World worldIn, int meta)
  752.        {
  753.            return new OBJDynamicEyeTileEntity();
  754.        }
  755.  
  756.        @Override
  757.        public boolean isOpaqueCube()
  758.        {
  759.            return false;
  760.        }
  761.  
  762.        @Override
  763.        public boolean isFullCube()
  764.        {
  765.            return false;
  766.        }
  767.  
  768.        @Override
  769.        public boolean hasTileEntity(IBlockState state)
  770.        {
  771.            return true;
  772.        }
  773.  
  774.        @Override
  775.        public IBlockState getExtendedState(IBlockState state, IBlockAccess world, BlockPos pos)
  776.        {
  777.            if (world.getTileEntity(pos) != null && world.getTileEntity(pos) instanceof OBJDynamicEyeTileEntity)
  778.            {
  779.                OBJDynamicEyeTileEntity te = (OBJDynamicEyeTileEntity) world.getTileEntity(pos);
  780.                if (te.state != null)
  781.                {
  782.                    return ((IExtendedBlockState) this.state.getBaseState()).withProperty(OBJModel.OBJProperty.instance, te.state);
  783.                }
  784.            }
  785.            return state;
  786.        }
  787.  
  788.        @Override
  789.        public BlockState createBlockState()
  790.        {
  791.            return new ExtendedBlockState(this, new IProperty[0], new IUnlistedProperty[] {OBJModel.OBJProperty.instance});
  792.        }
  793.    }
  794.  
  795.    public static class OBJDynamicEyeTileEntity extends TileEntity implements ITickable
  796.    {
  797.        public OBJModel.OBJState state;
  798.  
  799.        public OBJDynamicEyeTileEntity()
  800.        {
  801.            this.state = new OBJModel.OBJState(Lists.newArrayList(OBJModel.Group.ALL), true);
  802.        }
  803.  
  804.        @Override
  805.        public void update()
  806.        {
  807.            if (this.worldObj.isRemote)
  808.            {
  809.                Vector3d teLoc = new Vector3d(this.getPos().getX(), this.getPos().getY(), this.getPos().getZ());
  810.                EntityPlayer player = Minecraft.getMinecraft().thePlayer;
  811.                Vector3d playerLoc = new Vector3d();
  812.                playerLoc.setX(player.posX);
  813.                playerLoc.setY(player.posY + player.getEyeHeight());
  814.                playerLoc.setZ(player.posZ);
  815.                Vector3d lookVec = new Vector3d(playerLoc.getX() - teLoc.getX(), playerLoc.getY() - teLoc.getY(), playerLoc.getZ() - teLoc.getZ());
  816.                double angleYaw = Math.atan2(lookVec.getZ(), lookVec.getX()) - Math.PI/2d;
  817.                double anglePitch = Math.atan2(lookVec.getY(), Math.sqrt(lookVec.getX() * lookVec.getX() + lookVec.getZ() * lookVec.getZ()));
  818.                AxisAngle4d yaw = new AxisAngle4d(0, 1, 0, -angleYaw);
  819.                AxisAngle4d pitch = new AxisAngle4d(1, 0, 0, -anglePitch);
  820.                Quat4f rot = new Quat4f(0, 0, 0, 1);
  821.                Quat4f yawQuat = new Quat4f();
  822.                Quat4f pitchQuat = new Quat4f();
  823.                yawQuat.set(yaw);
  824.                rot.mul(yawQuat);
  825.                pitchQuat.set(pitch);
  826.                rot.mul(pitchQuat);
  827.                Matrix4f matrix = new Matrix4f();
  828.                matrix.setIdentity();
  829.                matrix.setRotation(rot);
  830.                TRSRTransformation transform = new TRSRTransformation(matrix);
  831.                transform = TRSRTransformation.blockCenterToCorner(transform);
  832.                this.state = new OBJModel.OBJState(Lists.newArrayList(OBJModel.Group.ALL), true, transform);
  833.                this.worldObj.markBlockRangeForRenderUpdate(this.pos, this.pos);
  834.            }
  835.        }
  836.    }
  837. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement