1.12.2 Houe de repantation



  • Bonjour,
    J'essaie de créer une houe qui replante en faisant clic droit mais l'item ne spawn pas et le block reste comme tel.

    public class ItemMagicHoe extends ItemHoe implements IHasModel
    {
        public ItemMagicHoe(String name, ToolMaterial material)
        {
            super(material);
            setUnlocalizedName(name);
            setRegistryName(name);
            setCreativeTab(CreativeTabs.MATERIALS);
            ItemInit.ITEMS.add(this);
        }
        
        @Override
        public void registerModels()
        {
            Main.proxy.registerItemRederer(this, 0);
        }
        
        @Override
        public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
        {
            IBlockState iblockstate = worldIn.getBlockState(pos);
            Block block = iblockstate.getBlock();
            
            if(block instanceof BlockCrops && !worldIn.isRemote)
            {
                if(block.equals(Blocks.POTATOES) && block.getBlockState().getBaseState() == block.getStateById(7))
                {
                    worldIn.setBlockState(pos, ((BlockCrops)block).withAge(0));
                    worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.POTATO, 4)));
                }
                else if(block.equals(Blocks.BEETROOTS) && block.getBlockState().getBaseState() == block.getStateById(3))
                {
                    worldIn.setBlockState(pos, ((BlockCrops)block).withAge(0));  
                    worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.BEETROOT, 3)));
                    worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.BEETROOT_SEEDS, 1)));
                }
                else if(block.equals(Blocks.CARROTS) && block.getBlockState().getBaseState() == block.getStateById(7))
                {
                    worldIn.setBlockState(pos, ((BlockCrops)block).withAge(0));  
                    worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.CARROT, 4)));
                }
                else if(block.equals(Blocks.WHEAT) && block.getBlockState().getBaseState() == block.getStateById(7))
                {
                    worldIn.setBlockState(pos, ((BlockCrops)block).withAge(0)); 
                    worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.WHEAT, 3)));
                    worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.WHEAT_SEEDS, 1)));
                }
                return EnumActionResult.SUCCESS;
            }
            else
            {
                return EnumActionResult.PASS;
            }
        }
    }
    


  • Désolé de répondre tardivement mais j'ai trouvé la solution :

    public class ItemMagicHoe extends ItemHoe implements IHasModel
    {
        public ItemMagicHoe(String name, ToolMaterial material)
        {
            super(material);
            setUnlocalizedName(name);
            setRegistryName(name);
            setCreativeTab(CreativeTabs.MATERIALS);
            ItemInit.ITEMS.add(this);
        }
        
        @Override
        public void registerModels()
        {
            Main.proxy.registerItemRederer(this, 0);
        }
        
        @SuppressWarnings("incomplete-switch")
        @Override
        public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
        {
            IBlockState iblockstate = worldIn.getBlockState(pos);
            Block block = iblockstate.getBlock();
            
            Random rand = new Random();
            ItemStack itemstack = player.getHeldItem(hand);
    
            if(block instanceof BlockCrops && !worldIn.isRemote)
            {
                if(block.equals(Blocks.POTATOES) && ((BlockCrops)block).isMaxAge(iblockstate))
                {
                    worldIn.setBlockState(pos, ((BlockCrops)block).withAge(0));
                    worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.POTATO, rand.nextInt(6))));
                }
                else if(block.equals(Blocks.BEETROOTS) && ((BlockCrops)block).isMaxAge(iblockstate))
                {
                    worldIn.setBlockState(pos, ((BlockCrops)block).withAge(0));  
                    worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.BEETROOT, rand.nextInt(5))));
                    worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.BEETROOT_SEEDS, rand.nextInt(3))));
                }
                else if(block.equals(Blocks.CARROTS) && ((BlockCrops)block).isMaxAge(iblockstate))
                {
                    worldIn.setBlockState(pos, ((BlockCrops)block).withAge(0));  
                    worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.CARROT, rand.nextInt(6))));
                }
                else if(block.equals(Blocks.WHEAT) && ((BlockCrops)block).isMaxAge(iblockstate))
                {
                    worldIn.setBlockState(pos, ((BlockCrops)block).withAge(0)); 
                    worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.WHEAT, rand.nextInt(5))));
                    worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.WHEAT_SEEDS, rand.nextInt(3))));
                }
                
                itemstack.damageItem(1, player);
                
                return EnumActionResult.SUCCESS;
            }
            
            else if(block instanceof BlockGrass || block instanceof BlockDirt || block instanceof BlockGrassPath)
            {
                int hook = net.minecraftforge.event.ForgeEventFactory.onHoeUse(itemstack, player, worldIn, pos);
                if (hook != 0) return hook > 0 ? EnumActionResult.SUCCESS : EnumActionResult.FAIL;
    
                if (facing != EnumFacing.DOWN && worldIn.isAirBlock(pos.up()))
                {
                    if (block == Blocks.GRASS || block == Blocks.GRASS_PATH)
                    {
                        this.setBlock(itemstack, player, worldIn, pos, Blocks.FARMLAND.getDefaultState());
                        return EnumActionResult.SUCCESS;
                    }
    
                    if (block == Blocks.DIRT)
                    {
                        switch ((BlockDirt.DirtType)iblockstate.getValue(BlockDirt.VARIANT))
                        {
                            case DIRT:
                                this.setBlock(itemstack, player, worldIn, pos, Blocks.FARMLAND.getDefaultState());
                                return EnumActionResult.SUCCESS;
                            case COARSE_DIRT:
                                this.setBlock(itemstack, player, worldIn, pos, Blocks.DIRT.getDefaultState().withProperty(BlockDirt.VARIANT, BlockDirt.DirtType.DIRT));
                                return EnumActionResult.SUCCESS;
                        }
                    }
                }
    
                return EnumActionResult.PASS;
            }
            return EnumActionResult.PASS;
        }
    }
    
    


  • ca ca peut pas fonctionner block.getBlockState().getBaseState() == block.getStateById(7) ca va toujour te donner un age de 0 parce que dans ce cas la le block est pas défini pour faire simple quel bloc tu regarde? réponse je sait pas tu ma pas dit après je dit peut être un connerie je suis en train de versifier

    ensuite faudrait surement supprimer le bloc de culture encore la avant d’essayer d'en placer 1 ou je te propose un truc plus simple changer son Age par 0 ce qui reviendrait a le faire pousser a l’envers pour le remettre a 0 😜 et de drop le résultat avant de faire ca pour pas la condition disparaisse

    if
    reset culture state
    drop item



  • Désolé de répondre tardivement mais j'ai trouvé la solution :

    public class ItemMagicHoe extends ItemHoe implements IHasModel
    {
        public ItemMagicHoe(String name, ToolMaterial material)
        {
            super(material);
            setUnlocalizedName(name);
            setRegistryName(name);
            setCreativeTab(CreativeTabs.MATERIALS);
            ItemInit.ITEMS.add(this);
        }
        
        @Override
        public void registerModels()
        {
            Main.proxy.registerItemRederer(this, 0);
        }
        
        @SuppressWarnings("incomplete-switch")
        @Override
        public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
        {
            IBlockState iblockstate = worldIn.getBlockState(pos);
            Block block = iblockstate.getBlock();
            
            Random rand = new Random();
            ItemStack itemstack = player.getHeldItem(hand);
    
            if(block instanceof BlockCrops && !worldIn.isRemote)
            {
                if(block.equals(Blocks.POTATOES) && ((BlockCrops)block).isMaxAge(iblockstate))
                {
                    worldIn.setBlockState(pos, ((BlockCrops)block).withAge(0));
                    worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.POTATO, rand.nextInt(6))));
                }
                else if(block.equals(Blocks.BEETROOTS) && ((BlockCrops)block).isMaxAge(iblockstate))
                {
                    worldIn.setBlockState(pos, ((BlockCrops)block).withAge(0));  
                    worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.BEETROOT, rand.nextInt(5))));
                    worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.BEETROOT_SEEDS, rand.nextInt(3))));
                }
                else if(block.equals(Blocks.CARROTS) && ((BlockCrops)block).isMaxAge(iblockstate))
                {
                    worldIn.setBlockState(pos, ((BlockCrops)block).withAge(0));  
                    worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.CARROT, rand.nextInt(6))));
                }
                else if(block.equals(Blocks.WHEAT) && ((BlockCrops)block).isMaxAge(iblockstate))
                {
                    worldIn.setBlockState(pos, ((BlockCrops)block).withAge(0)); 
                    worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.WHEAT, rand.nextInt(5))));
                    worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.WHEAT_SEEDS, rand.nextInt(3))));
                }
                
                itemstack.damageItem(1, player);
                
                return EnumActionResult.SUCCESS;
            }
            
            else if(block instanceof BlockGrass || block instanceof BlockDirt || block instanceof BlockGrassPath)
            {
                int hook = net.minecraftforge.event.ForgeEventFactory.onHoeUse(itemstack, player, worldIn, pos);
                if (hook != 0) return hook > 0 ? EnumActionResult.SUCCESS : EnumActionResult.FAIL;
    
                if (facing != EnumFacing.DOWN && worldIn.isAirBlock(pos.up()))
                {
                    if (block == Blocks.GRASS || block == Blocks.GRASS_PATH)
                    {
                        this.setBlock(itemstack, player, worldIn, pos, Blocks.FARMLAND.getDefaultState());
                        return EnumActionResult.SUCCESS;
                    }
    
                    if (block == Blocks.DIRT)
                    {
                        switch ((BlockDirt.DirtType)iblockstate.getValue(BlockDirt.VARIANT))
                        {
                            case DIRT:
                                this.setBlock(itemstack, player, worldIn, pos, Blocks.FARMLAND.getDefaultState());
                                return EnumActionResult.SUCCESS;
                            case COARSE_DIRT:
                                this.setBlock(itemstack, player, worldIn, pos, Blocks.DIRT.getDefaultState().withProperty(BlockDirt.VARIANT, BlockDirt.DirtType.DIRT));
                                return EnumActionResult.SUCCESS;
                        }
                    }
                }
    
                return EnumActionResult.PASS;
            }
            return EnumActionResult.PASS;
        }
    }
    
    


  • j’allais te l’envoyer vérifiait que ça fonctionnait xD



  • @PlagueZ En tt cas mrc de ton aide même si elle est arrivée un peut tard x)


Log in to reply