Block orientable



  • j'essaye de rendre orrientable mon block mais ceci ne fonctionne pas 😕 que faire ?

    Mon block

    package com.tuto.mod.blocks;
    
    import com.tuto.mod.init.ItemsMod;
    
    import net.minecraft.block.Block;
    import net.minecraft.block.BlockHorizontal;
    import net.minecraft.block.material.Material;
    import net.minecraft.block.properties.IProperty;
    import net.minecraft.block.properties.PropertyDirection;
    import net.minecraft.block.state.BlockStateContainer;
    import net.minecraft.block.state.IBlockState;
    import net.minecraft.entity.EntityLivingBase;
    import net.minecraft.entity.item.EntityItem;
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.init.Blocks;
    import net.minecraft.init.Items;
    import net.minecraft.item.ItemStack;
    import net.minecraft.util.EnumFacing;
    import net.minecraft.util.EnumHand;
    import net.minecraft.util.Mirror;
    import net.minecraft.util.Rotation;
    import net.minecraft.util.math.BlockPos;
    import net.minecraft.util.text.TextComponentString;
    import net.minecraft.util.text.TextComponentTranslation;
    import net.minecraft.util.text.TextFormatting;
    import net.minecraft.world.World;
    
    public class MachineMod extends Block
    {
    
        public static final PropertyDirection FACING = BlockHorizontal.FACING;
    
        public MachineMod(Material materialIn)
        {
            super(materialIn);
            setResistance(3F);
            setHardness(3F);
            setHarvestLevel("pickaxe", 1);
            this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
        }
    
        @Override
        public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer player,
                EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ)
        {
             if(player.getHeldItemMainhand() != null && player.getHeldItemMainhand().getItem() == ItemsMod.supT)
             {
                 player.inventory.setInventorySlotContents(player.inventory.currentItem, new ItemStack(ItemsMod.dust));
                 player.addChatMessage(new TextComponentString("§3Pouf !"));
             }
            return super.onBlockActivated(worldIn, pos, state, player, hand, heldItem, side, hitX, hitY, hitZ);
        }
    
        private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state)
        {
            if (!worldIn.isRemote)
            {
                IBlockState iblockstate = worldIn.getBlockState(pos.north());
                IBlockState iblockstate1 = worldIn.getBlockState(pos.south());
                IBlockState iblockstate2 = worldIn.getBlockState(pos.west());
                IBlockState iblockstate3 = worldIn.getBlockState(pos.east());
                EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
    
                if (enumfacing == EnumFacing.NORTH && iblockstate.isFullBlock() && !iblockstate1.isFullBlock())
                {
                    enumfacing = EnumFacing.SOUTH;
                }
                else if (enumfacing == EnumFacing.SOUTH && iblockstate1.isFullBlock() && !iblockstate.isFullBlock())
                {
                    enumfacing = EnumFacing.NORTH;
                }
                else if (enumfacing == EnumFacing.WEST && iblockstate2.isFullBlock() && !iblockstate3.isFullBlock())
                {
                    enumfacing = EnumFacing.EAST;
                }
                else if (enumfacing == EnumFacing.EAST && iblockstate3.isFullBlock() && !iblockstate2.isFullBlock())
                {
                    enumfacing = EnumFacing.WEST;
                }
    
                worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
            }
        }
    
        public IBlockState withRotation(IBlockState state, Rotation rot)
        {
            return state.withProperty(FACING, rot.rotate((EnumFacing)state.getValue(FACING)));
        }
    
        /**
         * Returns the blockstate with the given mirror of the passed blockstate. If inapplicable, returns the passed
         * blockstate.
         */
        public IBlockState withMirror(IBlockState state, Mirror mirrorIn)
        {
            return state.withRotation(mirrorIn.toRotation((EnumFacing)state.getValue(FACING)));
        }
    
        /**
         * Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments to the
         * IBlockstate
         */
        public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
        {
            return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
        }
    
        /**
         * Convert the given metadata into a BlockState for this Block
         */
        public IBlockState getStateFromMeta(int meta)
        {
            return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta));
        }
    
        /**
         * Convert the BlockState into the correct metadata value
         */
        public int getMetaFromState(IBlockState state)
        {
            return ((EnumFacing)state.getValue(FACING)).getHorizontalIndex();
        }
    
        protected BlockStateContainer createBlockState()
        {
            return new BlockStateContainer(this, new IProperty[] {FACING});
        }
    }
    

    Mes json

    {
       "variants": {
           "facing=north": { "model": "tuto:machine" },
           "facing=south": { "model": "tuto:machine", "y": 180 },
           "facing=west":  { "model": "tuto:machine", "y": 270 },
           "facing=east":  { "model": "tuto:machine", "y": 90 }
       }
    }
    
    
    {
       "textures": {
           "rubiks": "tuto:blocks/machine",
    "rubiks2": "tuto:blocks/machine2",
    "rubiks3": "tuto:blocks/machine3",
    "rubiks4": "tuto:blocks/machine4",
    "rubiks5": "tuto:blocks/machine5",
    "rubiks6": "tuto:blocks/machine6",
    "particle": "tuto:blocks/machine"
       },
       "elements": [{
           "from": [0, 0, 0],
           "to": [16, 16, 16],
    "faces": {
               "down": {
                   "texture": "#rubiks2",
    "uv": [0, 0, 16, 16]
               },
               "up": {
                   "texture": "#rubiks",
    "uv": [0, 0, 16, 16]
               },
               "north": {
                   "texture": "#rubiks5",
    "uv": [0, 0, 16, 16]
               },
               "south": {
                   "texture": "#rubiks4",
    "uv": [0, 0, 16, 16]
               },
               "west": {
                   "texture": "#rubiks3",
    "uv": [0, 0, 16, 16]
               },
               "east": {
                   "texture": "#rubiks6",
    "uv": [0, 0, 16, 16]
               }
           }
       }],
       "display": {
           "gui": {
               "rotation": [5, 5, 10],
               "translation": [0, 0, 10],
       "scale": [ 0.8, 0.8, 0.8 ]
           },
           "fixe": {
               "rotation": [-5, 0, 10],
               "translation": [0, 3, 10],
       "scale": [ 0.7, 0.7, 0.7 ]
           },
    "thirdperson_righthand": {
               "rotation": [ 75, 45, 0 ],
               "translation": [ 0, 3, 0],
               "scale": [ 0.3, 0.3, 0.3 ]
           },
           "firstperson_righthand": {
               "rotation": [ 0, 45, 0 ],
               "translation": [ 0, 5, 0 ],
               "scale": [ 0.5, 0.5, 0.5 ]
           },
           "firstperson_lefthand": {
               "rotation": [ 0, 225, 0 ],
               "translation": [ 0, 5, 0 ],
               "scale": [ 0.5, 0.5, 0.5 ]
           }
       }
    }
    

    J'ai déja regardé dans la citrouille etc mais sans résultat … Je vous demande alors !

    PS: regardez pas le model, les textures s'appellent rubiks mais sont belle et bien mes textures, j'ai juste copié le code 😄



  • Salut,
    Voici ce qu'il faudrait rajouter :

    
    /**
        * Called after the block is set in the Chunk data, but before the Tile Entity is set
        */
       public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
       {
           this.setDefaultFacing(worldIn, pos, state);
       }
    
       private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state)
       {
           if (!worldIn.isRemote)
           {
               IBlockState iblockstate = worldIn.getBlockState(pos.north());
               IBlockState iblockstate1 = worldIn.getBlockState(pos.south());
               IBlockState iblockstate2 = worldIn.getBlockState(pos.west());
               IBlockState iblockstate3 = worldIn.getBlockState(pos.east());
               EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
    
               if (enumfacing == EnumFacing.NORTH && iblockstate.isFullBlock() && !iblockstate1.isFullBlock())
               {
                   enumfacing = EnumFacing.SOUTH;
               }
               else if (enumfacing == EnumFacing.SOUTH && iblockstate1.isFullBlock() && !iblockstate.isFullBlock())
               {
                   enumfacing = EnumFacing.NORTH;
               }
               else if (enumfacing == EnumFacing.WEST && iblockstate2.isFullBlock() && !iblockstate3.isFullBlock())
               {
                   enumfacing = EnumFacing.EAST;
               }
               else if (enumfacing == EnumFacing.EAST && iblockstate3.isFullBlock() && !iblockstate2.isFullBlock())
               {
                   enumfacing = EnumFacing.WEST;
               }
    
               worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
           }
       }
    
       public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
       {
           return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
       }
    
       /**
        * Convert the given metadata into a BlockState for this Block
        */
       public IBlockState getStateFromMeta(int meta)
       {
           EnumFacing enumfacing = EnumFacing.getFront(meta);
    
           if (enumfacing.getAxis() == EnumFacing.Axis.Y)
           {
               enumfacing = EnumFacing.NORTH;
           }
    
           return this.getDefaultState().withProperty(FACING, enumfacing);
       }
    
       /**
        * Convert the BlockState into the correct metadata value
        */
       public int getMetaFromState(IBlockState state)
       {
           return ((EnumFacing)state.getValue(FACING)).getIndex();
       }
    
       /**
        * Returns the blockstate with the given rotation from the passed blockstate. If inapplicable, returns the passed
        * blockstate.
        */
       public IBlockState withRotation(IBlockState state, Rotation rot)
       {
           return state.withProperty(FACING, rot.rotate((EnumFacing)state.getValue(FACING)));
       }
    
       /**
        * Returns the blockstate with the given mirror of the passed blockstate. If inapplicable, returns the passed
        * blockstate.
        */
       public IBlockState withMirror(IBlockState state, Mirror mirrorIn)
       {
           return state.withRotation(mirrorIn.toRotation((EnumFacing)state.getValue(FACING)));
       }
    
       protected BlockStateContainer createBlockState()
       {
           return new BlockStateContainer(this, new IProperty[] {FACING});
       }
    
       public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
       {
           worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing().getOpposite()), 2);
       }
    
    

    EDIT = Question, tu as uniquement un problème d'orientation de block, ou tes textures sont également mal appliquées ?



  • Non, mes textures sont bien appliquer, juste l'orientation ! C'est un peu dérangeant une machine si on voit pas la face principal ^^